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> 409bffad1eSTheodore Ts'o 413dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 42ac27a0ecSDave Kleikamp #include "xattr.h" 43ac27a0ecSDave Kleikamp #include "acl.h" 449f125d64STheodore Ts'o #include "truncate.h" 45ac27a0ecSDave Kleikamp 469bffad1eSTheodore Ts'o #include <trace/events/ext4.h> 479bffad1eSTheodore Ts'o 48a1d6cc56SAneesh Kumar K.V #define MPAGE_DA_EXTENT_TAIL 0x01 49a1d6cc56SAneesh Kumar K.V 50814525f4SDarrick J. Wong static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw, 51814525f4SDarrick J. Wong struct ext4_inode_info *ei) 52814525f4SDarrick J. Wong { 53814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 54814525f4SDarrick J. Wong __u16 csum_lo; 55814525f4SDarrick J. Wong __u16 csum_hi = 0; 56814525f4SDarrick J. Wong __u32 csum; 57814525f4SDarrick J. Wong 58814525f4SDarrick J. Wong csum_lo = raw->i_checksum_lo; 59814525f4SDarrick J. Wong raw->i_checksum_lo = 0; 60814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 61814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) { 62814525f4SDarrick J. Wong csum_hi = raw->i_checksum_hi; 63814525f4SDarrick J. Wong raw->i_checksum_hi = 0; 64814525f4SDarrick J. Wong } 65814525f4SDarrick J. Wong 66814525f4SDarrick J. Wong csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw, 67814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)); 68814525f4SDarrick J. Wong 69814525f4SDarrick J. Wong raw->i_checksum_lo = csum_lo; 70814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 71814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 72814525f4SDarrick J. Wong raw->i_checksum_hi = csum_hi; 73814525f4SDarrick J. Wong 74814525f4SDarrick J. Wong return csum; 75814525f4SDarrick J. Wong } 76814525f4SDarrick J. Wong 77814525f4SDarrick J. Wong static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw, 78814525f4SDarrick J. Wong struct ext4_inode_info *ei) 79814525f4SDarrick J. Wong { 80814525f4SDarrick J. Wong __u32 provided, calculated; 81814525f4SDarrick J. Wong 82814525f4SDarrick J. Wong if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 83814525f4SDarrick J. Wong cpu_to_le32(EXT4_OS_LINUX) || 84814525f4SDarrick J. Wong !EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, 85814525f4SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) 86814525f4SDarrick J. Wong return 1; 87814525f4SDarrick J. Wong 88814525f4SDarrick J. Wong provided = le16_to_cpu(raw->i_checksum_lo); 89814525f4SDarrick J. Wong calculated = ext4_inode_csum(inode, raw, ei); 90814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 91814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 92814525f4SDarrick J. Wong provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16; 93814525f4SDarrick J. Wong else 94814525f4SDarrick J. Wong calculated &= 0xFFFF; 95814525f4SDarrick J. Wong 96814525f4SDarrick J. Wong return provided == calculated; 97814525f4SDarrick J. Wong } 98814525f4SDarrick J. Wong 99814525f4SDarrick J. Wong static void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw, 100814525f4SDarrick J. Wong struct ext4_inode_info *ei) 101814525f4SDarrick J. Wong { 102814525f4SDarrick J. Wong __u32 csum; 103814525f4SDarrick J. Wong 104814525f4SDarrick J. Wong if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 105814525f4SDarrick J. Wong cpu_to_le32(EXT4_OS_LINUX) || 106814525f4SDarrick J. Wong !EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, 107814525f4SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) 108814525f4SDarrick J. Wong return; 109814525f4SDarrick J. Wong 110814525f4SDarrick J. Wong csum = ext4_inode_csum(inode, raw, ei); 111814525f4SDarrick J. Wong raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF); 112814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 113814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 114814525f4SDarrick J. Wong raw->i_checksum_hi = cpu_to_le16(csum >> 16); 115814525f4SDarrick J. Wong } 116814525f4SDarrick J. Wong 117678aaf48SJan Kara static inline int ext4_begin_ordered_truncate(struct inode *inode, 118678aaf48SJan Kara loff_t new_size) 119678aaf48SJan Kara { 1207ff9c073STheodore Ts'o trace_ext4_begin_ordered_truncate(inode, new_size); 1218aefcd55STheodore Ts'o /* 1228aefcd55STheodore Ts'o * If jinode is zero, then we never opened the file for 1238aefcd55STheodore Ts'o * writing, so there's no need to call 1248aefcd55STheodore Ts'o * jbd2_journal_begin_ordered_truncate() since there's no 1258aefcd55STheodore Ts'o * outstanding writes we need to flush. 1268aefcd55STheodore Ts'o */ 1278aefcd55STheodore Ts'o if (!EXT4_I(inode)->jinode) 1288aefcd55STheodore Ts'o return 0; 1298aefcd55STheodore Ts'o return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode), 1308aefcd55STheodore Ts'o EXT4_I(inode)->jinode, 131678aaf48SJan Kara new_size); 132678aaf48SJan Kara } 133678aaf48SJan Kara 13464769240SAlex Tomas static void ext4_invalidatepage(struct page *page, unsigned long offset); 135cb20d518STheodore Ts'o static int __ext4_journalled_writepage(struct page *page, unsigned int len); 136cb20d518STheodore Ts'o static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh); 1375f163cc7SEric Sandeen static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle, 1385f163cc7SEric Sandeen struct inode *inode, struct page *page, loff_t from, 1395f163cc7SEric Sandeen loff_t length, int flags); 14064769240SAlex Tomas 141ac27a0ecSDave Kleikamp /* 142ac27a0ecSDave Kleikamp * Test whether an inode is a fast symlink. 143ac27a0ecSDave Kleikamp */ 144617ba13bSMingming Cao static int ext4_inode_is_fast_symlink(struct inode *inode) 145ac27a0ecSDave Kleikamp { 146617ba13bSMingming Cao int ea_blocks = EXT4_I(inode)->i_file_acl ? 147ac27a0ecSDave Kleikamp (inode->i_sb->s_blocksize >> 9) : 0; 148ac27a0ecSDave Kleikamp 149ac27a0ecSDave Kleikamp return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0); 150ac27a0ecSDave Kleikamp } 151ac27a0ecSDave Kleikamp 152ac27a0ecSDave Kleikamp /* 153ac27a0ecSDave Kleikamp * Restart the transaction associated with *handle. This does a commit, 154ac27a0ecSDave Kleikamp * so before we call here everything must be consistently dirtied against 155ac27a0ecSDave Kleikamp * this transaction. 156ac27a0ecSDave Kleikamp */ 157487caeefSJan Kara int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode, 158487caeefSJan Kara int nblocks) 159ac27a0ecSDave Kleikamp { 160487caeefSJan Kara int ret; 161487caeefSJan Kara 162487caeefSJan Kara /* 163e35fd660STheodore Ts'o * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this 164487caeefSJan Kara * moment, get_block can be called only for blocks inside i_size since 165487caeefSJan Kara * page cache has been already dropped and writes are blocked by 166487caeefSJan Kara * i_mutex. So we can safely drop the i_data_sem here. 167487caeefSJan Kara */ 1680390131bSFrank Mayhar BUG_ON(EXT4_JOURNAL(inode) == NULL); 169ac27a0ecSDave Kleikamp jbd_debug(2, "restarting handle %p\n", handle); 170487caeefSJan Kara up_write(&EXT4_I(inode)->i_data_sem); 1718e8eaabeSAmir Goldstein ret = ext4_journal_restart(handle, nblocks); 172487caeefSJan Kara down_write(&EXT4_I(inode)->i_data_sem); 173fa5d1113SAneesh Kumar K.V ext4_discard_preallocations(inode); 174487caeefSJan Kara 175487caeefSJan Kara return ret; 176ac27a0ecSDave Kleikamp } 177ac27a0ecSDave Kleikamp 178ac27a0ecSDave Kleikamp /* 179ac27a0ecSDave Kleikamp * Called at the last iput() if i_nlink is zero. 180ac27a0ecSDave Kleikamp */ 1810930fcc1SAl Viro void ext4_evict_inode(struct inode *inode) 182ac27a0ecSDave Kleikamp { 183ac27a0ecSDave Kleikamp handle_t *handle; 184bc965ab3STheodore Ts'o int err; 185ac27a0ecSDave Kleikamp 1867ff9c073STheodore Ts'o trace_ext4_evict_inode(inode); 1872581fdc8SJiaying Zhang 1882581fdc8SJiaying Zhang ext4_ioend_wait(inode); 1892581fdc8SJiaying Zhang 1900930fcc1SAl Viro if (inode->i_nlink) { 1912d859db3SJan Kara /* 1922d859db3SJan Kara * When journalling data dirty buffers are tracked only in the 1932d859db3SJan Kara * journal. So although mm thinks everything is clean and 1942d859db3SJan Kara * ready for reaping the inode might still have some pages to 1952d859db3SJan Kara * write in the running transaction or waiting to be 1962d859db3SJan Kara * checkpointed. Thus calling jbd2_journal_invalidatepage() 1972d859db3SJan Kara * (via truncate_inode_pages()) to discard these buffers can 1982d859db3SJan Kara * cause data loss. Also even if we did not discard these 1992d859db3SJan Kara * buffers, we would have no way to find them after the inode 2002d859db3SJan Kara * is reaped and thus user could see stale data if he tries to 2012d859db3SJan Kara * read them before the transaction is checkpointed. So be 2022d859db3SJan Kara * careful and force everything to disk here... We use 2032d859db3SJan Kara * ei->i_datasync_tid to store the newest transaction 2042d859db3SJan Kara * containing inode's data. 2052d859db3SJan Kara * 2062d859db3SJan Kara * Note that directories do not have this problem because they 2072d859db3SJan Kara * don't use page cache. 2082d859db3SJan Kara */ 2092d859db3SJan Kara if (ext4_should_journal_data(inode) && 2102d859db3SJan Kara (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode))) { 2112d859db3SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 2122d859db3SJan Kara tid_t commit_tid = EXT4_I(inode)->i_datasync_tid; 2132d859db3SJan Kara 2142d859db3SJan Kara jbd2_log_start_commit(journal, commit_tid); 2152d859db3SJan Kara jbd2_log_wait_commit(journal, commit_tid); 2162d859db3SJan Kara filemap_write_and_wait(&inode->i_data); 2172d859db3SJan Kara } 2180930fcc1SAl Viro truncate_inode_pages(&inode->i_data, 0); 2190930fcc1SAl Viro goto no_delete; 2200930fcc1SAl Viro } 2210930fcc1SAl Viro 222907f4554SChristoph Hellwig if (!is_bad_inode(inode)) 223871a2931SChristoph Hellwig dquot_initialize(inode); 224907f4554SChristoph Hellwig 225678aaf48SJan Kara if (ext4_should_order_data(inode)) 226678aaf48SJan Kara ext4_begin_ordered_truncate(inode, 0); 227ac27a0ecSDave Kleikamp truncate_inode_pages(&inode->i_data, 0); 228ac27a0ecSDave Kleikamp 229ac27a0ecSDave Kleikamp if (is_bad_inode(inode)) 230ac27a0ecSDave Kleikamp goto no_delete; 231ac27a0ecSDave Kleikamp 2328e8ad8a5SJan Kara /* 2338e8ad8a5SJan Kara * Protect us against freezing - iput() caller didn't have to have any 2348e8ad8a5SJan Kara * protection against it 2358e8ad8a5SJan Kara */ 2368e8ad8a5SJan Kara sb_start_intwrite(inode->i_sb); 2379924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, 2389924a92aSTheodore Ts'o ext4_blocks_for_truncate(inode)+3); 239ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 240bc965ab3STheodore Ts'o ext4_std_error(inode->i_sb, PTR_ERR(handle)); 241ac27a0ecSDave Kleikamp /* 242ac27a0ecSDave Kleikamp * If we're going to skip the normal cleanup, we still need to 243ac27a0ecSDave Kleikamp * make sure that the in-core orphan linked list is properly 244ac27a0ecSDave Kleikamp * cleaned up. 245ac27a0ecSDave Kleikamp */ 246617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 2478e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 248ac27a0ecSDave Kleikamp goto no_delete; 249ac27a0ecSDave Kleikamp } 250ac27a0ecSDave Kleikamp 251ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 2520390131bSFrank Mayhar ext4_handle_sync(handle); 253ac27a0ecSDave Kleikamp inode->i_size = 0; 254bc965ab3STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 255bc965ab3STheodore Ts'o if (err) { 25612062dddSEric Sandeen ext4_warning(inode->i_sb, 257bc965ab3STheodore Ts'o "couldn't mark inode dirty (err %d)", err); 258bc965ab3STheodore Ts'o goto stop_handle; 259bc965ab3STheodore Ts'o } 260ac27a0ecSDave Kleikamp if (inode->i_blocks) 261617ba13bSMingming Cao ext4_truncate(inode); 262bc965ab3STheodore Ts'o 263bc965ab3STheodore Ts'o /* 264bc965ab3STheodore Ts'o * ext4_ext_truncate() doesn't reserve any slop when it 265bc965ab3STheodore Ts'o * restarts journal transactions; therefore there may not be 266bc965ab3STheodore Ts'o * enough credits left in the handle to remove the inode from 267bc965ab3STheodore Ts'o * the orphan list and set the dtime field. 268bc965ab3STheodore Ts'o */ 2690390131bSFrank Mayhar if (!ext4_handle_has_enough_credits(handle, 3)) { 270bc965ab3STheodore Ts'o err = ext4_journal_extend(handle, 3); 271bc965ab3STheodore Ts'o if (err > 0) 272bc965ab3STheodore Ts'o err = ext4_journal_restart(handle, 3); 273bc965ab3STheodore Ts'o if (err != 0) { 27412062dddSEric Sandeen ext4_warning(inode->i_sb, 275bc965ab3STheodore Ts'o "couldn't extend journal (err %d)", err); 276bc965ab3STheodore Ts'o stop_handle: 277bc965ab3STheodore Ts'o ext4_journal_stop(handle); 27845388219STheodore Ts'o ext4_orphan_del(NULL, inode); 2798e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 280bc965ab3STheodore Ts'o goto no_delete; 281bc965ab3STheodore Ts'o } 282bc965ab3STheodore Ts'o } 283bc965ab3STheodore Ts'o 284ac27a0ecSDave Kleikamp /* 285617ba13bSMingming Cao * Kill off the orphan record which ext4_truncate created. 286ac27a0ecSDave Kleikamp * AKPM: I think this can be inside the above `if'. 287617ba13bSMingming Cao * Note that ext4_orphan_del() has to be able to cope with the 288ac27a0ecSDave Kleikamp * deletion of a non-existent orphan - this is because we don't 289617ba13bSMingming Cao * know if ext4_truncate() actually created an orphan record. 290ac27a0ecSDave Kleikamp * (Well, we could do this if we need to, but heck - it works) 291ac27a0ecSDave Kleikamp */ 292617ba13bSMingming Cao ext4_orphan_del(handle, inode); 293617ba13bSMingming Cao EXT4_I(inode)->i_dtime = get_seconds(); 294ac27a0ecSDave Kleikamp 295ac27a0ecSDave Kleikamp /* 296ac27a0ecSDave Kleikamp * One subtle ordering requirement: if anything has gone wrong 297ac27a0ecSDave Kleikamp * (transaction abort, IO errors, whatever), then we can still 298ac27a0ecSDave Kleikamp * do these next steps (the fs will already have been marked as 299ac27a0ecSDave Kleikamp * having errors), but we can't free the inode if the mark_dirty 300ac27a0ecSDave Kleikamp * fails. 301ac27a0ecSDave Kleikamp */ 302617ba13bSMingming Cao if (ext4_mark_inode_dirty(handle, inode)) 303ac27a0ecSDave Kleikamp /* If that failed, just do the required in-core inode clear. */ 3040930fcc1SAl Viro ext4_clear_inode(inode); 305ac27a0ecSDave Kleikamp else 306617ba13bSMingming Cao ext4_free_inode(handle, inode); 307617ba13bSMingming Cao ext4_journal_stop(handle); 3088e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 309ac27a0ecSDave Kleikamp return; 310ac27a0ecSDave Kleikamp no_delete: 3110930fcc1SAl Viro ext4_clear_inode(inode); /* We must guarantee clearing of inode... */ 312ac27a0ecSDave Kleikamp } 313ac27a0ecSDave Kleikamp 314a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 315a9e7f447SDmitry Monakhov qsize_t *ext4_get_reserved_space(struct inode *inode) 31660e58e0fSMingming Cao { 317a9e7f447SDmitry Monakhov return &EXT4_I(inode)->i_reserved_quota; 31860e58e0fSMingming Cao } 319a9e7f447SDmitry Monakhov #endif 3209d0be502STheodore Ts'o 32112219aeaSAneesh Kumar K.V /* 32212219aeaSAneesh Kumar K.V * Calculate the number of metadata blocks need to reserve 3239d0be502STheodore Ts'o * to allocate a block located at @lblock 32412219aeaSAneesh Kumar K.V */ 32501f49d0bSTheodore Ts'o static int ext4_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock) 32612219aeaSAneesh Kumar K.V { 32712e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3289d0be502STheodore Ts'o return ext4_ext_calc_metadata_amount(inode, lblock); 32912219aeaSAneesh Kumar K.V 3308bb2b247SAmir Goldstein return ext4_ind_calc_metadata_amount(inode, lblock); 33112219aeaSAneesh Kumar K.V } 33212219aeaSAneesh Kumar K.V 3330637c6f4STheodore Ts'o /* 3340637c6f4STheodore Ts'o * Called with i_data_sem down, which is important since we can call 3350637c6f4STheodore Ts'o * ext4_discard_preallocations() from here. 3360637c6f4STheodore Ts'o */ 3375f634d06SAneesh Kumar K.V void ext4_da_update_reserve_space(struct inode *inode, 3385f634d06SAneesh Kumar K.V int used, int quota_claim) 33912219aeaSAneesh Kumar K.V { 34012219aeaSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3410637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 34212219aeaSAneesh Kumar K.V 3430637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 344d8990240SAditya Kali trace_ext4_da_update_reserve_space(inode, used, quota_claim); 3450637c6f4STheodore Ts'o if (unlikely(used > ei->i_reserved_data_blocks)) { 3468de5c325STheodore Ts'o ext4_warning(inode->i_sb, "%s: ino %lu, used %d " 3471084f252STheodore Ts'o "with only %d reserved data blocks", 3480637c6f4STheodore Ts'o __func__, inode->i_ino, used, 3490637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 3500637c6f4STheodore Ts'o WARN_ON(1); 3510637c6f4STheodore Ts'o used = ei->i_reserved_data_blocks; 3526bc6e63fSAneesh Kumar K.V } 35312219aeaSAneesh Kumar K.V 35497795d2aSBrian Foster if (unlikely(ei->i_allocated_meta_blocks > ei->i_reserved_meta_blocks)) { 35501a523ebSTheodore Ts'o ext4_warning(inode->i_sb, "ino %lu, allocated %d " 35601a523ebSTheodore Ts'o "with only %d reserved metadata blocks " 35701a523ebSTheodore Ts'o "(releasing %d blocks with reserved %d data blocks)", 35897795d2aSBrian Foster inode->i_ino, ei->i_allocated_meta_blocks, 35901a523ebSTheodore Ts'o ei->i_reserved_meta_blocks, used, 36001a523ebSTheodore Ts'o ei->i_reserved_data_blocks); 36197795d2aSBrian Foster WARN_ON(1); 36297795d2aSBrian Foster ei->i_allocated_meta_blocks = ei->i_reserved_meta_blocks; 36397795d2aSBrian Foster } 36497795d2aSBrian Foster 3650637c6f4STheodore Ts'o /* Update per-inode reservations */ 3660637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= used; 3670637c6f4STheodore Ts'o ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks; 36857042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 36972b8ab9dSEric Sandeen used + ei->i_allocated_meta_blocks); 3700637c6f4STheodore Ts'o ei->i_allocated_meta_blocks = 0; 3710637c6f4STheodore Ts'o 3720637c6f4STheodore Ts'o if (ei->i_reserved_data_blocks == 0) { 3730637c6f4STheodore Ts'o /* 3740637c6f4STheodore Ts'o * We can release all of the reserved metadata blocks 3750637c6f4STheodore Ts'o * only when we have written all of the delayed 3760637c6f4STheodore Ts'o * allocation blocks. 3770637c6f4STheodore Ts'o */ 37857042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 37972b8ab9dSEric Sandeen ei->i_reserved_meta_blocks); 380ee5f4d9cSTheodore Ts'o ei->i_reserved_meta_blocks = 0; 3819d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 3820637c6f4STheodore Ts'o } 38312219aeaSAneesh Kumar K.V spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 38460e58e0fSMingming Cao 38572b8ab9dSEric Sandeen /* Update quota subsystem for data blocks */ 38672b8ab9dSEric Sandeen if (quota_claim) 3877b415bf6SAditya Kali dquot_claim_block(inode, EXT4_C2B(sbi, used)); 38872b8ab9dSEric Sandeen else { 3895f634d06SAneesh Kumar K.V /* 3905f634d06SAneesh Kumar K.V * We did fallocate with an offset that is already delayed 3915f634d06SAneesh Kumar K.V * allocated. So on delayed allocated writeback we should 39272b8ab9dSEric Sandeen * not re-claim the quota for fallocated blocks. 3935f634d06SAneesh Kumar K.V */ 3947b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, used)); 3955f634d06SAneesh Kumar K.V } 396d6014301SAneesh Kumar K.V 397d6014301SAneesh Kumar K.V /* 398d6014301SAneesh Kumar K.V * If we have done all the pending block allocations and if 399d6014301SAneesh Kumar K.V * there aren't any writers on the inode, we can discard the 400d6014301SAneesh Kumar K.V * inode's preallocations. 401d6014301SAneesh Kumar K.V */ 4020637c6f4STheodore Ts'o if ((ei->i_reserved_data_blocks == 0) && 4030637c6f4STheodore Ts'o (atomic_read(&inode->i_writecount) == 0)) 404d6014301SAneesh Kumar K.V ext4_discard_preallocations(inode); 40512219aeaSAneesh Kumar K.V } 40612219aeaSAneesh Kumar K.V 407e29136f8STheodore Ts'o static int __check_block_validity(struct inode *inode, const char *func, 408c398eda0STheodore Ts'o unsigned int line, 40924676da4STheodore Ts'o struct ext4_map_blocks *map) 4106fd058f7STheodore Ts'o { 41124676da4STheodore Ts'o if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk, 41224676da4STheodore Ts'o map->m_len)) { 413c398eda0STheodore Ts'o ext4_error_inode(inode, func, line, map->m_pblk, 414c398eda0STheodore Ts'o "lblock %lu mapped to illegal pblock " 41524676da4STheodore Ts'o "(length %d)", (unsigned long) map->m_lblk, 416c398eda0STheodore Ts'o map->m_len); 4176fd058f7STheodore Ts'o return -EIO; 4186fd058f7STheodore Ts'o } 4196fd058f7STheodore Ts'o return 0; 4206fd058f7STheodore Ts'o } 4216fd058f7STheodore Ts'o 422e29136f8STheodore Ts'o #define check_block_validity(inode, map) \ 423c398eda0STheodore Ts'o __check_block_validity((inode), __func__, __LINE__, (map)) 424e29136f8STheodore Ts'o 425f5ab0d1fSMingming Cao /* 4261f94533dSTheodore Ts'o * Return the number of contiguous dirty pages in a given inode 4271f94533dSTheodore Ts'o * starting at page frame idx. 42855138e0bSTheodore Ts'o */ 42955138e0bSTheodore Ts'o static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx, 43055138e0bSTheodore Ts'o unsigned int max_pages) 43155138e0bSTheodore Ts'o { 43255138e0bSTheodore Ts'o struct address_space *mapping = inode->i_mapping; 43355138e0bSTheodore Ts'o pgoff_t index; 43455138e0bSTheodore Ts'o struct pagevec pvec; 43555138e0bSTheodore Ts'o pgoff_t num = 0; 43655138e0bSTheodore Ts'o int i, nr_pages, done = 0; 43755138e0bSTheodore Ts'o 43855138e0bSTheodore Ts'o if (max_pages == 0) 43955138e0bSTheodore Ts'o return 0; 44055138e0bSTheodore Ts'o pagevec_init(&pvec, 0); 44155138e0bSTheodore Ts'o while (!done) { 44255138e0bSTheodore Ts'o index = idx; 44355138e0bSTheodore Ts'o nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 44455138e0bSTheodore Ts'o PAGECACHE_TAG_DIRTY, 44555138e0bSTheodore Ts'o (pgoff_t)PAGEVEC_SIZE); 44655138e0bSTheodore Ts'o if (nr_pages == 0) 44755138e0bSTheodore Ts'o break; 44855138e0bSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 44955138e0bSTheodore Ts'o struct page *page = pvec.pages[i]; 45055138e0bSTheodore Ts'o struct buffer_head *bh, *head; 45155138e0bSTheodore Ts'o 45255138e0bSTheodore Ts'o lock_page(page); 45355138e0bSTheodore Ts'o if (unlikely(page->mapping != mapping) || 45455138e0bSTheodore Ts'o !PageDirty(page) || 45555138e0bSTheodore Ts'o PageWriteback(page) || 45655138e0bSTheodore Ts'o page->index != idx) { 45755138e0bSTheodore Ts'o done = 1; 45855138e0bSTheodore Ts'o unlock_page(page); 45955138e0bSTheodore Ts'o break; 46055138e0bSTheodore Ts'o } 4611f94533dSTheodore Ts'o if (page_has_buffers(page)) { 4621f94533dSTheodore Ts'o bh = head = page_buffers(page); 46355138e0bSTheodore Ts'o do { 46455138e0bSTheodore Ts'o if (!buffer_delay(bh) && 4651f94533dSTheodore Ts'o !buffer_unwritten(bh)) 46655138e0bSTheodore Ts'o done = 1; 4671f94533dSTheodore Ts'o bh = bh->b_this_page; 4681f94533dSTheodore Ts'o } while (!done && (bh != head)); 46955138e0bSTheodore Ts'o } 47055138e0bSTheodore Ts'o unlock_page(page); 47155138e0bSTheodore Ts'o if (done) 47255138e0bSTheodore Ts'o break; 47355138e0bSTheodore Ts'o idx++; 47455138e0bSTheodore Ts'o num++; 475659c6009SEric Sandeen if (num >= max_pages) { 476659c6009SEric Sandeen done = 1; 47755138e0bSTheodore Ts'o break; 47855138e0bSTheodore Ts'o } 479659c6009SEric Sandeen } 48055138e0bSTheodore Ts'o pagevec_release(&pvec); 48155138e0bSTheodore Ts'o } 48255138e0bSTheodore Ts'o return num; 48355138e0bSTheodore Ts'o } 48455138e0bSTheodore Ts'o 485*921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 486*921f266bSDmitry Monakhov static void ext4_map_blocks_es_recheck(handle_t *handle, 487*921f266bSDmitry Monakhov struct inode *inode, 488*921f266bSDmitry Monakhov struct ext4_map_blocks *es_map, 489*921f266bSDmitry Monakhov struct ext4_map_blocks *map, 490*921f266bSDmitry Monakhov int flags) 491*921f266bSDmitry Monakhov { 492*921f266bSDmitry Monakhov int retval; 493*921f266bSDmitry Monakhov 494*921f266bSDmitry Monakhov map->m_flags = 0; 495*921f266bSDmitry Monakhov /* 496*921f266bSDmitry Monakhov * There is a race window that the result is not the same. 497*921f266bSDmitry Monakhov * e.g. xfstests #223 when dioread_nolock enables. The reason 498*921f266bSDmitry Monakhov * is that we lookup a block mapping in extent status tree with 499*921f266bSDmitry Monakhov * out taking i_data_sem. So at the time the unwritten extent 500*921f266bSDmitry Monakhov * could be converted. 501*921f266bSDmitry Monakhov */ 502*921f266bSDmitry Monakhov if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 503*921f266bSDmitry Monakhov down_read((&EXT4_I(inode)->i_data_sem)); 504*921f266bSDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 505*921f266bSDmitry Monakhov retval = ext4_ext_map_blocks(handle, inode, map, flags & 506*921f266bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 507*921f266bSDmitry Monakhov } else { 508*921f266bSDmitry Monakhov retval = ext4_ind_map_blocks(handle, inode, map, flags & 509*921f266bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 510*921f266bSDmitry Monakhov } 511*921f266bSDmitry Monakhov if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 512*921f266bSDmitry Monakhov up_read((&EXT4_I(inode)->i_data_sem)); 513*921f266bSDmitry Monakhov /* 514*921f266bSDmitry Monakhov * Clear EXT4_MAP_FROM_CLUSTER and EXT4_MAP_BOUNDARY flag 515*921f266bSDmitry Monakhov * because it shouldn't be marked in es_map->m_flags. 516*921f266bSDmitry Monakhov */ 517*921f266bSDmitry Monakhov map->m_flags &= ~(EXT4_MAP_FROM_CLUSTER | EXT4_MAP_BOUNDARY); 518*921f266bSDmitry Monakhov 519*921f266bSDmitry Monakhov /* 520*921f266bSDmitry Monakhov * We don't check m_len because extent will be collpased in status 521*921f266bSDmitry Monakhov * tree. So the m_len might not equal. 522*921f266bSDmitry Monakhov */ 523*921f266bSDmitry Monakhov if (es_map->m_lblk != map->m_lblk || 524*921f266bSDmitry Monakhov es_map->m_flags != map->m_flags || 525*921f266bSDmitry Monakhov es_map->m_pblk != map->m_pblk) { 526*921f266bSDmitry Monakhov printk("ES cache assertation failed for inode: %lu " 527*921f266bSDmitry Monakhov "es_cached ex [%d/%d/%llu/%x] != " 528*921f266bSDmitry Monakhov "found ex [%d/%d/%llu/%x] retval %d flags %x\n", 529*921f266bSDmitry Monakhov inode->i_ino, es_map->m_lblk, es_map->m_len, 530*921f266bSDmitry Monakhov es_map->m_pblk, es_map->m_flags, map->m_lblk, 531*921f266bSDmitry Monakhov map->m_len, map->m_pblk, map->m_flags, 532*921f266bSDmitry Monakhov retval, flags); 533*921f266bSDmitry Monakhov } 534*921f266bSDmitry Monakhov } 535*921f266bSDmitry Monakhov #endif /* ES_AGGRESSIVE_TEST */ 536*921f266bSDmitry Monakhov 53755138e0bSTheodore Ts'o /* 538e35fd660STheodore Ts'o * The ext4_map_blocks() function tries to look up the requested blocks, 5392b2d6d01STheodore Ts'o * and returns if the blocks are already mapped. 540f5ab0d1fSMingming Cao * 541f5ab0d1fSMingming Cao * Otherwise it takes the write lock of the i_data_sem and allocate blocks 542f5ab0d1fSMingming Cao * and store the allocated blocks in the result buffer head and mark it 543f5ab0d1fSMingming Cao * mapped. 544f5ab0d1fSMingming Cao * 545e35fd660STheodore Ts'o * If file type is extents based, it will call ext4_ext_map_blocks(), 546e35fd660STheodore Ts'o * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping 547f5ab0d1fSMingming Cao * based files 548f5ab0d1fSMingming Cao * 549f5ab0d1fSMingming Cao * On success, it returns the number of blocks being mapped or allocate. 550f5ab0d1fSMingming Cao * if create==0 and the blocks are pre-allocated and uninitialized block, 551f5ab0d1fSMingming Cao * the result buffer head is unmapped. If the create ==1, it will make sure 552f5ab0d1fSMingming Cao * the buffer head is mapped. 553f5ab0d1fSMingming Cao * 554f5ab0d1fSMingming Cao * It returns 0 if plain look up failed (blocks have not been allocated), in 555df3ab170STao Ma * that case, buffer head is unmapped 556f5ab0d1fSMingming Cao * 557f5ab0d1fSMingming Cao * It returns the error in case of allocation failure. 558f5ab0d1fSMingming Cao */ 559e35fd660STheodore Ts'o int ext4_map_blocks(handle_t *handle, struct inode *inode, 560e35fd660STheodore Ts'o struct ext4_map_blocks *map, int flags) 5610e855ac8SAneesh Kumar K.V { 562d100eef2SZheng Liu struct extent_status es; 5630e855ac8SAneesh Kumar K.V int retval; 564*921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 565*921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 566*921f266bSDmitry Monakhov 567*921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 568*921f266bSDmitry Monakhov #endif 569f5ab0d1fSMingming Cao 570e35fd660STheodore Ts'o map->m_flags = 0; 571e35fd660STheodore Ts'o ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u," 572e35fd660STheodore Ts'o "logical block %lu\n", inode->i_ino, flags, map->m_len, 573e35fd660STheodore Ts'o (unsigned long) map->m_lblk); 574d100eef2SZheng Liu 575d100eef2SZheng Liu /* Lookup extent status tree firstly */ 576d100eef2SZheng Liu if (ext4_es_lookup_extent(inode, map->m_lblk, &es)) { 577d100eef2SZheng Liu if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) { 578d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + 579d100eef2SZheng Liu map->m_lblk - es.es_lblk; 580d100eef2SZheng Liu map->m_flags |= ext4_es_is_written(&es) ? 581d100eef2SZheng Liu EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN; 582d100eef2SZheng Liu retval = es.es_len - (map->m_lblk - es.es_lblk); 583d100eef2SZheng Liu if (retval > map->m_len) 584d100eef2SZheng Liu retval = map->m_len; 585d100eef2SZheng Liu map->m_len = retval; 586d100eef2SZheng Liu } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) { 587d100eef2SZheng Liu retval = 0; 588d100eef2SZheng Liu } else { 589d100eef2SZheng Liu BUG_ON(1); 590d100eef2SZheng Liu } 591*921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 592*921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(handle, inode, map, 593*921f266bSDmitry Monakhov &orig_map, flags); 594*921f266bSDmitry Monakhov #endif 595d100eef2SZheng Liu goto found; 596d100eef2SZheng Liu } 597d100eef2SZheng Liu 5984df3d265SAneesh Kumar K.V /* 599b920c755STheodore Ts'o * Try to see if we can get the block without requesting a new 600b920c755STheodore Ts'o * file system block. 6014df3d265SAneesh Kumar K.V */ 602729f52c6SZheng Liu if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 6030e855ac8SAneesh Kumar K.V down_read((&EXT4_I(inode)->i_data_sem)); 60412e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 605a4e5d88bSDmitry Monakhov retval = ext4_ext_map_blocks(handle, inode, map, flags & 606a4e5d88bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 6074df3d265SAneesh Kumar K.V } else { 608a4e5d88bSDmitry Monakhov retval = ext4_ind_map_blocks(handle, inode, map, flags & 609a4e5d88bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 6100e855ac8SAneesh Kumar K.V } 611f7fec032SZheng Liu if (retval > 0) { 612f7fec032SZheng Liu int ret; 613f7fec032SZheng Liu unsigned long long status; 614f7fec032SZheng Liu 615*921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 616*921f266bSDmitry Monakhov if (retval != map->m_len) { 617*921f266bSDmitry Monakhov printk("ES len assertation failed for inode: %lu " 618*921f266bSDmitry Monakhov "retval %d != map->m_len %d " 619*921f266bSDmitry Monakhov "in %s (lookup)\n", inode->i_ino, retval, 620*921f266bSDmitry Monakhov map->m_len, __func__); 621*921f266bSDmitry Monakhov } 622*921f266bSDmitry Monakhov #endif 623*921f266bSDmitry Monakhov 624f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 625f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 626f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 627f7fec032SZheng Liu ext4_find_delalloc_range(inode, map->m_lblk, 628f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 629f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 630f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, 631f7fec032SZheng Liu map->m_len, map->m_pblk, status); 632f7fec032SZheng Liu if (ret < 0) 633f7fec032SZheng Liu retval = ret; 634f7fec032SZheng Liu } 635729f52c6SZheng Liu if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 6364df3d265SAneesh Kumar K.V up_read((&EXT4_I(inode)->i_data_sem)); 637f5ab0d1fSMingming Cao 638d100eef2SZheng Liu found: 639e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 640f7fec032SZheng Liu int ret = check_block_validity(inode, map); 6416fd058f7STheodore Ts'o if (ret != 0) 6426fd058f7STheodore Ts'o return ret; 6436fd058f7STheodore Ts'o } 6446fd058f7STheodore Ts'o 645f5ab0d1fSMingming Cao /* If it is only a block(s) look up */ 646c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 6474df3d265SAneesh Kumar K.V return retval; 6484df3d265SAneesh Kumar K.V 6494df3d265SAneesh Kumar K.V /* 650f5ab0d1fSMingming Cao * Returns if the blocks have already allocated 651f5ab0d1fSMingming Cao * 652f5ab0d1fSMingming Cao * Note that if blocks have been preallocated 653df3ab170STao Ma * ext4_ext_get_block() returns the create = 0 654f5ab0d1fSMingming Cao * with buffer head unmapped. 655f5ab0d1fSMingming Cao */ 656e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) 657f5ab0d1fSMingming Cao return retval; 658f5ab0d1fSMingming Cao 659f5ab0d1fSMingming Cao /* 660a25a4e1aSZheng Liu * Here we clear m_flags because after allocating an new extent, 661a25a4e1aSZheng Liu * it will be set again. 6622a8964d6SAneesh Kumar K.V */ 663a25a4e1aSZheng Liu map->m_flags &= ~EXT4_MAP_FLAGS; 6642a8964d6SAneesh Kumar K.V 6652a8964d6SAneesh Kumar K.V /* 666f5ab0d1fSMingming Cao * New blocks allocate and/or writing to uninitialized extent 667f5ab0d1fSMingming Cao * will possibly result in updating i_data, so we take 668f5ab0d1fSMingming Cao * the write lock of i_data_sem, and call get_blocks() 669f5ab0d1fSMingming Cao * with create == 1 flag. 6704df3d265SAneesh Kumar K.V */ 6714df3d265SAneesh Kumar K.V down_write((&EXT4_I(inode)->i_data_sem)); 672d2a17637SMingming Cao 673d2a17637SMingming Cao /* 674d2a17637SMingming Cao * if the caller is from delayed allocation writeout path 675d2a17637SMingming Cao * we have already reserved fs blocks for allocation 676d2a17637SMingming Cao * let the underlying get_block() function know to 677d2a17637SMingming Cao * avoid double accounting 678d2a17637SMingming Cao */ 679c2177057STheodore Ts'o if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 680f2321097STheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED); 6814df3d265SAneesh Kumar K.V /* 6824df3d265SAneesh Kumar K.V * We need to check for EXT4 here because migrate 6834df3d265SAneesh Kumar K.V * could have changed the inode type in between 6844df3d265SAneesh Kumar K.V */ 68512e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 686e35fd660STheodore Ts'o retval = ext4_ext_map_blocks(handle, inode, map, flags); 6870e855ac8SAneesh Kumar K.V } else { 688e35fd660STheodore Ts'o retval = ext4_ind_map_blocks(handle, inode, map, flags); 689267e4db9SAneesh Kumar K.V 690e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_NEW) { 691267e4db9SAneesh Kumar K.V /* 692267e4db9SAneesh Kumar K.V * We allocated new blocks which will result in 693267e4db9SAneesh Kumar K.V * i_data's format changing. Force the migrate 694267e4db9SAneesh Kumar K.V * to fail by clearing migrate flags 695267e4db9SAneesh Kumar K.V */ 69619f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE); 697267e4db9SAneesh Kumar K.V } 6982ac3b6e0STheodore Ts'o 699d2a17637SMingming Cao /* 7002ac3b6e0STheodore Ts'o * Update reserved blocks/metadata blocks after successful 7015f634d06SAneesh Kumar K.V * block allocation which had been deferred till now. We don't 7025f634d06SAneesh Kumar K.V * support fallocate for non extent files. So we can update 7035f634d06SAneesh Kumar K.V * reserve space here. 704d2a17637SMingming Cao */ 7055f634d06SAneesh Kumar K.V if ((retval > 0) && 7061296cc85SAneesh Kumar K.V (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)) 7075f634d06SAneesh Kumar K.V ext4_da_update_reserve_space(inode, retval, 1); 7085f634d06SAneesh Kumar K.V } 709f7fec032SZheng Liu if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 710f2321097STheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED); 711d2a17637SMingming Cao 712f7fec032SZheng Liu if (retval > 0) { 71351865fdaSZheng Liu int ret; 714f7fec032SZheng Liu unsigned long long status; 715f7fec032SZheng Liu 716*921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 717*921f266bSDmitry Monakhov if (retval != map->m_len) { 718*921f266bSDmitry Monakhov printk("ES len assertation failed for inode: %lu " 719*921f266bSDmitry Monakhov "retval %d != map->m_len %d " 720*921f266bSDmitry Monakhov "in %s (allocation)\n", inode->i_ino, retval, 721*921f266bSDmitry Monakhov map->m_len, __func__); 722*921f266bSDmitry Monakhov } 723*921f266bSDmitry Monakhov #endif 724*921f266bSDmitry Monakhov 725f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 726f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 727f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 728f7fec032SZheng Liu ext4_find_delalloc_range(inode, map->m_lblk, 729f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 730f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 731f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 732f7fec032SZheng Liu map->m_pblk, status); 73351865fdaSZheng Liu if (ret < 0) 73451865fdaSZheng Liu retval = ret; 73551865fdaSZheng Liu } 7365356f261SAditya Kali 7370e855ac8SAneesh Kumar K.V up_write((&EXT4_I(inode)->i_data_sem)); 738e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 739e29136f8STheodore Ts'o int ret = check_block_validity(inode, map); 7406fd058f7STheodore Ts'o if (ret != 0) 7416fd058f7STheodore Ts'o return ret; 7426fd058f7STheodore Ts'o } 7430e855ac8SAneesh Kumar K.V return retval; 7440e855ac8SAneesh Kumar K.V } 7450e855ac8SAneesh Kumar K.V 746f3bd1f3fSMingming Cao /* Maximum number of blocks we map for direct IO at once. */ 747f3bd1f3fSMingming Cao #define DIO_MAX_BLOCKS 4096 748f3bd1f3fSMingming Cao 7492ed88685STheodore Ts'o static int _ext4_get_block(struct inode *inode, sector_t iblock, 7502ed88685STheodore Ts'o struct buffer_head *bh, int flags) 751ac27a0ecSDave Kleikamp { 7523e4fdaf8SDmitriy Monakhov handle_t *handle = ext4_journal_current_handle(); 7532ed88685STheodore Ts'o struct ext4_map_blocks map; 7547fb5409dSJan Kara int ret = 0, started = 0; 755f3bd1f3fSMingming Cao int dio_credits; 756ac27a0ecSDave Kleikamp 75746c7f254STao Ma if (ext4_has_inline_data(inode)) 75846c7f254STao Ma return -ERANGE; 75946c7f254STao Ma 7602ed88685STheodore Ts'o map.m_lblk = iblock; 7612ed88685STheodore Ts'o map.m_len = bh->b_size >> inode->i_blkbits; 7622ed88685STheodore Ts'o 7638b0f165fSAnatol Pomozov if (flags && !(flags & EXT4_GET_BLOCKS_NO_LOCK) && !handle) { 7647fb5409dSJan Kara /* Direct IO write... */ 7652ed88685STheodore Ts'o if (map.m_len > DIO_MAX_BLOCKS) 7662ed88685STheodore Ts'o map.m_len = DIO_MAX_BLOCKS; 7672ed88685STheodore Ts'o dio_credits = ext4_chunk_trans_blocks(inode, map.m_len); 7689924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, 7699924a92aSTheodore Ts'o dio_credits); 7707fb5409dSJan Kara if (IS_ERR(handle)) { 771ac27a0ecSDave Kleikamp ret = PTR_ERR(handle); 7722ed88685STheodore Ts'o return ret; 7737fb5409dSJan Kara } 7747fb5409dSJan Kara started = 1; 775ac27a0ecSDave Kleikamp } 776ac27a0ecSDave Kleikamp 7772ed88685STheodore Ts'o ret = ext4_map_blocks(handle, inode, &map, flags); 778ac27a0ecSDave Kleikamp if (ret > 0) { 7792ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 7802ed88685STheodore Ts'o bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; 7812ed88685STheodore Ts'o bh->b_size = inode->i_sb->s_blocksize * map.m_len; 782ac27a0ecSDave Kleikamp ret = 0; 783ac27a0ecSDave Kleikamp } 7847fb5409dSJan Kara if (started) 7857fb5409dSJan Kara ext4_journal_stop(handle); 786ac27a0ecSDave Kleikamp return ret; 787ac27a0ecSDave Kleikamp } 788ac27a0ecSDave Kleikamp 7892ed88685STheodore Ts'o int ext4_get_block(struct inode *inode, sector_t iblock, 7902ed88685STheodore Ts'o struct buffer_head *bh, int create) 7912ed88685STheodore Ts'o { 7922ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh, 7932ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 7942ed88685STheodore Ts'o } 7952ed88685STheodore Ts'o 796ac27a0ecSDave Kleikamp /* 797ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 798ac27a0ecSDave Kleikamp */ 799617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 800725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *errp) 801ac27a0ecSDave Kleikamp { 8022ed88685STheodore Ts'o struct ext4_map_blocks map; 8032ed88685STheodore Ts'o struct buffer_head *bh; 804ac27a0ecSDave Kleikamp int fatal = 0, err; 805ac27a0ecSDave Kleikamp 806ac27a0ecSDave Kleikamp J_ASSERT(handle != NULL || create == 0); 807ac27a0ecSDave Kleikamp 8082ed88685STheodore Ts'o map.m_lblk = block; 8092ed88685STheodore Ts'o map.m_len = 1; 8102ed88685STheodore Ts'o err = ext4_map_blocks(handle, inode, &map, 8112ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 8122ed88685STheodore Ts'o 81390b0a973SCarlos Maiolino /* ensure we send some value back into *errp */ 81490b0a973SCarlos Maiolino *errp = 0; 81590b0a973SCarlos Maiolino 8160f70b406STheodore Ts'o if (create && err == 0) 8170f70b406STheodore Ts'o err = -ENOSPC; /* should never happen */ 8182ed88685STheodore Ts'o if (err < 0) 819ac27a0ecSDave Kleikamp *errp = err; 8202ed88685STheodore Ts'o if (err <= 0) 8212ed88685STheodore Ts'o return NULL; 8222ed88685STheodore Ts'o 8232ed88685STheodore Ts'o bh = sb_getblk(inode->i_sb, map.m_pblk); 824aebf0243SWang Shilong if (unlikely(!bh)) { 825860d21e2STheodore Ts'o *errp = -ENOMEM; 8262ed88685STheodore Ts'o return NULL; 827ac27a0ecSDave Kleikamp } 8282ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 829ac27a0ecSDave Kleikamp J_ASSERT(create != 0); 830ac39849dSAneesh Kumar K.V J_ASSERT(handle != NULL); 831ac27a0ecSDave Kleikamp 832ac27a0ecSDave Kleikamp /* 833ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 834ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 835ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 836617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 837ac27a0ecSDave Kleikamp * problem. 838ac27a0ecSDave Kleikamp */ 839ac27a0ecSDave Kleikamp lock_buffer(bh); 840ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 841617ba13bSMingming Cao fatal = ext4_journal_get_create_access(handle, bh); 842ac27a0ecSDave Kleikamp if (!fatal && !buffer_uptodate(bh)) { 843ac27a0ecSDave Kleikamp memset(bh->b_data, 0, inode->i_sb->s_blocksize); 844ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 845ac27a0ecSDave Kleikamp } 846ac27a0ecSDave Kleikamp unlock_buffer(bh); 8470390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 8480390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 849ac27a0ecSDave Kleikamp if (!fatal) 850ac27a0ecSDave Kleikamp fatal = err; 851ac27a0ecSDave Kleikamp } else { 852ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "not a new buffer"); 853ac27a0ecSDave Kleikamp } 854ac27a0ecSDave Kleikamp if (fatal) { 855ac27a0ecSDave Kleikamp *errp = fatal; 856ac27a0ecSDave Kleikamp brelse(bh); 857ac27a0ecSDave Kleikamp bh = NULL; 858ac27a0ecSDave Kleikamp } 859ac27a0ecSDave Kleikamp return bh; 860ac27a0ecSDave Kleikamp } 861ac27a0ecSDave Kleikamp 862617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 863725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *err) 864ac27a0ecSDave Kleikamp { 865ac27a0ecSDave Kleikamp struct buffer_head *bh; 866ac27a0ecSDave Kleikamp 867617ba13bSMingming Cao bh = ext4_getblk(handle, inode, block, create, err); 868ac27a0ecSDave Kleikamp if (!bh) 869ac27a0ecSDave Kleikamp return bh; 870ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 871ac27a0ecSDave Kleikamp return bh; 87265299a3bSChristoph Hellwig ll_rw_block(READ | REQ_META | REQ_PRIO, 1, &bh); 873ac27a0ecSDave Kleikamp wait_on_buffer(bh); 874ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 875ac27a0ecSDave Kleikamp return bh; 876ac27a0ecSDave Kleikamp put_bh(bh); 877ac27a0ecSDave Kleikamp *err = -EIO; 878ac27a0ecSDave Kleikamp return NULL; 879ac27a0ecSDave Kleikamp } 880ac27a0ecSDave Kleikamp 881f19d5870STao Ma int ext4_walk_page_buffers(handle_t *handle, 882ac27a0ecSDave Kleikamp struct buffer_head *head, 883ac27a0ecSDave Kleikamp unsigned from, 884ac27a0ecSDave Kleikamp unsigned to, 885ac27a0ecSDave Kleikamp int *partial, 886ac27a0ecSDave Kleikamp int (*fn)(handle_t *handle, 887ac27a0ecSDave Kleikamp struct buffer_head *bh)) 888ac27a0ecSDave Kleikamp { 889ac27a0ecSDave Kleikamp struct buffer_head *bh; 890ac27a0ecSDave Kleikamp unsigned block_start, block_end; 891ac27a0ecSDave Kleikamp unsigned blocksize = head->b_size; 892ac27a0ecSDave Kleikamp int err, ret = 0; 893ac27a0ecSDave Kleikamp struct buffer_head *next; 894ac27a0ecSDave Kleikamp 895ac27a0ecSDave Kleikamp for (bh = head, block_start = 0; 896ac27a0ecSDave Kleikamp ret == 0 && (bh != head || !block_start); 897de9a55b8STheodore Ts'o block_start = block_end, bh = next) { 898ac27a0ecSDave Kleikamp next = bh->b_this_page; 899ac27a0ecSDave Kleikamp block_end = block_start + blocksize; 900ac27a0ecSDave Kleikamp if (block_end <= from || block_start >= to) { 901ac27a0ecSDave Kleikamp if (partial && !buffer_uptodate(bh)) 902ac27a0ecSDave Kleikamp *partial = 1; 903ac27a0ecSDave Kleikamp continue; 904ac27a0ecSDave Kleikamp } 905ac27a0ecSDave Kleikamp err = (*fn)(handle, bh); 906ac27a0ecSDave Kleikamp if (!ret) 907ac27a0ecSDave Kleikamp ret = err; 908ac27a0ecSDave Kleikamp } 909ac27a0ecSDave Kleikamp return ret; 910ac27a0ecSDave Kleikamp } 911ac27a0ecSDave Kleikamp 912ac27a0ecSDave Kleikamp /* 913ac27a0ecSDave Kleikamp * To preserve ordering, it is essential that the hole instantiation and 914ac27a0ecSDave Kleikamp * the data write be encapsulated in a single transaction. We cannot 915617ba13bSMingming Cao * close off a transaction and start a new one between the ext4_get_block() 916dab291afSMingming Cao * and the commit_write(). So doing the jbd2_journal_start at the start of 917ac27a0ecSDave Kleikamp * prepare_write() is the right place. 918ac27a0ecSDave Kleikamp * 91936ade451SJan Kara * Also, this function can nest inside ext4_writepage(). In that case, we 92036ade451SJan Kara * *know* that ext4_writepage() has generated enough buffer credits to do the 92136ade451SJan Kara * whole page. So we won't block on the journal in that case, which is good, 92236ade451SJan Kara * because the caller may be PF_MEMALLOC. 923ac27a0ecSDave Kleikamp * 924617ba13bSMingming Cao * By accident, ext4 can be reentered when a transaction is open via 925ac27a0ecSDave Kleikamp * quota file writes. If we were to commit the transaction while thus 926ac27a0ecSDave Kleikamp * reentered, there can be a deadlock - we would be holding a quota 927ac27a0ecSDave Kleikamp * lock, and the commit would never complete if another thread had a 928ac27a0ecSDave Kleikamp * transaction open and was blocking on the quota lock - a ranking 929ac27a0ecSDave Kleikamp * violation. 930ac27a0ecSDave Kleikamp * 931dab291afSMingming Cao * So what we do is to rely on the fact that jbd2_journal_stop/journal_start 932ac27a0ecSDave Kleikamp * will _not_ run commit under these circumstances because handle->h_ref 933ac27a0ecSDave Kleikamp * is elevated. We'll still have enough credits for the tiny quotafile 934ac27a0ecSDave Kleikamp * write. 935ac27a0ecSDave Kleikamp */ 936f19d5870STao Ma int do_journal_get_write_access(handle_t *handle, 937ac27a0ecSDave Kleikamp struct buffer_head *bh) 938ac27a0ecSDave Kleikamp { 93956d35a4cSJan Kara int dirty = buffer_dirty(bh); 94056d35a4cSJan Kara int ret; 94156d35a4cSJan Kara 942ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 943ac27a0ecSDave Kleikamp return 0; 94456d35a4cSJan Kara /* 945ebdec241SChristoph Hellwig * __block_write_begin() could have dirtied some buffers. Clean 94656d35a4cSJan Kara * the dirty bit as jbd2_journal_get_write_access() could complain 94756d35a4cSJan Kara * otherwise about fs integrity issues. Setting of the dirty bit 948ebdec241SChristoph Hellwig * by __block_write_begin() isn't a real problem here as we clear 94956d35a4cSJan Kara * the bit before releasing a page lock and thus writeback cannot 95056d35a4cSJan Kara * ever write the buffer. 95156d35a4cSJan Kara */ 95256d35a4cSJan Kara if (dirty) 95356d35a4cSJan Kara clear_buffer_dirty(bh); 95456d35a4cSJan Kara ret = ext4_journal_get_write_access(handle, bh); 95556d35a4cSJan Kara if (!ret && dirty) 95656d35a4cSJan Kara ret = ext4_handle_dirty_metadata(handle, NULL, bh); 95756d35a4cSJan Kara return ret; 958ac27a0ecSDave Kleikamp } 959ac27a0ecSDave Kleikamp 9608b0f165fSAnatol Pomozov static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock, 9618b0f165fSAnatol Pomozov struct buffer_head *bh_result, int create); 962bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 963bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned flags, 964bfc1af65SNick Piggin struct page **pagep, void **fsdata) 965ac27a0ecSDave Kleikamp { 966bfc1af65SNick Piggin struct inode *inode = mapping->host; 9671938a150SAneesh Kumar K.V int ret, needed_blocks; 968ac27a0ecSDave Kleikamp handle_t *handle; 969ac27a0ecSDave Kleikamp int retries = 0; 970bfc1af65SNick Piggin struct page *page; 971bfc1af65SNick Piggin pgoff_t index; 972bfc1af65SNick Piggin unsigned from, to; 973bfc1af65SNick Piggin 9749bffad1eSTheodore Ts'o trace_ext4_write_begin(inode, pos, len, flags); 9751938a150SAneesh Kumar K.V /* 9761938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 9771938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 9781938a150SAneesh Kumar K.V */ 9791938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 980bfc1af65SNick Piggin index = pos >> PAGE_CACHE_SHIFT; 981bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 982bfc1af65SNick Piggin to = from + len; 983ac27a0ecSDave Kleikamp 984f19d5870STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 985f19d5870STao Ma ret = ext4_try_to_write_inline_data(mapping, inode, pos, len, 986f19d5870STao Ma flags, pagep); 987f19d5870STao Ma if (ret < 0) 98847564bfbSTheodore Ts'o return ret; 98947564bfbSTheodore Ts'o if (ret == 1) 99047564bfbSTheodore Ts'o return 0; 991f19d5870STao Ma } 992f19d5870STao Ma 99347564bfbSTheodore Ts'o /* 99447564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 99547564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 99647564bfbSTheodore Ts'o * is being written back. So grab it first before we start 99747564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 99847564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 99947564bfbSTheodore Ts'o */ 100047564bfbSTheodore Ts'o retry_grab: 100147564bfbSTheodore Ts'o page = grab_cache_page_write_begin(mapping, index, flags); 100247564bfbSTheodore Ts'o if (!page) 100347564bfbSTheodore Ts'o return -ENOMEM; 100447564bfbSTheodore Ts'o unlock_page(page); 100547564bfbSTheodore Ts'o 100647564bfbSTheodore Ts'o retry_journal: 10079924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks); 10087479d2b9SAndrew Morton if (IS_ERR(handle)) { 100947564bfbSTheodore Ts'o page_cache_release(page); 101047564bfbSTheodore Ts'o return PTR_ERR(handle); 10117479d2b9SAndrew Morton } 1012ac27a0ecSDave Kleikamp 101347564bfbSTheodore Ts'o lock_page(page); 101447564bfbSTheodore Ts'o if (page->mapping != mapping) { 101547564bfbSTheodore Ts'o /* The page got truncated from under us */ 101647564bfbSTheodore Ts'o unlock_page(page); 101747564bfbSTheodore Ts'o page_cache_release(page); 1018cf108bcaSJan Kara ext4_journal_stop(handle); 101947564bfbSTheodore Ts'o goto retry_grab; 1020cf108bcaSJan Kara } 102147564bfbSTheodore Ts'o wait_on_page_writeback(page); 1022cf108bcaSJan Kara 1023744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 10246e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block_write); 1025744692dcSJiaying Zhang else 10266e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block); 1027bfc1af65SNick Piggin 1028bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 1029f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_buffers(page), 1030f19d5870STao Ma from, to, NULL, 1031f19d5870STao Ma do_journal_get_write_access); 1032b46be050SAndrey Savochkin } 1033bfc1af65SNick Piggin 1034bfc1af65SNick Piggin if (ret) { 1035bfc1af65SNick Piggin unlock_page(page); 1036ae4d5372SAneesh Kumar K.V /* 10376e1db88dSChristoph Hellwig * __block_write_begin may have instantiated a few blocks 1038ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 1039ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 10401938a150SAneesh Kumar K.V * 10411938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 10421938a150SAneesh Kumar K.V * truncate finishes 1043ae4d5372SAneesh Kumar K.V */ 1044ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 10451938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 10461938a150SAneesh Kumar K.V 10471938a150SAneesh Kumar K.V ext4_journal_stop(handle); 10481938a150SAneesh Kumar K.V if (pos + len > inode->i_size) { 1049b9a4207dSJan Kara ext4_truncate_failed_write(inode); 10501938a150SAneesh Kumar K.V /* 1051ffacfa7aSJan Kara * If truncate failed early the inode might 10521938a150SAneesh Kumar K.V * still be on the orphan list; we need to 10531938a150SAneesh Kumar K.V * make sure the inode is removed from the 10541938a150SAneesh Kumar K.V * orphan list in that case. 10551938a150SAneesh Kumar K.V */ 10561938a150SAneesh Kumar K.V if (inode->i_nlink) 10571938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 10581938a150SAneesh Kumar K.V } 1059bfc1af65SNick Piggin 106047564bfbSTheodore Ts'o if (ret == -ENOSPC && 106147564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 106247564bfbSTheodore Ts'o goto retry_journal; 106347564bfbSTheodore Ts'o page_cache_release(page); 106447564bfbSTheodore Ts'o return ret; 106547564bfbSTheodore Ts'o } 106647564bfbSTheodore Ts'o *pagep = page; 1067ac27a0ecSDave Kleikamp return ret; 1068ac27a0ecSDave Kleikamp } 1069ac27a0ecSDave Kleikamp 1070bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 1071bfc1af65SNick Piggin static int write_end_fn(handle_t *handle, struct buffer_head *bh) 1072ac27a0ecSDave Kleikamp { 1073ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1074ac27a0ecSDave Kleikamp return 0; 1075ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 10760390131bSFrank Mayhar return ext4_handle_dirty_metadata(handle, NULL, bh); 1077ac27a0ecSDave Kleikamp } 1078ac27a0ecSDave Kleikamp 1079f8514083SAneesh Kumar K.V static int ext4_generic_write_end(struct file *file, 1080f8514083SAneesh Kumar K.V struct address_space *mapping, 1081f8514083SAneesh Kumar K.V loff_t pos, unsigned len, unsigned copied, 1082f8514083SAneesh Kumar K.V struct page *page, void *fsdata) 1083f8514083SAneesh Kumar K.V { 1084f8514083SAneesh Kumar K.V int i_size_changed = 0; 1085f8514083SAneesh Kumar K.V struct inode *inode = mapping->host; 1086f8514083SAneesh Kumar K.V handle_t *handle = ext4_journal_current_handle(); 1087f8514083SAneesh Kumar K.V 1088f19d5870STao Ma if (ext4_has_inline_data(inode)) 1089f19d5870STao Ma copied = ext4_write_inline_data_end(inode, pos, len, 1090f19d5870STao Ma copied, page); 1091f19d5870STao Ma else 1092f19d5870STao Ma copied = block_write_end(file, mapping, pos, 1093f19d5870STao Ma len, copied, page, fsdata); 1094f8514083SAneesh Kumar K.V 1095f8514083SAneesh Kumar K.V /* 1096f8514083SAneesh Kumar K.V * No need to use i_size_read() here, the i_size 1097f8514083SAneesh Kumar K.V * cannot change under us because we hold i_mutex. 1098f8514083SAneesh Kumar K.V * 1099f8514083SAneesh Kumar K.V * But it's important to update i_size while still holding page lock: 1100f8514083SAneesh Kumar K.V * page writeout could otherwise come in and zero beyond i_size. 1101f8514083SAneesh Kumar K.V */ 1102f8514083SAneesh Kumar K.V if (pos + copied > inode->i_size) { 1103f8514083SAneesh Kumar K.V i_size_write(inode, pos + copied); 1104f8514083SAneesh Kumar K.V i_size_changed = 1; 1105f8514083SAneesh Kumar K.V } 1106f8514083SAneesh Kumar K.V 1107f8514083SAneesh Kumar K.V if (pos + copied > EXT4_I(inode)->i_disksize) { 1108f8514083SAneesh Kumar K.V /* We need to mark inode dirty even if 1109f8514083SAneesh Kumar K.V * new_i_size is less that inode->i_size 1110f8514083SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 1111f8514083SAneesh Kumar K.V */ 1112f8514083SAneesh Kumar K.V ext4_update_i_disksize(inode, (pos + copied)); 1113f8514083SAneesh Kumar K.V i_size_changed = 1; 1114f8514083SAneesh Kumar K.V } 1115f8514083SAneesh Kumar K.V unlock_page(page); 1116f8514083SAneesh Kumar K.V page_cache_release(page); 1117f8514083SAneesh Kumar K.V 1118f8514083SAneesh Kumar K.V /* 1119f8514083SAneesh Kumar K.V * Don't mark the inode dirty under page lock. First, it unnecessarily 1120f8514083SAneesh Kumar K.V * makes the holding time of page lock longer. Second, it forces lock 1121f8514083SAneesh Kumar K.V * ordering of page lock and transaction start for journaling 1122f8514083SAneesh Kumar K.V * filesystems. 1123f8514083SAneesh Kumar K.V */ 1124f8514083SAneesh Kumar K.V if (i_size_changed) 1125f8514083SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 1126f8514083SAneesh Kumar K.V 1127f8514083SAneesh Kumar K.V return copied; 1128f8514083SAneesh Kumar K.V } 1129f8514083SAneesh Kumar K.V 1130ac27a0ecSDave Kleikamp /* 1131ac27a0ecSDave Kleikamp * We need to pick up the new inode size which generic_commit_write gave us 1132ac27a0ecSDave Kleikamp * `file' can be NULL - eg, when called from page_symlink(). 1133ac27a0ecSDave Kleikamp * 1134617ba13bSMingming Cao * ext4 never places buffers on inode->i_mapping->private_list. metadata 1135ac27a0ecSDave Kleikamp * buffers are managed internally. 1136ac27a0ecSDave Kleikamp */ 1137bfc1af65SNick Piggin static int ext4_ordered_write_end(struct file *file, 1138bfc1af65SNick Piggin struct address_space *mapping, 1139bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1140bfc1af65SNick Piggin struct page *page, void *fsdata) 1141ac27a0ecSDave Kleikamp { 1142617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1143cf108bcaSJan Kara struct inode *inode = mapping->host; 1144ac27a0ecSDave Kleikamp int ret = 0, ret2; 1145ac27a0ecSDave Kleikamp 11469bffad1eSTheodore Ts'o trace_ext4_ordered_write_end(inode, pos, len, copied); 1147678aaf48SJan Kara ret = ext4_jbd2_file_inode(handle, inode); 1148ac27a0ecSDave Kleikamp 1149ac27a0ecSDave Kleikamp if (ret == 0) { 1150f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 1151bfc1af65SNick Piggin page, fsdata); 1152f8a87d89SRoel Kluin copied = ret2; 1153ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1154f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1155f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1156f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1157f8514083SAneesh Kumar K.V */ 1158f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1159f8a87d89SRoel Kluin if (ret2 < 0) 1160f8a87d89SRoel Kluin ret = ret2; 116109e0834fSAkira Fujita } else { 116209e0834fSAkira Fujita unlock_page(page); 116309e0834fSAkira Fujita page_cache_release(page); 1164ac27a0ecSDave Kleikamp } 116509e0834fSAkira Fujita 1166617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1167ac27a0ecSDave Kleikamp if (!ret) 1168ac27a0ecSDave Kleikamp ret = ret2; 1169bfc1af65SNick Piggin 1170f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1171b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1172f8514083SAneesh Kumar K.V /* 1173ffacfa7aSJan Kara * If truncate failed early the inode might still be 1174f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1175f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1176f8514083SAneesh Kumar K.V */ 1177f8514083SAneesh Kumar K.V if (inode->i_nlink) 1178f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1179f8514083SAneesh Kumar K.V } 1180f8514083SAneesh Kumar K.V 1181f8514083SAneesh Kumar K.V 1182bfc1af65SNick Piggin return ret ? ret : copied; 1183ac27a0ecSDave Kleikamp } 1184ac27a0ecSDave Kleikamp 1185bfc1af65SNick Piggin static int ext4_writeback_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(); 1191cf108bcaSJan Kara struct inode *inode = mapping->host; 1192ac27a0ecSDave Kleikamp int ret = 0, ret2; 1193ac27a0ecSDave Kleikamp 11949bffad1eSTheodore Ts'o trace_ext4_writeback_write_end(inode, pos, len, copied); 1195f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 1196bfc1af65SNick Piggin page, fsdata); 1197f8a87d89SRoel Kluin copied = ret2; 1198ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1199f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1200f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1201f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1202f8514083SAneesh Kumar K.V */ 1203f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1204f8514083SAneesh Kumar K.V 1205f8a87d89SRoel Kluin if (ret2 < 0) 1206f8a87d89SRoel Kluin ret = ret2; 1207ac27a0ecSDave Kleikamp 1208617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1209ac27a0ecSDave Kleikamp if (!ret) 1210ac27a0ecSDave Kleikamp ret = ret2; 1211bfc1af65SNick Piggin 1212f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1213b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1214f8514083SAneesh Kumar K.V /* 1215ffacfa7aSJan Kara * If truncate failed early the inode might still be 1216f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1217f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1218f8514083SAneesh Kumar K.V */ 1219f8514083SAneesh Kumar K.V if (inode->i_nlink) 1220f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1221f8514083SAneesh Kumar K.V } 1222f8514083SAneesh Kumar K.V 1223bfc1af65SNick Piggin return ret ? ret : copied; 1224ac27a0ecSDave Kleikamp } 1225ac27a0ecSDave Kleikamp 1226bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 1227bfc1af65SNick Piggin struct address_space *mapping, 1228bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1229bfc1af65SNick Piggin struct page *page, void *fsdata) 1230ac27a0ecSDave Kleikamp { 1231617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1232bfc1af65SNick Piggin struct inode *inode = mapping->host; 1233ac27a0ecSDave Kleikamp int ret = 0, ret2; 1234ac27a0ecSDave Kleikamp int partial = 0; 1235bfc1af65SNick Piggin unsigned from, to; 1236cf17fea6SAneesh Kumar K.V loff_t new_i_size; 1237ac27a0ecSDave Kleikamp 12389bffad1eSTheodore Ts'o trace_ext4_journalled_write_end(inode, pos, len, copied); 1239bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 1240bfc1af65SNick Piggin to = from + len; 1241bfc1af65SNick Piggin 1242441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1243441c8508SCurt Wohlgemuth 12443fdcfb66STao Ma if (ext4_has_inline_data(inode)) 12453fdcfb66STao Ma copied = ext4_write_inline_data_end(inode, pos, len, 12463fdcfb66STao Ma copied, page); 12473fdcfb66STao Ma else { 1248bfc1af65SNick Piggin if (copied < len) { 1249bfc1af65SNick Piggin if (!PageUptodate(page)) 1250bfc1af65SNick Piggin copied = 0; 1251bfc1af65SNick Piggin page_zero_new_buffers(page, from+copied, to); 1252bfc1af65SNick Piggin } 1253ac27a0ecSDave Kleikamp 1254f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_buffers(page), from, 1255bfc1af65SNick Piggin to, &partial, write_end_fn); 1256ac27a0ecSDave Kleikamp if (!partial) 1257ac27a0ecSDave Kleikamp SetPageUptodate(page); 12583fdcfb66STao Ma } 1259cf17fea6SAneesh Kumar K.V new_i_size = pos + copied; 1260cf17fea6SAneesh Kumar K.V if (new_i_size > inode->i_size) 1261bfc1af65SNick Piggin i_size_write(inode, pos+copied); 126219f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 12632d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 1264cf17fea6SAneesh Kumar K.V if (new_i_size > EXT4_I(inode)->i_disksize) { 1265cf17fea6SAneesh Kumar K.V ext4_update_i_disksize(inode, new_i_size); 1266617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1267ac27a0ecSDave Kleikamp if (!ret) 1268ac27a0ecSDave Kleikamp ret = ret2; 1269ac27a0ecSDave Kleikamp } 1270bfc1af65SNick Piggin 1271cf108bcaSJan Kara unlock_page(page); 1272f8514083SAneesh Kumar K.V page_cache_release(page); 1273ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1274f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1275f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1276f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1277f8514083SAneesh Kumar K.V */ 1278f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1279f8514083SAneesh Kumar K.V 1280617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1281ac27a0ecSDave Kleikamp if (!ret) 1282ac27a0ecSDave Kleikamp ret = ret2; 1283f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1284b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1285f8514083SAneesh Kumar K.V /* 1286ffacfa7aSJan Kara * If truncate failed early the inode might still be 1287f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1288f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1289f8514083SAneesh Kumar K.V */ 1290f8514083SAneesh Kumar K.V if (inode->i_nlink) 1291f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1292f8514083SAneesh Kumar K.V } 1293bfc1af65SNick Piggin 1294bfc1af65SNick Piggin return ret ? ret : copied; 1295ac27a0ecSDave Kleikamp } 1296d2a17637SMingming Cao 12979d0be502STheodore Ts'o /* 12987b415bf6SAditya Kali * Reserve a single cluster located at lblock 12999d0be502STheodore Ts'o */ 130001f49d0bSTheodore Ts'o static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock) 1301d2a17637SMingming Cao { 1302030ba6bcSAneesh Kumar K.V int retries = 0; 1303d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 13040637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 13057b415bf6SAditya Kali unsigned int md_needed; 13065dd4056dSChristoph Hellwig int ret; 130703179fe9STheodore Ts'o ext4_lblk_t save_last_lblock; 130803179fe9STheodore Ts'o int save_len; 1309d2a17637SMingming Cao 131060e58e0fSMingming Cao /* 131172b8ab9dSEric Sandeen * We will charge metadata quota at writeout time; this saves 131272b8ab9dSEric Sandeen * us from metadata over-estimation, though we may go over by 131372b8ab9dSEric Sandeen * a small amount in the end. Here we just reserve for data. 131460e58e0fSMingming Cao */ 13157b415bf6SAditya Kali ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1)); 13165dd4056dSChristoph Hellwig if (ret) 13175dd4056dSChristoph Hellwig return ret; 131803179fe9STheodore Ts'o 131903179fe9STheodore Ts'o /* 132003179fe9STheodore Ts'o * recalculate the amount of metadata blocks to reserve 132103179fe9STheodore Ts'o * in order to allocate nrblocks 132203179fe9STheodore Ts'o * worse case is one extent per block 132303179fe9STheodore Ts'o */ 132403179fe9STheodore Ts'o repeat: 132503179fe9STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 132603179fe9STheodore Ts'o /* 132703179fe9STheodore Ts'o * ext4_calc_metadata_amount() has side effects, which we have 132803179fe9STheodore Ts'o * to be prepared undo if we fail to claim space. 132903179fe9STheodore Ts'o */ 133003179fe9STheodore Ts'o save_len = ei->i_da_metadata_calc_len; 133103179fe9STheodore Ts'o save_last_lblock = ei->i_da_metadata_calc_last_lblock; 133203179fe9STheodore Ts'o md_needed = EXT4_NUM_B2C(sbi, 133303179fe9STheodore Ts'o ext4_calc_metadata_amount(inode, lblock)); 133403179fe9STheodore Ts'o trace_ext4_da_reserve_space(inode, md_needed); 133503179fe9STheodore Ts'o 133672b8ab9dSEric Sandeen /* 133772b8ab9dSEric Sandeen * We do still charge estimated metadata to the sb though; 133872b8ab9dSEric Sandeen * we cannot afford to run out of free blocks. 133972b8ab9dSEric Sandeen */ 1340e7d5f315STheodore Ts'o if (ext4_claim_free_clusters(sbi, md_needed + 1, 0)) { 134103179fe9STheodore Ts'o ei->i_da_metadata_calc_len = save_len; 134203179fe9STheodore Ts'o ei->i_da_metadata_calc_last_lblock = save_last_lblock; 134303179fe9STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 1344030ba6bcSAneesh Kumar K.V if (ext4_should_retry_alloc(inode->i_sb, &retries)) { 1345030ba6bcSAneesh Kumar K.V yield(); 1346030ba6bcSAneesh Kumar K.V goto repeat; 1347030ba6bcSAneesh Kumar K.V } 134803179fe9STheodore Ts'o dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1)); 1349d2a17637SMingming Cao return -ENOSPC; 1350d2a17637SMingming Cao } 13519d0be502STheodore Ts'o ei->i_reserved_data_blocks++; 13520637c6f4STheodore Ts'o ei->i_reserved_meta_blocks += md_needed; 13530637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 135439bc680aSDmitry Monakhov 1355d2a17637SMingming Cao return 0; /* success */ 1356d2a17637SMingming Cao } 1357d2a17637SMingming Cao 135812219aeaSAneesh Kumar K.V static void ext4_da_release_space(struct inode *inode, int to_free) 1359d2a17637SMingming Cao { 1360d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 13610637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 1362d2a17637SMingming Cao 1363cd213226SMingming Cao if (!to_free) 1364cd213226SMingming Cao return; /* Nothing to release, exit */ 1365cd213226SMingming Cao 1366d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1367cd213226SMingming Cao 13685a58ec87SLi Zefan trace_ext4_da_release_space(inode, to_free); 13690637c6f4STheodore Ts'o if (unlikely(to_free > ei->i_reserved_data_blocks)) { 1370cd213226SMingming Cao /* 13710637c6f4STheodore Ts'o * if there aren't enough reserved blocks, then the 13720637c6f4STheodore Ts'o * counter is messed up somewhere. Since this 13730637c6f4STheodore Ts'o * function is called from invalidate page, it's 13740637c6f4STheodore Ts'o * harmless to return without any action. 1375cd213226SMingming Cao */ 13768de5c325STheodore Ts'o ext4_warning(inode->i_sb, "ext4_da_release_space: " 13770637c6f4STheodore Ts'o "ino %lu, to_free %d with only %d reserved " 13781084f252STheodore Ts'o "data blocks", inode->i_ino, to_free, 13790637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 13800637c6f4STheodore Ts'o WARN_ON(1); 13810637c6f4STheodore Ts'o to_free = ei->i_reserved_data_blocks; 13820637c6f4STheodore Ts'o } 13830637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= to_free; 13840637c6f4STheodore Ts'o 13850637c6f4STheodore Ts'o if (ei->i_reserved_data_blocks == 0) { 13860637c6f4STheodore Ts'o /* 13870637c6f4STheodore Ts'o * We can release all of the reserved metadata blocks 13880637c6f4STheodore Ts'o * only when we have written all of the delayed 13890637c6f4STheodore Ts'o * allocation blocks. 13907b415bf6SAditya Kali * Note that in case of bigalloc, i_reserved_meta_blocks, 13917b415bf6SAditya Kali * i_reserved_data_blocks, etc. refer to number of clusters. 13920637c6f4STheodore Ts'o */ 139357042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 139472b8ab9dSEric Sandeen ei->i_reserved_meta_blocks); 1395ee5f4d9cSTheodore Ts'o ei->i_reserved_meta_blocks = 0; 13969d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 1397cd213226SMingming Cao } 1398cd213226SMingming Cao 139972b8ab9dSEric Sandeen /* update fs dirty data blocks counter */ 140057042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free); 1401d2a17637SMingming Cao 1402d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 140360e58e0fSMingming Cao 14047b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free)); 1405d2a17637SMingming Cao } 1406d2a17637SMingming Cao 1407d2a17637SMingming Cao static void ext4_da_page_release_reservation(struct page *page, 1408d2a17637SMingming Cao unsigned long offset) 1409d2a17637SMingming Cao { 1410d2a17637SMingming Cao int to_release = 0; 1411d2a17637SMingming Cao struct buffer_head *head, *bh; 1412d2a17637SMingming Cao unsigned int curr_off = 0; 14137b415bf6SAditya Kali struct inode *inode = page->mapping->host; 14147b415bf6SAditya Kali struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 14157b415bf6SAditya Kali int num_clusters; 141651865fdaSZheng Liu ext4_fsblk_t lblk; 1417d2a17637SMingming Cao 1418d2a17637SMingming Cao head = page_buffers(page); 1419d2a17637SMingming Cao bh = head; 1420d2a17637SMingming Cao do { 1421d2a17637SMingming Cao unsigned int next_off = curr_off + bh->b_size; 1422d2a17637SMingming Cao 1423d2a17637SMingming Cao if ((offset <= curr_off) && (buffer_delay(bh))) { 1424d2a17637SMingming Cao to_release++; 1425d2a17637SMingming Cao clear_buffer_delay(bh); 1426d2a17637SMingming Cao } 1427d2a17637SMingming Cao curr_off = next_off; 1428d2a17637SMingming Cao } while ((bh = bh->b_this_page) != head); 14297b415bf6SAditya Kali 143051865fdaSZheng Liu if (to_release) { 143151865fdaSZheng Liu lblk = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits); 143251865fdaSZheng Liu ext4_es_remove_extent(inode, lblk, to_release); 143351865fdaSZheng Liu } 143451865fdaSZheng Liu 14357b415bf6SAditya Kali /* If we have released all the blocks belonging to a cluster, then we 14367b415bf6SAditya Kali * need to release the reserved space for that cluster. */ 14377b415bf6SAditya Kali num_clusters = EXT4_NUM_B2C(sbi, to_release); 14387b415bf6SAditya Kali while (num_clusters > 0) { 14397b415bf6SAditya Kali lblk = (page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits)) + 14407b415bf6SAditya Kali ((num_clusters - 1) << sbi->s_cluster_bits); 14417b415bf6SAditya Kali if (sbi->s_cluster_ratio == 1 || 14427d1b1fbcSZheng Liu !ext4_find_delalloc_cluster(inode, lblk)) 14437b415bf6SAditya Kali ext4_da_release_space(inode, 1); 14447b415bf6SAditya Kali 14457b415bf6SAditya Kali num_clusters--; 14467b415bf6SAditya Kali } 1447d2a17637SMingming Cao } 1448ac27a0ecSDave Kleikamp 1449ac27a0ecSDave Kleikamp /* 145064769240SAlex Tomas * Delayed allocation stuff 145164769240SAlex Tomas */ 145264769240SAlex Tomas 145364769240SAlex Tomas /* 145464769240SAlex Tomas * mpage_da_submit_io - walks through extent of pages and try to write 1455a1d6cc56SAneesh Kumar K.V * them with writepage() call back 145664769240SAlex Tomas * 145764769240SAlex Tomas * @mpd->inode: inode 145864769240SAlex Tomas * @mpd->first_page: first page of the extent 145964769240SAlex Tomas * @mpd->next_page: page after the last page of the extent 146064769240SAlex Tomas * 146164769240SAlex Tomas * By the time mpage_da_submit_io() is called we expect all blocks 146264769240SAlex Tomas * to be allocated. this may be wrong if allocation failed. 146364769240SAlex Tomas * 146464769240SAlex Tomas * As pages are already locked by write_cache_pages(), we can't use it 146564769240SAlex Tomas */ 14661de3e3dfSTheodore Ts'o static int mpage_da_submit_io(struct mpage_da_data *mpd, 14671de3e3dfSTheodore Ts'o struct ext4_map_blocks *map) 146864769240SAlex Tomas { 1469791b7f08SAneesh Kumar K.V struct pagevec pvec; 1470791b7f08SAneesh Kumar K.V unsigned long index, end; 1471791b7f08SAneesh Kumar K.V int ret = 0, err, nr_pages, i; 1472791b7f08SAneesh Kumar K.V struct inode *inode = mpd->inode; 1473791b7f08SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 1474cb20d518STheodore Ts'o loff_t size = i_size_read(inode); 14753ecdb3a1STheodore Ts'o unsigned int len, block_start; 14763ecdb3a1STheodore Ts'o struct buffer_head *bh, *page_bufs = NULL; 14771de3e3dfSTheodore Ts'o sector_t pblock = 0, cur_logical = 0; 1478bd2d0210STheodore Ts'o struct ext4_io_submit io_submit; 147964769240SAlex Tomas 148064769240SAlex Tomas BUG_ON(mpd->next_page <= mpd->first_page); 1481bd2d0210STheodore Ts'o memset(&io_submit, 0, sizeof(io_submit)); 1482791b7f08SAneesh Kumar K.V /* 1483791b7f08SAneesh Kumar K.V * We need to start from the first_page to the next_page - 1 1484791b7f08SAneesh Kumar K.V * to make sure we also write the mapped dirty buffer_heads. 14858dc207c0STheodore Ts'o * If we look at mpd->b_blocknr we would only be looking 1486791b7f08SAneesh Kumar K.V * at the currently mapped buffer_heads. 1487791b7f08SAneesh Kumar K.V */ 148864769240SAlex Tomas index = mpd->first_page; 148964769240SAlex Tomas end = mpd->next_page - 1; 149064769240SAlex Tomas 1491791b7f08SAneesh Kumar K.V pagevec_init(&pvec, 0); 149264769240SAlex Tomas while (index <= end) { 1493791b7f08SAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 149464769240SAlex Tomas if (nr_pages == 0) 149564769240SAlex Tomas break; 149664769240SAlex Tomas for (i = 0; i < nr_pages; i++) { 1497f8bec370SJan Kara int skip_page = 0; 149864769240SAlex Tomas struct page *page = pvec.pages[i]; 149964769240SAlex Tomas 1500791b7f08SAneesh Kumar K.V index = page->index; 1501791b7f08SAneesh Kumar K.V if (index > end) 1502791b7f08SAneesh Kumar K.V break; 1503cb20d518STheodore Ts'o 1504cb20d518STheodore Ts'o if (index == size >> PAGE_CACHE_SHIFT) 1505cb20d518STheodore Ts'o len = size & ~PAGE_CACHE_MASK; 1506cb20d518STheodore Ts'o else 1507cb20d518STheodore Ts'o len = PAGE_CACHE_SIZE; 15081de3e3dfSTheodore Ts'o if (map) { 15091de3e3dfSTheodore Ts'o cur_logical = index << (PAGE_CACHE_SHIFT - 15101de3e3dfSTheodore Ts'o inode->i_blkbits); 15111de3e3dfSTheodore Ts'o pblock = map->m_pblk + (cur_logical - 15121de3e3dfSTheodore Ts'o map->m_lblk); 15131de3e3dfSTheodore Ts'o } 1514791b7f08SAneesh Kumar K.V index++; 1515791b7f08SAneesh Kumar K.V 1516791b7f08SAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1517791b7f08SAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1518791b7f08SAneesh Kumar K.V 15193ecdb3a1STheodore Ts'o bh = page_bufs = page_buffers(page); 15203ecdb3a1STheodore Ts'o block_start = 0; 15213ecdb3a1STheodore Ts'o do { 15221de3e3dfSTheodore Ts'o if (map && (cur_logical >= map->m_lblk) && 15231de3e3dfSTheodore Ts'o (cur_logical <= (map->m_lblk + 15241de3e3dfSTheodore Ts'o (map->m_len - 1)))) { 15251de3e3dfSTheodore Ts'o if (buffer_delay(bh)) { 15261de3e3dfSTheodore Ts'o clear_buffer_delay(bh); 15271de3e3dfSTheodore Ts'o bh->b_blocknr = pblock; 15281de3e3dfSTheodore Ts'o } 15291de3e3dfSTheodore Ts'o if (buffer_unwritten(bh) || 15301de3e3dfSTheodore Ts'o buffer_mapped(bh)) 15311de3e3dfSTheodore Ts'o BUG_ON(bh->b_blocknr != pblock); 15321de3e3dfSTheodore Ts'o if (map->m_flags & EXT4_MAP_UNINIT) 15331de3e3dfSTheodore Ts'o set_buffer_uninit(bh); 15341de3e3dfSTheodore Ts'o clear_buffer_unwritten(bh); 15351de3e3dfSTheodore Ts'o } 15361de3e3dfSTheodore Ts'o 153713a79a47SYongqiang Yang /* 153813a79a47SYongqiang Yang * skip page if block allocation undone and 153913a79a47SYongqiang Yang * block is dirty 154013a79a47SYongqiang Yang */ 154113a79a47SYongqiang Yang if (ext4_bh_delay_or_unwritten(NULL, bh)) 154297498956STheodore Ts'o skip_page = 1; 15433ecdb3a1STheodore Ts'o bh = bh->b_this_page; 15443ecdb3a1STheodore Ts'o block_start += bh->b_size; 15451de3e3dfSTheodore Ts'o cur_logical++; 15461de3e3dfSTheodore Ts'o pblock++; 15471de3e3dfSTheodore Ts'o } while (bh != page_bufs); 15481de3e3dfSTheodore Ts'o 1549f8bec370SJan Kara if (skip_page) { 1550f8bec370SJan Kara unlock_page(page); 1551f8bec370SJan Kara continue; 1552f8bec370SJan Kara } 1553cb20d518STheodore Ts'o 155497498956STheodore Ts'o clear_page_dirty_for_io(page); 1555fe089c77SJan Kara err = ext4_bio_write_page(&io_submit, page, len, 1556fe089c77SJan Kara mpd->wbc); 1557cb20d518STheodore Ts'o if (!err) 1558a1d6cc56SAneesh Kumar K.V mpd->pages_written++; 155964769240SAlex Tomas /* 156064769240SAlex Tomas * In error case, we have to continue because 156164769240SAlex Tomas * remaining pages are still locked 156264769240SAlex Tomas */ 156364769240SAlex Tomas if (ret == 0) 156464769240SAlex Tomas ret = err; 156564769240SAlex Tomas } 156664769240SAlex Tomas pagevec_release(&pvec); 156764769240SAlex Tomas } 1568bd2d0210STheodore Ts'o ext4_io_submit(&io_submit); 156964769240SAlex Tomas return ret; 157064769240SAlex Tomas } 157164769240SAlex Tomas 1572c7f5938aSCurt Wohlgemuth static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd) 1573c4a0c46eSAneesh Kumar K.V { 1574c4a0c46eSAneesh Kumar K.V int nr_pages, i; 1575c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 1576c4a0c46eSAneesh Kumar K.V struct pagevec pvec; 1577c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 1578c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 157951865fdaSZheng Liu ext4_lblk_t start, last; 1580c4a0c46eSAneesh Kumar K.V 1581c7f5938aSCurt Wohlgemuth index = mpd->first_page; 1582c7f5938aSCurt Wohlgemuth end = mpd->next_page - 1; 158351865fdaSZheng Liu 158451865fdaSZheng Liu start = index << (PAGE_CACHE_SHIFT - inode->i_blkbits); 158551865fdaSZheng Liu last = end << (PAGE_CACHE_SHIFT - inode->i_blkbits); 158651865fdaSZheng Liu ext4_es_remove_extent(inode, start, last - start + 1); 158751865fdaSZheng Liu 158866bea92cSEric Sandeen pagevec_init(&pvec, 0); 1589c4a0c46eSAneesh Kumar K.V while (index <= end) { 1590c4a0c46eSAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 1591c4a0c46eSAneesh Kumar K.V if (nr_pages == 0) 1592c4a0c46eSAneesh Kumar K.V break; 1593c4a0c46eSAneesh Kumar K.V for (i = 0; i < nr_pages; i++) { 1594c4a0c46eSAneesh Kumar K.V struct page *page = pvec.pages[i]; 15959b1d0998SJan Kara if (page->index > end) 1596c4a0c46eSAneesh Kumar K.V break; 1597c4a0c46eSAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1598c4a0c46eSAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1599c4a0c46eSAneesh Kumar K.V block_invalidatepage(page, 0); 1600c4a0c46eSAneesh Kumar K.V ClearPageUptodate(page); 1601c4a0c46eSAneesh Kumar K.V unlock_page(page); 1602c4a0c46eSAneesh Kumar K.V } 16039b1d0998SJan Kara index = pvec.pages[nr_pages - 1]->index + 1; 16049b1d0998SJan Kara pagevec_release(&pvec); 1605c4a0c46eSAneesh Kumar K.V } 1606c4a0c46eSAneesh Kumar K.V return; 1607c4a0c46eSAneesh Kumar K.V } 1608c4a0c46eSAneesh Kumar K.V 1609df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 1610df22291fSAneesh Kumar K.V { 1611df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 161292b97816STheodore Ts'o struct super_block *sb = inode->i_sb; 161392b97816STheodore Ts'o 161492b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld", 16155dee5437STheodore Ts'o EXT4_C2B(EXT4_SB(inode->i_sb), 16165dee5437STheodore Ts'o ext4_count_free_clusters(inode->i_sb))); 161792b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Free/Dirty block details"); 161892b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "free_blocks=%lld", 161957042651STheodore Ts'o (long long) EXT4_C2B(EXT4_SB(inode->i_sb), 162057042651STheodore Ts'o percpu_counter_sum(&sbi->s_freeclusters_counter))); 162192b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld", 16227b415bf6SAditya Kali (long long) EXT4_C2B(EXT4_SB(inode->i_sb), 16237b415bf6SAditya Kali percpu_counter_sum(&sbi->s_dirtyclusters_counter))); 162492b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Block reservation details"); 162592b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u", 1626df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_data_blocks); 162792b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_meta_blocks=%u", 1628df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_meta_blocks); 1629df22291fSAneesh Kumar K.V return; 1630df22291fSAneesh Kumar K.V } 1631df22291fSAneesh Kumar K.V 1632b920c755STheodore Ts'o /* 16335a87b7a5STheodore Ts'o * mpage_da_map_and_submit - go through given space, map them 16345a87b7a5STheodore Ts'o * if necessary, and then submit them for I/O 163564769240SAlex Tomas * 16368dc207c0STheodore Ts'o * @mpd - bh describing space 163764769240SAlex Tomas * 163864769240SAlex Tomas * The function skips space we know is already mapped to disk blocks. 163964769240SAlex Tomas * 164064769240SAlex Tomas */ 16415a87b7a5STheodore Ts'o static void mpage_da_map_and_submit(struct mpage_da_data *mpd) 164264769240SAlex Tomas { 16432ac3b6e0STheodore Ts'o int err, blks, get_blocks_flags; 16441de3e3dfSTheodore Ts'o struct ext4_map_blocks map, *mapp = NULL; 16452fa3cdfbSTheodore Ts'o sector_t next = mpd->b_blocknr; 16462fa3cdfbSTheodore Ts'o unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits; 16472fa3cdfbSTheodore Ts'o loff_t disksize = EXT4_I(mpd->inode)->i_disksize; 16482fa3cdfbSTheodore Ts'o handle_t *handle = NULL; 164964769240SAlex Tomas 165064769240SAlex Tomas /* 16515a87b7a5STheodore Ts'o * If the blocks are mapped already, or we couldn't accumulate 16525a87b7a5STheodore Ts'o * any blocks, then proceed immediately to the submission stage. 165364769240SAlex Tomas */ 16545a87b7a5STheodore Ts'o if ((mpd->b_size == 0) || 16555a87b7a5STheodore Ts'o ((mpd->b_state & (1 << BH_Mapped)) && 165629fa89d0SAneesh Kumar K.V !(mpd->b_state & (1 << BH_Delay)) && 16575a87b7a5STheodore Ts'o !(mpd->b_state & (1 << BH_Unwritten)))) 16585a87b7a5STheodore Ts'o goto submit_io; 16592fa3cdfbSTheodore Ts'o 16602fa3cdfbSTheodore Ts'o handle = ext4_journal_current_handle(); 16612fa3cdfbSTheodore Ts'o BUG_ON(!handle); 16622fa3cdfbSTheodore Ts'o 166379ffab34SAneesh Kumar K.V /* 166479e83036SEric Sandeen * Call ext4_map_blocks() to allocate any delayed allocation 16652ac3b6e0STheodore Ts'o * blocks, or to convert an uninitialized extent to be 16662ac3b6e0STheodore Ts'o * initialized (in the case where we have written into 16672ac3b6e0STheodore Ts'o * one or more preallocated blocks). 16682ac3b6e0STheodore Ts'o * 16692ac3b6e0STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to 16702ac3b6e0STheodore Ts'o * indicate that we are on the delayed allocation path. This 16712ac3b6e0STheodore Ts'o * affects functions in many different parts of the allocation 16722ac3b6e0STheodore Ts'o * call path. This flag exists primarily because we don't 167379e83036SEric Sandeen * want to change *many* call functions, so ext4_map_blocks() 1674f2321097STheodore Ts'o * will set the EXT4_STATE_DELALLOC_RESERVED flag once the 16752ac3b6e0STheodore Ts'o * inode's allocation semaphore is taken. 16762ac3b6e0STheodore Ts'o * 16772ac3b6e0STheodore Ts'o * If the blocks in questions were delalloc blocks, set 16782ac3b6e0STheodore Ts'o * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting 16792ac3b6e0STheodore Ts'o * variables are updated after the blocks have been allocated. 168079ffab34SAneesh Kumar K.V */ 16812ed88685STheodore Ts'o map.m_lblk = next; 16822ed88685STheodore Ts'o map.m_len = max_blocks; 16831296cc85SAneesh Kumar K.V get_blocks_flags = EXT4_GET_BLOCKS_CREATE; 1684744692dcSJiaying Zhang if (ext4_should_dioread_nolock(mpd->inode)) 1685744692dcSJiaying Zhang get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; 16862ac3b6e0STheodore Ts'o if (mpd->b_state & (1 << BH_Delay)) 16871296cc85SAneesh Kumar K.V get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; 16881296cc85SAneesh Kumar K.V 16892ed88685STheodore Ts'o blks = ext4_map_blocks(handle, mpd->inode, &map, get_blocks_flags); 16902fa3cdfbSTheodore Ts'o if (blks < 0) { 1691e3570639SEric Sandeen struct super_block *sb = mpd->inode->i_sb; 1692e3570639SEric Sandeen 16932fa3cdfbSTheodore Ts'o err = blks; 1694ed5bde0bSTheodore Ts'o /* 16955a87b7a5STheodore Ts'o * If get block returns EAGAIN or ENOSPC and there 169697498956STheodore Ts'o * appears to be free blocks we will just let 169797498956STheodore Ts'o * mpage_da_submit_io() unlock all of the pages. 1698c4a0c46eSAneesh Kumar K.V */ 1699c4a0c46eSAneesh Kumar K.V if (err == -EAGAIN) 17005a87b7a5STheodore Ts'o goto submit_io; 1701df22291fSAneesh Kumar K.V 17025dee5437STheodore Ts'o if (err == -ENOSPC && ext4_count_free_clusters(sb)) { 1703df22291fSAneesh Kumar K.V mpd->retval = err; 17045a87b7a5STheodore Ts'o goto submit_io; 1705df22291fSAneesh Kumar K.V } 1706df22291fSAneesh Kumar K.V 1707c4a0c46eSAneesh Kumar K.V /* 1708ed5bde0bSTheodore Ts'o * get block failure will cause us to loop in 1709ed5bde0bSTheodore Ts'o * writepages, because a_ops->writepage won't be able 1710ed5bde0bSTheodore Ts'o * to make progress. The page will be redirtied by 1711ed5bde0bSTheodore Ts'o * writepage and writepages will again try to write 1712ed5bde0bSTheodore Ts'o * the same. 1713c4a0c46eSAneesh Kumar K.V */ 1714e3570639SEric Sandeen if (!(EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) { 1715e3570639SEric Sandeen ext4_msg(sb, KERN_CRIT, 1716e3570639SEric Sandeen "delayed block allocation failed for inode %lu " 1717e3570639SEric Sandeen "at logical offset %llu with max blocks %zd " 1718e3570639SEric Sandeen "with error %d", mpd->inode->i_ino, 1719c4a0c46eSAneesh Kumar K.V (unsigned long long) next, 17208dc207c0STheodore Ts'o mpd->b_size >> mpd->inode->i_blkbits, err); 1721e3570639SEric Sandeen ext4_msg(sb, KERN_CRIT, 172201a523ebSTheodore Ts'o "This should not happen!! Data will be lost"); 1723e3570639SEric Sandeen if (err == -ENOSPC) 1724df22291fSAneesh Kumar K.V ext4_print_free_blocks(mpd->inode); 1725030ba6bcSAneesh Kumar K.V } 17262fa3cdfbSTheodore Ts'o /* invalidate all the pages */ 1727c7f5938aSCurt Wohlgemuth ext4_da_block_invalidatepages(mpd); 1728e0fd9b90SCurt Wohlgemuth 1729e0fd9b90SCurt Wohlgemuth /* Mark this page range as having been completed */ 1730e0fd9b90SCurt Wohlgemuth mpd->io_done = 1; 17315a87b7a5STheodore Ts'o return; 1732c4a0c46eSAneesh Kumar K.V } 17332fa3cdfbSTheodore Ts'o BUG_ON(blks == 0); 17342fa3cdfbSTheodore Ts'o 17351de3e3dfSTheodore Ts'o mapp = ↦ 17362ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 17372ed88685STheodore Ts'o struct block_device *bdev = mpd->inode->i_sb->s_bdev; 17382ed88685STheodore Ts'o int i; 173964769240SAlex Tomas 17402ed88685STheodore Ts'o for (i = 0; i < map.m_len; i++) 17412ed88685STheodore Ts'o unmap_underlying_metadata(bdev, map.m_pblk + i); 17422fa3cdfbSTheodore Ts'o } 17432fa3cdfbSTheodore Ts'o 17442fa3cdfbSTheodore Ts'o /* 174503f5d8bcSJan Kara * Update on-disk size along with block allocation. 17462fa3cdfbSTheodore Ts'o */ 17472fa3cdfbSTheodore Ts'o disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits; 17482fa3cdfbSTheodore Ts'o if (disksize > i_size_read(mpd->inode)) 17492fa3cdfbSTheodore Ts'o disksize = i_size_read(mpd->inode); 17502fa3cdfbSTheodore Ts'o if (disksize > EXT4_I(mpd->inode)->i_disksize) { 17512fa3cdfbSTheodore Ts'o ext4_update_i_disksize(mpd->inode, disksize); 17525a87b7a5STheodore Ts'o err = ext4_mark_inode_dirty(handle, mpd->inode); 17535a87b7a5STheodore Ts'o if (err) 17545a87b7a5STheodore Ts'o ext4_error(mpd->inode->i_sb, 17555a87b7a5STheodore Ts'o "Failed to mark inode %lu dirty", 17565a87b7a5STheodore Ts'o mpd->inode->i_ino); 17572fa3cdfbSTheodore Ts'o } 17582fa3cdfbSTheodore Ts'o 17595a87b7a5STheodore Ts'o submit_io: 17601de3e3dfSTheodore Ts'o mpage_da_submit_io(mpd, mapp); 17615a87b7a5STheodore Ts'o mpd->io_done = 1; 176264769240SAlex Tomas } 176364769240SAlex Tomas 1764bf068ee2SAneesh Kumar K.V #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \ 1765bf068ee2SAneesh Kumar K.V (1 << BH_Delay) | (1 << BH_Unwritten)) 176664769240SAlex Tomas 176764769240SAlex Tomas /* 176864769240SAlex Tomas * mpage_add_bh_to_extent - try to add one more block to extent of blocks 176964769240SAlex Tomas * 177064769240SAlex Tomas * @mpd->lbh - extent of blocks 177164769240SAlex Tomas * @logical - logical number of the block in the file 1772b6a8e62fSJan Kara * @b_state - b_state of the buffer head added 177364769240SAlex Tomas * 177464769240SAlex Tomas * the function is used to collect contig. blocks in same state 177564769240SAlex Tomas */ 1776b6a8e62fSJan Kara static void mpage_add_bh_to_extent(struct mpage_da_data *mpd, sector_t logical, 17778dc207c0STheodore Ts'o unsigned long b_state) 177864769240SAlex Tomas { 177964769240SAlex Tomas sector_t next; 1780b6a8e62fSJan Kara int blkbits = mpd->inode->i_blkbits; 1781b6a8e62fSJan Kara int nrblocks = mpd->b_size >> blkbits; 178264769240SAlex Tomas 1783c445e3e0SEric Sandeen /* 1784c445e3e0SEric Sandeen * XXX Don't go larger than mballoc is willing to allocate 1785c445e3e0SEric Sandeen * This is a stopgap solution. We eventually need to fold 1786c445e3e0SEric Sandeen * mpage_da_submit_io() into this function and then call 178779e83036SEric Sandeen * ext4_map_blocks() multiple times in a loop 1788c445e3e0SEric Sandeen */ 1789b6a8e62fSJan Kara if (nrblocks >= (8*1024*1024 >> blkbits)) 1790c445e3e0SEric Sandeen goto flush_it; 1791c445e3e0SEric Sandeen 1792525f4ed8SMingming Cao /* check if the reserved journal credits might overflow */ 1793b6a8e62fSJan Kara if (!ext4_test_inode_flag(mpd->inode, EXT4_INODE_EXTENTS)) { 1794525f4ed8SMingming Cao if (nrblocks >= EXT4_MAX_TRANS_DATA) { 1795525f4ed8SMingming Cao /* 1796525f4ed8SMingming Cao * With non-extent format we are limited by the journal 1797525f4ed8SMingming Cao * credit available. Total credit needed to insert 1798525f4ed8SMingming Cao * nrblocks contiguous blocks is dependent on the 1799525f4ed8SMingming Cao * nrblocks. So limit nrblocks. 1800525f4ed8SMingming Cao */ 1801525f4ed8SMingming Cao goto flush_it; 1802525f4ed8SMingming Cao } 1803525f4ed8SMingming Cao } 180464769240SAlex Tomas /* 180564769240SAlex Tomas * First block in the extent 180664769240SAlex Tomas */ 18078dc207c0STheodore Ts'o if (mpd->b_size == 0) { 18088dc207c0STheodore Ts'o mpd->b_blocknr = logical; 1809b6a8e62fSJan Kara mpd->b_size = 1 << blkbits; 18108dc207c0STheodore Ts'o mpd->b_state = b_state & BH_FLAGS; 181164769240SAlex Tomas return; 181264769240SAlex Tomas } 181364769240SAlex Tomas 18148dc207c0STheodore Ts'o next = mpd->b_blocknr + nrblocks; 181564769240SAlex Tomas /* 181664769240SAlex Tomas * Can we merge the block to our big extent? 181764769240SAlex Tomas */ 18188dc207c0STheodore Ts'o if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) { 1819b6a8e62fSJan Kara mpd->b_size += 1 << blkbits; 182064769240SAlex Tomas return; 182164769240SAlex Tomas } 182264769240SAlex Tomas 1823525f4ed8SMingming Cao flush_it: 182464769240SAlex Tomas /* 182564769240SAlex Tomas * We couldn't merge the block to our extent, so we 182664769240SAlex Tomas * need to flush current extent and start new one 182764769240SAlex Tomas */ 18285a87b7a5STheodore Ts'o mpage_da_map_and_submit(mpd); 1829a1d6cc56SAneesh Kumar K.V return; 183064769240SAlex Tomas } 183164769240SAlex Tomas 1832c364b22cSAneesh Kumar K.V static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh) 183329fa89d0SAneesh Kumar K.V { 1834c364b22cSAneesh Kumar K.V return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 183529fa89d0SAneesh Kumar K.V } 183629fa89d0SAneesh Kumar K.V 183764769240SAlex Tomas /* 18385356f261SAditya Kali * This function is grabs code from the very beginning of 18395356f261SAditya Kali * ext4_map_blocks, but assumes that the caller is from delayed write 18405356f261SAditya Kali * time. This function looks up the requested blocks and sets the 18415356f261SAditya Kali * buffer delay bit under the protection of i_data_sem. 18425356f261SAditya Kali */ 18435356f261SAditya Kali static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, 18445356f261SAditya Kali struct ext4_map_blocks *map, 18455356f261SAditya Kali struct buffer_head *bh) 18465356f261SAditya Kali { 1847d100eef2SZheng Liu struct extent_status es; 18485356f261SAditya Kali int retval; 18495356f261SAditya Kali sector_t invalid_block = ~((sector_t) 0xffff); 1850*921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1851*921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 1852*921f266bSDmitry Monakhov 1853*921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 1854*921f266bSDmitry Monakhov #endif 18555356f261SAditya Kali 18565356f261SAditya Kali if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 18575356f261SAditya Kali invalid_block = ~0; 18585356f261SAditya Kali 18595356f261SAditya Kali map->m_flags = 0; 18605356f261SAditya Kali ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u," 18615356f261SAditya Kali "logical block %lu\n", inode->i_ino, map->m_len, 18625356f261SAditya Kali (unsigned long) map->m_lblk); 1863d100eef2SZheng Liu 1864d100eef2SZheng Liu /* Lookup extent status tree firstly */ 1865d100eef2SZheng Liu if (ext4_es_lookup_extent(inode, iblock, &es)) { 1866d100eef2SZheng Liu 1867d100eef2SZheng Liu if (ext4_es_is_hole(&es)) { 1868d100eef2SZheng Liu retval = 0; 1869d100eef2SZheng Liu down_read((&EXT4_I(inode)->i_data_sem)); 1870d100eef2SZheng Liu goto add_delayed; 1871d100eef2SZheng Liu } 1872d100eef2SZheng Liu 1873d100eef2SZheng Liu /* 1874d100eef2SZheng Liu * Delayed extent could be allocated by fallocate. 1875d100eef2SZheng Liu * So we need to check it. 1876d100eef2SZheng Liu */ 1877d100eef2SZheng Liu if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) { 1878d100eef2SZheng Liu map_bh(bh, inode->i_sb, invalid_block); 1879d100eef2SZheng Liu set_buffer_new(bh); 1880d100eef2SZheng Liu set_buffer_delay(bh); 1881d100eef2SZheng Liu return 0; 1882d100eef2SZheng Liu } 1883d100eef2SZheng Liu 1884d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk; 1885d100eef2SZheng Liu retval = es.es_len - (iblock - es.es_lblk); 1886d100eef2SZheng Liu if (retval > map->m_len) 1887d100eef2SZheng Liu retval = map->m_len; 1888d100eef2SZheng Liu map->m_len = retval; 1889d100eef2SZheng Liu if (ext4_es_is_written(&es)) 1890d100eef2SZheng Liu map->m_flags |= EXT4_MAP_MAPPED; 1891d100eef2SZheng Liu else if (ext4_es_is_unwritten(&es)) 1892d100eef2SZheng Liu map->m_flags |= EXT4_MAP_UNWRITTEN; 1893d100eef2SZheng Liu else 1894d100eef2SZheng Liu BUG_ON(1); 1895d100eef2SZheng Liu 1896*921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1897*921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0); 1898*921f266bSDmitry Monakhov #endif 1899d100eef2SZheng Liu return retval; 1900d100eef2SZheng Liu } 1901d100eef2SZheng Liu 19025356f261SAditya Kali /* 19035356f261SAditya Kali * Try to see if we can get the block without requesting a new 19045356f261SAditya Kali * file system block. 19055356f261SAditya Kali */ 19065356f261SAditya Kali down_read((&EXT4_I(inode)->i_data_sem)); 19079c3569b5STao Ma if (ext4_has_inline_data(inode)) { 19089c3569b5STao Ma /* 19099c3569b5STao Ma * We will soon create blocks for this page, and let 19109c3569b5STao Ma * us pretend as if the blocks aren't allocated yet. 19119c3569b5STao Ma * In case of clusters, we have to handle the work 19129c3569b5STao Ma * of mapping from cluster so that the reserved space 19139c3569b5STao Ma * is calculated properly. 19149c3569b5STao Ma */ 19159c3569b5STao Ma if ((EXT4_SB(inode->i_sb)->s_cluster_ratio > 1) && 19169c3569b5STao Ma ext4_find_delalloc_cluster(inode, map->m_lblk)) 19179c3569b5STao Ma map->m_flags |= EXT4_MAP_FROM_CLUSTER; 19189c3569b5STao Ma retval = 0; 19199c3569b5STao Ma } else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 1920d100eef2SZheng Liu retval = ext4_ext_map_blocks(NULL, inode, map, 1921d100eef2SZheng Liu EXT4_GET_BLOCKS_NO_PUT_HOLE); 19225356f261SAditya Kali else 1923d100eef2SZheng Liu retval = ext4_ind_map_blocks(NULL, inode, map, 1924d100eef2SZheng Liu EXT4_GET_BLOCKS_NO_PUT_HOLE); 19255356f261SAditya Kali 1926d100eef2SZheng Liu add_delayed: 19275356f261SAditya Kali if (retval == 0) { 1928f7fec032SZheng Liu int ret; 19295356f261SAditya Kali /* 19305356f261SAditya Kali * XXX: __block_prepare_write() unmaps passed block, 19315356f261SAditya Kali * is it OK? 19325356f261SAditya Kali */ 19335356f261SAditya Kali /* If the block was allocated from previously allocated cluster, 19345356f261SAditya Kali * then we dont need to reserve it again. */ 19355356f261SAditya Kali if (!(map->m_flags & EXT4_MAP_FROM_CLUSTER)) { 1936f7fec032SZheng Liu ret = ext4_da_reserve_space(inode, iblock); 1937f7fec032SZheng Liu if (ret) { 19385356f261SAditya Kali /* not enough space to reserve */ 1939f7fec032SZheng Liu retval = ret; 19405356f261SAditya Kali goto out_unlock; 19415356f261SAditya Kali } 1942f7fec032SZheng Liu } 19435356f261SAditya Kali 1944f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 1945fdc0212eSZheng Liu ~0, EXTENT_STATUS_DELAYED); 1946f7fec032SZheng Liu if (ret) { 1947f7fec032SZheng Liu retval = ret; 194851865fdaSZheng Liu goto out_unlock; 1949f7fec032SZheng Liu } 195051865fdaSZheng Liu 19515356f261SAditya Kali /* Clear EXT4_MAP_FROM_CLUSTER flag since its purpose is served 19525356f261SAditya Kali * and it should not appear on the bh->b_state. 19535356f261SAditya Kali */ 19545356f261SAditya Kali map->m_flags &= ~EXT4_MAP_FROM_CLUSTER; 19555356f261SAditya Kali 19565356f261SAditya Kali map_bh(bh, inode->i_sb, invalid_block); 19575356f261SAditya Kali set_buffer_new(bh); 19585356f261SAditya Kali set_buffer_delay(bh); 1959f7fec032SZheng Liu } else if (retval > 0) { 1960f7fec032SZheng Liu int ret; 1961f7fec032SZheng Liu unsigned long long status; 1962f7fec032SZheng Liu 1963*921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1964*921f266bSDmitry Monakhov if (retval != map->m_len) { 1965*921f266bSDmitry Monakhov printk("ES len assertation failed for inode: %lu " 1966*921f266bSDmitry Monakhov "retval %d != map->m_len %d " 1967*921f266bSDmitry Monakhov "in %s (lookup)\n", inode->i_ino, retval, 1968*921f266bSDmitry Monakhov map->m_len, __func__); 1969*921f266bSDmitry Monakhov } 1970*921f266bSDmitry Monakhov #endif 1971*921f266bSDmitry Monakhov 1972f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 1973f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 1974f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 1975f7fec032SZheng Liu map->m_pblk, status); 1976f7fec032SZheng Liu if (ret != 0) 1977f7fec032SZheng Liu retval = ret; 19785356f261SAditya Kali } 19795356f261SAditya Kali 19805356f261SAditya Kali out_unlock: 19815356f261SAditya Kali up_read((&EXT4_I(inode)->i_data_sem)); 19825356f261SAditya Kali 19835356f261SAditya Kali return retval; 19845356f261SAditya Kali } 19855356f261SAditya Kali 19865356f261SAditya Kali /* 1987b920c755STheodore Ts'o * This is a special get_blocks_t callback which is used by 1988b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 1989b920c755STheodore Ts'o * reserve space for a single block. 199029fa89d0SAneesh Kumar K.V * 199129fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 199229fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 199329fa89d0SAneesh Kumar K.V * 199429fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 199529fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 199629fa89d0SAneesh Kumar K.V * initialized properly. 199764769240SAlex Tomas */ 19989c3569b5STao Ma int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 19992ed88685STheodore Ts'o struct buffer_head *bh, int create) 200064769240SAlex Tomas { 20012ed88685STheodore Ts'o struct ext4_map_blocks map; 200264769240SAlex Tomas int ret = 0; 200364769240SAlex Tomas 200464769240SAlex Tomas BUG_ON(create == 0); 20052ed88685STheodore Ts'o BUG_ON(bh->b_size != inode->i_sb->s_blocksize); 20062ed88685STheodore Ts'o 20072ed88685STheodore Ts'o map.m_lblk = iblock; 20082ed88685STheodore Ts'o map.m_len = 1; 200964769240SAlex Tomas 201064769240SAlex Tomas /* 201164769240SAlex Tomas * first, we need to know whether the block is allocated already 201264769240SAlex Tomas * preallocated blocks are unmapped but should treated 201364769240SAlex Tomas * the same as allocated blocks. 201464769240SAlex Tomas */ 20155356f261SAditya Kali ret = ext4_da_map_blocks(inode, iblock, &map, bh); 20165356f261SAditya Kali if (ret <= 0) 20172ed88685STheodore Ts'o return ret; 201864769240SAlex Tomas 20192ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 20202ed88685STheodore Ts'o bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; 20212ed88685STheodore Ts'o 20222ed88685STheodore Ts'o if (buffer_unwritten(bh)) { 20232ed88685STheodore Ts'o /* A delayed write to unwritten bh should be marked 20242ed88685STheodore Ts'o * new and mapped. Mapped ensures that we don't do 20252ed88685STheodore Ts'o * get_block multiple times when we write to the same 20262ed88685STheodore Ts'o * offset and new ensures that we do proper zero out 20272ed88685STheodore Ts'o * for partial write. 20282ed88685STheodore Ts'o */ 20292ed88685STheodore Ts'o set_buffer_new(bh); 2030c8205636STheodore Ts'o set_buffer_mapped(bh); 20312ed88685STheodore Ts'o } 20322ed88685STheodore Ts'o return 0; 203364769240SAlex Tomas } 203461628a3fSMingming Cao 203562e086beSAneesh Kumar K.V static int bget_one(handle_t *handle, struct buffer_head *bh) 203662e086beSAneesh Kumar K.V { 203762e086beSAneesh Kumar K.V get_bh(bh); 203862e086beSAneesh Kumar K.V return 0; 203962e086beSAneesh Kumar K.V } 204062e086beSAneesh Kumar K.V 204162e086beSAneesh Kumar K.V static int bput_one(handle_t *handle, struct buffer_head *bh) 204262e086beSAneesh Kumar K.V { 204362e086beSAneesh Kumar K.V put_bh(bh); 204462e086beSAneesh Kumar K.V return 0; 204562e086beSAneesh Kumar K.V } 204662e086beSAneesh Kumar K.V 204762e086beSAneesh Kumar K.V static int __ext4_journalled_writepage(struct page *page, 204862e086beSAneesh Kumar K.V unsigned int len) 204962e086beSAneesh Kumar K.V { 205062e086beSAneesh Kumar K.V struct address_space *mapping = page->mapping; 205162e086beSAneesh Kumar K.V struct inode *inode = mapping->host; 20523fdcfb66STao Ma struct buffer_head *page_bufs = NULL; 205362e086beSAneesh Kumar K.V handle_t *handle = NULL; 20543fdcfb66STao Ma int ret = 0, err = 0; 20553fdcfb66STao Ma int inline_data = ext4_has_inline_data(inode); 20563fdcfb66STao Ma struct buffer_head *inode_bh = NULL; 205762e086beSAneesh Kumar K.V 2058cb20d518STheodore Ts'o ClearPageChecked(page); 20593fdcfb66STao Ma 20603fdcfb66STao Ma if (inline_data) { 20613fdcfb66STao Ma BUG_ON(page->index != 0); 20623fdcfb66STao Ma BUG_ON(len > ext4_get_max_inline_size(inode)); 20633fdcfb66STao Ma inode_bh = ext4_journalled_write_inline_data(inode, len, page); 20643fdcfb66STao Ma if (inode_bh == NULL) 20653fdcfb66STao Ma goto out; 20663fdcfb66STao Ma } else { 206762e086beSAneesh Kumar K.V page_bufs = page_buffers(page); 20683fdcfb66STao Ma if (!page_bufs) { 20693fdcfb66STao Ma BUG(); 20703fdcfb66STao Ma goto out; 20713fdcfb66STao Ma } 20723fdcfb66STao Ma ext4_walk_page_buffers(handle, page_bufs, 0, len, 20733fdcfb66STao Ma NULL, bget_one); 20743fdcfb66STao Ma } 207562e086beSAneesh Kumar K.V /* As soon as we unlock the page, it can go away, but we have 207662e086beSAneesh Kumar K.V * references to buffers so we are safe */ 207762e086beSAneesh Kumar K.V unlock_page(page); 207862e086beSAneesh Kumar K.V 20799924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 20809924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 208162e086beSAneesh Kumar K.V if (IS_ERR(handle)) { 208262e086beSAneesh Kumar K.V ret = PTR_ERR(handle); 208362e086beSAneesh Kumar K.V goto out; 208462e086beSAneesh Kumar K.V } 208562e086beSAneesh Kumar K.V 2086441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 2087441c8508SCurt Wohlgemuth 20883fdcfb66STao Ma if (inline_data) { 20893fdcfb66STao Ma ret = ext4_journal_get_write_access(handle, inode_bh); 20903fdcfb66STao Ma 20913fdcfb66STao Ma err = ext4_handle_dirty_metadata(handle, inode, inode_bh); 20923fdcfb66STao Ma 20933fdcfb66STao Ma } else { 2094f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 209562e086beSAneesh Kumar K.V do_journal_get_write_access); 209662e086beSAneesh Kumar K.V 2097f19d5870STao Ma err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 209862e086beSAneesh Kumar K.V write_end_fn); 20993fdcfb66STao Ma } 210062e086beSAneesh Kumar K.V if (ret == 0) 210162e086beSAneesh Kumar K.V ret = err; 21022d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 210362e086beSAneesh Kumar K.V err = ext4_journal_stop(handle); 210462e086beSAneesh Kumar K.V if (!ret) 210562e086beSAneesh Kumar K.V ret = err; 210662e086beSAneesh Kumar K.V 21073fdcfb66STao Ma if (!ext4_has_inline_data(inode)) 21083fdcfb66STao Ma ext4_walk_page_buffers(handle, page_bufs, 0, len, 21093fdcfb66STao Ma NULL, bput_one); 211019f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 211162e086beSAneesh Kumar K.V out: 21123fdcfb66STao Ma brelse(inode_bh); 211362e086beSAneesh Kumar K.V return ret; 211462e086beSAneesh Kumar K.V } 211562e086beSAneesh Kumar K.V 211661628a3fSMingming Cao /* 211743ce1d23SAneesh Kumar K.V * Note that we don't need to start a transaction unless we're journaling data 211843ce1d23SAneesh Kumar K.V * because we should have holes filled from ext4_page_mkwrite(). We even don't 211943ce1d23SAneesh Kumar K.V * need to file the inode to the transaction's list in ordered mode because if 212043ce1d23SAneesh Kumar K.V * we are writing back data added by write(), the inode is already there and if 212143ce1d23SAneesh Kumar K.V * we are writing back data modified via mmap(), no one guarantees in which 212243ce1d23SAneesh Kumar K.V * transaction the data will hit the disk. In case we are journaling data, we 212343ce1d23SAneesh Kumar K.V * cannot start transaction directly because transaction start ranks above page 212443ce1d23SAneesh Kumar K.V * lock so we have to do some magic. 212543ce1d23SAneesh Kumar K.V * 2126b920c755STheodore Ts'o * This function can get called via... 2127b920c755STheodore Ts'o * - ext4_da_writepages after taking page lock (have journal handle) 2128b920c755STheodore Ts'o * - journal_submit_inode_data_buffers (no journal handle) 2129f6463b0dSArtem Bityutskiy * - shrink_page_list via the kswapd/direct reclaim (no journal handle) 2130b920c755STheodore Ts'o * - grab_page_cache when doing write_begin (have journal handle) 213143ce1d23SAneesh Kumar K.V * 213243ce1d23SAneesh Kumar K.V * We don't do any block allocation in this function. If we have page with 213343ce1d23SAneesh Kumar K.V * multiple blocks we need to write those buffer_heads that are mapped. This 213443ce1d23SAneesh Kumar K.V * is important for mmaped based write. So if we do with blocksize 1K 213543ce1d23SAneesh Kumar K.V * truncate(f, 1024); 213643ce1d23SAneesh Kumar K.V * a = mmap(f, 0, 4096); 213743ce1d23SAneesh Kumar K.V * a[0] = 'a'; 213843ce1d23SAneesh Kumar K.V * truncate(f, 4096); 213943ce1d23SAneesh Kumar K.V * we have in the page first buffer_head mapped via page_mkwrite call back 214090802ed9SPaul Bolle * but other buffer_heads would be unmapped but dirty (dirty done via the 214143ce1d23SAneesh Kumar K.V * do_wp_page). So writepage should write the first block. If we modify 214243ce1d23SAneesh Kumar K.V * the mmap area beyond 1024 we will again get a page_fault and the 214343ce1d23SAneesh Kumar K.V * page_mkwrite callback will do the block allocation and mark the 214443ce1d23SAneesh Kumar K.V * buffer_heads mapped. 214543ce1d23SAneesh Kumar K.V * 214643ce1d23SAneesh Kumar K.V * We redirty the page if we have any buffer_heads that is either delay or 214743ce1d23SAneesh Kumar K.V * unwritten in the page. 214843ce1d23SAneesh Kumar K.V * 214943ce1d23SAneesh Kumar K.V * We can get recursively called as show below. 215043ce1d23SAneesh Kumar K.V * 215143ce1d23SAneesh Kumar K.V * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> 215243ce1d23SAneesh Kumar K.V * ext4_writepage() 215343ce1d23SAneesh Kumar K.V * 215443ce1d23SAneesh Kumar K.V * But since we don't do any block allocation we should not deadlock. 215543ce1d23SAneesh Kumar K.V * Page also have the dirty flag cleared so we don't get recurive page_lock. 215661628a3fSMingming Cao */ 215743ce1d23SAneesh Kumar K.V static int ext4_writepage(struct page *page, 215864769240SAlex Tomas struct writeback_control *wbc) 215964769240SAlex Tomas { 2160f8bec370SJan Kara int ret = 0; 216161628a3fSMingming Cao loff_t size; 2162498e5f24STheodore Ts'o unsigned int len; 2163744692dcSJiaying Zhang struct buffer_head *page_bufs = NULL; 216461628a3fSMingming Cao struct inode *inode = page->mapping->host; 216536ade451SJan Kara struct ext4_io_submit io_submit; 216664769240SAlex Tomas 2167a9c667f8SLukas Czerner trace_ext4_writepage(page); 216861628a3fSMingming Cao size = i_size_read(inode); 216961628a3fSMingming Cao if (page->index == size >> PAGE_CACHE_SHIFT) 217061628a3fSMingming Cao len = size & ~PAGE_CACHE_MASK; 217161628a3fSMingming Cao else 217261628a3fSMingming Cao len = PAGE_CACHE_SIZE; 217361628a3fSMingming Cao 2174f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 2175fe386132SJan Kara /* 2176fe386132SJan Kara * We cannot do block allocation or other extent handling in this 2177fe386132SJan Kara * function. If there are buffers needing that, we have to redirty 2178fe386132SJan Kara * the page. But we may reach here when we do a journal commit via 2179fe386132SJan Kara * journal_submit_inode_data_buffers() and in that case we must write 2180fe386132SJan Kara * allocated buffers to achieve data=ordered mode guarantees. 2181fe386132SJan Kara */ 2182f19d5870STao Ma if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2183c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 2184f8bec370SJan Kara redirty_page_for_writepage(wbc, page); 2185fe386132SJan Kara if (current->flags & PF_MEMALLOC) { 2186fe386132SJan Kara /* 2187fe386132SJan Kara * For memory cleaning there's no point in writing only 2188fe386132SJan Kara * some buffers. So just bail out. Warn if we came here 2189fe386132SJan Kara * from direct reclaim. 2190fe386132SJan Kara */ 2191fe386132SJan Kara WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) 2192fe386132SJan Kara == PF_MEMALLOC); 2193f8bec370SJan Kara unlock_page(page); 2194f8bec370SJan Kara return 0; 2195f0e6c985SAneesh Kumar K.V } 2196fe386132SJan Kara } 219764769240SAlex Tomas 2198cb20d518STheodore Ts'o if (PageChecked(page) && ext4_should_journal_data(inode)) 219943ce1d23SAneesh Kumar K.V /* 220043ce1d23SAneesh Kumar K.V * It's mmapped pagecache. Add buffers and journal it. There 220143ce1d23SAneesh Kumar K.V * doesn't seem much point in redirtying the page here. 220243ce1d23SAneesh Kumar K.V */ 22033f0ca309SWu Fengguang return __ext4_journalled_writepage(page, len); 220443ce1d23SAneesh Kumar K.V 220536ade451SJan Kara memset(&io_submit, 0, sizeof(io_submit)); 220636ade451SJan Kara ret = ext4_bio_write_page(&io_submit, page, len, wbc); 220736ade451SJan Kara ext4_io_submit(&io_submit); 220864769240SAlex Tomas return ret; 220964769240SAlex Tomas } 221064769240SAlex Tomas 221161628a3fSMingming Cao /* 2212525f4ed8SMingming Cao * This is called via ext4_da_writepages() to 221325985edcSLucas De Marchi * calculate the total number of credits to reserve to fit 2214525f4ed8SMingming Cao * a single extent allocation into a single transaction, 2215525f4ed8SMingming Cao * ext4_da_writpeages() will loop calling this before 2216525f4ed8SMingming Cao * the block allocation. 221761628a3fSMingming Cao */ 2218525f4ed8SMingming Cao 2219525f4ed8SMingming Cao static int ext4_da_writepages_trans_blocks(struct inode *inode) 2220525f4ed8SMingming Cao { 2221525f4ed8SMingming Cao int max_blocks = EXT4_I(inode)->i_reserved_data_blocks; 2222525f4ed8SMingming Cao 2223525f4ed8SMingming Cao /* 2224525f4ed8SMingming Cao * With non-extent format the journal credit needed to 2225525f4ed8SMingming Cao * insert nrblocks contiguous block is dependent on 2226525f4ed8SMingming Cao * number of contiguous block. So we will limit 2227525f4ed8SMingming Cao * number of contiguous block to a sane value 2228525f4ed8SMingming Cao */ 222912e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) && 2230525f4ed8SMingming Cao (max_blocks > EXT4_MAX_TRANS_DATA)) 2231525f4ed8SMingming Cao max_blocks = EXT4_MAX_TRANS_DATA; 2232525f4ed8SMingming Cao 2233525f4ed8SMingming Cao return ext4_chunk_trans_blocks(inode, max_blocks); 2234525f4ed8SMingming Cao } 223561628a3fSMingming Cao 22368e48dcfbSTheodore Ts'o /* 22378e48dcfbSTheodore Ts'o * write_cache_pages_da - walk the list of dirty pages of the given 22388eb9e5ceSTheodore Ts'o * address space and accumulate pages that need writing, and call 2239168fc022STheodore Ts'o * mpage_da_map_and_submit to map a single contiguous memory region 2240168fc022STheodore Ts'o * and then write them. 22418e48dcfbSTheodore Ts'o */ 22429c3569b5STao Ma static int write_cache_pages_da(handle_t *handle, 22439c3569b5STao Ma struct address_space *mapping, 22448e48dcfbSTheodore Ts'o struct writeback_control *wbc, 224572f84e65SEric Sandeen struct mpage_da_data *mpd, 224672f84e65SEric Sandeen pgoff_t *done_index) 22478e48dcfbSTheodore Ts'o { 22488eb9e5ceSTheodore Ts'o struct buffer_head *bh, *head; 2249168fc022STheodore Ts'o struct inode *inode = mapping->host; 22508e48dcfbSTheodore Ts'o struct pagevec pvec; 22514f01b02cSTheodore Ts'o unsigned int nr_pages; 22524f01b02cSTheodore Ts'o sector_t logical; 22534f01b02cSTheodore Ts'o pgoff_t index, end; 22548e48dcfbSTheodore Ts'o long nr_to_write = wbc->nr_to_write; 22554f01b02cSTheodore Ts'o int i, tag, ret = 0; 22568e48dcfbSTheodore Ts'o 2257168fc022STheodore Ts'o memset(mpd, 0, sizeof(struct mpage_da_data)); 2258168fc022STheodore Ts'o mpd->wbc = wbc; 2259168fc022STheodore Ts'o mpd->inode = inode; 22608e48dcfbSTheodore Ts'o pagevec_init(&pvec, 0); 22618e48dcfbSTheodore Ts'o index = wbc->range_start >> PAGE_CACHE_SHIFT; 22628e48dcfbSTheodore Ts'o end = wbc->range_end >> PAGE_CACHE_SHIFT; 22638e48dcfbSTheodore Ts'o 22646e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 22655b41d924SEric Sandeen tag = PAGECACHE_TAG_TOWRITE; 22665b41d924SEric Sandeen else 22675b41d924SEric Sandeen tag = PAGECACHE_TAG_DIRTY; 22685b41d924SEric Sandeen 226972f84e65SEric Sandeen *done_index = index; 22704f01b02cSTheodore Ts'o while (index <= end) { 22715b41d924SEric Sandeen nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, 22728e48dcfbSTheodore Ts'o min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); 22738e48dcfbSTheodore Ts'o if (nr_pages == 0) 22744f01b02cSTheodore Ts'o return 0; 22758e48dcfbSTheodore Ts'o 22768e48dcfbSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 22778e48dcfbSTheodore Ts'o struct page *page = pvec.pages[i]; 22788e48dcfbSTheodore Ts'o 22798e48dcfbSTheodore Ts'o /* 22808e48dcfbSTheodore Ts'o * At this point, the page may be truncated or 22818e48dcfbSTheodore Ts'o * invalidated (changing page->mapping to NULL), or 22828e48dcfbSTheodore Ts'o * even swizzled back from swapper_space to tmpfs file 22838e48dcfbSTheodore Ts'o * mapping. However, page->index will not change 22848e48dcfbSTheodore Ts'o * because we have a reference on the page. 22858e48dcfbSTheodore Ts'o */ 22864f01b02cSTheodore Ts'o if (page->index > end) 22874f01b02cSTheodore Ts'o goto out; 22888e48dcfbSTheodore Ts'o 228972f84e65SEric Sandeen *done_index = page->index + 1; 229072f84e65SEric Sandeen 229178aaced3STheodore Ts'o /* 229278aaced3STheodore Ts'o * If we can't merge this page, and we have 229378aaced3STheodore Ts'o * accumulated an contiguous region, write it 229478aaced3STheodore Ts'o */ 229578aaced3STheodore Ts'o if ((mpd->next_page != page->index) && 229678aaced3STheodore Ts'o (mpd->next_page != mpd->first_page)) { 229778aaced3STheodore Ts'o mpage_da_map_and_submit(mpd); 229878aaced3STheodore Ts'o goto ret_extent_tail; 229978aaced3STheodore Ts'o } 230078aaced3STheodore Ts'o 23018e48dcfbSTheodore Ts'o lock_page(page); 23028e48dcfbSTheodore Ts'o 23038e48dcfbSTheodore Ts'o /* 23044f01b02cSTheodore Ts'o * If the page is no longer dirty, or its 23054f01b02cSTheodore Ts'o * mapping no longer corresponds to inode we 23064f01b02cSTheodore Ts'o * are writing (which means it has been 23074f01b02cSTheodore Ts'o * truncated or invalidated), or the page is 23084f01b02cSTheodore Ts'o * already under writeback and we are not 23094f01b02cSTheodore Ts'o * doing a data integrity writeback, skip the page 23108e48dcfbSTheodore Ts'o */ 23114f01b02cSTheodore Ts'o if (!PageDirty(page) || 23124f01b02cSTheodore Ts'o (PageWriteback(page) && 23134f01b02cSTheodore Ts'o (wbc->sync_mode == WB_SYNC_NONE)) || 23144f01b02cSTheodore Ts'o unlikely(page->mapping != mapping)) { 23158e48dcfbSTheodore Ts'o unlock_page(page); 23168e48dcfbSTheodore Ts'o continue; 23178e48dcfbSTheodore Ts'o } 23188e48dcfbSTheodore Ts'o 23198e48dcfbSTheodore Ts'o wait_on_page_writeback(page); 23208e48dcfbSTheodore Ts'o BUG_ON(PageWriteback(page)); 23218e48dcfbSTheodore Ts'o 23229c3569b5STao Ma /* 23239c3569b5STao Ma * If we have inline data and arrive here, it means that 23249c3569b5STao Ma * we will soon create the block for the 1st page, so 23259c3569b5STao Ma * we'd better clear the inline data here. 23269c3569b5STao Ma */ 23279c3569b5STao Ma if (ext4_has_inline_data(inode)) { 23289c3569b5STao Ma BUG_ON(ext4_test_inode_state(inode, 23299c3569b5STao Ma EXT4_STATE_MAY_INLINE_DATA)); 23309c3569b5STao Ma ext4_destroy_inline_data(handle, inode); 23319c3569b5STao Ma } 23329c3569b5STao Ma 2333168fc022STheodore Ts'o if (mpd->next_page != page->index) 23348eb9e5ceSTheodore Ts'o mpd->first_page = page->index; 23358eb9e5ceSTheodore Ts'o mpd->next_page = page->index + 1; 23368eb9e5ceSTheodore Ts'o logical = (sector_t) page->index << 23378eb9e5ceSTheodore Ts'o (PAGE_CACHE_SHIFT - inode->i_blkbits); 23388eb9e5ceSTheodore Ts'o 2339f8bec370SJan Kara /* Add all dirty buffers to mpd */ 23408eb9e5ceSTheodore Ts'o head = page_buffers(page); 23418eb9e5ceSTheodore Ts'o bh = head; 23428eb9e5ceSTheodore Ts'o do { 23438eb9e5ceSTheodore Ts'o BUG_ON(buffer_locked(bh)); 23448eb9e5ceSTheodore Ts'o /* 2345f8bec370SJan Kara * We need to try to allocate unmapped blocks 2346f8bec370SJan Kara * in the same page. Otherwise we won't make 2347f8bec370SJan Kara * progress with the page in ext4_writepage 23488eb9e5ceSTheodore Ts'o */ 23498eb9e5ceSTheodore Ts'o if (ext4_bh_delay_or_unwritten(NULL, bh)) { 23508eb9e5ceSTheodore Ts'o mpage_add_bh_to_extent(mpd, logical, 23518eb9e5ceSTheodore Ts'o bh->b_state); 23524f01b02cSTheodore Ts'o if (mpd->io_done) 23534f01b02cSTheodore Ts'o goto ret_extent_tail; 2354f8bec370SJan Kara } else if (buffer_dirty(bh) && 2355f8bec370SJan Kara buffer_mapped(bh)) { 23568eb9e5ceSTheodore Ts'o /* 2357f8bec370SJan Kara * mapped dirty buffer. We need to 2358f8bec370SJan Kara * update the b_state because we look 2359f8bec370SJan Kara * at b_state in mpage_da_map_blocks. 2360f8bec370SJan Kara * We don't update b_size because if we 2361f8bec370SJan Kara * find an unmapped buffer_head later 2362f8bec370SJan Kara * we need to use the b_state flag of 2363f8bec370SJan Kara * that buffer_head. 23648eb9e5ceSTheodore Ts'o */ 23658eb9e5ceSTheodore Ts'o if (mpd->b_size == 0) 2366f8bec370SJan Kara mpd->b_state = 2367f8bec370SJan Kara bh->b_state & BH_FLAGS; 23688e48dcfbSTheodore Ts'o } 23698eb9e5ceSTheodore Ts'o logical++; 23708eb9e5ceSTheodore Ts'o } while ((bh = bh->b_this_page) != head); 23718e48dcfbSTheodore Ts'o 23728e48dcfbSTheodore Ts'o if (nr_to_write > 0) { 23738e48dcfbSTheodore Ts'o nr_to_write--; 23748e48dcfbSTheodore Ts'o if (nr_to_write == 0 && 23754f01b02cSTheodore Ts'o wbc->sync_mode == WB_SYNC_NONE) 23768e48dcfbSTheodore Ts'o /* 23778e48dcfbSTheodore Ts'o * We stop writing back only if we are 23788e48dcfbSTheodore Ts'o * not doing integrity sync. In case of 23798e48dcfbSTheodore Ts'o * integrity sync we have to keep going 23808e48dcfbSTheodore Ts'o * because someone may be concurrently 23818e48dcfbSTheodore Ts'o * dirtying pages, and we might have 23828e48dcfbSTheodore Ts'o * synced a lot of newly appeared dirty 23838e48dcfbSTheodore Ts'o * pages, but have not synced all of the 23848e48dcfbSTheodore Ts'o * old dirty pages. 23858e48dcfbSTheodore Ts'o */ 23864f01b02cSTheodore Ts'o goto out; 23878e48dcfbSTheodore Ts'o } 23888e48dcfbSTheodore Ts'o } 23898e48dcfbSTheodore Ts'o pagevec_release(&pvec); 23908e48dcfbSTheodore Ts'o cond_resched(); 23918e48dcfbSTheodore Ts'o } 23924f01b02cSTheodore Ts'o return 0; 23934f01b02cSTheodore Ts'o ret_extent_tail: 23944f01b02cSTheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 23958eb9e5ceSTheodore Ts'o out: 23968eb9e5ceSTheodore Ts'o pagevec_release(&pvec); 23978eb9e5ceSTheodore Ts'o cond_resched(); 23988e48dcfbSTheodore Ts'o return ret; 23998e48dcfbSTheodore Ts'o } 24008e48dcfbSTheodore Ts'o 24018e48dcfbSTheodore Ts'o 240264769240SAlex Tomas static int ext4_da_writepages(struct address_space *mapping, 240364769240SAlex Tomas struct writeback_control *wbc) 240464769240SAlex Tomas { 240522208dedSAneesh Kumar K.V pgoff_t index; 240622208dedSAneesh Kumar K.V int range_whole = 0; 240761628a3fSMingming Cao handle_t *handle = NULL; 2408df22291fSAneesh Kumar K.V struct mpage_da_data mpd; 24095e745b04SAneesh Kumar K.V struct inode *inode = mapping->host; 2410498e5f24STheodore Ts'o int pages_written = 0; 241155138e0bSTheodore Ts'o unsigned int max_pages; 24122acf2c26SAneesh Kumar K.V int range_cyclic, cycled = 1, io_done = 0; 241355138e0bSTheodore Ts'o int needed_blocks, ret = 0; 241455138e0bSTheodore Ts'o long desired_nr_to_write, nr_to_writebump = 0; 2415de89de6eSTheodore Ts'o loff_t range_start = wbc->range_start; 24165e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 241772f84e65SEric Sandeen pgoff_t done_index = 0; 24185b41d924SEric Sandeen pgoff_t end; 24191bce63d1SShaohua Li struct blk_plug plug; 242061628a3fSMingming Cao 24219bffad1eSTheodore Ts'o trace_ext4_da_writepages(inode, wbc); 2422ba80b101STheodore Ts'o 242361628a3fSMingming Cao /* 242461628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 242561628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 242661628a3fSMingming Cao * because that could violate lock ordering on umount 242761628a3fSMingming Cao */ 2428a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 242961628a3fSMingming Cao return 0; 24302a21e37eSTheodore Ts'o 24312a21e37eSTheodore Ts'o /* 24322a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 24332a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 24342a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 24354ab2f15bSTheodore Ts'o * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because 24362a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 24372a21e37eSTheodore Ts'o * read-only, and in that case, ext4_da_writepages should 24382a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 24392a21e37eSTheodore Ts'o * the stack trace. 24402a21e37eSTheodore Ts'o */ 24414ab2f15bSTheodore Ts'o if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) 24422a21e37eSTheodore Ts'o return -EROFS; 24432a21e37eSTheodore Ts'o 244422208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 244522208dedSAneesh Kumar K.V range_whole = 1; 244661628a3fSMingming Cao 24472acf2c26SAneesh Kumar K.V range_cyclic = wbc->range_cyclic; 24482acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 244922208dedSAneesh Kumar K.V index = mapping->writeback_index; 24502acf2c26SAneesh Kumar K.V if (index) 24512acf2c26SAneesh Kumar K.V cycled = 0; 24522acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 24532acf2c26SAneesh Kumar K.V wbc->range_end = LLONG_MAX; 24542acf2c26SAneesh Kumar K.V wbc->range_cyclic = 0; 24555b41d924SEric Sandeen end = -1; 24565b41d924SEric Sandeen } else { 245722208dedSAneesh Kumar K.V index = wbc->range_start >> PAGE_CACHE_SHIFT; 24585b41d924SEric Sandeen end = wbc->range_end >> PAGE_CACHE_SHIFT; 24595b41d924SEric Sandeen } 2460a1d6cc56SAneesh Kumar K.V 246155138e0bSTheodore Ts'o /* 246255138e0bSTheodore Ts'o * This works around two forms of stupidity. The first is in 246355138e0bSTheodore Ts'o * the writeback code, which caps the maximum number of pages 246455138e0bSTheodore Ts'o * written to be 1024 pages. This is wrong on multiple 246555138e0bSTheodore Ts'o * levels; different architectues have a different page size, 246655138e0bSTheodore Ts'o * which changes the maximum amount of data which gets 246755138e0bSTheodore Ts'o * written. Secondly, 4 megabytes is way too small. XFS 246855138e0bSTheodore Ts'o * forces this value to be 16 megabytes by multiplying 246955138e0bSTheodore Ts'o * nr_to_write parameter by four, and then relies on its 247055138e0bSTheodore Ts'o * allocator to allocate larger extents to make them 247155138e0bSTheodore Ts'o * contiguous. Unfortunately this brings us to the second 247255138e0bSTheodore Ts'o * stupidity, which is that ext4's mballoc code only allocates 247355138e0bSTheodore Ts'o * at most 2048 blocks. So we force contiguous writes up to 247455138e0bSTheodore Ts'o * the number of dirty blocks in the inode, or 247555138e0bSTheodore Ts'o * sbi->max_writeback_mb_bump whichever is smaller. 247655138e0bSTheodore Ts'o */ 247755138e0bSTheodore Ts'o max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT); 2478b443e733SEric Sandeen if (!range_cyclic && range_whole) { 2479b443e733SEric Sandeen if (wbc->nr_to_write == LONG_MAX) 2480b443e733SEric Sandeen desired_nr_to_write = wbc->nr_to_write; 248155138e0bSTheodore Ts'o else 2482b443e733SEric Sandeen desired_nr_to_write = wbc->nr_to_write * 8; 2483b443e733SEric Sandeen } else 248455138e0bSTheodore Ts'o desired_nr_to_write = ext4_num_dirty_pages(inode, index, 248555138e0bSTheodore Ts'o max_pages); 248655138e0bSTheodore Ts'o if (desired_nr_to_write > max_pages) 248755138e0bSTheodore Ts'o desired_nr_to_write = max_pages; 248855138e0bSTheodore Ts'o 248955138e0bSTheodore Ts'o if (wbc->nr_to_write < desired_nr_to_write) { 249055138e0bSTheodore Ts'o nr_to_writebump = desired_nr_to_write - wbc->nr_to_write; 249155138e0bSTheodore Ts'o wbc->nr_to_write = desired_nr_to_write; 249255138e0bSTheodore Ts'o } 249355138e0bSTheodore Ts'o 24942acf2c26SAneesh Kumar K.V retry: 24956e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 24965b41d924SEric Sandeen tag_pages_for_writeback(mapping, index, end); 24975b41d924SEric Sandeen 24981bce63d1SShaohua Li blk_start_plug(&plug); 249922208dedSAneesh Kumar K.V while (!ret && wbc->nr_to_write > 0) { 2500a1d6cc56SAneesh Kumar K.V 2501a1d6cc56SAneesh Kumar K.V /* 2502a1d6cc56SAneesh Kumar K.V * we insert one extent at a time. So we need 2503a1d6cc56SAneesh Kumar K.V * credit needed for single extent allocation. 2504a1d6cc56SAneesh Kumar K.V * journalled mode is currently not supported 2505a1d6cc56SAneesh Kumar K.V * by delalloc 2506a1d6cc56SAneesh Kumar K.V */ 2507a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 2508525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 2509a1d6cc56SAneesh Kumar K.V 251061628a3fSMingming Cao /* start a new transaction*/ 25119924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 25129924a92aSTheodore Ts'o needed_blocks); 251361628a3fSMingming Cao if (IS_ERR(handle)) { 251461628a3fSMingming Cao ret = PTR_ERR(handle); 25151693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2516fbe845ddSCurt Wohlgemuth "%ld pages, ino %lu; err %d", __func__, 2517a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 25183c1fcb2cSNamjae Jeon blk_finish_plug(&plug); 251961628a3fSMingming Cao goto out_writepages; 252061628a3fSMingming Cao } 2521f63e6005STheodore Ts'o 2522f63e6005STheodore Ts'o /* 25238eb9e5ceSTheodore Ts'o * Now call write_cache_pages_da() to find the next 2524f63e6005STheodore Ts'o * contiguous region of logical blocks that need 25258eb9e5ceSTheodore Ts'o * blocks to be allocated by ext4 and submit them. 2526f63e6005STheodore Ts'o */ 25279c3569b5STao Ma ret = write_cache_pages_da(handle, mapping, 25289c3569b5STao Ma wbc, &mpd, &done_index); 2529f63e6005STheodore Ts'o /* 2530af901ca1SAndré Goddard Rosa * If we have a contiguous extent of pages and we 2531f63e6005STheodore Ts'o * haven't done the I/O yet, map the blocks and submit 2532f63e6005STheodore Ts'o * them for I/O. 2533f63e6005STheodore Ts'o */ 2534f63e6005STheodore Ts'o if (!mpd.io_done && mpd.next_page != mpd.first_page) { 25355a87b7a5STheodore Ts'o mpage_da_map_and_submit(&mpd); 2536f63e6005STheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 2537f63e6005STheodore Ts'o } 2538b3a3ca8cSTheodore Ts'o trace_ext4_da_write_pages(inode, &mpd); 2539f63e6005STheodore Ts'o wbc->nr_to_write -= mpd.pages_written; 2540df22291fSAneesh Kumar K.V 254161628a3fSMingming Cao ext4_journal_stop(handle); 2542df22291fSAneesh Kumar K.V 25438f64b32eSEric Sandeen if ((mpd.retval == -ENOSPC) && sbi->s_journal) { 254422208dedSAneesh Kumar K.V /* commit the transaction which would 254522208dedSAneesh Kumar K.V * free blocks released in the transaction 254622208dedSAneesh Kumar K.V * and try again 254722208dedSAneesh Kumar K.V */ 2548df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 254922208dedSAneesh Kumar K.V ret = 0; 255022208dedSAneesh Kumar K.V } else if (ret == MPAGE_DA_EXTENT_TAIL) { 2551a1d6cc56SAneesh Kumar K.V /* 25528de49e67SKazuya Mio * Got one extent now try with rest of the pages. 25538de49e67SKazuya Mio * If mpd.retval is set -EIO, journal is aborted. 25548de49e67SKazuya Mio * So we don't need to write any more. 2555a1d6cc56SAneesh Kumar K.V */ 255622208dedSAneesh Kumar K.V pages_written += mpd.pages_written; 25578de49e67SKazuya Mio ret = mpd.retval; 25582acf2c26SAneesh Kumar K.V io_done = 1; 255922208dedSAneesh Kumar K.V } else if (wbc->nr_to_write) 256061628a3fSMingming Cao /* 256161628a3fSMingming Cao * There is no more writeout needed 256261628a3fSMingming Cao * or we requested for a noblocking writeout 256361628a3fSMingming Cao * and we found the device congested 256461628a3fSMingming Cao */ 256561628a3fSMingming Cao break; 256661628a3fSMingming Cao } 25671bce63d1SShaohua Li blk_finish_plug(&plug); 25682acf2c26SAneesh Kumar K.V if (!io_done && !cycled) { 25692acf2c26SAneesh Kumar K.V cycled = 1; 25702acf2c26SAneesh Kumar K.V index = 0; 25712acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 25722acf2c26SAneesh Kumar K.V wbc->range_end = mapping->writeback_index - 1; 25732acf2c26SAneesh Kumar K.V goto retry; 25742acf2c26SAneesh Kumar K.V } 257561628a3fSMingming Cao 257622208dedSAneesh Kumar K.V /* Update index */ 25772acf2c26SAneesh Kumar K.V wbc->range_cyclic = range_cyclic; 257822208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 257922208dedSAneesh Kumar K.V /* 258022208dedSAneesh Kumar K.V * set the writeback_index so that range_cyclic 258122208dedSAneesh Kumar K.V * mode will write it back later 258222208dedSAneesh Kumar K.V */ 258372f84e65SEric Sandeen mapping->writeback_index = done_index; 2584a1d6cc56SAneesh Kumar K.V 258561628a3fSMingming Cao out_writepages: 258622208dedSAneesh Kumar K.V wbc->nr_to_write -= nr_to_writebump; 2587de89de6eSTheodore Ts'o wbc->range_start = range_start; 25889bffad1eSTheodore Ts'o trace_ext4_da_writepages_result(inode, wbc, ret, pages_written); 258961628a3fSMingming Cao return ret; 259064769240SAlex Tomas } 259164769240SAlex Tomas 259279f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 259379f0be8dSAneesh Kumar K.V { 259479f0be8dSAneesh Kumar K.V s64 free_blocks, dirty_blocks; 259579f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 259679f0be8dSAneesh Kumar K.V 259779f0be8dSAneesh Kumar K.V /* 259879f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 259979f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 2600179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 260179f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 260279f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 260379f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 260479f0be8dSAneesh Kumar K.V */ 260557042651STheodore Ts'o free_blocks = EXT4_C2B(sbi, 260657042651STheodore Ts'o percpu_counter_read_positive(&sbi->s_freeclusters_counter)); 260757042651STheodore Ts'o dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyclusters_counter); 260800d4e736STheodore Ts'o /* 260900d4e736STheodore Ts'o * Start pushing delalloc when 1/2 of free blocks are dirty. 261000d4e736STheodore Ts'o */ 261100d4e736STheodore Ts'o if (dirty_blocks && (free_blocks < 2 * dirty_blocks) && 261200d4e736STheodore Ts'o !writeback_in_progress(sb->s_bdi) && 261300d4e736STheodore Ts'o down_read_trylock(&sb->s_umount)) { 261400d4e736STheodore Ts'o writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE); 261500d4e736STheodore Ts'o up_read(&sb->s_umount); 261600d4e736STheodore Ts'o } 261700d4e736STheodore Ts'o 261879f0be8dSAneesh Kumar K.V if (2 * free_blocks < 3 * dirty_blocks || 2619df55c99dSTheodore Ts'o free_blocks < (dirty_blocks + EXT4_FREECLUSTERS_WATERMARK)) { 262079f0be8dSAneesh Kumar K.V /* 2621c8afb446SEric Sandeen * free block count is less than 150% of dirty blocks 2622c8afb446SEric Sandeen * or free blocks is less than watermark 262379f0be8dSAneesh Kumar K.V */ 262479f0be8dSAneesh Kumar K.V return 1; 262579f0be8dSAneesh Kumar K.V } 262679f0be8dSAneesh Kumar K.V return 0; 262779f0be8dSAneesh Kumar K.V } 262879f0be8dSAneesh Kumar K.V 262964769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 263064769240SAlex Tomas loff_t pos, unsigned len, unsigned flags, 263164769240SAlex Tomas struct page **pagep, void **fsdata) 263264769240SAlex Tomas { 263372b8ab9dSEric Sandeen int ret, retries = 0; 263464769240SAlex Tomas struct page *page; 263564769240SAlex Tomas pgoff_t index; 263664769240SAlex Tomas struct inode *inode = mapping->host; 263764769240SAlex Tomas handle_t *handle; 263864769240SAlex Tomas 263964769240SAlex Tomas index = pos >> PAGE_CACHE_SHIFT; 264079f0be8dSAneesh Kumar K.V 264179f0be8dSAneesh Kumar K.V if (ext4_nonda_switch(inode->i_sb)) { 264279f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 264379f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 264479f0be8dSAneesh Kumar K.V len, flags, pagep, fsdata); 264579f0be8dSAneesh Kumar K.V } 264679f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 26479bffad1eSTheodore Ts'o trace_ext4_da_write_begin(inode, pos, len, flags); 26489c3569b5STao Ma 26499c3569b5STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 26509c3569b5STao Ma ret = ext4_da_write_inline_data_begin(mapping, inode, 26519c3569b5STao Ma pos, len, flags, 26529c3569b5STao Ma pagep, fsdata); 26539c3569b5STao Ma if (ret < 0) 265447564bfbSTheodore Ts'o return ret; 265547564bfbSTheodore Ts'o if (ret == 1) 265647564bfbSTheodore Ts'o return 0; 26579c3569b5STao Ma } 26589c3569b5STao Ma 265947564bfbSTheodore Ts'o /* 266047564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 266147564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 266247564bfbSTheodore Ts'o * is being written back. So grab it first before we start 266347564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 266447564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 266547564bfbSTheodore Ts'o */ 266647564bfbSTheodore Ts'o retry_grab: 266747564bfbSTheodore Ts'o page = grab_cache_page_write_begin(mapping, index, flags); 266847564bfbSTheodore Ts'o if (!page) 266947564bfbSTheodore Ts'o return -ENOMEM; 267047564bfbSTheodore Ts'o unlock_page(page); 267147564bfbSTheodore Ts'o 267264769240SAlex Tomas /* 267364769240SAlex Tomas * With delayed allocation, we don't log the i_disksize update 267464769240SAlex Tomas * if there is delayed block allocation. But we still need 267564769240SAlex Tomas * to journalling the i_disksize update if writes to the end 267664769240SAlex Tomas * of file which has an already mapped buffer. 267764769240SAlex Tomas */ 267847564bfbSTheodore Ts'o retry_journal: 26799924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 1); 268064769240SAlex Tomas if (IS_ERR(handle)) { 268147564bfbSTheodore Ts'o page_cache_release(page); 268247564bfbSTheodore Ts'o return PTR_ERR(handle); 268364769240SAlex Tomas } 268464769240SAlex Tomas 268547564bfbSTheodore Ts'o lock_page(page); 268647564bfbSTheodore Ts'o if (page->mapping != mapping) { 268747564bfbSTheodore Ts'o /* The page got truncated from under us */ 268847564bfbSTheodore Ts'o unlock_page(page); 268947564bfbSTheodore Ts'o page_cache_release(page); 2690d5a0d4f7SEric Sandeen ext4_journal_stop(handle); 269147564bfbSTheodore Ts'o goto retry_grab; 2692d5a0d4f7SEric Sandeen } 269347564bfbSTheodore Ts'o /* In case writeback began while the page was unlocked */ 269447564bfbSTheodore Ts'o wait_on_page_writeback(page); 269564769240SAlex Tomas 26966e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep); 269764769240SAlex Tomas if (ret < 0) { 269864769240SAlex Tomas unlock_page(page); 269964769240SAlex Tomas ext4_journal_stop(handle); 2700ae4d5372SAneesh Kumar K.V /* 2701ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 2702ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 2703ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 2704ae4d5372SAneesh Kumar K.V */ 2705ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 2706b9a4207dSJan Kara ext4_truncate_failed_write(inode); 270747564bfbSTheodore Ts'o 270847564bfbSTheodore Ts'o if (ret == -ENOSPC && 270947564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 271047564bfbSTheodore Ts'o goto retry_journal; 271147564bfbSTheodore Ts'o 271247564bfbSTheodore Ts'o page_cache_release(page); 271347564bfbSTheodore Ts'o return ret; 271464769240SAlex Tomas } 271564769240SAlex Tomas 271647564bfbSTheodore Ts'o *pagep = page; 271764769240SAlex Tomas return ret; 271864769240SAlex Tomas } 271964769240SAlex Tomas 2720632eaeabSMingming Cao /* 2721632eaeabSMingming Cao * Check if we should update i_disksize 2722632eaeabSMingming Cao * when write to the end of file but not require block allocation 2723632eaeabSMingming Cao */ 2724632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page, 2725632eaeabSMingming Cao unsigned long offset) 2726632eaeabSMingming Cao { 2727632eaeabSMingming Cao struct buffer_head *bh; 2728632eaeabSMingming Cao struct inode *inode = page->mapping->host; 2729632eaeabSMingming Cao unsigned int idx; 2730632eaeabSMingming Cao int i; 2731632eaeabSMingming Cao 2732632eaeabSMingming Cao bh = page_buffers(page); 2733632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 2734632eaeabSMingming Cao 2735632eaeabSMingming Cao for (i = 0; i < idx; i++) 2736632eaeabSMingming Cao bh = bh->b_this_page; 2737632eaeabSMingming Cao 273829fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 2739632eaeabSMingming Cao return 0; 2740632eaeabSMingming Cao return 1; 2741632eaeabSMingming Cao } 2742632eaeabSMingming Cao 274364769240SAlex Tomas static int ext4_da_write_end(struct file *file, 274464769240SAlex Tomas struct address_space *mapping, 274564769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 274664769240SAlex Tomas struct page *page, void *fsdata) 274764769240SAlex Tomas { 274864769240SAlex Tomas struct inode *inode = mapping->host; 274964769240SAlex Tomas int ret = 0, ret2; 275064769240SAlex Tomas handle_t *handle = ext4_journal_current_handle(); 275164769240SAlex Tomas loff_t new_i_size; 2752632eaeabSMingming Cao unsigned long start, end; 275379f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 275479f0be8dSAneesh Kumar K.V 275579f0be8dSAneesh Kumar K.V if (write_mode == FALL_BACK_TO_NONDELALLOC) { 27563d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 27573d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 275879f0be8dSAneesh Kumar K.V return ext4_ordered_write_end(file, mapping, pos, 275979f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 27603d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 276179f0be8dSAneesh Kumar K.V return ext4_writeback_write_end(file, mapping, pos, 276279f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 27633d2b1582SLukas Czerner default: 276479f0be8dSAneesh Kumar K.V BUG(); 276579f0be8dSAneesh Kumar K.V } 276679f0be8dSAneesh Kumar K.V } 2767632eaeabSMingming Cao 27689bffad1eSTheodore Ts'o trace_ext4_da_write_end(inode, pos, len, copied); 2769632eaeabSMingming Cao start = pos & (PAGE_CACHE_SIZE - 1); 2770632eaeabSMingming Cao end = start + copied - 1; 277164769240SAlex Tomas 277264769240SAlex Tomas /* 277364769240SAlex Tomas * generic_write_end() will run mark_inode_dirty() if i_size 277464769240SAlex Tomas * changes. So let's piggyback the i_disksize mark_inode_dirty 277564769240SAlex Tomas * into that. 277664769240SAlex Tomas */ 277764769240SAlex Tomas new_i_size = pos + copied; 2778ea51d132SAndrea Arcangeli if (copied && new_i_size > EXT4_I(inode)->i_disksize) { 27799c3569b5STao Ma if (ext4_has_inline_data(inode) || 27809c3569b5STao Ma ext4_da_should_update_i_disksize(page, end)) { 2781632eaeabSMingming Cao down_write(&EXT4_I(inode)->i_data_sem); 2782f3b59291STheodore Ts'o if (new_i_size > EXT4_I(inode)->i_disksize) 278364769240SAlex Tomas EXT4_I(inode)->i_disksize = new_i_size; 2784632eaeabSMingming Cao up_write(&EXT4_I(inode)->i_data_sem); 2785cf17fea6SAneesh Kumar K.V /* We need to mark inode dirty even if 2786cf17fea6SAneesh Kumar K.V * new_i_size is less that inode->i_size 2787cf17fea6SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 2788cf17fea6SAneesh Kumar K.V */ 2789cf17fea6SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 2790632eaeabSMingming Cao } 2791632eaeabSMingming Cao } 27929c3569b5STao Ma 27939c3569b5STao Ma if (write_mode != CONVERT_INLINE_DATA && 27949c3569b5STao Ma ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) && 27959c3569b5STao Ma ext4_has_inline_data(inode)) 27969c3569b5STao Ma ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied, 27979c3569b5STao Ma page); 27989c3569b5STao Ma else 279964769240SAlex Tomas ret2 = generic_write_end(file, mapping, pos, len, copied, 280064769240SAlex Tomas page, fsdata); 28019c3569b5STao Ma 280264769240SAlex Tomas copied = ret2; 280364769240SAlex Tomas if (ret2 < 0) 280464769240SAlex Tomas ret = ret2; 280564769240SAlex Tomas ret2 = ext4_journal_stop(handle); 280664769240SAlex Tomas if (!ret) 280764769240SAlex Tomas ret = ret2; 280864769240SAlex Tomas 280964769240SAlex Tomas return ret ? ret : copied; 281064769240SAlex Tomas } 281164769240SAlex Tomas 281264769240SAlex Tomas static void ext4_da_invalidatepage(struct page *page, unsigned long offset) 281364769240SAlex Tomas { 281464769240SAlex Tomas /* 281564769240SAlex Tomas * Drop reserved blocks 281664769240SAlex Tomas */ 281764769240SAlex Tomas BUG_ON(!PageLocked(page)); 281864769240SAlex Tomas if (!page_has_buffers(page)) 281964769240SAlex Tomas goto out; 282064769240SAlex Tomas 2821d2a17637SMingming Cao ext4_da_page_release_reservation(page, offset); 282264769240SAlex Tomas 282364769240SAlex Tomas out: 282464769240SAlex Tomas ext4_invalidatepage(page, offset); 282564769240SAlex Tomas 282664769240SAlex Tomas return; 282764769240SAlex Tomas } 282864769240SAlex Tomas 2829ccd2506bSTheodore Ts'o /* 2830ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 2831ccd2506bSTheodore Ts'o */ 2832ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 2833ccd2506bSTheodore Ts'o { 2834fb40ba0dSTheodore Ts'o trace_ext4_alloc_da_blocks(inode); 2835fb40ba0dSTheodore Ts'o 2836ccd2506bSTheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks && 2837ccd2506bSTheodore Ts'o !EXT4_I(inode)->i_reserved_meta_blocks) 2838ccd2506bSTheodore Ts'o return 0; 2839ccd2506bSTheodore Ts'o 2840ccd2506bSTheodore Ts'o /* 2841ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 2842ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 2843ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 2844ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 2845ccd2506bSTheodore Ts'o * would require replicating code paths in: 2846ccd2506bSTheodore Ts'o * 2847ccd2506bSTheodore Ts'o * ext4_da_writepages() -> 2848ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 2849ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 2850ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 2851ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 2852ccd2506bSTheodore Ts'o * 2853ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 2854ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 2855ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 2856ccd2506bSTheodore Ts'o * doing I/O at all. 2857ccd2506bSTheodore Ts'o * 2858ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 2859380cf090SWu Fengguang * the pages by calling redirty_page_for_writepage() but that 2860ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 2861ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 286225985edcSLucas De Marchi * simplifying them because we wouldn't actually intend to 2863ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 2864ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 2865ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 2866ccd2506bSTheodore Ts'o * 2867ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 2868ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 2869ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 2870ccd2506bSTheodore Ts'o */ 2871ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 2872ccd2506bSTheodore Ts'o } 287364769240SAlex Tomas 287464769240SAlex Tomas /* 2875ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 2876ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 2877ac27a0ecSDave Kleikamp * 2878ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 2879617ba13bSMingming Cao * journal. If somebody makes a swapfile on an ext4 data-journaling 2880ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 2881ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 2882ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 2883ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 2884ac27a0ecSDave Kleikamp * 2885ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 2886ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 2887ac27a0ecSDave Kleikamp */ 2888617ba13bSMingming Cao static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 2889ac27a0ecSDave Kleikamp { 2890ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 2891ac27a0ecSDave Kleikamp journal_t *journal; 2892ac27a0ecSDave Kleikamp int err; 2893ac27a0ecSDave Kleikamp 289446c7f254STao Ma /* 289546c7f254STao Ma * We can get here for an inline file via the FIBMAP ioctl 289646c7f254STao Ma */ 289746c7f254STao Ma if (ext4_has_inline_data(inode)) 289846c7f254STao Ma return 0; 289946c7f254STao Ma 290064769240SAlex Tomas if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 290164769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) { 290264769240SAlex Tomas /* 290364769240SAlex Tomas * With delalloc we want to sync the file 290464769240SAlex Tomas * so that we can make sure we allocate 290564769240SAlex Tomas * blocks for file 290664769240SAlex Tomas */ 290764769240SAlex Tomas filemap_write_and_wait(mapping); 290864769240SAlex Tomas } 290964769240SAlex Tomas 291019f5fb7aSTheodore Ts'o if (EXT4_JOURNAL(inode) && 291119f5fb7aSTheodore Ts'o ext4_test_inode_state(inode, EXT4_STATE_JDATA)) { 2912ac27a0ecSDave Kleikamp /* 2913ac27a0ecSDave Kleikamp * This is a REALLY heavyweight approach, but the use of 2914ac27a0ecSDave Kleikamp * bmap on dirty files is expected to be extremely rare: 2915ac27a0ecSDave Kleikamp * only if we run lilo or swapon on a freshly made file 2916ac27a0ecSDave Kleikamp * do we expect this to happen. 2917ac27a0ecSDave Kleikamp * 2918ac27a0ecSDave Kleikamp * (bmap requires CAP_SYS_RAWIO so this does not 2919ac27a0ecSDave Kleikamp * represent an unprivileged user DOS attack --- we'd be 2920ac27a0ecSDave Kleikamp * in trouble if mortal users could trigger this path at 2921ac27a0ecSDave Kleikamp * will.) 2922ac27a0ecSDave Kleikamp * 2923617ba13bSMingming Cao * NB. EXT4_STATE_JDATA is not set on files other than 2924ac27a0ecSDave Kleikamp * regular files. If somebody wants to bmap a directory 2925ac27a0ecSDave Kleikamp * or symlink and gets confused because the buffer 2926ac27a0ecSDave Kleikamp * hasn't yet been flushed to disk, they deserve 2927ac27a0ecSDave Kleikamp * everything they get. 2928ac27a0ecSDave Kleikamp */ 2929ac27a0ecSDave Kleikamp 293019f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_JDATA); 2931617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 2932dab291afSMingming Cao jbd2_journal_lock_updates(journal); 2933dab291afSMingming Cao err = jbd2_journal_flush(journal); 2934dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 2935ac27a0ecSDave Kleikamp 2936ac27a0ecSDave Kleikamp if (err) 2937ac27a0ecSDave Kleikamp return 0; 2938ac27a0ecSDave Kleikamp } 2939ac27a0ecSDave Kleikamp 2940617ba13bSMingming Cao return generic_block_bmap(mapping, block, ext4_get_block); 2941ac27a0ecSDave Kleikamp } 2942ac27a0ecSDave Kleikamp 2943617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page) 2944ac27a0ecSDave Kleikamp { 294546c7f254STao Ma int ret = -EAGAIN; 294646c7f254STao Ma struct inode *inode = page->mapping->host; 294746c7f254STao Ma 29480562e0baSJiaying Zhang trace_ext4_readpage(page); 294946c7f254STao Ma 295046c7f254STao Ma if (ext4_has_inline_data(inode)) 295146c7f254STao Ma ret = ext4_readpage_inline(inode, page); 295246c7f254STao Ma 295346c7f254STao Ma if (ret == -EAGAIN) 2954617ba13bSMingming Cao return mpage_readpage(page, ext4_get_block); 295546c7f254STao Ma 295646c7f254STao Ma return ret; 2957ac27a0ecSDave Kleikamp } 2958ac27a0ecSDave Kleikamp 2959ac27a0ecSDave Kleikamp static int 2960617ba13bSMingming Cao ext4_readpages(struct file *file, struct address_space *mapping, 2961ac27a0ecSDave Kleikamp struct list_head *pages, unsigned nr_pages) 2962ac27a0ecSDave Kleikamp { 296346c7f254STao Ma struct inode *inode = mapping->host; 296446c7f254STao Ma 296546c7f254STao Ma /* If the file has inline data, no need to do readpages. */ 296646c7f254STao Ma if (ext4_has_inline_data(inode)) 296746c7f254STao Ma return 0; 296846c7f254STao Ma 2969617ba13bSMingming Cao return mpage_readpages(mapping, pages, nr_pages, ext4_get_block); 2970ac27a0ecSDave Kleikamp } 2971ac27a0ecSDave Kleikamp 2972617ba13bSMingming Cao static void ext4_invalidatepage(struct page *page, unsigned long offset) 2973ac27a0ecSDave Kleikamp { 29740562e0baSJiaying Zhang trace_ext4_invalidatepage(page, offset); 29750562e0baSJiaying Zhang 29764520fb3cSJan Kara /* No journalling happens on data buffers when this function is used */ 29774520fb3cSJan Kara WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page))); 29784520fb3cSJan Kara 29794520fb3cSJan Kara block_invalidatepage(page, offset); 29804520fb3cSJan Kara } 29814520fb3cSJan Kara 298253e87268SJan Kara static int __ext4_journalled_invalidatepage(struct page *page, 29834520fb3cSJan Kara unsigned long offset) 29844520fb3cSJan Kara { 29854520fb3cSJan Kara journal_t *journal = EXT4_JOURNAL(page->mapping->host); 29864520fb3cSJan Kara 29874520fb3cSJan Kara trace_ext4_journalled_invalidatepage(page, offset); 29884520fb3cSJan Kara 2989744692dcSJiaying Zhang /* 2990ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 2991ac27a0ecSDave Kleikamp */ 2992ac27a0ecSDave Kleikamp if (offset == 0) 2993ac27a0ecSDave Kleikamp ClearPageChecked(page); 2994ac27a0ecSDave Kleikamp 299553e87268SJan Kara return jbd2_journal_invalidatepage(journal, page, offset); 299653e87268SJan Kara } 299753e87268SJan Kara 299853e87268SJan Kara /* Wrapper for aops... */ 299953e87268SJan Kara static void ext4_journalled_invalidatepage(struct page *page, 300053e87268SJan Kara unsigned long offset) 300153e87268SJan Kara { 300253e87268SJan Kara WARN_ON(__ext4_journalled_invalidatepage(page, offset) < 0); 3003ac27a0ecSDave Kleikamp } 3004ac27a0ecSDave Kleikamp 3005617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait) 3006ac27a0ecSDave Kleikamp { 3007617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 3008ac27a0ecSDave Kleikamp 30090562e0baSJiaying Zhang trace_ext4_releasepage(page); 30100562e0baSJiaying Zhang 3011ac27a0ecSDave Kleikamp WARN_ON(PageChecked(page)); 3012ac27a0ecSDave Kleikamp if (!page_has_buffers(page)) 3013ac27a0ecSDave Kleikamp return 0; 30140390131bSFrank Mayhar if (journal) 3015dab291afSMingming Cao return jbd2_journal_try_to_free_buffers(journal, page, wait); 30160390131bSFrank Mayhar else 30170390131bSFrank Mayhar return try_to_free_buffers(page); 3018ac27a0ecSDave Kleikamp } 3019ac27a0ecSDave Kleikamp 3020ac27a0ecSDave Kleikamp /* 30212ed88685STheodore Ts'o * ext4_get_block used when preparing for a DIO write or buffer write. 30222ed88685STheodore Ts'o * We allocate an uinitialized extent if blocks haven't been allocated. 30232ed88685STheodore Ts'o * The extent will be converted to initialized after the IO is complete. 30242ed88685STheodore Ts'o */ 3025f19d5870STao Ma int ext4_get_block_write(struct inode *inode, sector_t iblock, 30264c0425ffSMingming Cao struct buffer_head *bh_result, int create) 30274c0425ffSMingming Cao { 3028c7064ef1SJiaying Zhang ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n", 30298d5d02e6SMingming Cao inode->i_ino, create); 30302ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh_result, 30312ed88685STheodore Ts'o EXT4_GET_BLOCKS_IO_CREATE_EXT); 30324c0425ffSMingming Cao } 30334c0425ffSMingming Cao 3034729f52c6SZheng Liu static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock, 30358b0f165fSAnatol Pomozov struct buffer_head *bh_result, int create) 3036729f52c6SZheng Liu { 30378b0f165fSAnatol Pomozov ext4_debug("ext4_get_block_write_nolock: inode %lu, create flag %d\n", 30388b0f165fSAnatol Pomozov inode->i_ino, create); 30398b0f165fSAnatol Pomozov return _ext4_get_block(inode, iblock, bh_result, 30408b0f165fSAnatol Pomozov EXT4_GET_BLOCKS_NO_LOCK); 3041729f52c6SZheng Liu } 3042729f52c6SZheng Liu 30434c0425ffSMingming Cao static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset, 3044552ef802SChristoph Hellwig ssize_t size, void *private, int ret, 3045552ef802SChristoph Hellwig bool is_async) 30464c0425ffSMingming Cao { 304772c5052dSChristoph Hellwig struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode; 30484c0425ffSMingming Cao ext4_io_end_t *io_end = iocb->private; 30494c0425ffSMingming Cao 30504b70df18SMingming /* if not async direct IO or dio with 0 bytes write, just return */ 30514b70df18SMingming if (!io_end || !size) 3052552ef802SChristoph Hellwig goto out; 30534b70df18SMingming 30548d5d02e6SMingming Cao ext_debug("ext4_end_io_dio(): io_end 0x%p " 3055ace36ad4SJoe Perches "for inode %lu, iocb 0x%p, offset %llu, size %zd\n", 30568d5d02e6SMingming Cao iocb->private, io_end->inode->i_ino, iocb, offset, 30578d5d02e6SMingming Cao size); 30588d5d02e6SMingming Cao 3059b5a7e970STheodore Ts'o iocb->private = NULL; 3060b5a7e970STheodore Ts'o 30618d5d02e6SMingming Cao /* if not aio dio with unwritten extents, just free io and return */ 3062bd2d0210STheodore Ts'o if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) { 30638d5d02e6SMingming Cao ext4_free_io_end(io_end); 30645b3ff237Sjiayingz@google.com (Jiaying Zhang) out: 3065091e26dfSJan Kara inode_dio_done(inode); 30665b3ff237Sjiayingz@google.com (Jiaying Zhang) if (is_async) 30675b3ff237Sjiayingz@google.com (Jiaying Zhang) aio_complete(iocb, ret, 0); 30685b3ff237Sjiayingz@google.com (Jiaying Zhang) return; 30698d5d02e6SMingming Cao } 30708d5d02e6SMingming Cao 30714c0425ffSMingming Cao io_end->offset = offset; 30724c0425ffSMingming Cao io_end->size = size; 30735b3ff237Sjiayingz@google.com (Jiaying Zhang) if (is_async) { 30745b3ff237Sjiayingz@google.com (Jiaying Zhang) io_end->iocb = iocb; 30755b3ff237Sjiayingz@google.com (Jiaying Zhang) io_end->result = ret; 30765b3ff237Sjiayingz@google.com (Jiaying Zhang) } 30774c0425ffSMingming Cao 307828a535f9SDmitry Monakhov ext4_add_complete_io(io_end); 30794c0425ffSMingming Cao } 3080c7064ef1SJiaying Zhang 30814c0425ffSMingming Cao /* 30824c0425ffSMingming Cao * For ext4 extent files, ext4 will do direct-io write to holes, 30834c0425ffSMingming Cao * preallocated extents, and those write extend the file, no need to 30844c0425ffSMingming Cao * fall back to buffered IO. 30854c0425ffSMingming Cao * 3086b595076aSUwe Kleine-König * For holes, we fallocate those blocks, mark them as uninitialized 308769c499d1STheodore Ts'o * If those blocks were preallocated, we mark sure they are split, but 3088b595076aSUwe Kleine-König * still keep the range to write as uninitialized. 30894c0425ffSMingming Cao * 309069c499d1STheodore Ts'o * The unwritten extents will be converted to written when DIO is completed. 30918d5d02e6SMingming Cao * For async direct IO, since the IO may still pending when return, we 309225985edcSLucas De Marchi * set up an end_io call back function, which will do the conversion 30938d5d02e6SMingming Cao * when async direct IO completed. 30944c0425ffSMingming Cao * 30954c0425ffSMingming Cao * If the O_DIRECT write will extend the file then add this inode to the 30964c0425ffSMingming Cao * orphan list. So recovery will truncate it back to the original size 30974c0425ffSMingming Cao * if the machine crashes during the write. 30984c0425ffSMingming Cao * 30994c0425ffSMingming Cao */ 31004c0425ffSMingming Cao static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb, 31014c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 31024c0425ffSMingming Cao unsigned long nr_segs) 31034c0425ffSMingming Cao { 31044c0425ffSMingming Cao struct file *file = iocb->ki_filp; 31054c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 31064c0425ffSMingming Cao ssize_t ret; 31074c0425ffSMingming Cao size_t count = iov_length(iov, nr_segs); 3108729f52c6SZheng Liu int overwrite = 0; 31098b0f165fSAnatol Pomozov get_block_t *get_block_func = NULL; 31108b0f165fSAnatol Pomozov int dio_flags = 0; 311169c499d1STheodore Ts'o loff_t final_size = offset + count; 311269c499d1STheodore Ts'o 311369c499d1STheodore Ts'o /* Use the old path for reads and writes beyond i_size. */ 311469c499d1STheodore Ts'o if (rw != WRITE || final_size > inode->i_size) 311569c499d1STheodore Ts'o return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 3116729f52c6SZheng Liu 31174bd809dbSZheng Liu BUG_ON(iocb->private == NULL); 31184bd809dbSZheng Liu 31194bd809dbSZheng Liu /* If we do a overwrite dio, i_mutex locking can be released */ 31204bd809dbSZheng Liu overwrite = *((int *)iocb->private); 31214bd809dbSZheng Liu 31224bd809dbSZheng Liu if (overwrite) { 31231f555cfaSDmitry Monakhov atomic_inc(&inode->i_dio_count); 31244bd809dbSZheng Liu down_read(&EXT4_I(inode)->i_data_sem); 31254bd809dbSZheng Liu mutex_unlock(&inode->i_mutex); 31264bd809dbSZheng Liu } 31274bd809dbSZheng Liu 31284c0425ffSMingming Cao /* 31298d5d02e6SMingming Cao * We could direct write to holes and fallocate. 31308d5d02e6SMingming Cao * 313169c499d1STheodore Ts'o * Allocated blocks to fill the hole are marked as 313269c499d1STheodore Ts'o * uninitialized to prevent parallel buffered read to expose 313369c499d1STheodore Ts'o * the stale data before DIO complete the data IO. 31348d5d02e6SMingming Cao * 313569c499d1STheodore Ts'o * As to previously fallocated extents, ext4 get_block will 313669c499d1STheodore Ts'o * just simply mark the buffer mapped but still keep the 313769c499d1STheodore Ts'o * extents uninitialized. 31384c0425ffSMingming Cao * 313969c499d1STheodore Ts'o * For non AIO case, we will convert those unwritten extents 31408d5d02e6SMingming Cao * to written after return back from blockdev_direct_IO. 31414c0425ffSMingming Cao * 314269c499d1STheodore Ts'o * For async DIO, the conversion needs to be deferred when the 314369c499d1STheodore Ts'o * IO is completed. The ext4 end_io callback function will be 314469c499d1STheodore Ts'o * called to take care of the conversion work. Here for async 314569c499d1STheodore Ts'o * case, we allocate an io_end structure to hook to the iocb. 31464c0425ffSMingming Cao */ 31478d5d02e6SMingming Cao iocb->private = NULL; 3148f45ee3a1SDmitry Monakhov ext4_inode_aio_set(inode, NULL); 31498d5d02e6SMingming Cao if (!is_sync_kiocb(iocb)) { 315069c499d1STheodore Ts'o ext4_io_end_t *io_end = ext4_init_io_end(inode, GFP_NOFS); 31514bd809dbSZheng Liu if (!io_end) { 31524bd809dbSZheng Liu ret = -ENOMEM; 31534bd809dbSZheng Liu goto retake_lock; 31544bd809dbSZheng Liu } 3155266991b1SJeff Moyer io_end->flag |= EXT4_IO_END_DIRECT; 3156266991b1SJeff Moyer iocb->private = io_end; 31578d5d02e6SMingming Cao /* 315869c499d1STheodore Ts'o * we save the io structure for current async direct 315969c499d1STheodore Ts'o * IO, so that later ext4_map_blocks() could flag the 316069c499d1STheodore Ts'o * io structure whether there is a unwritten extents 316169c499d1STheodore Ts'o * needs to be converted when IO is completed. 31628d5d02e6SMingming Cao */ 3163f45ee3a1SDmitry Monakhov ext4_inode_aio_set(inode, io_end); 31648d5d02e6SMingming Cao } 31658d5d02e6SMingming Cao 31668b0f165fSAnatol Pomozov if (overwrite) { 31678b0f165fSAnatol Pomozov get_block_func = ext4_get_block_write_nolock; 31688b0f165fSAnatol Pomozov } else { 31698b0f165fSAnatol Pomozov get_block_func = ext4_get_block_write; 31708b0f165fSAnatol Pomozov dio_flags = DIO_LOCKING; 31718b0f165fSAnatol Pomozov } 3172729f52c6SZheng Liu ret = __blockdev_direct_IO(rw, iocb, inode, 3173729f52c6SZheng Liu inode->i_sb->s_bdev, iov, 3174729f52c6SZheng Liu offset, nr_segs, 31758b0f165fSAnatol Pomozov get_block_func, 3176729f52c6SZheng Liu ext4_end_io_dio, 3177729f52c6SZheng Liu NULL, 31788b0f165fSAnatol Pomozov dio_flags); 31798b0f165fSAnatol Pomozov 31808d5d02e6SMingming Cao if (iocb->private) 3181f45ee3a1SDmitry Monakhov ext4_inode_aio_set(inode, NULL); 31828d5d02e6SMingming Cao /* 318369c499d1STheodore Ts'o * The io_end structure takes a reference to the inode, that 318469c499d1STheodore Ts'o * structure needs to be destroyed and the reference to the 318569c499d1STheodore Ts'o * inode need to be dropped, when IO is complete, even with 0 318669c499d1STheodore Ts'o * byte write, or failed. 31878d5d02e6SMingming Cao * 318869c499d1STheodore Ts'o * In the successful AIO DIO case, the io_end structure will 318969c499d1STheodore Ts'o * be destroyed and the reference to the inode will be dropped 31908d5d02e6SMingming Cao * after the end_io call back function is called. 31918d5d02e6SMingming Cao * 319269c499d1STheodore Ts'o * In the case there is 0 byte write, or error case, since VFS 319369c499d1STheodore Ts'o * direct IO won't invoke the end_io call back function, we 319469c499d1STheodore Ts'o * need to free the end_io structure here. 31958d5d02e6SMingming Cao */ 31968d5d02e6SMingming Cao if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) { 31978d5d02e6SMingming Cao ext4_free_io_end(iocb->private); 31988d5d02e6SMingming Cao iocb->private = NULL; 3199729f52c6SZheng Liu } else if (ret > 0 && !overwrite && ext4_test_inode_state(inode, 32005f524950SMingming EXT4_STATE_DIO_UNWRITTEN)) { 3201109f5565SMingming int err; 32028d5d02e6SMingming Cao /* 32038d5d02e6SMingming Cao * for non AIO case, since the IO is already 320425985edcSLucas De Marchi * completed, we could do the conversion right here 32058d5d02e6SMingming Cao */ 3206109f5565SMingming err = ext4_convert_unwritten_extents(inode, 32078d5d02e6SMingming Cao offset, ret); 3208109f5565SMingming if (err < 0) 3209109f5565SMingming ret = err; 321019f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN); 3211109f5565SMingming } 32124bd809dbSZheng Liu 32134bd809dbSZheng Liu retake_lock: 32144bd809dbSZheng Liu /* take i_mutex locking again if we do a ovewrite dio */ 32154bd809dbSZheng Liu if (overwrite) { 32161f555cfaSDmitry Monakhov inode_dio_done(inode); 32174bd809dbSZheng Liu up_read(&EXT4_I(inode)->i_data_sem); 32184bd809dbSZheng Liu mutex_lock(&inode->i_mutex); 32194bd809dbSZheng Liu } 32204bd809dbSZheng Liu 32214c0425ffSMingming Cao return ret; 32224c0425ffSMingming Cao } 32238d5d02e6SMingming Cao 32244c0425ffSMingming Cao static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb, 32254c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 32264c0425ffSMingming Cao unsigned long nr_segs) 32274c0425ffSMingming Cao { 32284c0425ffSMingming Cao struct file *file = iocb->ki_filp; 32294c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 32300562e0baSJiaying Zhang ssize_t ret; 32314c0425ffSMingming Cao 323284ebd795STheodore Ts'o /* 323384ebd795STheodore Ts'o * If we are doing data journalling we don't support O_DIRECT 323484ebd795STheodore Ts'o */ 323584ebd795STheodore Ts'o if (ext4_should_journal_data(inode)) 323684ebd795STheodore Ts'o return 0; 323784ebd795STheodore Ts'o 323846c7f254STao Ma /* Let buffer I/O handle the inline data case. */ 323946c7f254STao Ma if (ext4_has_inline_data(inode)) 324046c7f254STao Ma return 0; 324146c7f254STao Ma 32420562e0baSJiaying Zhang trace_ext4_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw); 324312e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 32440562e0baSJiaying Zhang ret = ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs); 32450562e0baSJiaying Zhang else 32460562e0baSJiaying Zhang ret = ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 32470562e0baSJiaying Zhang trace_ext4_direct_IO_exit(inode, offset, 32480562e0baSJiaying Zhang iov_length(iov, nr_segs), rw, ret); 32490562e0baSJiaying Zhang return ret; 32504c0425ffSMingming Cao } 32514c0425ffSMingming Cao 3252ac27a0ecSDave Kleikamp /* 3253617ba13bSMingming Cao * Pages can be marked dirty completely asynchronously from ext4's journalling 3254ac27a0ecSDave Kleikamp * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do 3255ac27a0ecSDave Kleikamp * much here because ->set_page_dirty is called under VFS locks. The page is 3256ac27a0ecSDave Kleikamp * not necessarily locked. 3257ac27a0ecSDave Kleikamp * 3258ac27a0ecSDave Kleikamp * We cannot just dirty the page and leave attached buffers clean, because the 3259ac27a0ecSDave Kleikamp * buffers' dirty state is "definitive". We cannot just set the buffers dirty 3260ac27a0ecSDave Kleikamp * or jbddirty because all the journalling code will explode. 3261ac27a0ecSDave Kleikamp * 3262ac27a0ecSDave Kleikamp * So what we do is to mark the page "pending dirty" and next time writepage 3263ac27a0ecSDave Kleikamp * is called, propagate that into the buffers appropriately. 3264ac27a0ecSDave Kleikamp */ 3265617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page) 3266ac27a0ecSDave Kleikamp { 3267ac27a0ecSDave Kleikamp SetPageChecked(page); 3268ac27a0ecSDave Kleikamp return __set_page_dirty_nobuffers(page); 3269ac27a0ecSDave Kleikamp } 3270ac27a0ecSDave Kleikamp 3271617ba13bSMingming Cao static const struct address_space_operations ext4_ordered_aops = { 3272617ba13bSMingming Cao .readpage = ext4_readpage, 3273617ba13bSMingming Cao .readpages = ext4_readpages, 327443ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3275bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3276bfc1af65SNick Piggin .write_end = ext4_ordered_write_end, 3277617ba13bSMingming Cao .bmap = ext4_bmap, 3278617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3279617ba13bSMingming Cao .releasepage = ext4_releasepage, 3280617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 3281ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 32828ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3283aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3284ac27a0ecSDave Kleikamp }; 3285ac27a0ecSDave Kleikamp 3286617ba13bSMingming Cao static const struct address_space_operations ext4_writeback_aops = { 3287617ba13bSMingming Cao .readpage = ext4_readpage, 3288617ba13bSMingming Cao .readpages = ext4_readpages, 328943ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3290bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3291bfc1af65SNick Piggin .write_end = ext4_writeback_write_end, 3292617ba13bSMingming Cao .bmap = ext4_bmap, 3293617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3294617ba13bSMingming Cao .releasepage = ext4_releasepage, 3295617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 3296ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 32978ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3298aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3299ac27a0ecSDave Kleikamp }; 3300ac27a0ecSDave Kleikamp 3301617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 3302617ba13bSMingming Cao .readpage = ext4_readpage, 3303617ba13bSMingming Cao .readpages = ext4_readpages, 330443ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3305bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3306bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 3307617ba13bSMingming Cao .set_page_dirty = ext4_journalled_set_page_dirty, 3308617ba13bSMingming Cao .bmap = ext4_bmap, 33094520fb3cSJan Kara .invalidatepage = ext4_journalled_invalidatepage, 3310617ba13bSMingming Cao .releasepage = ext4_releasepage, 331184ebd795STheodore Ts'o .direct_IO = ext4_direct_IO, 33128ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3313aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3314ac27a0ecSDave Kleikamp }; 3315ac27a0ecSDave Kleikamp 331664769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 331764769240SAlex Tomas .readpage = ext4_readpage, 331864769240SAlex Tomas .readpages = ext4_readpages, 331943ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 332064769240SAlex Tomas .writepages = ext4_da_writepages, 332164769240SAlex Tomas .write_begin = ext4_da_write_begin, 332264769240SAlex Tomas .write_end = ext4_da_write_end, 332364769240SAlex Tomas .bmap = ext4_bmap, 332464769240SAlex Tomas .invalidatepage = ext4_da_invalidatepage, 332564769240SAlex Tomas .releasepage = ext4_releasepage, 332664769240SAlex Tomas .direct_IO = ext4_direct_IO, 332764769240SAlex Tomas .migratepage = buffer_migrate_page, 33288ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3329aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 333064769240SAlex Tomas }; 333164769240SAlex Tomas 3332617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 3333ac27a0ecSDave Kleikamp { 33343d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 33353d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 33363d2b1582SLukas Czerner if (test_opt(inode->i_sb, DELALLOC)) 3337cd1aac32SAneesh Kumar K.V inode->i_mapping->a_ops = &ext4_da_aops; 3338ac27a0ecSDave Kleikamp else 33393d2b1582SLukas Czerner inode->i_mapping->a_ops = &ext4_ordered_aops; 33403d2b1582SLukas Czerner break; 33413d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 33423d2b1582SLukas Czerner if (test_opt(inode->i_sb, DELALLOC)) 33433d2b1582SLukas Czerner inode->i_mapping->a_ops = &ext4_da_aops; 33443d2b1582SLukas Czerner else 33453d2b1582SLukas Czerner inode->i_mapping->a_ops = &ext4_writeback_aops; 33463d2b1582SLukas Czerner break; 33473d2b1582SLukas Czerner case EXT4_INODE_JOURNAL_DATA_MODE: 3348617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 33493d2b1582SLukas Czerner break; 33503d2b1582SLukas Czerner default: 33513d2b1582SLukas Czerner BUG(); 33523d2b1582SLukas Czerner } 3353ac27a0ecSDave Kleikamp } 3354ac27a0ecSDave Kleikamp 33554e96b2dbSAllison Henderson 33564e96b2dbSAllison Henderson /* 33574e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers() 33584e96b2dbSAllison Henderson * Wrapper function for ext4_discard_partial_page_buffers_no_lock. 33594e96b2dbSAllison Henderson * This function finds and locks the page containing the offset 33604e96b2dbSAllison Henderson * "from" and passes it to ext4_discard_partial_page_buffers_no_lock. 33614e96b2dbSAllison Henderson * Calling functions that already have the page locked should call 33624e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers_no_lock directly. 33634e96b2dbSAllison Henderson */ 33644e96b2dbSAllison Henderson int ext4_discard_partial_page_buffers(handle_t *handle, 33654e96b2dbSAllison Henderson struct address_space *mapping, loff_t from, 33664e96b2dbSAllison Henderson loff_t length, int flags) 33674e96b2dbSAllison Henderson { 33684e96b2dbSAllison Henderson struct inode *inode = mapping->host; 33694e96b2dbSAllison Henderson struct page *page; 33704e96b2dbSAllison Henderson int err = 0; 33714e96b2dbSAllison Henderson 33724e96b2dbSAllison Henderson page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT, 33734e96b2dbSAllison Henderson mapping_gfp_mask(mapping) & ~__GFP_FS); 33744e96b2dbSAllison Henderson if (!page) 33755129d05fSYongqiang Yang return -ENOMEM; 33764e96b2dbSAllison Henderson 33774e96b2dbSAllison Henderson err = ext4_discard_partial_page_buffers_no_lock(handle, inode, page, 33784e96b2dbSAllison Henderson from, length, flags); 33794e96b2dbSAllison Henderson 33804e96b2dbSAllison Henderson unlock_page(page); 33814e96b2dbSAllison Henderson page_cache_release(page); 33824e96b2dbSAllison Henderson return err; 33834e96b2dbSAllison Henderson } 33844e96b2dbSAllison Henderson 33854e96b2dbSAllison Henderson /* 33864e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers_no_lock() 33874e96b2dbSAllison Henderson * Zeros a page range of length 'length' starting from offset 'from'. 33884e96b2dbSAllison Henderson * Buffer heads that correspond to the block aligned regions of the 33894e96b2dbSAllison Henderson * zeroed range will be unmapped. Unblock aligned regions 33904e96b2dbSAllison Henderson * will have the corresponding buffer head mapped if needed so that 33914e96b2dbSAllison Henderson * that region of the page can be updated with the partial zero out. 33924e96b2dbSAllison Henderson * 33934e96b2dbSAllison Henderson * This function assumes that the page has already been locked. The 33944e96b2dbSAllison Henderson * The range to be discarded must be contained with in the given page. 33954e96b2dbSAllison Henderson * If the specified range exceeds the end of the page it will be shortened 33964e96b2dbSAllison Henderson * to the end of the page that corresponds to 'from'. This function is 33974e96b2dbSAllison Henderson * appropriate for updating a page and it buffer heads to be unmapped and 33984e96b2dbSAllison Henderson * zeroed for blocks that have been either released, or are going to be 33994e96b2dbSAllison Henderson * released. 34004e96b2dbSAllison Henderson * 34014e96b2dbSAllison Henderson * handle: The journal handle 34024e96b2dbSAllison Henderson * inode: The files inode 34034e96b2dbSAllison Henderson * page: A locked page that contains the offset "from" 34044907cb7bSAnatol Pomozov * from: The starting byte offset (from the beginning of the file) 34054e96b2dbSAllison Henderson * to begin discarding 34064e96b2dbSAllison Henderson * len: The length of bytes to discard 34074e96b2dbSAllison Henderson * flags: Optional flags that may be used: 34084e96b2dbSAllison Henderson * 34094e96b2dbSAllison Henderson * EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED 34104e96b2dbSAllison Henderson * Only zero the regions of the page whose buffer heads 34114e96b2dbSAllison Henderson * have already been unmapped. This flag is appropriate 34124907cb7bSAnatol Pomozov * for updating the contents of a page whose blocks may 34134e96b2dbSAllison Henderson * have already been released, and we only want to zero 34144e96b2dbSAllison Henderson * out the regions that correspond to those released blocks. 34154e96b2dbSAllison Henderson * 34164907cb7bSAnatol Pomozov * Returns zero on success or negative on failure. 34174e96b2dbSAllison Henderson */ 34185f163cc7SEric Sandeen static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle, 34194e96b2dbSAllison Henderson struct inode *inode, struct page *page, loff_t from, 34204e96b2dbSAllison Henderson loff_t length, int flags) 34214e96b2dbSAllison Henderson { 34224e96b2dbSAllison Henderson ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; 34234e96b2dbSAllison Henderson unsigned int offset = from & (PAGE_CACHE_SIZE-1); 34244e96b2dbSAllison Henderson unsigned int blocksize, max, pos; 34254e96b2dbSAllison Henderson ext4_lblk_t iblock; 34264e96b2dbSAllison Henderson struct buffer_head *bh; 34274e96b2dbSAllison Henderson int err = 0; 34284e96b2dbSAllison Henderson 34294e96b2dbSAllison Henderson blocksize = inode->i_sb->s_blocksize; 34304e96b2dbSAllison Henderson max = PAGE_CACHE_SIZE - offset; 34314e96b2dbSAllison Henderson 34324e96b2dbSAllison Henderson if (index != page->index) 34334e96b2dbSAllison Henderson return -EINVAL; 34344e96b2dbSAllison Henderson 34354e96b2dbSAllison Henderson /* 34364e96b2dbSAllison Henderson * correct length if it does not fall between 34374e96b2dbSAllison Henderson * 'from' and the end of the page 34384e96b2dbSAllison Henderson */ 34394e96b2dbSAllison Henderson if (length > max || length < 0) 34404e96b2dbSAllison Henderson length = max; 34414e96b2dbSAllison Henderson 34424e96b2dbSAllison Henderson iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); 34434e96b2dbSAllison Henderson 3444093e6e36SYongqiang Yang if (!page_has_buffers(page)) 34454e96b2dbSAllison Henderson create_empty_buffers(page, blocksize, 0); 34464e96b2dbSAllison Henderson 34474e96b2dbSAllison Henderson /* Find the buffer that contains "offset" */ 34484e96b2dbSAllison Henderson bh = page_buffers(page); 34494e96b2dbSAllison Henderson pos = blocksize; 34504e96b2dbSAllison Henderson while (offset >= pos) { 34514e96b2dbSAllison Henderson bh = bh->b_this_page; 34524e96b2dbSAllison Henderson iblock++; 34534e96b2dbSAllison Henderson pos += blocksize; 34544e96b2dbSAllison Henderson } 34554e96b2dbSAllison Henderson 34564e96b2dbSAllison Henderson pos = offset; 34574e96b2dbSAllison Henderson while (pos < offset + length) { 3458e260daf2SYongqiang Yang unsigned int end_of_block, range_to_discard; 3459e260daf2SYongqiang Yang 34604e96b2dbSAllison Henderson err = 0; 34614e96b2dbSAllison Henderson 34624e96b2dbSAllison Henderson /* The length of space left to zero and unmap */ 34634e96b2dbSAllison Henderson range_to_discard = offset + length - pos; 34644e96b2dbSAllison Henderson 34654e96b2dbSAllison Henderson /* The length of space until the end of the block */ 34664e96b2dbSAllison Henderson end_of_block = blocksize - (pos & (blocksize-1)); 34674e96b2dbSAllison Henderson 34684e96b2dbSAllison Henderson /* 34694e96b2dbSAllison Henderson * Do not unmap or zero past end of block 34704e96b2dbSAllison Henderson * for this buffer head 34714e96b2dbSAllison Henderson */ 34724e96b2dbSAllison Henderson if (range_to_discard > end_of_block) 34734e96b2dbSAllison Henderson range_to_discard = end_of_block; 34744e96b2dbSAllison Henderson 34754e96b2dbSAllison Henderson 34764e96b2dbSAllison Henderson /* 34774e96b2dbSAllison Henderson * Skip this buffer head if we are only zeroing unampped 34784e96b2dbSAllison Henderson * regions of the page 34794e96b2dbSAllison Henderson */ 34804e96b2dbSAllison Henderson if (flags & EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED && 34814e96b2dbSAllison Henderson buffer_mapped(bh)) 34824e96b2dbSAllison Henderson goto next; 34834e96b2dbSAllison Henderson 34844e96b2dbSAllison Henderson /* If the range is block aligned, unmap */ 34854e96b2dbSAllison Henderson if (range_to_discard == blocksize) { 34864e96b2dbSAllison Henderson clear_buffer_dirty(bh); 34874e96b2dbSAllison Henderson bh->b_bdev = NULL; 34884e96b2dbSAllison Henderson clear_buffer_mapped(bh); 34894e96b2dbSAllison Henderson clear_buffer_req(bh); 34904e96b2dbSAllison Henderson clear_buffer_new(bh); 34914e96b2dbSAllison Henderson clear_buffer_delay(bh); 34924e96b2dbSAllison Henderson clear_buffer_unwritten(bh); 34934e96b2dbSAllison Henderson clear_buffer_uptodate(bh); 34944e96b2dbSAllison Henderson zero_user(page, pos, range_to_discard); 34954e96b2dbSAllison Henderson BUFFER_TRACE(bh, "Buffer discarded"); 34964e96b2dbSAllison Henderson goto next; 34974e96b2dbSAllison Henderson } 34984e96b2dbSAllison Henderson 34994e96b2dbSAllison Henderson /* 35004e96b2dbSAllison Henderson * If this block is not completely contained in the range 35014e96b2dbSAllison Henderson * to be discarded, then it is not going to be released. Because 35024e96b2dbSAllison Henderson * we need to keep this block, we need to make sure this part 35034e96b2dbSAllison Henderson * of the page is uptodate before we modify it by writeing 35044e96b2dbSAllison Henderson * partial zeros on it. 35054e96b2dbSAllison Henderson */ 35064e96b2dbSAllison Henderson if (!buffer_mapped(bh)) { 35074e96b2dbSAllison Henderson /* 35084e96b2dbSAllison Henderson * Buffer head must be mapped before we can read 35094e96b2dbSAllison Henderson * from the block 35104e96b2dbSAllison Henderson */ 35114e96b2dbSAllison Henderson BUFFER_TRACE(bh, "unmapped"); 35124e96b2dbSAllison Henderson ext4_get_block(inode, iblock, bh, 0); 35134e96b2dbSAllison Henderson /* unmapped? It's a hole - nothing to do */ 35144e96b2dbSAllison Henderson if (!buffer_mapped(bh)) { 35154e96b2dbSAllison Henderson BUFFER_TRACE(bh, "still unmapped"); 35164e96b2dbSAllison Henderson goto next; 35174e96b2dbSAllison Henderson } 35184e96b2dbSAllison Henderson } 35194e96b2dbSAllison Henderson 35204e96b2dbSAllison Henderson /* Ok, it's mapped. Make sure it's up-to-date */ 35214e96b2dbSAllison Henderson if (PageUptodate(page)) 35224e96b2dbSAllison Henderson set_buffer_uptodate(bh); 35234e96b2dbSAllison Henderson 35244e96b2dbSAllison Henderson if (!buffer_uptodate(bh)) { 35254e96b2dbSAllison Henderson err = -EIO; 35264e96b2dbSAllison Henderson ll_rw_block(READ, 1, &bh); 35274e96b2dbSAllison Henderson wait_on_buffer(bh); 35284e96b2dbSAllison Henderson /* Uhhuh. Read error. Complain and punt.*/ 35294e96b2dbSAllison Henderson if (!buffer_uptodate(bh)) 35304e96b2dbSAllison Henderson goto next; 35314e96b2dbSAllison Henderson } 35324e96b2dbSAllison Henderson 35334e96b2dbSAllison Henderson if (ext4_should_journal_data(inode)) { 35344e96b2dbSAllison Henderson BUFFER_TRACE(bh, "get write access"); 35354e96b2dbSAllison Henderson err = ext4_journal_get_write_access(handle, bh); 35364e96b2dbSAllison Henderson if (err) 35374e96b2dbSAllison Henderson goto next; 35384e96b2dbSAllison Henderson } 35394e96b2dbSAllison Henderson 35404e96b2dbSAllison Henderson zero_user(page, pos, range_to_discard); 35414e96b2dbSAllison Henderson 35424e96b2dbSAllison Henderson err = 0; 35434e96b2dbSAllison Henderson if (ext4_should_journal_data(inode)) { 35444e96b2dbSAllison Henderson err = ext4_handle_dirty_metadata(handle, inode, bh); 3545decbd919STheodore Ts'o } else 35464e96b2dbSAllison Henderson mark_buffer_dirty(bh); 35474e96b2dbSAllison Henderson 35484e96b2dbSAllison Henderson BUFFER_TRACE(bh, "Partial buffer zeroed"); 35494e96b2dbSAllison Henderson next: 35504e96b2dbSAllison Henderson bh = bh->b_this_page; 35514e96b2dbSAllison Henderson iblock++; 35524e96b2dbSAllison Henderson pos += range_to_discard; 35534e96b2dbSAllison Henderson } 35544e96b2dbSAllison Henderson 35554e96b2dbSAllison Henderson return err; 35564e96b2dbSAllison Henderson } 35574e96b2dbSAllison Henderson 355891ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 355991ef4cafSDuane Griffin { 356091ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 356191ef4cafSDuane Griffin return 1; 356291ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 356391ef4cafSDuane Griffin return 1; 356491ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 356591ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 356691ef4cafSDuane Griffin return 0; 356791ef4cafSDuane Griffin } 356891ef4cafSDuane Griffin 3569ac27a0ecSDave Kleikamp /* 3570a4bb6b64SAllison Henderson * ext4_punch_hole: punches a hole in a file by releaseing the blocks 3571a4bb6b64SAllison Henderson * associated with the given offset and length 3572a4bb6b64SAllison Henderson * 3573a4bb6b64SAllison Henderson * @inode: File inode 3574a4bb6b64SAllison Henderson * @offset: The offset where the hole will begin 3575a4bb6b64SAllison Henderson * @len: The length of the hole 3576a4bb6b64SAllison Henderson * 35774907cb7bSAnatol Pomozov * Returns: 0 on success or negative on failure 3578a4bb6b64SAllison Henderson */ 3579a4bb6b64SAllison Henderson 3580a4bb6b64SAllison Henderson int ext4_punch_hole(struct file *file, loff_t offset, loff_t length) 3581a4bb6b64SAllison Henderson { 3582a4bb6b64SAllison Henderson struct inode *inode = file->f_path.dentry->d_inode; 3583a4bb6b64SAllison Henderson if (!S_ISREG(inode->i_mode)) 358473355192SAllison Henderson return -EOPNOTSUPP; 3585a4bb6b64SAllison Henderson 35868bad6fc8SZheng Liu if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 35878bad6fc8SZheng Liu return ext4_ind_punch_hole(file, offset, length); 3588a4bb6b64SAllison Henderson 3589bab08ab9STheodore Ts'o if (EXT4_SB(inode->i_sb)->s_cluster_ratio > 1) { 3590bab08ab9STheodore Ts'o /* TODO: Add support for bigalloc file systems */ 359173355192SAllison Henderson return -EOPNOTSUPP; 3592bab08ab9STheodore Ts'o } 3593bab08ab9STheodore Ts'o 3594aaddea81SZheng Liu trace_ext4_punch_hole(inode, offset, length); 3595aaddea81SZheng Liu 3596a4bb6b64SAllison Henderson return ext4_ext_punch_hole(file, offset, length); 3597a4bb6b64SAllison Henderson } 3598a4bb6b64SAllison Henderson 3599a4bb6b64SAllison Henderson /* 3600617ba13bSMingming Cao * ext4_truncate() 3601ac27a0ecSDave Kleikamp * 3602617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 3603617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 3604ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 3605ac27a0ecSDave Kleikamp * 360642b2aa86SJustin P. Mattock * As we work through the truncate and commit bits of it to the journal there 3607ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 3608ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 3609ac27a0ecSDave Kleikamp * 3610ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 3611ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 3612ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 3613ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 3614ac27a0ecSDave Kleikamp * left-to-right works OK too). 3615ac27a0ecSDave Kleikamp * 3616ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 3617ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 3618ac27a0ecSDave Kleikamp * 3619ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 3620617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 3621ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 3622617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 3623617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 3624ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 3625617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 3626ac27a0ecSDave Kleikamp */ 3627617ba13bSMingming Cao void ext4_truncate(struct inode *inode) 3628ac27a0ecSDave Kleikamp { 36290562e0baSJiaying Zhang trace_ext4_truncate_enter(inode); 36300562e0baSJiaying Zhang 363191ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 3632ac27a0ecSDave Kleikamp return; 3633ac27a0ecSDave Kleikamp 363412e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS); 3635c8d46e41SJiaying Zhang 36365534fb5bSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 363719f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE); 36387d8f9f7dSTheodore Ts'o 3639aef1c851STao Ma if (ext4_has_inline_data(inode)) { 3640aef1c851STao Ma int has_inline = 1; 3641aef1c851STao Ma 3642aef1c851STao Ma ext4_inline_data_truncate(inode, &has_inline); 3643aef1c851STao Ma if (has_inline) 3644aef1c851STao Ma return; 3645aef1c851STao Ma } 3646aef1c851STao Ma 3647ff9893dcSAmir Goldstein if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3648cf108bcaSJan Kara ext4_ext_truncate(inode); 3649ff9893dcSAmir Goldstein else 3650ff9893dcSAmir Goldstein ext4_ind_truncate(inode); 3651a86c6181SAlex Tomas 36520562e0baSJiaying Zhang trace_ext4_truncate_exit(inode); 3653ac27a0ecSDave Kleikamp } 3654ac27a0ecSDave Kleikamp 3655ac27a0ecSDave Kleikamp /* 3656617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 3657ac27a0ecSDave Kleikamp * underlying buffer_head on success. If 'in_mem' is true, we have all 3658ac27a0ecSDave Kleikamp * data in memory that is needed to recreate the on-disk version of this 3659ac27a0ecSDave Kleikamp * inode. 3660ac27a0ecSDave Kleikamp */ 3661617ba13bSMingming Cao static int __ext4_get_inode_loc(struct inode *inode, 3662617ba13bSMingming Cao struct ext4_iloc *iloc, int in_mem) 3663ac27a0ecSDave Kleikamp { 3664240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 3665ac27a0ecSDave Kleikamp struct buffer_head *bh; 3666240799cdSTheodore Ts'o struct super_block *sb = inode->i_sb; 3667240799cdSTheodore Ts'o ext4_fsblk_t block; 3668240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 3669ac27a0ecSDave Kleikamp 36703a06d778SAneesh Kumar K.V iloc->bh = NULL; 3671240799cdSTheodore Ts'o if (!ext4_valid_inum(sb, inode->i_ino)) 3672ac27a0ecSDave Kleikamp return -EIO; 3673ac27a0ecSDave Kleikamp 3674240799cdSTheodore Ts'o iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb); 3675240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 3676240799cdSTheodore Ts'o if (!gdp) 3677240799cdSTheodore Ts'o return -EIO; 3678240799cdSTheodore Ts'o 3679240799cdSTheodore Ts'o /* 3680240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 3681240799cdSTheodore Ts'o */ 368200d09882STao Ma inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 3683240799cdSTheodore Ts'o inode_offset = ((inode->i_ino - 1) % 3684240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 3685240799cdSTheodore Ts'o block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); 3686240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 3687240799cdSTheodore Ts'o 3688240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 3689aebf0243SWang Shilong if (unlikely(!bh)) 3690860d21e2STheodore Ts'o return -ENOMEM; 3691ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 3692ac27a0ecSDave Kleikamp lock_buffer(bh); 36939c83a923SHidehiro Kawai 36949c83a923SHidehiro Kawai /* 36959c83a923SHidehiro Kawai * If the buffer has the write error flag, we have failed 36969c83a923SHidehiro Kawai * to write out another inode in the same block. In this 36979c83a923SHidehiro Kawai * case, we don't have to read the block because we may 36989c83a923SHidehiro Kawai * read the old inode data successfully. 36999c83a923SHidehiro Kawai */ 37009c83a923SHidehiro Kawai if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) 37019c83a923SHidehiro Kawai set_buffer_uptodate(bh); 37029c83a923SHidehiro Kawai 3703ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) { 3704ac27a0ecSDave Kleikamp /* someone brought it uptodate while we waited */ 3705ac27a0ecSDave Kleikamp unlock_buffer(bh); 3706ac27a0ecSDave Kleikamp goto has_buffer; 3707ac27a0ecSDave Kleikamp } 3708ac27a0ecSDave Kleikamp 3709ac27a0ecSDave Kleikamp /* 3710ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 3711ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 3712ac27a0ecSDave Kleikamp * block. 3713ac27a0ecSDave Kleikamp */ 3714ac27a0ecSDave Kleikamp if (in_mem) { 3715ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 3716240799cdSTheodore Ts'o int i, start; 3717ac27a0ecSDave Kleikamp 3718240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 3719ac27a0ecSDave Kleikamp 3720ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 3721240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 3722aebf0243SWang Shilong if (unlikely(!bitmap_bh)) 3723ac27a0ecSDave Kleikamp goto make_io; 3724ac27a0ecSDave Kleikamp 3725ac27a0ecSDave Kleikamp /* 3726ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 3727ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 3728ac27a0ecSDave Kleikamp * of one, so skip it. 3729ac27a0ecSDave Kleikamp */ 3730ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 3731ac27a0ecSDave Kleikamp brelse(bitmap_bh); 3732ac27a0ecSDave Kleikamp goto make_io; 3733ac27a0ecSDave Kleikamp } 3734240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 3735ac27a0ecSDave Kleikamp if (i == inode_offset) 3736ac27a0ecSDave Kleikamp continue; 3737617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 3738ac27a0ecSDave Kleikamp break; 3739ac27a0ecSDave Kleikamp } 3740ac27a0ecSDave Kleikamp brelse(bitmap_bh); 3741240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 3742ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 3743ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 3744ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 3745ac27a0ecSDave Kleikamp unlock_buffer(bh); 3746ac27a0ecSDave Kleikamp goto has_buffer; 3747ac27a0ecSDave Kleikamp } 3748ac27a0ecSDave Kleikamp } 3749ac27a0ecSDave Kleikamp 3750ac27a0ecSDave Kleikamp make_io: 3751ac27a0ecSDave Kleikamp /* 3752240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 3753240799cdSTheodore Ts'o * blocks from the inode table. 3754240799cdSTheodore Ts'o */ 3755240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 3756240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 3757240799cdSTheodore Ts'o unsigned num; 3758240799cdSTheodore Ts'o 3759240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 3760b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 3761240799cdSTheodore Ts'o b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1); 3762240799cdSTheodore Ts'o if (table > b) 3763240799cdSTheodore Ts'o b = table; 3764240799cdSTheodore Ts'o end = b + EXT4_SB(sb)->s_inode_readahead_blks; 3765240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 3766feb0ab32SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) 3767560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 3768240799cdSTheodore Ts'o table += num / inodes_per_block; 3769240799cdSTheodore Ts'o if (end > table) 3770240799cdSTheodore Ts'o end = table; 3771240799cdSTheodore Ts'o while (b <= end) 3772240799cdSTheodore Ts'o sb_breadahead(sb, b++); 3773240799cdSTheodore Ts'o } 3774240799cdSTheodore Ts'o 3775240799cdSTheodore Ts'o /* 3776ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 3777ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 3778ac27a0ecSDave Kleikamp * Read the block from disk. 3779ac27a0ecSDave Kleikamp */ 37800562e0baSJiaying Zhang trace_ext4_load_inode(inode); 3781ac27a0ecSDave Kleikamp get_bh(bh); 3782ac27a0ecSDave Kleikamp bh->b_end_io = end_buffer_read_sync; 378365299a3bSChristoph Hellwig submit_bh(READ | REQ_META | REQ_PRIO, bh); 3784ac27a0ecSDave Kleikamp wait_on_buffer(bh); 3785ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 3786c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, block, 3787c398eda0STheodore Ts'o "unable to read itable block"); 3788ac27a0ecSDave Kleikamp brelse(bh); 3789ac27a0ecSDave Kleikamp return -EIO; 3790ac27a0ecSDave Kleikamp } 3791ac27a0ecSDave Kleikamp } 3792ac27a0ecSDave Kleikamp has_buffer: 3793ac27a0ecSDave Kleikamp iloc->bh = bh; 3794ac27a0ecSDave Kleikamp return 0; 3795ac27a0ecSDave Kleikamp } 3796ac27a0ecSDave Kleikamp 3797617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 3798ac27a0ecSDave Kleikamp { 3799ac27a0ecSDave Kleikamp /* We have all inode data except xattrs in memory here. */ 3800617ba13bSMingming Cao return __ext4_get_inode_loc(inode, iloc, 380119f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_XATTR)); 3802ac27a0ecSDave Kleikamp } 3803ac27a0ecSDave Kleikamp 3804617ba13bSMingming Cao void ext4_set_inode_flags(struct inode *inode) 3805ac27a0ecSDave Kleikamp { 3806617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 3807ac27a0ecSDave Kleikamp 3808ac27a0ecSDave Kleikamp inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); 3809617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 3810ac27a0ecSDave Kleikamp inode->i_flags |= S_SYNC; 3811617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 3812ac27a0ecSDave Kleikamp inode->i_flags |= S_APPEND; 3813617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 3814ac27a0ecSDave Kleikamp inode->i_flags |= S_IMMUTABLE; 3815617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 3816ac27a0ecSDave Kleikamp inode->i_flags |= S_NOATIME; 3817617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 3818ac27a0ecSDave Kleikamp inode->i_flags |= S_DIRSYNC; 3819ac27a0ecSDave Kleikamp } 3820ac27a0ecSDave Kleikamp 3821ff9ddf7eSJan Kara /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */ 3822ff9ddf7eSJan Kara void ext4_get_inode_flags(struct ext4_inode_info *ei) 3823ff9ddf7eSJan Kara { 382484a8dce2SDmitry Monakhov unsigned int vfs_fl; 382584a8dce2SDmitry Monakhov unsigned long old_fl, new_fl; 3826ff9ddf7eSJan Kara 382784a8dce2SDmitry Monakhov do { 382884a8dce2SDmitry Monakhov vfs_fl = ei->vfs_inode.i_flags; 382984a8dce2SDmitry Monakhov old_fl = ei->i_flags; 383084a8dce2SDmitry Monakhov new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL| 383184a8dce2SDmitry Monakhov EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL| 383284a8dce2SDmitry Monakhov EXT4_DIRSYNC_FL); 383384a8dce2SDmitry Monakhov if (vfs_fl & S_SYNC) 383484a8dce2SDmitry Monakhov new_fl |= EXT4_SYNC_FL; 383584a8dce2SDmitry Monakhov if (vfs_fl & S_APPEND) 383684a8dce2SDmitry Monakhov new_fl |= EXT4_APPEND_FL; 383784a8dce2SDmitry Monakhov if (vfs_fl & S_IMMUTABLE) 383884a8dce2SDmitry Monakhov new_fl |= EXT4_IMMUTABLE_FL; 383984a8dce2SDmitry Monakhov if (vfs_fl & S_NOATIME) 384084a8dce2SDmitry Monakhov new_fl |= EXT4_NOATIME_FL; 384184a8dce2SDmitry Monakhov if (vfs_fl & S_DIRSYNC) 384284a8dce2SDmitry Monakhov new_fl |= EXT4_DIRSYNC_FL; 384384a8dce2SDmitry Monakhov } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl); 3844ff9ddf7eSJan Kara } 3845de9a55b8STheodore Ts'o 38460fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 38470fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 38480fc1b451SAneesh Kumar K.V { 38490fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 38508180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 38518180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 38520fc1b451SAneesh Kumar K.V 38530fc1b451SAneesh Kumar K.V if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 38540fc1b451SAneesh Kumar K.V EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) { 38550fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 38560fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 38570fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 385807a03824STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) { 38598180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 38608180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 38618180a562SAneesh Kumar K.V } else { 38620fc1b451SAneesh Kumar K.V return i_blocks; 38638180a562SAneesh Kumar K.V } 38640fc1b451SAneesh Kumar K.V } else { 38650fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 38660fc1b451SAneesh Kumar K.V } 38670fc1b451SAneesh Kumar K.V } 3868ff9ddf7eSJan Kara 3869152a7b0aSTao Ma static inline void ext4_iget_extra_inode(struct inode *inode, 3870152a7b0aSTao Ma struct ext4_inode *raw_inode, 3871152a7b0aSTao Ma struct ext4_inode_info *ei) 3872152a7b0aSTao Ma { 3873152a7b0aSTao Ma __le32 *magic = (void *)raw_inode + 3874152a7b0aSTao Ma EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize; 387567cf5b09STao Ma if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) { 3876152a7b0aSTao Ma ext4_set_inode_state(inode, EXT4_STATE_XATTR); 387767cf5b09STao Ma ext4_find_inline_data_nolock(inode); 3878f19d5870STao Ma } else 3879f19d5870STao Ma EXT4_I(inode)->i_inline_off = 0; 3880152a7b0aSTao Ma } 3881152a7b0aSTao Ma 38821d1fe1eeSDavid Howells struct inode *ext4_iget(struct super_block *sb, unsigned long ino) 3883ac27a0ecSDave Kleikamp { 3884617ba13bSMingming Cao struct ext4_iloc iloc; 3885617ba13bSMingming Cao struct ext4_inode *raw_inode; 38861d1fe1eeSDavid Howells struct ext4_inode_info *ei; 38871d1fe1eeSDavid Howells struct inode *inode; 3888b436b9beSJan Kara journal_t *journal = EXT4_SB(sb)->s_journal; 38891d1fe1eeSDavid Howells long ret; 3890ac27a0ecSDave Kleikamp int block; 389108cefc7aSEric W. Biederman uid_t i_uid; 389208cefc7aSEric W. Biederman gid_t i_gid; 3893ac27a0ecSDave Kleikamp 38941d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 38951d1fe1eeSDavid Howells if (!inode) 38961d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 38971d1fe1eeSDavid Howells if (!(inode->i_state & I_NEW)) 38981d1fe1eeSDavid Howells return inode; 38991d1fe1eeSDavid Howells 39001d1fe1eeSDavid Howells ei = EXT4_I(inode); 39017dc57615SPeter Huewe iloc.bh = NULL; 3902ac27a0ecSDave Kleikamp 39031d1fe1eeSDavid Howells ret = __ext4_get_inode_loc(inode, &iloc, 0); 39041d1fe1eeSDavid Howells if (ret < 0) 3905ac27a0ecSDave Kleikamp goto bad_inode; 3906617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 3907814525f4SDarrick J. Wong 3908814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 3909814525f4SDarrick J. Wong ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 3910814525f4SDarrick J. Wong if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 3911814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)) { 3912814525f4SDarrick J. Wong EXT4_ERROR_INODE(inode, "bad extra_isize (%u != %u)", 3913814525f4SDarrick J. Wong EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize, 3914814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)); 3915814525f4SDarrick J. Wong ret = -EIO; 3916814525f4SDarrick J. Wong goto bad_inode; 3917814525f4SDarrick J. Wong } 3918814525f4SDarrick J. Wong } else 3919814525f4SDarrick J. Wong ei->i_extra_isize = 0; 3920814525f4SDarrick J. Wong 3921814525f4SDarrick J. Wong /* Precompute checksum seed for inode metadata */ 3922814525f4SDarrick J. Wong if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 3923814525f4SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) { 3924814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3925814525f4SDarrick J. Wong __u32 csum; 3926814525f4SDarrick J. Wong __le32 inum = cpu_to_le32(inode->i_ino); 3927814525f4SDarrick J. Wong __le32 gen = raw_inode->i_generation; 3928814525f4SDarrick J. Wong csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, 3929814525f4SDarrick J. Wong sizeof(inum)); 3930814525f4SDarrick J. Wong ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, 3931814525f4SDarrick J. Wong sizeof(gen)); 3932814525f4SDarrick J. Wong } 3933814525f4SDarrick J. Wong 3934814525f4SDarrick J. Wong if (!ext4_inode_csum_verify(inode, raw_inode, ei)) { 3935814525f4SDarrick J. Wong EXT4_ERROR_INODE(inode, "checksum invalid"); 3936814525f4SDarrick J. Wong ret = -EIO; 3937814525f4SDarrick J. Wong goto bad_inode; 3938814525f4SDarrick J. Wong } 3939814525f4SDarrick J. Wong 3940ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 394108cefc7aSEric W. Biederman i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 394208cefc7aSEric W. Biederman i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 3943ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 394408cefc7aSEric W. Biederman i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 394508cefc7aSEric W. Biederman i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 3946ac27a0ecSDave Kleikamp } 394708cefc7aSEric W. Biederman i_uid_write(inode, i_uid); 394808cefc7aSEric W. Biederman i_gid_write(inode, i_gid); 3949bfe86848SMiklos Szeredi set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); 3950ac27a0ecSDave Kleikamp 3951353eb83cSTheodore Ts'o ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 395267cf5b09STao Ma ei->i_inline_off = 0; 3953ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 3954ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 3955ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 3956ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 3957ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 3958ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 3959ac27a0ecSDave Kleikamp */ 3960ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 3961ac27a0ecSDave Kleikamp if (inode->i_mode == 0 || 3962617ba13bSMingming Cao !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) { 3963ac27a0ecSDave Kleikamp /* this inode is deleted */ 39641d1fe1eeSDavid Howells ret = -ESTALE; 3965ac27a0ecSDave Kleikamp goto bad_inode; 3966ac27a0ecSDave Kleikamp } 3967ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 3968ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 3969ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 3970ac27a0ecSDave Kleikamp * the process of deleting those. */ 3971ac27a0ecSDave Kleikamp } 3972ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 39730fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 39747973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 3975a9e81742STheodore Ts'o if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) 3976a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 3977a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 3978a48380f7SAneesh Kumar K.V inode->i_size = ext4_isize(raw_inode); 3979ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 3980a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 3981a9e7f447SDmitry Monakhov ei->i_reserved_quota = 0; 3982a9e7f447SDmitry Monakhov #endif 3983ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 3984ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 3985a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 3986ac27a0ecSDave Kleikamp /* 3987ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 3988ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 3989ac27a0ecSDave Kleikamp */ 3990617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 3991ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 3992ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 3993ac27a0ecSDave Kleikamp 3994b436b9beSJan Kara /* 3995b436b9beSJan Kara * Set transaction id's of transactions that have to be committed 3996b436b9beSJan Kara * to finish f[data]sync. We set them to currently running transaction 3997b436b9beSJan Kara * as we cannot be sure that the inode or some of its metadata isn't 3998b436b9beSJan Kara * part of the transaction - the inode could have been reclaimed and 3999b436b9beSJan Kara * now it is reread from disk. 4000b436b9beSJan Kara */ 4001b436b9beSJan Kara if (journal) { 4002b436b9beSJan Kara transaction_t *transaction; 4003b436b9beSJan Kara tid_t tid; 4004b436b9beSJan Kara 4005a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 4006b436b9beSJan Kara if (journal->j_running_transaction) 4007b436b9beSJan Kara transaction = journal->j_running_transaction; 4008b436b9beSJan Kara else 4009b436b9beSJan Kara transaction = journal->j_committing_transaction; 4010b436b9beSJan Kara if (transaction) 4011b436b9beSJan Kara tid = transaction->t_tid; 4012b436b9beSJan Kara else 4013b436b9beSJan Kara tid = journal->j_commit_sequence; 4014a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 4015b436b9beSJan Kara ei->i_sync_tid = tid; 4016b436b9beSJan Kara ei->i_datasync_tid = tid; 4017b436b9beSJan Kara } 4018b436b9beSJan Kara 40190040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4020ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 4021ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 4022617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 4023617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 4024ac27a0ecSDave Kleikamp } else { 4025152a7b0aSTao Ma ext4_iget_extra_inode(inode, raw_inode, ei); 4026ac27a0ecSDave Kleikamp } 4027814525f4SDarrick J. Wong } 4028ac27a0ecSDave Kleikamp 4029ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 4030ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 4031ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 4032ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 4033ef7f3835SKalpak Shah 403425ec56b5SJean Noel Cordenner inode->i_version = le32_to_cpu(raw_inode->i_disk_version); 403525ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 403625ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 403725ec56b5SJean Noel Cordenner inode->i_version |= 403825ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 403925ec56b5SJean Noel Cordenner } 404025ec56b5SJean Noel Cordenner 4041c4b5a614STheodore Ts'o ret = 0; 4042485c26ecSTheodore Ts'o if (ei->i_file_acl && 40431032988cSTheodore Ts'o !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) { 404424676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "bad extended attribute block %llu", 404524676da4STheodore Ts'o ei->i_file_acl); 4046485c26ecSTheodore Ts'o ret = -EIO; 4047485c26ecSTheodore Ts'o goto bad_inode; 4048f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 4049f19d5870STao Ma if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 4050f19d5870STao Ma if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 4051c4b5a614STheodore Ts'o (S_ISLNK(inode->i_mode) && 4052f19d5870STao Ma !ext4_inode_is_fast_symlink(inode)))) 40537a262f7cSAneesh Kumar K.V /* Validate extent which is part of inode */ 40547a262f7cSAneesh Kumar K.V ret = ext4_ext_check_inode(inode); 4055fe2c8191SThiemo Nagel } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 4056fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 4057fe2c8191SThiemo Nagel !ext4_inode_is_fast_symlink(inode))) { 4058fe2c8191SThiemo Nagel /* Validate block references which are part of inode */ 40591f7d1e77STheodore Ts'o ret = ext4_ind_check_inode(inode); 4060fe2c8191SThiemo Nagel } 4061f19d5870STao Ma } 4062567f3e9aSTheodore Ts'o if (ret) 40637a262f7cSAneesh Kumar K.V goto bad_inode; 40647a262f7cSAneesh Kumar K.V 4065ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 4066617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 4067617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 4068617ba13bSMingming Cao ext4_set_aops(inode); 4069ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 4070617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 4071617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 4072ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 4073e83c1397SDuane Griffin if (ext4_inode_is_fast_symlink(inode)) { 4074617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 4075e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 4076e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 4077e83c1397SDuane Griffin } else { 4078617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 4079617ba13bSMingming Cao ext4_set_aops(inode); 4080ac27a0ecSDave Kleikamp } 4081563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 4082563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 4083617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 4084ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 4085ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4086ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 4087ac27a0ecSDave Kleikamp else 4088ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4089ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 4090563bdd61STheodore Ts'o } else { 4091563bdd61STheodore Ts'o ret = -EIO; 409224676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode); 4093563bdd61STheodore Ts'o goto bad_inode; 4094ac27a0ecSDave Kleikamp } 4095ac27a0ecSDave Kleikamp brelse(iloc.bh); 4096617ba13bSMingming Cao ext4_set_inode_flags(inode); 40971d1fe1eeSDavid Howells unlock_new_inode(inode); 40981d1fe1eeSDavid Howells return inode; 4099ac27a0ecSDave Kleikamp 4100ac27a0ecSDave Kleikamp bad_inode: 4101567f3e9aSTheodore Ts'o brelse(iloc.bh); 41021d1fe1eeSDavid Howells iget_failed(inode); 41031d1fe1eeSDavid Howells return ERR_PTR(ret); 4104ac27a0ecSDave Kleikamp } 4105ac27a0ecSDave Kleikamp 41060fc1b451SAneesh Kumar K.V static int ext4_inode_blocks_set(handle_t *handle, 41070fc1b451SAneesh Kumar K.V struct ext4_inode *raw_inode, 41080fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 41090fc1b451SAneesh Kumar K.V { 41100fc1b451SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 41110fc1b451SAneesh Kumar K.V u64 i_blocks = inode->i_blocks; 41120fc1b451SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 41130fc1b451SAneesh Kumar K.V 41140fc1b451SAneesh Kumar K.V if (i_blocks <= ~0U) { 41150fc1b451SAneesh Kumar K.V /* 41164907cb7bSAnatol Pomozov * i_blocks can be represented in a 32 bit variable 41170fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 41180fc1b451SAneesh Kumar K.V */ 41198180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 41200fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = 0; 412184a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 4122f287a1a5STheodore Ts'o return 0; 4123f287a1a5STheodore Ts'o } 4124f287a1a5STheodore Ts'o if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) 4125f287a1a5STheodore Ts'o return -EFBIG; 4126f287a1a5STheodore Ts'o 4127f287a1a5STheodore Ts'o if (i_blocks <= 0xffffffffffffULL) { 41280fc1b451SAneesh Kumar K.V /* 41290fc1b451SAneesh Kumar K.V * i_blocks can be represented in a 48 bit variable 41300fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 41310fc1b451SAneesh Kumar K.V */ 41328180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 41330fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 413484a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 41350fc1b451SAneesh Kumar K.V } else { 413684a8dce2SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE); 41378180a562SAneesh Kumar K.V /* i_block is stored in file system block size */ 41388180a562SAneesh Kumar K.V i_blocks = i_blocks >> (inode->i_blkbits - 9); 41398180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 41408180a562SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 41410fc1b451SAneesh Kumar K.V } 4142f287a1a5STheodore Ts'o return 0; 41430fc1b451SAneesh Kumar K.V } 41440fc1b451SAneesh Kumar K.V 4145ac27a0ecSDave Kleikamp /* 4146ac27a0ecSDave Kleikamp * Post the struct inode info into an on-disk inode location in the 4147ac27a0ecSDave Kleikamp * buffer-cache. This gobbles the caller's reference to the 4148ac27a0ecSDave Kleikamp * buffer_head in the inode location struct. 4149ac27a0ecSDave Kleikamp * 4150ac27a0ecSDave Kleikamp * The caller must have write access to iloc->bh. 4151ac27a0ecSDave Kleikamp */ 4152617ba13bSMingming Cao static int ext4_do_update_inode(handle_t *handle, 4153ac27a0ecSDave Kleikamp struct inode *inode, 4154830156c7SFrank Mayhar struct ext4_iloc *iloc) 4155ac27a0ecSDave Kleikamp { 4156617ba13bSMingming Cao struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 4157617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 4158ac27a0ecSDave Kleikamp struct buffer_head *bh = iloc->bh; 4159ac27a0ecSDave Kleikamp int err = 0, rc, block; 4160b71fc079SJan Kara int need_datasync = 0; 416108cefc7aSEric W. Biederman uid_t i_uid; 416208cefc7aSEric W. Biederman gid_t i_gid; 4163ac27a0ecSDave Kleikamp 4164ac27a0ecSDave Kleikamp /* For fields not not tracking in the in-memory inode, 4165ac27a0ecSDave Kleikamp * initialise them to zero for new inodes. */ 416619f5fb7aSTheodore Ts'o if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) 4167617ba13bSMingming Cao memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 4168ac27a0ecSDave Kleikamp 4169ff9ddf7eSJan Kara ext4_get_inode_flags(ei); 4170ac27a0ecSDave Kleikamp raw_inode->i_mode = cpu_to_le16(inode->i_mode); 417108cefc7aSEric W. Biederman i_uid = i_uid_read(inode); 417208cefc7aSEric W. Biederman i_gid = i_gid_read(inode); 4173ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 417408cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid)); 417508cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid)); 4176ac27a0ecSDave Kleikamp /* 4177ac27a0ecSDave Kleikamp * Fix up interoperability with old kernels. Otherwise, old inodes get 4178ac27a0ecSDave Kleikamp * re-used with the upper 16 bits of the uid/gid intact 4179ac27a0ecSDave Kleikamp */ 4180ac27a0ecSDave Kleikamp if (!ei->i_dtime) { 4181ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 418208cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_uid)); 4183ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 418408cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_gid)); 4185ac27a0ecSDave Kleikamp } else { 4186ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4187ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4188ac27a0ecSDave Kleikamp } 4189ac27a0ecSDave Kleikamp } else { 419008cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid)); 419108cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid)); 4192ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4193ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4194ac27a0ecSDave Kleikamp } 4195ac27a0ecSDave Kleikamp raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 4196ef7f3835SKalpak Shah 4197ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 4198ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 4199ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 4200ef7f3835SKalpak Shah EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 4201ef7f3835SKalpak Shah 42020fc1b451SAneesh Kumar K.V if (ext4_inode_blocks_set(handle, raw_inode, ei)) 42030fc1b451SAneesh Kumar K.V goto out_brelse; 4204ac27a0ecSDave Kleikamp raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 4205353eb83cSTheodore Ts'o raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF); 42069b8f1f01SMingming Cao if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 42079b8f1f01SMingming Cao cpu_to_le32(EXT4_OS_HURD)) 4208a1ddeb7eSBadari Pulavarty raw_inode->i_file_acl_high = 4209a1ddeb7eSBadari Pulavarty cpu_to_le16(ei->i_file_acl >> 32); 42107973c0c1SAneesh Kumar K.V raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 4211b71fc079SJan Kara if (ei->i_disksize != ext4_isize(raw_inode)) { 4212a48380f7SAneesh Kumar K.V ext4_isize_set(raw_inode, ei->i_disksize); 4213b71fc079SJan Kara need_datasync = 1; 4214b71fc079SJan Kara } 4215ac27a0ecSDave Kleikamp if (ei->i_disksize > 0x7fffffffULL) { 4216ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 4217617ba13bSMingming Cao if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, 4218617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE) || 4219617ba13bSMingming Cao EXT4_SB(sb)->s_es->s_rev_level == 4220617ba13bSMingming Cao cpu_to_le32(EXT4_GOOD_OLD_REV)) { 4221ac27a0ecSDave Kleikamp /* If this is the first large file 4222ac27a0ecSDave Kleikamp * created, add a flag to the superblock. 4223ac27a0ecSDave Kleikamp */ 4224617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, 4225617ba13bSMingming Cao EXT4_SB(sb)->s_sbh); 4226ac27a0ecSDave Kleikamp if (err) 4227ac27a0ecSDave Kleikamp goto out_brelse; 4228617ba13bSMingming Cao ext4_update_dynamic_rev(sb); 4229617ba13bSMingming Cao EXT4_SET_RO_COMPAT_FEATURE(sb, 4230617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE); 42310390131bSFrank Mayhar ext4_handle_sync(handle); 4232b50924c2SArtem Bityutskiy err = ext4_handle_dirty_super(handle, sb); 4233ac27a0ecSDave Kleikamp } 4234ac27a0ecSDave Kleikamp } 4235ac27a0ecSDave Kleikamp raw_inode->i_generation = cpu_to_le32(inode->i_generation); 4236ac27a0ecSDave Kleikamp if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 4237ac27a0ecSDave Kleikamp if (old_valid_dev(inode->i_rdev)) { 4238ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 4239ac27a0ecSDave Kleikamp cpu_to_le32(old_encode_dev(inode->i_rdev)); 4240ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 0; 4241ac27a0ecSDave Kleikamp } else { 4242ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 0; 4243ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 4244ac27a0ecSDave Kleikamp cpu_to_le32(new_encode_dev(inode->i_rdev)); 4245ac27a0ecSDave Kleikamp raw_inode->i_block[2] = 0; 4246ac27a0ecSDave Kleikamp } 4247f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 4248de9a55b8STheodore Ts'o for (block = 0; block < EXT4_N_BLOCKS; block++) 4249ac27a0ecSDave Kleikamp raw_inode->i_block[block] = ei->i_data[block]; 4250f19d5870STao Ma } 4251ac27a0ecSDave Kleikamp 425225ec56b5SJean Noel Cordenner raw_inode->i_disk_version = cpu_to_le32(inode->i_version); 425325ec56b5SJean Noel Cordenner if (ei->i_extra_isize) { 425425ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 425525ec56b5SJean Noel Cordenner raw_inode->i_version_hi = 425625ec56b5SJean Noel Cordenner cpu_to_le32(inode->i_version >> 32); 4257ac27a0ecSDave Kleikamp raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize); 425825ec56b5SJean Noel Cordenner } 425925ec56b5SJean Noel Cordenner 4260814525f4SDarrick J. Wong ext4_inode_csum_set(inode, raw_inode, ei); 4261814525f4SDarrick J. Wong 42620390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 426373b50c1cSCurt Wohlgemuth rc = ext4_handle_dirty_metadata(handle, NULL, bh); 4264ac27a0ecSDave Kleikamp if (!err) 4265ac27a0ecSDave Kleikamp err = rc; 426619f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_NEW); 4267ac27a0ecSDave Kleikamp 4268b71fc079SJan Kara ext4_update_inode_fsync_trans(handle, inode, need_datasync); 4269ac27a0ecSDave Kleikamp out_brelse: 4270ac27a0ecSDave Kleikamp brelse(bh); 4271617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4272ac27a0ecSDave Kleikamp return err; 4273ac27a0ecSDave Kleikamp } 4274ac27a0ecSDave Kleikamp 4275ac27a0ecSDave Kleikamp /* 4276617ba13bSMingming Cao * ext4_write_inode() 4277ac27a0ecSDave Kleikamp * 4278ac27a0ecSDave Kleikamp * We are called from a few places: 4279ac27a0ecSDave Kleikamp * 4280ac27a0ecSDave Kleikamp * - Within generic_file_write() for O_SYNC files. 4281ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 42824907cb7bSAnatol Pomozov * transaction to commit. 4283ac27a0ecSDave Kleikamp * 4284ac27a0ecSDave Kleikamp * - Within sys_sync(), kupdate and such. 4285ac27a0ecSDave Kleikamp * We wait on commit, if tol to. 4286ac27a0ecSDave Kleikamp * 4287ac27a0ecSDave Kleikamp * - Within prune_icache() (PF_MEMALLOC == true) 4288ac27a0ecSDave Kleikamp * Here we simply return. We can't afford to block kswapd on the 4289ac27a0ecSDave Kleikamp * journal commit. 4290ac27a0ecSDave Kleikamp * 4291ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 4292ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 4293617ba13bSMingming Cao * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for 4294ac27a0ecSDave Kleikamp * knfsd. 4295ac27a0ecSDave Kleikamp * 4296ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 4297ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 4298ac27a0ecSDave Kleikamp * which we are interested. 4299ac27a0ecSDave Kleikamp * 4300ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 4301ac27a0ecSDave Kleikamp * 4302ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 4303ac27a0ecSDave Kleikamp * stuff(); 4304ac27a0ecSDave Kleikamp * inode->i_size = expr; 4305ac27a0ecSDave Kleikamp * 4306ac27a0ecSDave Kleikamp * is in error because a kswapd-driven write_inode() could occur while 4307ac27a0ecSDave Kleikamp * `stuff()' is running, and the new i_size will be lost. Plus the inode 4308ac27a0ecSDave Kleikamp * will no longer be on the superblock's dirty inode list. 4309ac27a0ecSDave Kleikamp */ 4310a9185b41SChristoph Hellwig int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) 4311ac27a0ecSDave Kleikamp { 431291ac6f43SFrank Mayhar int err; 431391ac6f43SFrank Mayhar 4314ac27a0ecSDave Kleikamp if (current->flags & PF_MEMALLOC) 4315ac27a0ecSDave Kleikamp return 0; 4316ac27a0ecSDave Kleikamp 431791ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal) { 4318617ba13bSMingming Cao if (ext4_journal_current_handle()) { 4319b38bd33aSMingming Cao jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); 4320ac27a0ecSDave Kleikamp dump_stack(); 4321ac27a0ecSDave Kleikamp return -EIO; 4322ac27a0ecSDave Kleikamp } 4323ac27a0ecSDave Kleikamp 4324a9185b41SChristoph Hellwig if (wbc->sync_mode != WB_SYNC_ALL) 4325ac27a0ecSDave Kleikamp return 0; 4326ac27a0ecSDave Kleikamp 432791ac6f43SFrank Mayhar err = ext4_force_commit(inode->i_sb); 432891ac6f43SFrank Mayhar } else { 432991ac6f43SFrank Mayhar struct ext4_iloc iloc; 433091ac6f43SFrank Mayhar 43318b472d73SCurt Wohlgemuth err = __ext4_get_inode_loc(inode, &iloc, 0); 433291ac6f43SFrank Mayhar if (err) 433391ac6f43SFrank Mayhar return err; 4334a9185b41SChristoph Hellwig if (wbc->sync_mode == WB_SYNC_ALL) 4335830156c7SFrank Mayhar sync_dirty_buffer(iloc.bh); 4336830156c7SFrank Mayhar if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { 4337c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr, 4338c398eda0STheodore Ts'o "IO error syncing inode"); 4339830156c7SFrank Mayhar err = -EIO; 4340830156c7SFrank Mayhar } 4341fd2dd9fbSCurt Wohlgemuth brelse(iloc.bh); 434291ac6f43SFrank Mayhar } 434391ac6f43SFrank Mayhar return err; 4344ac27a0ecSDave Kleikamp } 4345ac27a0ecSDave Kleikamp 4346ac27a0ecSDave Kleikamp /* 434753e87268SJan Kara * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate 434853e87268SJan Kara * buffers that are attached to a page stradding i_size and are undergoing 434953e87268SJan Kara * commit. In that case we have to wait for commit to finish and try again. 435053e87268SJan Kara */ 435153e87268SJan Kara static void ext4_wait_for_tail_page_commit(struct inode *inode) 435253e87268SJan Kara { 435353e87268SJan Kara struct page *page; 435453e87268SJan Kara unsigned offset; 435553e87268SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 435653e87268SJan Kara tid_t commit_tid = 0; 435753e87268SJan Kara int ret; 435853e87268SJan Kara 435953e87268SJan Kara offset = inode->i_size & (PAGE_CACHE_SIZE - 1); 436053e87268SJan Kara /* 436153e87268SJan Kara * All buffers in the last page remain valid? Then there's nothing to 436253e87268SJan Kara * do. We do the check mainly to optimize the common PAGE_CACHE_SIZE == 436353e87268SJan Kara * blocksize case 436453e87268SJan Kara */ 436553e87268SJan Kara if (offset > PAGE_CACHE_SIZE - (1 << inode->i_blkbits)) 436653e87268SJan Kara return; 436753e87268SJan Kara while (1) { 436853e87268SJan Kara page = find_lock_page(inode->i_mapping, 436953e87268SJan Kara inode->i_size >> PAGE_CACHE_SHIFT); 437053e87268SJan Kara if (!page) 437153e87268SJan Kara return; 437253e87268SJan Kara ret = __ext4_journalled_invalidatepage(page, offset); 437353e87268SJan Kara unlock_page(page); 437453e87268SJan Kara page_cache_release(page); 437553e87268SJan Kara if (ret != -EBUSY) 437653e87268SJan Kara return; 437753e87268SJan Kara commit_tid = 0; 437853e87268SJan Kara read_lock(&journal->j_state_lock); 437953e87268SJan Kara if (journal->j_committing_transaction) 438053e87268SJan Kara commit_tid = journal->j_committing_transaction->t_tid; 438153e87268SJan Kara read_unlock(&journal->j_state_lock); 438253e87268SJan Kara if (commit_tid) 438353e87268SJan Kara jbd2_log_wait_commit(journal, commit_tid); 438453e87268SJan Kara } 438553e87268SJan Kara } 438653e87268SJan Kara 438753e87268SJan Kara /* 4388617ba13bSMingming Cao * ext4_setattr() 4389ac27a0ecSDave Kleikamp * 4390ac27a0ecSDave Kleikamp * Called from notify_change. 4391ac27a0ecSDave Kleikamp * 4392ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 4393ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 4394ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 4395ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 4396ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 4397ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 4398ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 4399ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 4400ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 4401ac27a0ecSDave Kleikamp * 4402678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 4403678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 4404678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 4405678aaf48SJan Kara * This way we are sure that all the data written in the previous 4406678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 4407678aaf48SJan Kara * writeback). 4408678aaf48SJan Kara * 4409678aaf48SJan Kara * Called with inode->i_mutex down. 4410ac27a0ecSDave Kleikamp */ 4411617ba13bSMingming Cao int ext4_setattr(struct dentry *dentry, struct iattr *attr) 4412ac27a0ecSDave Kleikamp { 4413ac27a0ecSDave Kleikamp struct inode *inode = dentry->d_inode; 4414ac27a0ecSDave Kleikamp int error, rc = 0; 44153d287de3SDmitry Monakhov int orphan = 0; 4416ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 4417ac27a0ecSDave Kleikamp 4418ac27a0ecSDave Kleikamp error = inode_change_ok(inode, attr); 4419ac27a0ecSDave Kleikamp if (error) 4420ac27a0ecSDave Kleikamp return error; 4421ac27a0ecSDave Kleikamp 442212755627SDmitry Monakhov if (is_quota_modification(inode, attr)) 4423871a2931SChristoph Hellwig dquot_initialize(inode); 442408cefc7aSEric W. Biederman if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) || 442508cefc7aSEric W. Biederman (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) { 4426ac27a0ecSDave Kleikamp handle_t *handle; 4427ac27a0ecSDave Kleikamp 4428ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 4429ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 44309924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 44319924a92aSTheodore Ts'o (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) + 4432194074acSDmitry Monakhov EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3); 4433ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 4434ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 4435ac27a0ecSDave Kleikamp goto err_out; 4436ac27a0ecSDave Kleikamp } 4437b43fa828SChristoph Hellwig error = dquot_transfer(inode, attr); 4438ac27a0ecSDave Kleikamp if (error) { 4439617ba13bSMingming Cao ext4_journal_stop(handle); 4440ac27a0ecSDave Kleikamp return error; 4441ac27a0ecSDave Kleikamp } 4442ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 4443ac27a0ecSDave Kleikamp * one transaction */ 4444ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_UID) 4445ac27a0ecSDave Kleikamp inode->i_uid = attr->ia_uid; 4446ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_GID) 4447ac27a0ecSDave Kleikamp inode->i_gid = attr->ia_gid; 4448617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 4449617ba13bSMingming Cao ext4_journal_stop(handle); 4450ac27a0ecSDave Kleikamp } 4451ac27a0ecSDave Kleikamp 4452e2b46574SEric Sandeen if (attr->ia_valid & ATTR_SIZE) { 4453562c72aaSChristoph Hellwig 445412e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 4455e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4456e2b46574SEric Sandeen 44570c095c7fSTheodore Ts'o if (attr->ia_size > sbi->s_bitmap_maxbytes) 44580c095c7fSTheodore Ts'o return -EFBIG; 4459e2b46574SEric Sandeen } 4460e2b46574SEric Sandeen } 4461e2b46574SEric Sandeen 4462ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode) && 4463c8d46e41SJiaying Zhang attr->ia_valid & ATTR_SIZE && 4464072bd7eaSTheodore Ts'o (attr->ia_size < inode->i_size)) { 4465ac27a0ecSDave Kleikamp handle_t *handle; 4466ac27a0ecSDave Kleikamp 44679924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 3); 4468ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 4469ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 4470ac27a0ecSDave Kleikamp goto err_out; 4471ac27a0ecSDave Kleikamp } 44723d287de3SDmitry Monakhov if (ext4_handle_valid(handle)) { 4473617ba13bSMingming Cao error = ext4_orphan_add(handle, inode); 44743d287de3SDmitry Monakhov orphan = 1; 44753d287de3SDmitry Monakhov } 4476617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 4477617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 4478ac27a0ecSDave Kleikamp if (!error) 4479ac27a0ecSDave Kleikamp error = rc; 4480617ba13bSMingming Cao ext4_journal_stop(handle); 4481678aaf48SJan Kara 4482678aaf48SJan Kara if (ext4_should_order_data(inode)) { 4483678aaf48SJan Kara error = ext4_begin_ordered_truncate(inode, 4484678aaf48SJan Kara attr->ia_size); 4485678aaf48SJan Kara if (error) { 4486678aaf48SJan Kara /* Do as much error cleanup as possible */ 44879924a92aSTheodore Ts'o handle = ext4_journal_start(inode, 44889924a92aSTheodore Ts'o EXT4_HT_INODE, 3); 4489678aaf48SJan Kara if (IS_ERR(handle)) { 4490678aaf48SJan Kara ext4_orphan_del(NULL, inode); 4491678aaf48SJan Kara goto err_out; 4492678aaf48SJan Kara } 4493678aaf48SJan Kara ext4_orphan_del(handle, inode); 44943d287de3SDmitry Monakhov orphan = 0; 4495678aaf48SJan Kara ext4_journal_stop(handle); 4496678aaf48SJan Kara goto err_out; 4497678aaf48SJan Kara } 4498678aaf48SJan Kara } 4499ac27a0ecSDave Kleikamp } 4500ac27a0ecSDave Kleikamp 4501072bd7eaSTheodore Ts'o if (attr->ia_valid & ATTR_SIZE) { 450253e87268SJan Kara if (attr->ia_size != inode->i_size) { 450353e87268SJan Kara loff_t oldsize = inode->i_size; 450453e87268SJan Kara 450553e87268SJan Kara i_size_write(inode, attr->ia_size); 450653e87268SJan Kara /* 450753e87268SJan Kara * Blocks are going to be removed from the inode. Wait 450853e87268SJan Kara * for dio in flight. Temporarily disable 450953e87268SJan Kara * dioread_nolock to prevent livelock. 451053e87268SJan Kara */ 45111b65007eSDmitry Monakhov if (orphan) { 451253e87268SJan Kara if (!ext4_should_journal_data(inode)) { 45131b65007eSDmitry Monakhov ext4_inode_block_unlocked_dio(inode); 45141c9114f9SDmitry Monakhov inode_dio_wait(inode); 45151b65007eSDmitry Monakhov ext4_inode_resume_unlocked_dio(inode); 451653e87268SJan Kara } else 451753e87268SJan Kara ext4_wait_for_tail_page_commit(inode); 45181b65007eSDmitry Monakhov } 451953e87268SJan Kara /* 452053e87268SJan Kara * Truncate pagecache after we've waited for commit 452153e87268SJan Kara * in data=journal mode to make pages freeable. 452253e87268SJan Kara */ 452353e87268SJan Kara truncate_pagecache(inode, oldsize, inode->i_size); 45241c9114f9SDmitry Monakhov } 4525072bd7eaSTheodore Ts'o ext4_truncate(inode); 4526072bd7eaSTheodore Ts'o } 4527ac27a0ecSDave Kleikamp 45281025774cSChristoph Hellwig if (!rc) { 45291025774cSChristoph Hellwig setattr_copy(inode, attr); 45301025774cSChristoph Hellwig mark_inode_dirty(inode); 45311025774cSChristoph Hellwig } 45321025774cSChristoph Hellwig 45331025774cSChristoph Hellwig /* 45341025774cSChristoph Hellwig * If the call to ext4_truncate failed to get a transaction handle at 45351025774cSChristoph Hellwig * all, we need to clean up the in-core orphan list manually. 45361025774cSChristoph Hellwig */ 45373d287de3SDmitry Monakhov if (orphan && inode->i_nlink) 4538617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 4539ac27a0ecSDave Kleikamp 4540ac27a0ecSDave Kleikamp if (!rc && (ia_valid & ATTR_MODE)) 4541617ba13bSMingming Cao rc = ext4_acl_chmod(inode); 4542ac27a0ecSDave Kleikamp 4543ac27a0ecSDave Kleikamp err_out: 4544617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 4545ac27a0ecSDave Kleikamp if (!error) 4546ac27a0ecSDave Kleikamp error = rc; 4547ac27a0ecSDave Kleikamp return error; 4548ac27a0ecSDave Kleikamp } 4549ac27a0ecSDave Kleikamp 45503e3398a0SMingming Cao int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry, 45513e3398a0SMingming Cao struct kstat *stat) 45523e3398a0SMingming Cao { 45533e3398a0SMingming Cao struct inode *inode; 45543e3398a0SMingming Cao unsigned long delalloc_blocks; 45553e3398a0SMingming Cao 45563e3398a0SMingming Cao inode = dentry->d_inode; 45573e3398a0SMingming Cao generic_fillattr(inode, stat); 45583e3398a0SMingming Cao 45593e3398a0SMingming Cao /* 45603e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 45613e3398a0SMingming Cao * otherwise in the case of system crash before the real block 45623e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 45633e3398a0SMingming Cao * on-disk file blocks. 45643e3398a0SMingming Cao * We always keep i_blocks updated together with real 45653e3398a0SMingming Cao * allocation. But to not confuse with user, stat 45663e3398a0SMingming Cao * will return the blocks that include the delayed allocation 45673e3398a0SMingming Cao * blocks for this file. 45683e3398a0SMingming Cao */ 456996607551STao Ma delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb), 457096607551STao Ma EXT4_I(inode)->i_reserved_data_blocks); 45713e3398a0SMingming Cao 45723e3398a0SMingming Cao stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9; 45733e3398a0SMingming Cao return 0; 45743e3398a0SMingming Cao } 4575ac27a0ecSDave Kleikamp 4576a02908f1SMingming Cao static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4577a02908f1SMingming Cao { 457812e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 45798bb2b247SAmir Goldstein return ext4_ind_trans_blocks(inode, nrblocks, chunk); 4580ac51d837STheodore Ts'o return ext4_ext_index_trans_blocks(inode, nrblocks, chunk); 4581a02908f1SMingming Cao } 4582ac51d837STheodore Ts'o 4583a02908f1SMingming Cao /* 4584a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 4585a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 4586a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 4587a02908f1SMingming Cao * 4588a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 45894907cb7bSAnatol Pomozov * different block groups too. If they are contiguous, with flexbg, 4590a02908f1SMingming Cao * they could still across block group boundary. 4591a02908f1SMingming Cao * 4592a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 4593a02908f1SMingming Cao */ 45941f109d5aSTheodore Ts'o static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4595a02908f1SMingming Cao { 45968df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 45978df9675fSTheodore Ts'o int gdpblocks; 4598a02908f1SMingming Cao int idxblocks; 4599a02908f1SMingming Cao int ret = 0; 4600a02908f1SMingming Cao 4601a02908f1SMingming Cao /* 4602a02908f1SMingming Cao * How many index blocks need to touch to modify nrblocks? 4603a02908f1SMingming Cao * The "Chunk" flag indicating whether the nrblocks is 4604a02908f1SMingming Cao * physically contiguous on disk 4605a02908f1SMingming Cao * 4606a02908f1SMingming Cao * For Direct IO and fallocate, they calls get_block to allocate 4607a02908f1SMingming Cao * one single extent at a time, so they could set the "Chunk" flag 4608a02908f1SMingming Cao */ 4609a02908f1SMingming Cao idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk); 4610a02908f1SMingming Cao 4611a02908f1SMingming Cao ret = idxblocks; 4612a02908f1SMingming Cao 4613a02908f1SMingming Cao /* 4614a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 4615a02908f1SMingming Cao * to account 4616a02908f1SMingming Cao */ 4617a02908f1SMingming Cao groups = idxblocks; 4618a02908f1SMingming Cao if (chunk) 4619a02908f1SMingming Cao groups += 1; 4620ac27a0ecSDave Kleikamp else 4621a02908f1SMingming Cao groups += nrblocks; 4622ac27a0ecSDave Kleikamp 4623a02908f1SMingming Cao gdpblocks = groups; 46248df9675fSTheodore Ts'o if (groups > ngroups) 46258df9675fSTheodore Ts'o groups = ngroups; 4626a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 4627a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 4628a02908f1SMingming Cao 4629a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 4630a02908f1SMingming Cao ret += groups + gdpblocks; 4631a02908f1SMingming Cao 4632a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 4633a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 4634ac27a0ecSDave Kleikamp 4635ac27a0ecSDave Kleikamp return ret; 4636ac27a0ecSDave Kleikamp } 4637ac27a0ecSDave Kleikamp 4638ac27a0ecSDave Kleikamp /* 463925985edcSLucas De Marchi * Calculate the total number of credits to reserve to fit 4640f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 4641f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 4642a02908f1SMingming Cao * 4643525f4ed8SMingming Cao * This could be called via ext4_write_begin() 4644a02908f1SMingming Cao * 4645525f4ed8SMingming Cao * We need to consider the worse case, when 4646a02908f1SMingming Cao * one new block per extent. 4647a02908f1SMingming Cao */ 4648a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 4649a02908f1SMingming Cao { 4650a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 4651a02908f1SMingming Cao int ret; 4652a02908f1SMingming Cao 4653a02908f1SMingming Cao ret = ext4_meta_trans_blocks(inode, bpp, 0); 4654a02908f1SMingming Cao 4655a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 4656a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 4657a02908f1SMingming Cao ret += bpp; 4658a02908f1SMingming Cao return ret; 4659a02908f1SMingming Cao } 4660f3bd1f3fSMingming Cao 4661f3bd1f3fSMingming Cao /* 4662f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 4663f3bd1f3fSMingming Cao * 4664f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 466579e83036SEric Sandeen * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks. 4666f3bd1f3fSMingming Cao * 4667f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 4668f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 4669f3bd1f3fSMingming Cao */ 4670f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 4671f3bd1f3fSMingming Cao { 4672f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 4673f3bd1f3fSMingming Cao } 4674f3bd1f3fSMingming Cao 4675a02908f1SMingming Cao /* 4676617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 4677ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 4678ac27a0ecSDave Kleikamp */ 4679617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 4680617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 4681ac27a0ecSDave Kleikamp { 4682ac27a0ecSDave Kleikamp int err = 0; 4683ac27a0ecSDave Kleikamp 4684c64db50eSTheodore Ts'o if (IS_I_VERSION(inode)) 468525ec56b5SJean Noel Cordenner inode_inc_iversion(inode); 468625ec56b5SJean Noel Cordenner 4687ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 4688ac27a0ecSDave Kleikamp get_bh(iloc->bh); 4689ac27a0ecSDave Kleikamp 4690dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 4691830156c7SFrank Mayhar err = ext4_do_update_inode(handle, inode, iloc); 4692ac27a0ecSDave Kleikamp put_bh(iloc->bh); 4693ac27a0ecSDave Kleikamp return err; 4694ac27a0ecSDave Kleikamp } 4695ac27a0ecSDave Kleikamp 4696ac27a0ecSDave Kleikamp /* 4697ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 4698ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 4699ac27a0ecSDave Kleikamp */ 4700ac27a0ecSDave Kleikamp 4701ac27a0ecSDave Kleikamp int 4702617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 4703617ba13bSMingming Cao struct ext4_iloc *iloc) 4704ac27a0ecSDave Kleikamp { 47050390131bSFrank Mayhar int err; 47060390131bSFrank Mayhar 4707617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 4708ac27a0ecSDave Kleikamp if (!err) { 4709ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 4710617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, iloc->bh); 4711ac27a0ecSDave Kleikamp if (err) { 4712ac27a0ecSDave Kleikamp brelse(iloc->bh); 4713ac27a0ecSDave Kleikamp iloc->bh = NULL; 4714ac27a0ecSDave Kleikamp } 4715ac27a0ecSDave Kleikamp } 4716617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4717ac27a0ecSDave Kleikamp return err; 4718ac27a0ecSDave Kleikamp } 4719ac27a0ecSDave Kleikamp 4720ac27a0ecSDave Kleikamp /* 47216dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 47226dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 47236dd4ee7cSKalpak Shah */ 47241d03ec98SAneesh Kumar K.V static int ext4_expand_extra_isize(struct inode *inode, 47251d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 47261d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 47271d03ec98SAneesh Kumar K.V handle_t *handle) 47286dd4ee7cSKalpak Shah { 47296dd4ee7cSKalpak Shah struct ext4_inode *raw_inode; 47306dd4ee7cSKalpak Shah struct ext4_xattr_ibody_header *header; 47316dd4ee7cSKalpak Shah 47326dd4ee7cSKalpak Shah if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) 47336dd4ee7cSKalpak Shah return 0; 47346dd4ee7cSKalpak Shah 47356dd4ee7cSKalpak Shah raw_inode = ext4_raw_inode(&iloc); 47366dd4ee7cSKalpak Shah 47376dd4ee7cSKalpak Shah header = IHDR(inode, raw_inode); 47386dd4ee7cSKalpak Shah 47396dd4ee7cSKalpak Shah /* No extended attributes present */ 474019f5fb7aSTheodore Ts'o if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) || 47416dd4ee7cSKalpak Shah header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 47426dd4ee7cSKalpak Shah memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0, 47436dd4ee7cSKalpak Shah new_extra_isize); 47446dd4ee7cSKalpak Shah EXT4_I(inode)->i_extra_isize = new_extra_isize; 47456dd4ee7cSKalpak Shah return 0; 47466dd4ee7cSKalpak Shah } 47476dd4ee7cSKalpak Shah 47486dd4ee7cSKalpak Shah /* try to expand with EAs present */ 47496dd4ee7cSKalpak Shah return ext4_expand_extra_isize_ea(inode, new_extra_isize, 47506dd4ee7cSKalpak Shah raw_inode, handle); 47516dd4ee7cSKalpak Shah } 47526dd4ee7cSKalpak Shah 47536dd4ee7cSKalpak Shah /* 4754ac27a0ecSDave Kleikamp * What we do here is to mark the in-core inode as clean with respect to inode 4755ac27a0ecSDave Kleikamp * dirtiness (it may still be data-dirty). 4756ac27a0ecSDave Kleikamp * This means that the in-core inode may be reaped by prune_icache 4757ac27a0ecSDave Kleikamp * without having to perform any I/O. This is a very good thing, 4758ac27a0ecSDave Kleikamp * because *any* task may call prune_icache - even ones which 4759ac27a0ecSDave Kleikamp * have a transaction open against a different journal. 4760ac27a0ecSDave Kleikamp * 4761ac27a0ecSDave Kleikamp * Is this cheating? Not really. Sure, we haven't written the 4762ac27a0ecSDave Kleikamp * inode out, but prune_icache isn't a user-visible syncing function. 4763ac27a0ecSDave Kleikamp * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 4764ac27a0ecSDave Kleikamp * we start and wait on commits. 4765ac27a0ecSDave Kleikamp */ 4766617ba13bSMingming Cao int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode) 4767ac27a0ecSDave Kleikamp { 4768617ba13bSMingming Cao struct ext4_iloc iloc; 47696dd4ee7cSKalpak Shah struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 47706dd4ee7cSKalpak Shah static unsigned int mnt_count; 47716dd4ee7cSKalpak Shah int err, ret; 4772ac27a0ecSDave Kleikamp 4773ac27a0ecSDave Kleikamp might_sleep(); 47747ff9c073STheodore Ts'o trace_ext4_mark_inode_dirty(inode, _RET_IP_); 4775617ba13bSMingming Cao err = ext4_reserve_inode_write(handle, inode, &iloc); 47760390131bSFrank Mayhar if (ext4_handle_valid(handle) && 47770390131bSFrank Mayhar EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize && 477819f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) { 47796dd4ee7cSKalpak Shah /* 47806dd4ee7cSKalpak Shah * We need extra buffer credits since we may write into EA block 47816dd4ee7cSKalpak Shah * with this same handle. If journal_extend fails, then it will 47826dd4ee7cSKalpak Shah * only result in a minor loss of functionality for that inode. 47836dd4ee7cSKalpak Shah * If this is felt to be critical, then e2fsck should be run to 47846dd4ee7cSKalpak Shah * force a large enough s_min_extra_isize. 47856dd4ee7cSKalpak Shah */ 47866dd4ee7cSKalpak Shah if ((jbd2_journal_extend(handle, 47876dd4ee7cSKalpak Shah EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) { 47886dd4ee7cSKalpak Shah ret = ext4_expand_extra_isize(inode, 47896dd4ee7cSKalpak Shah sbi->s_want_extra_isize, 47906dd4ee7cSKalpak Shah iloc, handle); 47916dd4ee7cSKalpak Shah if (ret) { 479219f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, 479319f5fb7aSTheodore Ts'o EXT4_STATE_NO_EXPAND); 4794c1bddad9SAneesh Kumar K.V if (mnt_count != 4795c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count)) { 479612062dddSEric Sandeen ext4_warning(inode->i_sb, 47976dd4ee7cSKalpak Shah "Unable to expand inode %lu. Delete" 47986dd4ee7cSKalpak Shah " some EAs or run e2fsck.", 47996dd4ee7cSKalpak Shah inode->i_ino); 4800c1bddad9SAneesh Kumar K.V mnt_count = 4801c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count); 48026dd4ee7cSKalpak Shah } 48036dd4ee7cSKalpak Shah } 48046dd4ee7cSKalpak Shah } 48056dd4ee7cSKalpak Shah } 4806ac27a0ecSDave Kleikamp if (!err) 4807617ba13bSMingming Cao err = ext4_mark_iloc_dirty(handle, inode, &iloc); 4808ac27a0ecSDave Kleikamp return err; 4809ac27a0ecSDave Kleikamp } 4810ac27a0ecSDave Kleikamp 4811ac27a0ecSDave Kleikamp /* 4812617ba13bSMingming Cao * ext4_dirty_inode() is called from __mark_inode_dirty() 4813ac27a0ecSDave Kleikamp * 4814ac27a0ecSDave Kleikamp * We're really interested in the case where a file is being extended. 4815ac27a0ecSDave Kleikamp * i_size has been changed by generic_commit_write() and we thus need 4816ac27a0ecSDave Kleikamp * to include the updated inode in the current transaction. 4817ac27a0ecSDave Kleikamp * 48185dd4056dSChristoph Hellwig * Also, dquot_alloc_block() will always dirty the inode when blocks 4819ac27a0ecSDave Kleikamp * are allocated to the file. 4820ac27a0ecSDave Kleikamp * 4821ac27a0ecSDave Kleikamp * If the inode is marked synchronous, we don't honour that here - doing 4822ac27a0ecSDave Kleikamp * so would cause a commit on atime updates, which we don't bother doing. 4823ac27a0ecSDave Kleikamp * We handle synchronous inodes at the highest possible level. 4824ac27a0ecSDave Kleikamp */ 4825aa385729SChristoph Hellwig void ext4_dirty_inode(struct inode *inode, int flags) 4826ac27a0ecSDave Kleikamp { 4827ac27a0ecSDave Kleikamp handle_t *handle; 4828ac27a0ecSDave Kleikamp 48299924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 2); 4830ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 4831ac27a0ecSDave Kleikamp goto out; 4832f3dc272fSCurt Wohlgemuth 4833617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 4834f3dc272fSCurt Wohlgemuth 4835617ba13bSMingming Cao ext4_journal_stop(handle); 4836ac27a0ecSDave Kleikamp out: 4837ac27a0ecSDave Kleikamp return; 4838ac27a0ecSDave Kleikamp } 4839ac27a0ecSDave Kleikamp 4840ac27a0ecSDave Kleikamp #if 0 4841ac27a0ecSDave Kleikamp /* 4842ac27a0ecSDave Kleikamp * Bind an inode's backing buffer_head into this transaction, to prevent 4843ac27a0ecSDave Kleikamp * it from being flushed to disk early. Unlike 4844617ba13bSMingming Cao * ext4_reserve_inode_write, this leaves behind no bh reference and 4845ac27a0ecSDave Kleikamp * returns no iloc structure, so the caller needs to repeat the iloc 4846ac27a0ecSDave Kleikamp * lookup to mark the inode dirty later. 4847ac27a0ecSDave Kleikamp */ 4848617ba13bSMingming Cao static int ext4_pin_inode(handle_t *handle, struct inode *inode) 4849ac27a0ecSDave Kleikamp { 4850617ba13bSMingming Cao struct ext4_iloc iloc; 4851ac27a0ecSDave Kleikamp 4852ac27a0ecSDave Kleikamp int err = 0; 4853ac27a0ecSDave Kleikamp if (handle) { 4854617ba13bSMingming Cao err = ext4_get_inode_loc(inode, &iloc); 4855ac27a0ecSDave Kleikamp if (!err) { 4856ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc.bh, "get_write_access"); 4857dab291afSMingming Cao err = jbd2_journal_get_write_access(handle, iloc.bh); 4858ac27a0ecSDave Kleikamp if (!err) 48590390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, 486073b50c1cSCurt Wohlgemuth NULL, 4861ac27a0ecSDave Kleikamp iloc.bh); 4862ac27a0ecSDave Kleikamp brelse(iloc.bh); 4863ac27a0ecSDave Kleikamp } 4864ac27a0ecSDave Kleikamp } 4865617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4866ac27a0ecSDave Kleikamp return err; 4867ac27a0ecSDave Kleikamp } 4868ac27a0ecSDave Kleikamp #endif 4869ac27a0ecSDave Kleikamp 4870617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val) 4871ac27a0ecSDave Kleikamp { 4872ac27a0ecSDave Kleikamp journal_t *journal; 4873ac27a0ecSDave Kleikamp handle_t *handle; 4874ac27a0ecSDave Kleikamp int err; 4875ac27a0ecSDave Kleikamp 4876ac27a0ecSDave Kleikamp /* 4877ac27a0ecSDave Kleikamp * We have to be very careful here: changing a data block's 4878ac27a0ecSDave Kleikamp * journaling status dynamically is dangerous. If we write a 4879ac27a0ecSDave Kleikamp * data block to the journal, change the status and then delete 4880ac27a0ecSDave Kleikamp * that block, we risk forgetting to revoke the old log record 4881ac27a0ecSDave Kleikamp * from the journal and so a subsequent replay can corrupt data. 4882ac27a0ecSDave Kleikamp * So, first we make sure that the journal is empty and that 4883ac27a0ecSDave Kleikamp * nobody is changing anything. 4884ac27a0ecSDave Kleikamp */ 4885ac27a0ecSDave Kleikamp 4886617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 48870390131bSFrank Mayhar if (!journal) 48880390131bSFrank Mayhar return 0; 4889d699594dSDave Hansen if (is_journal_aborted(journal)) 4890ac27a0ecSDave Kleikamp return -EROFS; 48912aff57b0SYongqiang Yang /* We have to allocate physical blocks for delalloc blocks 48922aff57b0SYongqiang Yang * before flushing journal. otherwise delalloc blocks can not 48932aff57b0SYongqiang Yang * be allocated any more. even more truncate on delalloc blocks 48942aff57b0SYongqiang Yang * could trigger BUG by flushing delalloc blocks in journal. 48952aff57b0SYongqiang Yang * There is no delalloc block in non-journal data mode. 48962aff57b0SYongqiang Yang */ 48972aff57b0SYongqiang Yang if (val && test_opt(inode->i_sb, DELALLOC)) { 48982aff57b0SYongqiang Yang err = ext4_alloc_da_blocks(inode); 48992aff57b0SYongqiang Yang if (err < 0) 49002aff57b0SYongqiang Yang return err; 49012aff57b0SYongqiang Yang } 4902ac27a0ecSDave Kleikamp 490317335dccSDmitry Monakhov /* Wait for all existing dio workers */ 490417335dccSDmitry Monakhov ext4_inode_block_unlocked_dio(inode); 490517335dccSDmitry Monakhov inode_dio_wait(inode); 490617335dccSDmitry Monakhov 4907dab291afSMingming Cao jbd2_journal_lock_updates(journal); 4908ac27a0ecSDave Kleikamp 4909ac27a0ecSDave Kleikamp /* 4910ac27a0ecSDave Kleikamp * OK, there are no updates running now, and all cached data is 4911ac27a0ecSDave Kleikamp * synced to disk. We are now in a completely consistent state 4912ac27a0ecSDave Kleikamp * which doesn't have anything in the journal, and we know that 4913ac27a0ecSDave Kleikamp * no filesystem updates are running, so it is safe to modify 4914ac27a0ecSDave Kleikamp * the inode's in-core data-journaling state flag now. 4915ac27a0ecSDave Kleikamp */ 4916ac27a0ecSDave Kleikamp 4917ac27a0ecSDave Kleikamp if (val) 491812e9b892SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 49195872ddaaSYongqiang Yang else { 49205872ddaaSYongqiang Yang jbd2_journal_flush(journal); 492112e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 49225872ddaaSYongqiang Yang } 4923617ba13bSMingming Cao ext4_set_aops(inode); 4924ac27a0ecSDave Kleikamp 4925dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 492617335dccSDmitry Monakhov ext4_inode_resume_unlocked_dio(inode); 4927ac27a0ecSDave Kleikamp 4928ac27a0ecSDave Kleikamp /* Finally we can mark the inode as dirty. */ 4929ac27a0ecSDave Kleikamp 49309924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 4931ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 4932ac27a0ecSDave Kleikamp return PTR_ERR(handle); 4933ac27a0ecSDave Kleikamp 4934617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 49350390131bSFrank Mayhar ext4_handle_sync(handle); 4936617ba13bSMingming Cao ext4_journal_stop(handle); 4937617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4938ac27a0ecSDave Kleikamp 4939ac27a0ecSDave Kleikamp return err; 4940ac27a0ecSDave Kleikamp } 49412e9ee850SAneesh Kumar K.V 49422e9ee850SAneesh Kumar K.V static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) 49432e9ee850SAneesh Kumar K.V { 49442e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 49452e9ee850SAneesh Kumar K.V } 49462e9ee850SAneesh Kumar K.V 4947c2ec175cSNick Piggin int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) 49482e9ee850SAneesh Kumar K.V { 4949c2ec175cSNick Piggin struct page *page = vmf->page; 49502e9ee850SAneesh Kumar K.V loff_t size; 49512e9ee850SAneesh Kumar K.V unsigned long len; 49529ea7df53SJan Kara int ret; 49532e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 49542e9ee850SAneesh Kumar K.V struct inode *inode = file->f_path.dentry->d_inode; 49552e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 49569ea7df53SJan Kara handle_t *handle; 49579ea7df53SJan Kara get_block_t *get_block; 49589ea7df53SJan Kara int retries = 0; 49592e9ee850SAneesh Kumar K.V 49608e8ad8a5SJan Kara sb_start_pagefault(inode->i_sb); 4961041bbb6dSTheodore Ts'o file_update_time(vma->vm_file); 49629ea7df53SJan Kara /* Delalloc case is easy... */ 49639ea7df53SJan Kara if (test_opt(inode->i_sb, DELALLOC) && 49649ea7df53SJan Kara !ext4_should_journal_data(inode) && 49659ea7df53SJan Kara !ext4_nonda_switch(inode->i_sb)) { 49669ea7df53SJan Kara do { 49679ea7df53SJan Kara ret = __block_page_mkwrite(vma, vmf, 49689ea7df53SJan Kara ext4_da_get_block_prep); 49699ea7df53SJan Kara } while (ret == -ENOSPC && 49709ea7df53SJan Kara ext4_should_retry_alloc(inode->i_sb, &retries)); 49719ea7df53SJan Kara goto out_ret; 49722e9ee850SAneesh Kumar K.V } 49730e499890SDarrick J. Wong 49740e499890SDarrick J. Wong lock_page(page); 49759ea7df53SJan Kara size = i_size_read(inode); 49769ea7df53SJan Kara /* Page got truncated from under us? */ 49779ea7df53SJan Kara if (page->mapping != mapping || page_offset(page) > size) { 49789ea7df53SJan Kara unlock_page(page); 49799ea7df53SJan Kara ret = VM_FAULT_NOPAGE; 49809ea7df53SJan Kara goto out; 49810e499890SDarrick J. Wong } 49822e9ee850SAneesh Kumar K.V 49832e9ee850SAneesh Kumar K.V if (page->index == size >> PAGE_CACHE_SHIFT) 49842e9ee850SAneesh Kumar K.V len = size & ~PAGE_CACHE_MASK; 49852e9ee850SAneesh Kumar K.V else 49862e9ee850SAneesh Kumar K.V len = PAGE_CACHE_SIZE; 4987a827eaffSAneesh Kumar K.V /* 49889ea7df53SJan Kara * Return if we have all the buffers mapped. This avoids the need to do 49899ea7df53SJan Kara * journal_start/journal_stop which can block and take a long time 4990a827eaffSAneesh Kumar K.V */ 49912e9ee850SAneesh Kumar K.V if (page_has_buffers(page)) { 4992f19d5870STao Ma if (!ext4_walk_page_buffers(NULL, page_buffers(page), 4993f19d5870STao Ma 0, len, NULL, 4994a827eaffSAneesh Kumar K.V ext4_bh_unmapped)) { 49959ea7df53SJan Kara /* Wait so that we don't change page under IO */ 49969ea7df53SJan Kara wait_on_page_writeback(page); 49979ea7df53SJan Kara ret = VM_FAULT_LOCKED; 49989ea7df53SJan Kara goto out; 49992e9ee850SAneesh Kumar K.V } 5000a827eaffSAneesh Kumar K.V } 5001a827eaffSAneesh Kumar K.V unlock_page(page); 50029ea7df53SJan Kara /* OK, we need to fill the hole... */ 50039ea7df53SJan Kara if (ext4_should_dioread_nolock(inode)) 50049ea7df53SJan Kara get_block = ext4_get_block_write; 50059ea7df53SJan Kara else 50069ea7df53SJan Kara get_block = ext4_get_block; 50079ea7df53SJan Kara retry_alloc: 50089924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 50099924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 50109ea7df53SJan Kara if (IS_ERR(handle)) { 5011c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 50129ea7df53SJan Kara goto out; 50139ea7df53SJan Kara } 50149ea7df53SJan Kara ret = __block_page_mkwrite(vma, vmf, get_block); 50159ea7df53SJan Kara if (!ret && ext4_should_journal_data(inode)) { 5016f19d5870STao Ma if (ext4_walk_page_buffers(handle, page_buffers(page), 0, 50179ea7df53SJan Kara PAGE_CACHE_SIZE, NULL, do_journal_get_write_access)) { 50189ea7df53SJan Kara unlock_page(page); 50199ea7df53SJan Kara ret = VM_FAULT_SIGBUS; 5020fcbb5515SYongqiang Yang ext4_journal_stop(handle); 50219ea7df53SJan Kara goto out; 50229ea7df53SJan Kara } 50239ea7df53SJan Kara ext4_set_inode_state(inode, EXT4_STATE_JDATA); 50249ea7df53SJan Kara } 50259ea7df53SJan Kara ext4_journal_stop(handle); 50269ea7df53SJan Kara if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 50279ea7df53SJan Kara goto retry_alloc; 50289ea7df53SJan Kara out_ret: 50299ea7df53SJan Kara ret = block_page_mkwrite_return(ret); 50309ea7df53SJan Kara out: 50318e8ad8a5SJan Kara sb_end_pagefault(inode->i_sb); 50322e9ee850SAneesh Kumar K.V return ret; 50332e9ee850SAneesh Kumar K.V } 5034