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 48555138e0bSTheodore Ts'o /* 486e35fd660STheodore Ts'o * The ext4_map_blocks() function tries to look up the requested blocks, 4872b2d6d01STheodore Ts'o * and returns if the blocks are already mapped. 488f5ab0d1fSMingming Cao * 489f5ab0d1fSMingming Cao * Otherwise it takes the write lock of the i_data_sem and allocate blocks 490f5ab0d1fSMingming Cao * and store the allocated blocks in the result buffer head and mark it 491f5ab0d1fSMingming Cao * mapped. 492f5ab0d1fSMingming Cao * 493e35fd660STheodore Ts'o * If file type is extents based, it will call ext4_ext_map_blocks(), 494e35fd660STheodore Ts'o * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping 495f5ab0d1fSMingming Cao * based files 496f5ab0d1fSMingming Cao * 497f5ab0d1fSMingming Cao * On success, it returns the number of blocks being mapped or allocate. 498f5ab0d1fSMingming Cao * if create==0 and the blocks are pre-allocated and uninitialized block, 499f5ab0d1fSMingming Cao * the result buffer head is unmapped. If the create ==1, it will make sure 500f5ab0d1fSMingming Cao * the buffer head is mapped. 501f5ab0d1fSMingming Cao * 502f5ab0d1fSMingming Cao * It returns 0 if plain look up failed (blocks have not been allocated), in 503df3ab170STao Ma * that case, buffer head is unmapped 504f5ab0d1fSMingming Cao * 505f5ab0d1fSMingming Cao * It returns the error in case of allocation failure. 506f5ab0d1fSMingming Cao */ 507e35fd660STheodore Ts'o int ext4_map_blocks(handle_t *handle, struct inode *inode, 508e35fd660STheodore Ts'o struct ext4_map_blocks *map, int flags) 5090e855ac8SAneesh Kumar K.V { 510*d100eef2SZheng Liu struct extent_status es; 5110e855ac8SAneesh Kumar K.V int retval; 512f5ab0d1fSMingming Cao 513e35fd660STheodore Ts'o map->m_flags = 0; 514e35fd660STheodore Ts'o ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u," 515e35fd660STheodore Ts'o "logical block %lu\n", inode->i_ino, flags, map->m_len, 516e35fd660STheodore Ts'o (unsigned long) map->m_lblk); 517*d100eef2SZheng Liu 518*d100eef2SZheng Liu /* Lookup extent status tree firstly */ 519*d100eef2SZheng Liu if (ext4_es_lookup_extent(inode, map->m_lblk, &es)) { 520*d100eef2SZheng Liu if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) { 521*d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + 522*d100eef2SZheng Liu map->m_lblk - es.es_lblk; 523*d100eef2SZheng Liu map->m_flags |= ext4_es_is_written(&es) ? 524*d100eef2SZheng Liu EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN; 525*d100eef2SZheng Liu retval = es.es_len - (map->m_lblk - es.es_lblk); 526*d100eef2SZheng Liu if (retval > map->m_len) 527*d100eef2SZheng Liu retval = map->m_len; 528*d100eef2SZheng Liu map->m_len = retval; 529*d100eef2SZheng Liu } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) { 530*d100eef2SZheng Liu retval = 0; 531*d100eef2SZheng Liu } else { 532*d100eef2SZheng Liu BUG_ON(1); 533*d100eef2SZheng Liu } 534*d100eef2SZheng Liu goto found; 535*d100eef2SZheng Liu } 536*d100eef2SZheng Liu 5374df3d265SAneesh Kumar K.V /* 538b920c755STheodore Ts'o * Try to see if we can get the block without requesting a new 539b920c755STheodore Ts'o * file system block. 5404df3d265SAneesh Kumar K.V */ 541729f52c6SZheng Liu if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 5420e855ac8SAneesh Kumar K.V down_read((&EXT4_I(inode)->i_data_sem)); 54312e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 544a4e5d88bSDmitry Monakhov retval = ext4_ext_map_blocks(handle, inode, map, flags & 545a4e5d88bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 5464df3d265SAneesh Kumar K.V } else { 547a4e5d88bSDmitry Monakhov retval = ext4_ind_map_blocks(handle, inode, map, flags & 548a4e5d88bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 5490e855ac8SAneesh Kumar K.V } 550f7fec032SZheng Liu if (retval > 0) { 551f7fec032SZheng Liu int ret; 552f7fec032SZheng Liu unsigned long long status; 553f7fec032SZheng Liu 554f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 555f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 556f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 557f7fec032SZheng Liu ext4_find_delalloc_range(inode, map->m_lblk, 558f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 559f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 560f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, 561f7fec032SZheng Liu map->m_len, map->m_pblk, status); 562f7fec032SZheng Liu if (ret < 0) 563f7fec032SZheng Liu retval = ret; 564f7fec032SZheng Liu } 565729f52c6SZheng Liu if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 5664df3d265SAneesh Kumar K.V up_read((&EXT4_I(inode)->i_data_sem)); 567f5ab0d1fSMingming Cao 568*d100eef2SZheng Liu found: 569e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 570f7fec032SZheng Liu int ret = check_block_validity(inode, map); 5716fd058f7STheodore Ts'o if (ret != 0) 5726fd058f7STheodore Ts'o return ret; 5736fd058f7STheodore Ts'o } 5746fd058f7STheodore Ts'o 575f5ab0d1fSMingming Cao /* If it is only a block(s) look up */ 576c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 5774df3d265SAneesh Kumar K.V return retval; 5784df3d265SAneesh Kumar K.V 5794df3d265SAneesh Kumar K.V /* 580f5ab0d1fSMingming Cao * Returns if the blocks have already allocated 581f5ab0d1fSMingming Cao * 582f5ab0d1fSMingming Cao * Note that if blocks have been preallocated 583df3ab170STao Ma * ext4_ext_get_block() returns the create = 0 584f5ab0d1fSMingming Cao * with buffer head unmapped. 585f5ab0d1fSMingming Cao */ 586e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) 587f5ab0d1fSMingming Cao return retval; 588f5ab0d1fSMingming Cao 589f5ab0d1fSMingming Cao /* 590a25a4e1aSZheng Liu * Here we clear m_flags because after allocating an new extent, 591a25a4e1aSZheng Liu * it will be set again. 5922a8964d6SAneesh Kumar K.V */ 593a25a4e1aSZheng Liu map->m_flags &= ~EXT4_MAP_FLAGS; 5942a8964d6SAneesh Kumar K.V 5952a8964d6SAneesh Kumar K.V /* 596f5ab0d1fSMingming Cao * New blocks allocate and/or writing to uninitialized extent 597f5ab0d1fSMingming Cao * will possibly result in updating i_data, so we take 598f5ab0d1fSMingming Cao * the write lock of i_data_sem, and call get_blocks() 599f5ab0d1fSMingming Cao * with create == 1 flag. 6004df3d265SAneesh Kumar K.V */ 6014df3d265SAneesh Kumar K.V down_write((&EXT4_I(inode)->i_data_sem)); 602d2a17637SMingming Cao 603d2a17637SMingming Cao /* 604d2a17637SMingming Cao * if the caller is from delayed allocation writeout path 605d2a17637SMingming Cao * we have already reserved fs blocks for allocation 606d2a17637SMingming Cao * let the underlying get_block() function know to 607d2a17637SMingming Cao * avoid double accounting 608d2a17637SMingming Cao */ 609c2177057STheodore Ts'o if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 610f2321097STheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED); 6114df3d265SAneesh Kumar K.V /* 6124df3d265SAneesh Kumar K.V * We need to check for EXT4 here because migrate 6134df3d265SAneesh Kumar K.V * could have changed the inode type in between 6144df3d265SAneesh Kumar K.V */ 61512e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 616e35fd660STheodore Ts'o retval = ext4_ext_map_blocks(handle, inode, map, flags); 6170e855ac8SAneesh Kumar K.V } else { 618e35fd660STheodore Ts'o retval = ext4_ind_map_blocks(handle, inode, map, flags); 619267e4db9SAneesh Kumar K.V 620e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_NEW) { 621267e4db9SAneesh Kumar K.V /* 622267e4db9SAneesh Kumar K.V * We allocated new blocks which will result in 623267e4db9SAneesh Kumar K.V * i_data's format changing. Force the migrate 624267e4db9SAneesh Kumar K.V * to fail by clearing migrate flags 625267e4db9SAneesh Kumar K.V */ 62619f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE); 627267e4db9SAneesh Kumar K.V } 6282ac3b6e0STheodore Ts'o 629d2a17637SMingming Cao /* 6302ac3b6e0STheodore Ts'o * Update reserved blocks/metadata blocks after successful 6315f634d06SAneesh Kumar K.V * block allocation which had been deferred till now. We don't 6325f634d06SAneesh Kumar K.V * support fallocate for non extent files. So we can update 6335f634d06SAneesh Kumar K.V * reserve space here. 634d2a17637SMingming Cao */ 6355f634d06SAneesh Kumar K.V if ((retval > 0) && 6361296cc85SAneesh Kumar K.V (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)) 6375f634d06SAneesh Kumar K.V ext4_da_update_reserve_space(inode, retval, 1); 6385f634d06SAneesh Kumar K.V } 639f7fec032SZheng Liu if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 640f2321097STheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED); 641d2a17637SMingming Cao 642f7fec032SZheng Liu if (retval > 0) { 64351865fdaSZheng Liu int ret; 644f7fec032SZheng Liu unsigned long long status; 645f7fec032SZheng Liu 646f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 647f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 648f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 649f7fec032SZheng Liu ext4_find_delalloc_range(inode, map->m_lblk, 650f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 651f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 652f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 653f7fec032SZheng Liu map->m_pblk, status); 65451865fdaSZheng Liu if (ret < 0) 65551865fdaSZheng Liu retval = ret; 65651865fdaSZheng Liu } 6575356f261SAditya Kali 6580e855ac8SAneesh Kumar K.V up_write((&EXT4_I(inode)->i_data_sem)); 659e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 660e29136f8STheodore Ts'o int ret = check_block_validity(inode, map); 6616fd058f7STheodore Ts'o if (ret != 0) 6626fd058f7STheodore Ts'o return ret; 6636fd058f7STheodore Ts'o } 6640e855ac8SAneesh Kumar K.V return retval; 6650e855ac8SAneesh Kumar K.V } 6660e855ac8SAneesh Kumar K.V 667f3bd1f3fSMingming Cao /* Maximum number of blocks we map for direct IO at once. */ 668f3bd1f3fSMingming Cao #define DIO_MAX_BLOCKS 4096 669f3bd1f3fSMingming Cao 6702ed88685STheodore Ts'o static int _ext4_get_block(struct inode *inode, sector_t iblock, 6712ed88685STheodore Ts'o struct buffer_head *bh, int flags) 672ac27a0ecSDave Kleikamp { 6733e4fdaf8SDmitriy Monakhov handle_t *handle = ext4_journal_current_handle(); 6742ed88685STheodore Ts'o struct ext4_map_blocks map; 6757fb5409dSJan Kara int ret = 0, started = 0; 676f3bd1f3fSMingming Cao int dio_credits; 677ac27a0ecSDave Kleikamp 67846c7f254STao Ma if (ext4_has_inline_data(inode)) 67946c7f254STao Ma return -ERANGE; 68046c7f254STao Ma 6812ed88685STheodore Ts'o map.m_lblk = iblock; 6822ed88685STheodore Ts'o map.m_len = bh->b_size >> inode->i_blkbits; 6832ed88685STheodore Ts'o 6848b0f165fSAnatol Pomozov if (flags && !(flags & EXT4_GET_BLOCKS_NO_LOCK) && !handle) { 6857fb5409dSJan Kara /* Direct IO write... */ 6862ed88685STheodore Ts'o if (map.m_len > DIO_MAX_BLOCKS) 6872ed88685STheodore Ts'o map.m_len = DIO_MAX_BLOCKS; 6882ed88685STheodore Ts'o dio_credits = ext4_chunk_trans_blocks(inode, map.m_len); 6899924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, 6909924a92aSTheodore Ts'o dio_credits); 6917fb5409dSJan Kara if (IS_ERR(handle)) { 692ac27a0ecSDave Kleikamp ret = PTR_ERR(handle); 6932ed88685STheodore Ts'o return ret; 6947fb5409dSJan Kara } 6957fb5409dSJan Kara started = 1; 696ac27a0ecSDave Kleikamp } 697ac27a0ecSDave Kleikamp 6982ed88685STheodore Ts'o ret = ext4_map_blocks(handle, inode, &map, flags); 699ac27a0ecSDave Kleikamp if (ret > 0) { 7002ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 7012ed88685STheodore Ts'o bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; 7022ed88685STheodore Ts'o bh->b_size = inode->i_sb->s_blocksize * map.m_len; 703ac27a0ecSDave Kleikamp ret = 0; 704ac27a0ecSDave Kleikamp } 7057fb5409dSJan Kara if (started) 7067fb5409dSJan Kara ext4_journal_stop(handle); 707ac27a0ecSDave Kleikamp return ret; 708ac27a0ecSDave Kleikamp } 709ac27a0ecSDave Kleikamp 7102ed88685STheodore Ts'o int ext4_get_block(struct inode *inode, sector_t iblock, 7112ed88685STheodore Ts'o struct buffer_head *bh, int create) 7122ed88685STheodore Ts'o { 7132ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh, 7142ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 7152ed88685STheodore Ts'o } 7162ed88685STheodore Ts'o 717ac27a0ecSDave Kleikamp /* 718ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 719ac27a0ecSDave Kleikamp */ 720617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 721725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *errp) 722ac27a0ecSDave Kleikamp { 7232ed88685STheodore Ts'o struct ext4_map_blocks map; 7242ed88685STheodore Ts'o struct buffer_head *bh; 725ac27a0ecSDave Kleikamp int fatal = 0, err; 726ac27a0ecSDave Kleikamp 727ac27a0ecSDave Kleikamp J_ASSERT(handle != NULL || create == 0); 728ac27a0ecSDave Kleikamp 7292ed88685STheodore Ts'o map.m_lblk = block; 7302ed88685STheodore Ts'o map.m_len = 1; 7312ed88685STheodore Ts'o err = ext4_map_blocks(handle, inode, &map, 7322ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 7332ed88685STheodore Ts'o 73490b0a973SCarlos Maiolino /* ensure we send some value back into *errp */ 73590b0a973SCarlos Maiolino *errp = 0; 73690b0a973SCarlos Maiolino 7370f70b406STheodore Ts'o if (create && err == 0) 7380f70b406STheodore Ts'o err = -ENOSPC; /* should never happen */ 7392ed88685STheodore Ts'o if (err < 0) 740ac27a0ecSDave Kleikamp *errp = err; 7412ed88685STheodore Ts'o if (err <= 0) 7422ed88685STheodore Ts'o return NULL; 7432ed88685STheodore Ts'o 7442ed88685STheodore Ts'o bh = sb_getblk(inode->i_sb, map.m_pblk); 745aebf0243SWang Shilong if (unlikely(!bh)) { 746860d21e2STheodore Ts'o *errp = -ENOMEM; 7472ed88685STheodore Ts'o return NULL; 748ac27a0ecSDave Kleikamp } 7492ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 750ac27a0ecSDave Kleikamp J_ASSERT(create != 0); 751ac39849dSAneesh Kumar K.V J_ASSERT(handle != NULL); 752ac27a0ecSDave Kleikamp 753ac27a0ecSDave Kleikamp /* 754ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 755ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 756ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 757617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 758ac27a0ecSDave Kleikamp * problem. 759ac27a0ecSDave Kleikamp */ 760ac27a0ecSDave Kleikamp lock_buffer(bh); 761ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 762617ba13bSMingming Cao fatal = ext4_journal_get_create_access(handle, bh); 763ac27a0ecSDave Kleikamp if (!fatal && !buffer_uptodate(bh)) { 764ac27a0ecSDave Kleikamp memset(bh->b_data, 0, inode->i_sb->s_blocksize); 765ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 766ac27a0ecSDave Kleikamp } 767ac27a0ecSDave Kleikamp unlock_buffer(bh); 7680390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 7690390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 770ac27a0ecSDave Kleikamp if (!fatal) 771ac27a0ecSDave Kleikamp fatal = err; 772ac27a0ecSDave Kleikamp } else { 773ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "not a new buffer"); 774ac27a0ecSDave Kleikamp } 775ac27a0ecSDave Kleikamp if (fatal) { 776ac27a0ecSDave Kleikamp *errp = fatal; 777ac27a0ecSDave Kleikamp brelse(bh); 778ac27a0ecSDave Kleikamp bh = NULL; 779ac27a0ecSDave Kleikamp } 780ac27a0ecSDave Kleikamp return bh; 781ac27a0ecSDave Kleikamp } 782ac27a0ecSDave Kleikamp 783617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 784725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *err) 785ac27a0ecSDave Kleikamp { 786ac27a0ecSDave Kleikamp struct buffer_head *bh; 787ac27a0ecSDave Kleikamp 788617ba13bSMingming Cao bh = ext4_getblk(handle, inode, block, create, err); 789ac27a0ecSDave Kleikamp if (!bh) 790ac27a0ecSDave Kleikamp return bh; 791ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 792ac27a0ecSDave Kleikamp return bh; 79365299a3bSChristoph Hellwig ll_rw_block(READ | REQ_META | REQ_PRIO, 1, &bh); 794ac27a0ecSDave Kleikamp wait_on_buffer(bh); 795ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 796ac27a0ecSDave Kleikamp return bh; 797ac27a0ecSDave Kleikamp put_bh(bh); 798ac27a0ecSDave Kleikamp *err = -EIO; 799ac27a0ecSDave Kleikamp return NULL; 800ac27a0ecSDave Kleikamp } 801ac27a0ecSDave Kleikamp 802f19d5870STao Ma int ext4_walk_page_buffers(handle_t *handle, 803ac27a0ecSDave Kleikamp struct buffer_head *head, 804ac27a0ecSDave Kleikamp unsigned from, 805ac27a0ecSDave Kleikamp unsigned to, 806ac27a0ecSDave Kleikamp int *partial, 807ac27a0ecSDave Kleikamp int (*fn)(handle_t *handle, 808ac27a0ecSDave Kleikamp struct buffer_head *bh)) 809ac27a0ecSDave Kleikamp { 810ac27a0ecSDave Kleikamp struct buffer_head *bh; 811ac27a0ecSDave Kleikamp unsigned block_start, block_end; 812ac27a0ecSDave Kleikamp unsigned blocksize = head->b_size; 813ac27a0ecSDave Kleikamp int err, ret = 0; 814ac27a0ecSDave Kleikamp struct buffer_head *next; 815ac27a0ecSDave Kleikamp 816ac27a0ecSDave Kleikamp for (bh = head, block_start = 0; 817ac27a0ecSDave Kleikamp ret == 0 && (bh != head || !block_start); 818de9a55b8STheodore Ts'o block_start = block_end, bh = next) { 819ac27a0ecSDave Kleikamp next = bh->b_this_page; 820ac27a0ecSDave Kleikamp block_end = block_start + blocksize; 821ac27a0ecSDave Kleikamp if (block_end <= from || block_start >= to) { 822ac27a0ecSDave Kleikamp if (partial && !buffer_uptodate(bh)) 823ac27a0ecSDave Kleikamp *partial = 1; 824ac27a0ecSDave Kleikamp continue; 825ac27a0ecSDave Kleikamp } 826ac27a0ecSDave Kleikamp err = (*fn)(handle, bh); 827ac27a0ecSDave Kleikamp if (!ret) 828ac27a0ecSDave Kleikamp ret = err; 829ac27a0ecSDave Kleikamp } 830ac27a0ecSDave Kleikamp return ret; 831ac27a0ecSDave Kleikamp } 832ac27a0ecSDave Kleikamp 833ac27a0ecSDave Kleikamp /* 834ac27a0ecSDave Kleikamp * To preserve ordering, it is essential that the hole instantiation and 835ac27a0ecSDave Kleikamp * the data write be encapsulated in a single transaction. We cannot 836617ba13bSMingming Cao * close off a transaction and start a new one between the ext4_get_block() 837dab291afSMingming Cao * and the commit_write(). So doing the jbd2_journal_start at the start of 838ac27a0ecSDave Kleikamp * prepare_write() is the right place. 839ac27a0ecSDave Kleikamp * 84036ade451SJan Kara * Also, this function can nest inside ext4_writepage(). In that case, we 84136ade451SJan Kara * *know* that ext4_writepage() has generated enough buffer credits to do the 84236ade451SJan Kara * whole page. So we won't block on the journal in that case, which is good, 84336ade451SJan Kara * because the caller may be PF_MEMALLOC. 844ac27a0ecSDave Kleikamp * 845617ba13bSMingming Cao * By accident, ext4 can be reentered when a transaction is open via 846ac27a0ecSDave Kleikamp * quota file writes. If we were to commit the transaction while thus 847ac27a0ecSDave Kleikamp * reentered, there can be a deadlock - we would be holding a quota 848ac27a0ecSDave Kleikamp * lock, and the commit would never complete if another thread had a 849ac27a0ecSDave Kleikamp * transaction open and was blocking on the quota lock - a ranking 850ac27a0ecSDave Kleikamp * violation. 851ac27a0ecSDave Kleikamp * 852dab291afSMingming Cao * So what we do is to rely on the fact that jbd2_journal_stop/journal_start 853ac27a0ecSDave Kleikamp * will _not_ run commit under these circumstances because handle->h_ref 854ac27a0ecSDave Kleikamp * is elevated. We'll still have enough credits for the tiny quotafile 855ac27a0ecSDave Kleikamp * write. 856ac27a0ecSDave Kleikamp */ 857f19d5870STao Ma int do_journal_get_write_access(handle_t *handle, 858ac27a0ecSDave Kleikamp struct buffer_head *bh) 859ac27a0ecSDave Kleikamp { 86056d35a4cSJan Kara int dirty = buffer_dirty(bh); 86156d35a4cSJan Kara int ret; 86256d35a4cSJan Kara 863ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 864ac27a0ecSDave Kleikamp return 0; 86556d35a4cSJan Kara /* 866ebdec241SChristoph Hellwig * __block_write_begin() could have dirtied some buffers. Clean 86756d35a4cSJan Kara * the dirty bit as jbd2_journal_get_write_access() could complain 86856d35a4cSJan Kara * otherwise about fs integrity issues. Setting of the dirty bit 869ebdec241SChristoph Hellwig * by __block_write_begin() isn't a real problem here as we clear 87056d35a4cSJan Kara * the bit before releasing a page lock and thus writeback cannot 87156d35a4cSJan Kara * ever write the buffer. 87256d35a4cSJan Kara */ 87356d35a4cSJan Kara if (dirty) 87456d35a4cSJan Kara clear_buffer_dirty(bh); 87556d35a4cSJan Kara ret = ext4_journal_get_write_access(handle, bh); 87656d35a4cSJan Kara if (!ret && dirty) 87756d35a4cSJan Kara ret = ext4_handle_dirty_metadata(handle, NULL, bh); 87856d35a4cSJan Kara return ret; 879ac27a0ecSDave Kleikamp } 880ac27a0ecSDave Kleikamp 8818b0f165fSAnatol Pomozov static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock, 8828b0f165fSAnatol Pomozov struct buffer_head *bh_result, int create); 883bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 884bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned flags, 885bfc1af65SNick Piggin struct page **pagep, void **fsdata) 886ac27a0ecSDave Kleikamp { 887bfc1af65SNick Piggin struct inode *inode = mapping->host; 8881938a150SAneesh Kumar K.V int ret, needed_blocks; 889ac27a0ecSDave Kleikamp handle_t *handle; 890ac27a0ecSDave Kleikamp int retries = 0; 891bfc1af65SNick Piggin struct page *page; 892bfc1af65SNick Piggin pgoff_t index; 893bfc1af65SNick Piggin unsigned from, to; 894bfc1af65SNick Piggin 8959bffad1eSTheodore Ts'o trace_ext4_write_begin(inode, pos, len, flags); 8961938a150SAneesh Kumar K.V /* 8971938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 8981938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 8991938a150SAneesh Kumar K.V */ 9001938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 901bfc1af65SNick Piggin index = pos >> PAGE_CACHE_SHIFT; 902bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 903bfc1af65SNick Piggin to = from + len; 904ac27a0ecSDave Kleikamp 905f19d5870STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 906f19d5870STao Ma ret = ext4_try_to_write_inline_data(mapping, inode, pos, len, 907f19d5870STao Ma flags, pagep); 908f19d5870STao Ma if (ret < 0) 90947564bfbSTheodore Ts'o return ret; 91047564bfbSTheodore Ts'o if (ret == 1) 91147564bfbSTheodore Ts'o return 0; 912f19d5870STao Ma } 913f19d5870STao Ma 91447564bfbSTheodore Ts'o /* 91547564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 91647564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 91747564bfbSTheodore Ts'o * is being written back. So grab it first before we start 91847564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 91947564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 92047564bfbSTheodore Ts'o */ 92147564bfbSTheodore Ts'o retry_grab: 92247564bfbSTheodore Ts'o page = grab_cache_page_write_begin(mapping, index, flags); 92347564bfbSTheodore Ts'o if (!page) 92447564bfbSTheodore Ts'o return -ENOMEM; 92547564bfbSTheodore Ts'o unlock_page(page); 92647564bfbSTheodore Ts'o 92747564bfbSTheodore Ts'o retry_journal: 9289924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks); 9297479d2b9SAndrew Morton if (IS_ERR(handle)) { 93047564bfbSTheodore Ts'o page_cache_release(page); 93147564bfbSTheodore Ts'o return PTR_ERR(handle); 9327479d2b9SAndrew Morton } 933ac27a0ecSDave Kleikamp 93447564bfbSTheodore Ts'o lock_page(page); 93547564bfbSTheodore Ts'o if (page->mapping != mapping) { 93647564bfbSTheodore Ts'o /* The page got truncated from under us */ 93747564bfbSTheodore Ts'o unlock_page(page); 93847564bfbSTheodore Ts'o page_cache_release(page); 939cf108bcaSJan Kara ext4_journal_stop(handle); 94047564bfbSTheodore Ts'o goto retry_grab; 941cf108bcaSJan Kara } 94247564bfbSTheodore Ts'o wait_on_page_writeback(page); 943cf108bcaSJan Kara 944744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 9456e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block_write); 946744692dcSJiaying Zhang else 9476e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block); 948bfc1af65SNick Piggin 949bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 950f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_buffers(page), 951f19d5870STao Ma from, to, NULL, 952f19d5870STao Ma do_journal_get_write_access); 953b46be050SAndrey Savochkin } 954bfc1af65SNick Piggin 955bfc1af65SNick Piggin if (ret) { 956bfc1af65SNick Piggin unlock_page(page); 957ae4d5372SAneesh Kumar K.V /* 9586e1db88dSChristoph Hellwig * __block_write_begin may have instantiated a few blocks 959ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 960ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 9611938a150SAneesh Kumar K.V * 9621938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 9631938a150SAneesh Kumar K.V * truncate finishes 964ae4d5372SAneesh Kumar K.V */ 965ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 9661938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 9671938a150SAneesh Kumar K.V 9681938a150SAneesh Kumar K.V ext4_journal_stop(handle); 9691938a150SAneesh Kumar K.V if (pos + len > inode->i_size) { 970b9a4207dSJan Kara ext4_truncate_failed_write(inode); 9711938a150SAneesh Kumar K.V /* 972ffacfa7aSJan Kara * If truncate failed early the inode might 9731938a150SAneesh Kumar K.V * still be on the orphan list; we need to 9741938a150SAneesh Kumar K.V * make sure the inode is removed from the 9751938a150SAneesh Kumar K.V * orphan list in that case. 9761938a150SAneesh Kumar K.V */ 9771938a150SAneesh Kumar K.V if (inode->i_nlink) 9781938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 9791938a150SAneesh Kumar K.V } 980bfc1af65SNick Piggin 98147564bfbSTheodore Ts'o if (ret == -ENOSPC && 98247564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 98347564bfbSTheodore Ts'o goto retry_journal; 98447564bfbSTheodore Ts'o page_cache_release(page); 98547564bfbSTheodore Ts'o return ret; 98647564bfbSTheodore Ts'o } 98747564bfbSTheodore Ts'o *pagep = page; 988ac27a0ecSDave Kleikamp return ret; 989ac27a0ecSDave Kleikamp } 990ac27a0ecSDave Kleikamp 991bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 992bfc1af65SNick Piggin static int write_end_fn(handle_t *handle, struct buffer_head *bh) 993ac27a0ecSDave Kleikamp { 994ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 995ac27a0ecSDave Kleikamp return 0; 996ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 9970390131bSFrank Mayhar return ext4_handle_dirty_metadata(handle, NULL, bh); 998ac27a0ecSDave Kleikamp } 999ac27a0ecSDave Kleikamp 1000f8514083SAneesh Kumar K.V static int ext4_generic_write_end(struct file *file, 1001f8514083SAneesh Kumar K.V struct address_space *mapping, 1002f8514083SAneesh Kumar K.V loff_t pos, unsigned len, unsigned copied, 1003f8514083SAneesh Kumar K.V struct page *page, void *fsdata) 1004f8514083SAneesh Kumar K.V { 1005f8514083SAneesh Kumar K.V int i_size_changed = 0; 1006f8514083SAneesh Kumar K.V struct inode *inode = mapping->host; 1007f8514083SAneesh Kumar K.V handle_t *handle = ext4_journal_current_handle(); 1008f8514083SAneesh Kumar K.V 1009f19d5870STao Ma if (ext4_has_inline_data(inode)) 1010f19d5870STao Ma copied = ext4_write_inline_data_end(inode, pos, len, 1011f19d5870STao Ma copied, page); 1012f19d5870STao Ma else 1013f19d5870STao Ma copied = block_write_end(file, mapping, pos, 1014f19d5870STao Ma len, copied, page, fsdata); 1015f8514083SAneesh Kumar K.V 1016f8514083SAneesh Kumar K.V /* 1017f8514083SAneesh Kumar K.V * No need to use i_size_read() here, the i_size 1018f8514083SAneesh Kumar K.V * cannot change under us because we hold i_mutex. 1019f8514083SAneesh Kumar K.V * 1020f8514083SAneesh Kumar K.V * But it's important to update i_size while still holding page lock: 1021f8514083SAneesh Kumar K.V * page writeout could otherwise come in and zero beyond i_size. 1022f8514083SAneesh Kumar K.V */ 1023f8514083SAneesh Kumar K.V if (pos + copied > inode->i_size) { 1024f8514083SAneesh Kumar K.V i_size_write(inode, pos + copied); 1025f8514083SAneesh Kumar K.V i_size_changed = 1; 1026f8514083SAneesh Kumar K.V } 1027f8514083SAneesh Kumar K.V 1028f8514083SAneesh Kumar K.V if (pos + copied > EXT4_I(inode)->i_disksize) { 1029f8514083SAneesh Kumar K.V /* We need to mark inode dirty even if 1030f8514083SAneesh Kumar K.V * new_i_size is less that inode->i_size 1031f8514083SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 1032f8514083SAneesh Kumar K.V */ 1033f8514083SAneesh Kumar K.V ext4_update_i_disksize(inode, (pos + copied)); 1034f8514083SAneesh Kumar K.V i_size_changed = 1; 1035f8514083SAneesh Kumar K.V } 1036f8514083SAneesh Kumar K.V unlock_page(page); 1037f8514083SAneesh Kumar K.V page_cache_release(page); 1038f8514083SAneesh Kumar K.V 1039f8514083SAneesh Kumar K.V /* 1040f8514083SAneesh Kumar K.V * Don't mark the inode dirty under page lock. First, it unnecessarily 1041f8514083SAneesh Kumar K.V * makes the holding time of page lock longer. Second, it forces lock 1042f8514083SAneesh Kumar K.V * ordering of page lock and transaction start for journaling 1043f8514083SAneesh Kumar K.V * filesystems. 1044f8514083SAneesh Kumar K.V */ 1045f8514083SAneesh Kumar K.V if (i_size_changed) 1046f8514083SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 1047f8514083SAneesh Kumar K.V 1048f8514083SAneesh Kumar K.V return copied; 1049f8514083SAneesh Kumar K.V } 1050f8514083SAneesh Kumar K.V 1051ac27a0ecSDave Kleikamp /* 1052ac27a0ecSDave Kleikamp * We need to pick up the new inode size which generic_commit_write gave us 1053ac27a0ecSDave Kleikamp * `file' can be NULL - eg, when called from page_symlink(). 1054ac27a0ecSDave Kleikamp * 1055617ba13bSMingming Cao * ext4 never places buffers on inode->i_mapping->private_list. metadata 1056ac27a0ecSDave Kleikamp * buffers are managed internally. 1057ac27a0ecSDave Kleikamp */ 1058bfc1af65SNick Piggin static int ext4_ordered_write_end(struct file *file, 1059bfc1af65SNick Piggin struct address_space *mapping, 1060bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1061bfc1af65SNick Piggin struct page *page, void *fsdata) 1062ac27a0ecSDave Kleikamp { 1063617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1064cf108bcaSJan Kara struct inode *inode = mapping->host; 1065ac27a0ecSDave Kleikamp int ret = 0, ret2; 1066ac27a0ecSDave Kleikamp 10679bffad1eSTheodore Ts'o trace_ext4_ordered_write_end(inode, pos, len, copied); 1068678aaf48SJan Kara ret = ext4_jbd2_file_inode(handle, inode); 1069ac27a0ecSDave Kleikamp 1070ac27a0ecSDave Kleikamp if (ret == 0) { 1071f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 1072bfc1af65SNick Piggin page, fsdata); 1073f8a87d89SRoel Kluin copied = ret2; 1074ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1075f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1076f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1077f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1078f8514083SAneesh Kumar K.V */ 1079f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1080f8a87d89SRoel Kluin if (ret2 < 0) 1081f8a87d89SRoel Kluin ret = ret2; 108209e0834fSAkira Fujita } else { 108309e0834fSAkira Fujita unlock_page(page); 108409e0834fSAkira Fujita page_cache_release(page); 1085ac27a0ecSDave Kleikamp } 108609e0834fSAkira Fujita 1087617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1088ac27a0ecSDave Kleikamp if (!ret) 1089ac27a0ecSDave Kleikamp ret = ret2; 1090bfc1af65SNick Piggin 1091f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1092b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1093f8514083SAneesh Kumar K.V /* 1094ffacfa7aSJan Kara * If truncate failed early the inode might still be 1095f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1096f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1097f8514083SAneesh Kumar K.V */ 1098f8514083SAneesh Kumar K.V if (inode->i_nlink) 1099f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1100f8514083SAneesh Kumar K.V } 1101f8514083SAneesh Kumar K.V 1102f8514083SAneesh Kumar K.V 1103bfc1af65SNick Piggin return ret ? ret : copied; 1104ac27a0ecSDave Kleikamp } 1105ac27a0ecSDave Kleikamp 1106bfc1af65SNick Piggin static int ext4_writeback_write_end(struct file *file, 1107bfc1af65SNick Piggin struct address_space *mapping, 1108bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1109bfc1af65SNick Piggin struct page *page, void *fsdata) 1110ac27a0ecSDave Kleikamp { 1111617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1112cf108bcaSJan Kara struct inode *inode = mapping->host; 1113ac27a0ecSDave Kleikamp int ret = 0, ret2; 1114ac27a0ecSDave Kleikamp 11159bffad1eSTheodore Ts'o trace_ext4_writeback_write_end(inode, pos, len, copied); 1116f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 1117bfc1af65SNick Piggin page, fsdata); 1118f8a87d89SRoel Kluin copied = ret2; 1119ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1120f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1121f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1122f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1123f8514083SAneesh Kumar K.V */ 1124f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1125f8514083SAneesh Kumar K.V 1126f8a87d89SRoel Kluin if (ret2 < 0) 1127f8a87d89SRoel Kluin ret = ret2; 1128ac27a0ecSDave Kleikamp 1129617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1130ac27a0ecSDave Kleikamp if (!ret) 1131ac27a0ecSDave Kleikamp ret = ret2; 1132bfc1af65SNick Piggin 1133f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1134b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1135f8514083SAneesh Kumar K.V /* 1136ffacfa7aSJan Kara * If truncate failed early the inode might still be 1137f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1138f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1139f8514083SAneesh Kumar K.V */ 1140f8514083SAneesh Kumar K.V if (inode->i_nlink) 1141f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1142f8514083SAneesh Kumar K.V } 1143f8514083SAneesh Kumar K.V 1144bfc1af65SNick Piggin return ret ? ret : copied; 1145ac27a0ecSDave Kleikamp } 1146ac27a0ecSDave Kleikamp 1147bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 1148bfc1af65SNick Piggin struct address_space *mapping, 1149bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1150bfc1af65SNick Piggin struct page *page, void *fsdata) 1151ac27a0ecSDave Kleikamp { 1152617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1153bfc1af65SNick Piggin struct inode *inode = mapping->host; 1154ac27a0ecSDave Kleikamp int ret = 0, ret2; 1155ac27a0ecSDave Kleikamp int partial = 0; 1156bfc1af65SNick Piggin unsigned from, to; 1157cf17fea6SAneesh Kumar K.V loff_t new_i_size; 1158ac27a0ecSDave Kleikamp 11599bffad1eSTheodore Ts'o trace_ext4_journalled_write_end(inode, pos, len, copied); 1160bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 1161bfc1af65SNick Piggin to = from + len; 1162bfc1af65SNick Piggin 1163441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1164441c8508SCurt Wohlgemuth 11653fdcfb66STao Ma if (ext4_has_inline_data(inode)) 11663fdcfb66STao Ma copied = ext4_write_inline_data_end(inode, pos, len, 11673fdcfb66STao Ma copied, page); 11683fdcfb66STao Ma else { 1169bfc1af65SNick Piggin if (copied < len) { 1170bfc1af65SNick Piggin if (!PageUptodate(page)) 1171bfc1af65SNick Piggin copied = 0; 1172bfc1af65SNick Piggin page_zero_new_buffers(page, from+copied, to); 1173bfc1af65SNick Piggin } 1174ac27a0ecSDave Kleikamp 1175f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_buffers(page), from, 1176bfc1af65SNick Piggin to, &partial, write_end_fn); 1177ac27a0ecSDave Kleikamp if (!partial) 1178ac27a0ecSDave Kleikamp SetPageUptodate(page); 11793fdcfb66STao Ma } 1180cf17fea6SAneesh Kumar K.V new_i_size = pos + copied; 1181cf17fea6SAneesh Kumar K.V if (new_i_size > inode->i_size) 1182bfc1af65SNick Piggin i_size_write(inode, pos+copied); 118319f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 11842d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 1185cf17fea6SAneesh Kumar K.V if (new_i_size > EXT4_I(inode)->i_disksize) { 1186cf17fea6SAneesh Kumar K.V ext4_update_i_disksize(inode, new_i_size); 1187617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1188ac27a0ecSDave Kleikamp if (!ret) 1189ac27a0ecSDave Kleikamp ret = ret2; 1190ac27a0ecSDave Kleikamp } 1191bfc1af65SNick Piggin 1192cf108bcaSJan Kara unlock_page(page); 1193f8514083SAneesh Kumar K.V page_cache_release(page); 1194ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1195f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1196f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1197f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1198f8514083SAneesh Kumar K.V */ 1199f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1200f8514083SAneesh Kumar K.V 1201617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1202ac27a0ecSDave Kleikamp if (!ret) 1203ac27a0ecSDave Kleikamp ret = ret2; 1204f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1205b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1206f8514083SAneesh Kumar K.V /* 1207ffacfa7aSJan Kara * If truncate failed early the inode might still be 1208f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1209f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1210f8514083SAneesh Kumar K.V */ 1211f8514083SAneesh Kumar K.V if (inode->i_nlink) 1212f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1213f8514083SAneesh Kumar K.V } 1214bfc1af65SNick Piggin 1215bfc1af65SNick Piggin return ret ? ret : copied; 1216ac27a0ecSDave Kleikamp } 1217d2a17637SMingming Cao 12189d0be502STheodore Ts'o /* 12197b415bf6SAditya Kali * Reserve a single cluster located at lblock 12209d0be502STheodore Ts'o */ 122101f49d0bSTheodore Ts'o static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock) 1222d2a17637SMingming Cao { 1223030ba6bcSAneesh Kumar K.V int retries = 0; 1224d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 12250637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 12267b415bf6SAditya Kali unsigned int md_needed; 12275dd4056dSChristoph Hellwig int ret; 122803179fe9STheodore Ts'o ext4_lblk_t save_last_lblock; 122903179fe9STheodore Ts'o int save_len; 1230d2a17637SMingming Cao 123160e58e0fSMingming Cao /* 123272b8ab9dSEric Sandeen * We will charge metadata quota at writeout time; this saves 123372b8ab9dSEric Sandeen * us from metadata over-estimation, though we may go over by 123472b8ab9dSEric Sandeen * a small amount in the end. Here we just reserve for data. 123560e58e0fSMingming Cao */ 12367b415bf6SAditya Kali ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1)); 12375dd4056dSChristoph Hellwig if (ret) 12385dd4056dSChristoph Hellwig return ret; 123903179fe9STheodore Ts'o 124003179fe9STheodore Ts'o /* 124103179fe9STheodore Ts'o * recalculate the amount of metadata blocks to reserve 124203179fe9STheodore Ts'o * in order to allocate nrblocks 124303179fe9STheodore Ts'o * worse case is one extent per block 124403179fe9STheodore Ts'o */ 124503179fe9STheodore Ts'o repeat: 124603179fe9STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 124703179fe9STheodore Ts'o /* 124803179fe9STheodore Ts'o * ext4_calc_metadata_amount() has side effects, which we have 124903179fe9STheodore Ts'o * to be prepared undo if we fail to claim space. 125003179fe9STheodore Ts'o */ 125103179fe9STheodore Ts'o save_len = ei->i_da_metadata_calc_len; 125203179fe9STheodore Ts'o save_last_lblock = ei->i_da_metadata_calc_last_lblock; 125303179fe9STheodore Ts'o md_needed = EXT4_NUM_B2C(sbi, 125403179fe9STheodore Ts'o ext4_calc_metadata_amount(inode, lblock)); 125503179fe9STheodore Ts'o trace_ext4_da_reserve_space(inode, md_needed); 125603179fe9STheodore Ts'o 125772b8ab9dSEric Sandeen /* 125872b8ab9dSEric Sandeen * We do still charge estimated metadata to the sb though; 125972b8ab9dSEric Sandeen * we cannot afford to run out of free blocks. 126072b8ab9dSEric Sandeen */ 1261e7d5f315STheodore Ts'o if (ext4_claim_free_clusters(sbi, md_needed + 1, 0)) { 126203179fe9STheodore Ts'o ei->i_da_metadata_calc_len = save_len; 126303179fe9STheodore Ts'o ei->i_da_metadata_calc_last_lblock = save_last_lblock; 126403179fe9STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 1265030ba6bcSAneesh Kumar K.V if (ext4_should_retry_alloc(inode->i_sb, &retries)) { 1266030ba6bcSAneesh Kumar K.V yield(); 1267030ba6bcSAneesh Kumar K.V goto repeat; 1268030ba6bcSAneesh Kumar K.V } 126903179fe9STheodore Ts'o dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1)); 1270d2a17637SMingming Cao return -ENOSPC; 1271d2a17637SMingming Cao } 12729d0be502STheodore Ts'o ei->i_reserved_data_blocks++; 12730637c6f4STheodore Ts'o ei->i_reserved_meta_blocks += md_needed; 12740637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 127539bc680aSDmitry Monakhov 1276d2a17637SMingming Cao return 0; /* success */ 1277d2a17637SMingming Cao } 1278d2a17637SMingming Cao 127912219aeaSAneesh Kumar K.V static void ext4_da_release_space(struct inode *inode, int to_free) 1280d2a17637SMingming Cao { 1281d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 12820637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 1283d2a17637SMingming Cao 1284cd213226SMingming Cao if (!to_free) 1285cd213226SMingming Cao return; /* Nothing to release, exit */ 1286cd213226SMingming Cao 1287d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1288cd213226SMingming Cao 12895a58ec87SLi Zefan trace_ext4_da_release_space(inode, to_free); 12900637c6f4STheodore Ts'o if (unlikely(to_free > ei->i_reserved_data_blocks)) { 1291cd213226SMingming Cao /* 12920637c6f4STheodore Ts'o * if there aren't enough reserved blocks, then the 12930637c6f4STheodore Ts'o * counter is messed up somewhere. Since this 12940637c6f4STheodore Ts'o * function is called from invalidate page, it's 12950637c6f4STheodore Ts'o * harmless to return without any action. 1296cd213226SMingming Cao */ 12978de5c325STheodore Ts'o ext4_warning(inode->i_sb, "ext4_da_release_space: " 12980637c6f4STheodore Ts'o "ino %lu, to_free %d with only %d reserved " 12991084f252STheodore Ts'o "data blocks", inode->i_ino, to_free, 13000637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 13010637c6f4STheodore Ts'o WARN_ON(1); 13020637c6f4STheodore Ts'o to_free = ei->i_reserved_data_blocks; 13030637c6f4STheodore Ts'o } 13040637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= to_free; 13050637c6f4STheodore Ts'o 13060637c6f4STheodore Ts'o if (ei->i_reserved_data_blocks == 0) { 13070637c6f4STheodore Ts'o /* 13080637c6f4STheodore Ts'o * We can release all of the reserved metadata blocks 13090637c6f4STheodore Ts'o * only when we have written all of the delayed 13100637c6f4STheodore Ts'o * allocation blocks. 13117b415bf6SAditya Kali * Note that in case of bigalloc, i_reserved_meta_blocks, 13127b415bf6SAditya Kali * i_reserved_data_blocks, etc. refer to number of clusters. 13130637c6f4STheodore Ts'o */ 131457042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 131572b8ab9dSEric Sandeen ei->i_reserved_meta_blocks); 1316ee5f4d9cSTheodore Ts'o ei->i_reserved_meta_blocks = 0; 13179d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 1318cd213226SMingming Cao } 1319cd213226SMingming Cao 132072b8ab9dSEric Sandeen /* update fs dirty data blocks counter */ 132157042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free); 1322d2a17637SMingming Cao 1323d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 132460e58e0fSMingming Cao 13257b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free)); 1326d2a17637SMingming Cao } 1327d2a17637SMingming Cao 1328d2a17637SMingming Cao static void ext4_da_page_release_reservation(struct page *page, 1329d2a17637SMingming Cao unsigned long offset) 1330d2a17637SMingming Cao { 1331d2a17637SMingming Cao int to_release = 0; 1332d2a17637SMingming Cao struct buffer_head *head, *bh; 1333d2a17637SMingming Cao unsigned int curr_off = 0; 13347b415bf6SAditya Kali struct inode *inode = page->mapping->host; 13357b415bf6SAditya Kali struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 13367b415bf6SAditya Kali int num_clusters; 133751865fdaSZheng Liu ext4_fsblk_t lblk; 1338d2a17637SMingming Cao 1339d2a17637SMingming Cao head = page_buffers(page); 1340d2a17637SMingming Cao bh = head; 1341d2a17637SMingming Cao do { 1342d2a17637SMingming Cao unsigned int next_off = curr_off + bh->b_size; 1343d2a17637SMingming Cao 1344d2a17637SMingming Cao if ((offset <= curr_off) && (buffer_delay(bh))) { 1345d2a17637SMingming Cao to_release++; 1346d2a17637SMingming Cao clear_buffer_delay(bh); 1347d2a17637SMingming Cao } 1348d2a17637SMingming Cao curr_off = next_off; 1349d2a17637SMingming Cao } while ((bh = bh->b_this_page) != head); 13507b415bf6SAditya Kali 135151865fdaSZheng Liu if (to_release) { 135251865fdaSZheng Liu lblk = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits); 135351865fdaSZheng Liu ext4_es_remove_extent(inode, lblk, to_release); 135451865fdaSZheng Liu } 135551865fdaSZheng Liu 13567b415bf6SAditya Kali /* If we have released all the blocks belonging to a cluster, then we 13577b415bf6SAditya Kali * need to release the reserved space for that cluster. */ 13587b415bf6SAditya Kali num_clusters = EXT4_NUM_B2C(sbi, to_release); 13597b415bf6SAditya Kali while (num_clusters > 0) { 13607b415bf6SAditya Kali lblk = (page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits)) + 13617b415bf6SAditya Kali ((num_clusters - 1) << sbi->s_cluster_bits); 13627b415bf6SAditya Kali if (sbi->s_cluster_ratio == 1 || 13637d1b1fbcSZheng Liu !ext4_find_delalloc_cluster(inode, lblk)) 13647b415bf6SAditya Kali ext4_da_release_space(inode, 1); 13657b415bf6SAditya Kali 13667b415bf6SAditya Kali num_clusters--; 13677b415bf6SAditya Kali } 1368d2a17637SMingming Cao } 1369ac27a0ecSDave Kleikamp 1370ac27a0ecSDave Kleikamp /* 137164769240SAlex Tomas * Delayed allocation stuff 137264769240SAlex Tomas */ 137364769240SAlex Tomas 137464769240SAlex Tomas /* 137564769240SAlex Tomas * mpage_da_submit_io - walks through extent of pages and try to write 1376a1d6cc56SAneesh Kumar K.V * them with writepage() call back 137764769240SAlex Tomas * 137864769240SAlex Tomas * @mpd->inode: inode 137964769240SAlex Tomas * @mpd->first_page: first page of the extent 138064769240SAlex Tomas * @mpd->next_page: page after the last page of the extent 138164769240SAlex Tomas * 138264769240SAlex Tomas * By the time mpage_da_submit_io() is called we expect all blocks 138364769240SAlex Tomas * to be allocated. this may be wrong if allocation failed. 138464769240SAlex Tomas * 138564769240SAlex Tomas * As pages are already locked by write_cache_pages(), we can't use it 138664769240SAlex Tomas */ 13871de3e3dfSTheodore Ts'o static int mpage_da_submit_io(struct mpage_da_data *mpd, 13881de3e3dfSTheodore Ts'o struct ext4_map_blocks *map) 138964769240SAlex Tomas { 1390791b7f08SAneesh Kumar K.V struct pagevec pvec; 1391791b7f08SAneesh Kumar K.V unsigned long index, end; 1392791b7f08SAneesh Kumar K.V int ret = 0, err, nr_pages, i; 1393791b7f08SAneesh Kumar K.V struct inode *inode = mpd->inode; 1394791b7f08SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 1395cb20d518STheodore Ts'o loff_t size = i_size_read(inode); 13963ecdb3a1STheodore Ts'o unsigned int len, block_start; 13973ecdb3a1STheodore Ts'o struct buffer_head *bh, *page_bufs = NULL; 13981de3e3dfSTheodore Ts'o sector_t pblock = 0, cur_logical = 0; 1399bd2d0210STheodore Ts'o struct ext4_io_submit io_submit; 140064769240SAlex Tomas 140164769240SAlex Tomas BUG_ON(mpd->next_page <= mpd->first_page); 1402bd2d0210STheodore Ts'o memset(&io_submit, 0, sizeof(io_submit)); 1403791b7f08SAneesh Kumar K.V /* 1404791b7f08SAneesh Kumar K.V * We need to start from the first_page to the next_page - 1 1405791b7f08SAneesh Kumar K.V * to make sure we also write the mapped dirty buffer_heads. 14068dc207c0STheodore Ts'o * If we look at mpd->b_blocknr we would only be looking 1407791b7f08SAneesh Kumar K.V * at the currently mapped buffer_heads. 1408791b7f08SAneesh Kumar K.V */ 140964769240SAlex Tomas index = mpd->first_page; 141064769240SAlex Tomas end = mpd->next_page - 1; 141164769240SAlex Tomas 1412791b7f08SAneesh Kumar K.V pagevec_init(&pvec, 0); 141364769240SAlex Tomas while (index <= end) { 1414791b7f08SAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 141564769240SAlex Tomas if (nr_pages == 0) 141664769240SAlex Tomas break; 141764769240SAlex Tomas for (i = 0; i < nr_pages; i++) { 1418f8bec370SJan Kara int skip_page = 0; 141964769240SAlex Tomas struct page *page = pvec.pages[i]; 142064769240SAlex Tomas 1421791b7f08SAneesh Kumar K.V index = page->index; 1422791b7f08SAneesh Kumar K.V if (index > end) 1423791b7f08SAneesh Kumar K.V break; 1424cb20d518STheodore Ts'o 1425cb20d518STheodore Ts'o if (index == size >> PAGE_CACHE_SHIFT) 1426cb20d518STheodore Ts'o len = size & ~PAGE_CACHE_MASK; 1427cb20d518STheodore Ts'o else 1428cb20d518STheodore Ts'o len = PAGE_CACHE_SIZE; 14291de3e3dfSTheodore Ts'o if (map) { 14301de3e3dfSTheodore Ts'o cur_logical = index << (PAGE_CACHE_SHIFT - 14311de3e3dfSTheodore Ts'o inode->i_blkbits); 14321de3e3dfSTheodore Ts'o pblock = map->m_pblk + (cur_logical - 14331de3e3dfSTheodore Ts'o map->m_lblk); 14341de3e3dfSTheodore Ts'o } 1435791b7f08SAneesh Kumar K.V index++; 1436791b7f08SAneesh Kumar K.V 1437791b7f08SAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1438791b7f08SAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1439791b7f08SAneesh Kumar K.V 14403ecdb3a1STheodore Ts'o bh = page_bufs = page_buffers(page); 14413ecdb3a1STheodore Ts'o block_start = 0; 14423ecdb3a1STheodore Ts'o do { 14431de3e3dfSTheodore Ts'o if (map && (cur_logical >= map->m_lblk) && 14441de3e3dfSTheodore Ts'o (cur_logical <= (map->m_lblk + 14451de3e3dfSTheodore Ts'o (map->m_len - 1)))) { 14461de3e3dfSTheodore Ts'o if (buffer_delay(bh)) { 14471de3e3dfSTheodore Ts'o clear_buffer_delay(bh); 14481de3e3dfSTheodore Ts'o bh->b_blocknr = pblock; 14491de3e3dfSTheodore Ts'o } 14501de3e3dfSTheodore Ts'o if (buffer_unwritten(bh) || 14511de3e3dfSTheodore Ts'o buffer_mapped(bh)) 14521de3e3dfSTheodore Ts'o BUG_ON(bh->b_blocknr != pblock); 14531de3e3dfSTheodore Ts'o if (map->m_flags & EXT4_MAP_UNINIT) 14541de3e3dfSTheodore Ts'o set_buffer_uninit(bh); 14551de3e3dfSTheodore Ts'o clear_buffer_unwritten(bh); 14561de3e3dfSTheodore Ts'o } 14571de3e3dfSTheodore Ts'o 145813a79a47SYongqiang Yang /* 145913a79a47SYongqiang Yang * skip page if block allocation undone and 146013a79a47SYongqiang Yang * block is dirty 146113a79a47SYongqiang Yang */ 146213a79a47SYongqiang Yang if (ext4_bh_delay_or_unwritten(NULL, bh)) 146397498956STheodore Ts'o skip_page = 1; 14643ecdb3a1STheodore Ts'o bh = bh->b_this_page; 14653ecdb3a1STheodore Ts'o block_start += bh->b_size; 14661de3e3dfSTheodore Ts'o cur_logical++; 14671de3e3dfSTheodore Ts'o pblock++; 14681de3e3dfSTheodore Ts'o } while (bh != page_bufs); 14691de3e3dfSTheodore Ts'o 1470f8bec370SJan Kara if (skip_page) { 1471f8bec370SJan Kara unlock_page(page); 1472f8bec370SJan Kara continue; 1473f8bec370SJan Kara } 1474cb20d518STheodore Ts'o 147597498956STheodore Ts'o clear_page_dirty_for_io(page); 1476fe089c77SJan Kara err = ext4_bio_write_page(&io_submit, page, len, 1477fe089c77SJan Kara mpd->wbc); 1478cb20d518STheodore Ts'o if (!err) 1479a1d6cc56SAneesh Kumar K.V mpd->pages_written++; 148064769240SAlex Tomas /* 148164769240SAlex Tomas * In error case, we have to continue because 148264769240SAlex Tomas * remaining pages are still locked 148364769240SAlex Tomas */ 148464769240SAlex Tomas if (ret == 0) 148564769240SAlex Tomas ret = err; 148664769240SAlex Tomas } 148764769240SAlex Tomas pagevec_release(&pvec); 148864769240SAlex Tomas } 1489bd2d0210STheodore Ts'o ext4_io_submit(&io_submit); 149064769240SAlex Tomas return ret; 149164769240SAlex Tomas } 149264769240SAlex Tomas 1493c7f5938aSCurt Wohlgemuth static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd) 1494c4a0c46eSAneesh Kumar K.V { 1495c4a0c46eSAneesh Kumar K.V int nr_pages, i; 1496c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 1497c4a0c46eSAneesh Kumar K.V struct pagevec pvec; 1498c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 1499c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 150051865fdaSZheng Liu ext4_lblk_t start, last; 1501c4a0c46eSAneesh Kumar K.V 1502c7f5938aSCurt Wohlgemuth index = mpd->first_page; 1503c7f5938aSCurt Wohlgemuth end = mpd->next_page - 1; 150451865fdaSZheng Liu 150551865fdaSZheng Liu start = index << (PAGE_CACHE_SHIFT - inode->i_blkbits); 150651865fdaSZheng Liu last = end << (PAGE_CACHE_SHIFT - inode->i_blkbits); 150751865fdaSZheng Liu ext4_es_remove_extent(inode, start, last - start + 1); 150851865fdaSZheng Liu 150966bea92cSEric Sandeen pagevec_init(&pvec, 0); 1510c4a0c46eSAneesh Kumar K.V while (index <= end) { 1511c4a0c46eSAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 1512c4a0c46eSAneesh Kumar K.V if (nr_pages == 0) 1513c4a0c46eSAneesh Kumar K.V break; 1514c4a0c46eSAneesh Kumar K.V for (i = 0; i < nr_pages; i++) { 1515c4a0c46eSAneesh Kumar K.V struct page *page = pvec.pages[i]; 15169b1d0998SJan Kara if (page->index > end) 1517c4a0c46eSAneesh Kumar K.V break; 1518c4a0c46eSAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1519c4a0c46eSAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1520c4a0c46eSAneesh Kumar K.V block_invalidatepage(page, 0); 1521c4a0c46eSAneesh Kumar K.V ClearPageUptodate(page); 1522c4a0c46eSAneesh Kumar K.V unlock_page(page); 1523c4a0c46eSAneesh Kumar K.V } 15249b1d0998SJan Kara index = pvec.pages[nr_pages - 1]->index + 1; 15259b1d0998SJan Kara pagevec_release(&pvec); 1526c4a0c46eSAneesh Kumar K.V } 1527c4a0c46eSAneesh Kumar K.V return; 1528c4a0c46eSAneesh Kumar K.V } 1529c4a0c46eSAneesh Kumar K.V 1530df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 1531df22291fSAneesh Kumar K.V { 1532df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 153392b97816STheodore Ts'o struct super_block *sb = inode->i_sb; 153492b97816STheodore Ts'o 153592b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld", 15365dee5437STheodore Ts'o EXT4_C2B(EXT4_SB(inode->i_sb), 15375dee5437STheodore Ts'o ext4_count_free_clusters(inode->i_sb))); 153892b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Free/Dirty block details"); 153992b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "free_blocks=%lld", 154057042651STheodore Ts'o (long long) EXT4_C2B(EXT4_SB(inode->i_sb), 154157042651STheodore Ts'o percpu_counter_sum(&sbi->s_freeclusters_counter))); 154292b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld", 15437b415bf6SAditya Kali (long long) EXT4_C2B(EXT4_SB(inode->i_sb), 15447b415bf6SAditya Kali percpu_counter_sum(&sbi->s_dirtyclusters_counter))); 154592b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Block reservation details"); 154692b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u", 1547df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_data_blocks); 154892b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_meta_blocks=%u", 1549df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_meta_blocks); 1550df22291fSAneesh Kumar K.V return; 1551df22291fSAneesh Kumar K.V } 1552df22291fSAneesh Kumar K.V 1553b920c755STheodore Ts'o /* 15545a87b7a5STheodore Ts'o * mpage_da_map_and_submit - go through given space, map them 15555a87b7a5STheodore Ts'o * if necessary, and then submit them for I/O 155664769240SAlex Tomas * 15578dc207c0STheodore Ts'o * @mpd - bh describing space 155864769240SAlex Tomas * 155964769240SAlex Tomas * The function skips space we know is already mapped to disk blocks. 156064769240SAlex Tomas * 156164769240SAlex Tomas */ 15625a87b7a5STheodore Ts'o static void mpage_da_map_and_submit(struct mpage_da_data *mpd) 156364769240SAlex Tomas { 15642ac3b6e0STheodore Ts'o int err, blks, get_blocks_flags; 15651de3e3dfSTheodore Ts'o struct ext4_map_blocks map, *mapp = NULL; 15662fa3cdfbSTheodore Ts'o sector_t next = mpd->b_blocknr; 15672fa3cdfbSTheodore Ts'o unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits; 15682fa3cdfbSTheodore Ts'o loff_t disksize = EXT4_I(mpd->inode)->i_disksize; 15692fa3cdfbSTheodore Ts'o handle_t *handle = NULL; 157064769240SAlex Tomas 157164769240SAlex Tomas /* 15725a87b7a5STheodore Ts'o * If the blocks are mapped already, or we couldn't accumulate 15735a87b7a5STheodore Ts'o * any blocks, then proceed immediately to the submission stage. 157464769240SAlex Tomas */ 15755a87b7a5STheodore Ts'o if ((mpd->b_size == 0) || 15765a87b7a5STheodore Ts'o ((mpd->b_state & (1 << BH_Mapped)) && 157729fa89d0SAneesh Kumar K.V !(mpd->b_state & (1 << BH_Delay)) && 15785a87b7a5STheodore Ts'o !(mpd->b_state & (1 << BH_Unwritten)))) 15795a87b7a5STheodore Ts'o goto submit_io; 15802fa3cdfbSTheodore Ts'o 15812fa3cdfbSTheodore Ts'o handle = ext4_journal_current_handle(); 15822fa3cdfbSTheodore Ts'o BUG_ON(!handle); 15832fa3cdfbSTheodore Ts'o 158479ffab34SAneesh Kumar K.V /* 158579e83036SEric Sandeen * Call ext4_map_blocks() to allocate any delayed allocation 15862ac3b6e0STheodore Ts'o * blocks, or to convert an uninitialized extent to be 15872ac3b6e0STheodore Ts'o * initialized (in the case where we have written into 15882ac3b6e0STheodore Ts'o * one or more preallocated blocks). 15892ac3b6e0STheodore Ts'o * 15902ac3b6e0STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to 15912ac3b6e0STheodore Ts'o * indicate that we are on the delayed allocation path. This 15922ac3b6e0STheodore Ts'o * affects functions in many different parts of the allocation 15932ac3b6e0STheodore Ts'o * call path. This flag exists primarily because we don't 159479e83036SEric Sandeen * want to change *many* call functions, so ext4_map_blocks() 1595f2321097STheodore Ts'o * will set the EXT4_STATE_DELALLOC_RESERVED flag once the 15962ac3b6e0STheodore Ts'o * inode's allocation semaphore is taken. 15972ac3b6e0STheodore Ts'o * 15982ac3b6e0STheodore Ts'o * If the blocks in questions were delalloc blocks, set 15992ac3b6e0STheodore Ts'o * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting 16002ac3b6e0STheodore Ts'o * variables are updated after the blocks have been allocated. 160179ffab34SAneesh Kumar K.V */ 16022ed88685STheodore Ts'o map.m_lblk = next; 16032ed88685STheodore Ts'o map.m_len = max_blocks; 16041296cc85SAneesh Kumar K.V get_blocks_flags = EXT4_GET_BLOCKS_CREATE; 1605744692dcSJiaying Zhang if (ext4_should_dioread_nolock(mpd->inode)) 1606744692dcSJiaying Zhang get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; 16072ac3b6e0STheodore Ts'o if (mpd->b_state & (1 << BH_Delay)) 16081296cc85SAneesh Kumar K.V get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; 16091296cc85SAneesh Kumar K.V 16102ed88685STheodore Ts'o blks = ext4_map_blocks(handle, mpd->inode, &map, get_blocks_flags); 16112fa3cdfbSTheodore Ts'o if (blks < 0) { 1612e3570639SEric Sandeen struct super_block *sb = mpd->inode->i_sb; 1613e3570639SEric Sandeen 16142fa3cdfbSTheodore Ts'o err = blks; 1615ed5bde0bSTheodore Ts'o /* 16165a87b7a5STheodore Ts'o * If get block returns EAGAIN or ENOSPC and there 161797498956STheodore Ts'o * appears to be free blocks we will just let 161897498956STheodore Ts'o * mpage_da_submit_io() unlock all of the pages. 1619c4a0c46eSAneesh Kumar K.V */ 1620c4a0c46eSAneesh Kumar K.V if (err == -EAGAIN) 16215a87b7a5STheodore Ts'o goto submit_io; 1622df22291fSAneesh Kumar K.V 16235dee5437STheodore Ts'o if (err == -ENOSPC && ext4_count_free_clusters(sb)) { 1624df22291fSAneesh Kumar K.V mpd->retval = err; 16255a87b7a5STheodore Ts'o goto submit_io; 1626df22291fSAneesh Kumar K.V } 1627df22291fSAneesh Kumar K.V 1628c4a0c46eSAneesh Kumar K.V /* 1629ed5bde0bSTheodore Ts'o * get block failure will cause us to loop in 1630ed5bde0bSTheodore Ts'o * writepages, because a_ops->writepage won't be able 1631ed5bde0bSTheodore Ts'o * to make progress. The page will be redirtied by 1632ed5bde0bSTheodore Ts'o * writepage and writepages will again try to write 1633ed5bde0bSTheodore Ts'o * the same. 1634c4a0c46eSAneesh Kumar K.V */ 1635e3570639SEric Sandeen if (!(EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) { 1636e3570639SEric Sandeen ext4_msg(sb, KERN_CRIT, 1637e3570639SEric Sandeen "delayed block allocation failed for inode %lu " 1638e3570639SEric Sandeen "at logical offset %llu with max blocks %zd " 1639e3570639SEric Sandeen "with error %d", mpd->inode->i_ino, 1640c4a0c46eSAneesh Kumar K.V (unsigned long long) next, 16418dc207c0STheodore Ts'o mpd->b_size >> mpd->inode->i_blkbits, err); 1642e3570639SEric Sandeen ext4_msg(sb, KERN_CRIT, 164301a523ebSTheodore Ts'o "This should not happen!! Data will be lost"); 1644e3570639SEric Sandeen if (err == -ENOSPC) 1645df22291fSAneesh Kumar K.V ext4_print_free_blocks(mpd->inode); 1646030ba6bcSAneesh Kumar K.V } 16472fa3cdfbSTheodore Ts'o /* invalidate all the pages */ 1648c7f5938aSCurt Wohlgemuth ext4_da_block_invalidatepages(mpd); 1649e0fd9b90SCurt Wohlgemuth 1650e0fd9b90SCurt Wohlgemuth /* Mark this page range as having been completed */ 1651e0fd9b90SCurt Wohlgemuth mpd->io_done = 1; 16525a87b7a5STheodore Ts'o return; 1653c4a0c46eSAneesh Kumar K.V } 16542fa3cdfbSTheodore Ts'o BUG_ON(blks == 0); 16552fa3cdfbSTheodore Ts'o 16561de3e3dfSTheodore Ts'o mapp = ↦ 16572ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 16582ed88685STheodore Ts'o struct block_device *bdev = mpd->inode->i_sb->s_bdev; 16592ed88685STheodore Ts'o int i; 166064769240SAlex Tomas 16612ed88685STheodore Ts'o for (i = 0; i < map.m_len; i++) 16622ed88685STheodore Ts'o unmap_underlying_metadata(bdev, map.m_pblk + i); 16632fa3cdfbSTheodore Ts'o } 16642fa3cdfbSTheodore Ts'o 16652fa3cdfbSTheodore Ts'o /* 166603f5d8bcSJan Kara * Update on-disk size along with block allocation. 16672fa3cdfbSTheodore Ts'o */ 16682fa3cdfbSTheodore Ts'o disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits; 16692fa3cdfbSTheodore Ts'o if (disksize > i_size_read(mpd->inode)) 16702fa3cdfbSTheodore Ts'o disksize = i_size_read(mpd->inode); 16712fa3cdfbSTheodore Ts'o if (disksize > EXT4_I(mpd->inode)->i_disksize) { 16722fa3cdfbSTheodore Ts'o ext4_update_i_disksize(mpd->inode, disksize); 16735a87b7a5STheodore Ts'o err = ext4_mark_inode_dirty(handle, mpd->inode); 16745a87b7a5STheodore Ts'o if (err) 16755a87b7a5STheodore Ts'o ext4_error(mpd->inode->i_sb, 16765a87b7a5STheodore Ts'o "Failed to mark inode %lu dirty", 16775a87b7a5STheodore Ts'o mpd->inode->i_ino); 16782fa3cdfbSTheodore Ts'o } 16792fa3cdfbSTheodore Ts'o 16805a87b7a5STheodore Ts'o submit_io: 16811de3e3dfSTheodore Ts'o mpage_da_submit_io(mpd, mapp); 16825a87b7a5STheodore Ts'o mpd->io_done = 1; 168364769240SAlex Tomas } 168464769240SAlex Tomas 1685bf068ee2SAneesh Kumar K.V #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \ 1686bf068ee2SAneesh Kumar K.V (1 << BH_Delay) | (1 << BH_Unwritten)) 168764769240SAlex Tomas 168864769240SAlex Tomas /* 168964769240SAlex Tomas * mpage_add_bh_to_extent - try to add one more block to extent of blocks 169064769240SAlex Tomas * 169164769240SAlex Tomas * @mpd->lbh - extent of blocks 169264769240SAlex Tomas * @logical - logical number of the block in the file 1693b6a8e62fSJan Kara * @b_state - b_state of the buffer head added 169464769240SAlex Tomas * 169564769240SAlex Tomas * the function is used to collect contig. blocks in same state 169664769240SAlex Tomas */ 1697b6a8e62fSJan Kara static void mpage_add_bh_to_extent(struct mpage_da_data *mpd, sector_t logical, 16988dc207c0STheodore Ts'o unsigned long b_state) 169964769240SAlex Tomas { 170064769240SAlex Tomas sector_t next; 1701b6a8e62fSJan Kara int blkbits = mpd->inode->i_blkbits; 1702b6a8e62fSJan Kara int nrblocks = mpd->b_size >> blkbits; 170364769240SAlex Tomas 1704c445e3e0SEric Sandeen /* 1705c445e3e0SEric Sandeen * XXX Don't go larger than mballoc is willing to allocate 1706c445e3e0SEric Sandeen * This is a stopgap solution. We eventually need to fold 1707c445e3e0SEric Sandeen * mpage_da_submit_io() into this function and then call 170879e83036SEric Sandeen * ext4_map_blocks() multiple times in a loop 1709c445e3e0SEric Sandeen */ 1710b6a8e62fSJan Kara if (nrblocks >= (8*1024*1024 >> blkbits)) 1711c445e3e0SEric Sandeen goto flush_it; 1712c445e3e0SEric Sandeen 1713525f4ed8SMingming Cao /* check if the reserved journal credits might overflow */ 1714b6a8e62fSJan Kara if (!ext4_test_inode_flag(mpd->inode, EXT4_INODE_EXTENTS)) { 1715525f4ed8SMingming Cao if (nrblocks >= EXT4_MAX_TRANS_DATA) { 1716525f4ed8SMingming Cao /* 1717525f4ed8SMingming Cao * With non-extent format we are limited by the journal 1718525f4ed8SMingming Cao * credit available. Total credit needed to insert 1719525f4ed8SMingming Cao * nrblocks contiguous blocks is dependent on the 1720525f4ed8SMingming Cao * nrblocks. So limit nrblocks. 1721525f4ed8SMingming Cao */ 1722525f4ed8SMingming Cao goto flush_it; 1723525f4ed8SMingming Cao } 1724525f4ed8SMingming Cao } 172564769240SAlex Tomas /* 172664769240SAlex Tomas * First block in the extent 172764769240SAlex Tomas */ 17288dc207c0STheodore Ts'o if (mpd->b_size == 0) { 17298dc207c0STheodore Ts'o mpd->b_blocknr = logical; 1730b6a8e62fSJan Kara mpd->b_size = 1 << blkbits; 17318dc207c0STheodore Ts'o mpd->b_state = b_state & BH_FLAGS; 173264769240SAlex Tomas return; 173364769240SAlex Tomas } 173464769240SAlex Tomas 17358dc207c0STheodore Ts'o next = mpd->b_blocknr + nrblocks; 173664769240SAlex Tomas /* 173764769240SAlex Tomas * Can we merge the block to our big extent? 173864769240SAlex Tomas */ 17398dc207c0STheodore Ts'o if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) { 1740b6a8e62fSJan Kara mpd->b_size += 1 << blkbits; 174164769240SAlex Tomas return; 174264769240SAlex Tomas } 174364769240SAlex Tomas 1744525f4ed8SMingming Cao flush_it: 174564769240SAlex Tomas /* 174664769240SAlex Tomas * We couldn't merge the block to our extent, so we 174764769240SAlex Tomas * need to flush current extent and start new one 174864769240SAlex Tomas */ 17495a87b7a5STheodore Ts'o mpage_da_map_and_submit(mpd); 1750a1d6cc56SAneesh Kumar K.V return; 175164769240SAlex Tomas } 175264769240SAlex Tomas 1753c364b22cSAneesh Kumar K.V static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh) 175429fa89d0SAneesh Kumar K.V { 1755c364b22cSAneesh Kumar K.V return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 175629fa89d0SAneesh Kumar K.V } 175729fa89d0SAneesh Kumar K.V 175864769240SAlex Tomas /* 17595356f261SAditya Kali * This function is grabs code from the very beginning of 17605356f261SAditya Kali * ext4_map_blocks, but assumes that the caller is from delayed write 17615356f261SAditya Kali * time. This function looks up the requested blocks and sets the 17625356f261SAditya Kali * buffer delay bit under the protection of i_data_sem. 17635356f261SAditya Kali */ 17645356f261SAditya Kali static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, 17655356f261SAditya Kali struct ext4_map_blocks *map, 17665356f261SAditya Kali struct buffer_head *bh) 17675356f261SAditya Kali { 1768*d100eef2SZheng Liu struct extent_status es; 17695356f261SAditya Kali int retval; 17705356f261SAditya Kali sector_t invalid_block = ~((sector_t) 0xffff); 17715356f261SAditya Kali 17725356f261SAditya Kali if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 17735356f261SAditya Kali invalid_block = ~0; 17745356f261SAditya Kali 17755356f261SAditya Kali map->m_flags = 0; 17765356f261SAditya Kali ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u," 17775356f261SAditya Kali "logical block %lu\n", inode->i_ino, map->m_len, 17785356f261SAditya Kali (unsigned long) map->m_lblk); 1779*d100eef2SZheng Liu 1780*d100eef2SZheng Liu /* Lookup extent status tree firstly */ 1781*d100eef2SZheng Liu if (ext4_es_lookup_extent(inode, iblock, &es)) { 1782*d100eef2SZheng Liu 1783*d100eef2SZheng Liu if (ext4_es_is_hole(&es)) { 1784*d100eef2SZheng Liu retval = 0; 1785*d100eef2SZheng Liu down_read((&EXT4_I(inode)->i_data_sem)); 1786*d100eef2SZheng Liu goto add_delayed; 1787*d100eef2SZheng Liu } 1788*d100eef2SZheng Liu 1789*d100eef2SZheng Liu /* 1790*d100eef2SZheng Liu * Delayed extent could be allocated by fallocate. 1791*d100eef2SZheng Liu * So we need to check it. 1792*d100eef2SZheng Liu */ 1793*d100eef2SZheng Liu if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) { 1794*d100eef2SZheng Liu map_bh(bh, inode->i_sb, invalid_block); 1795*d100eef2SZheng Liu set_buffer_new(bh); 1796*d100eef2SZheng Liu set_buffer_delay(bh); 1797*d100eef2SZheng Liu return 0; 1798*d100eef2SZheng Liu } 1799*d100eef2SZheng Liu 1800*d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk; 1801*d100eef2SZheng Liu retval = es.es_len - (iblock - es.es_lblk); 1802*d100eef2SZheng Liu if (retval > map->m_len) 1803*d100eef2SZheng Liu retval = map->m_len; 1804*d100eef2SZheng Liu map->m_len = retval; 1805*d100eef2SZheng Liu if (ext4_es_is_written(&es)) 1806*d100eef2SZheng Liu map->m_flags |= EXT4_MAP_MAPPED; 1807*d100eef2SZheng Liu else if (ext4_es_is_unwritten(&es)) 1808*d100eef2SZheng Liu map->m_flags |= EXT4_MAP_UNWRITTEN; 1809*d100eef2SZheng Liu else 1810*d100eef2SZheng Liu BUG_ON(1); 1811*d100eef2SZheng Liu 1812*d100eef2SZheng Liu return retval; 1813*d100eef2SZheng Liu } 1814*d100eef2SZheng Liu 18155356f261SAditya Kali /* 18165356f261SAditya Kali * Try to see if we can get the block without requesting a new 18175356f261SAditya Kali * file system block. 18185356f261SAditya Kali */ 18195356f261SAditya Kali down_read((&EXT4_I(inode)->i_data_sem)); 18209c3569b5STao Ma if (ext4_has_inline_data(inode)) { 18219c3569b5STao Ma /* 18229c3569b5STao Ma * We will soon create blocks for this page, and let 18239c3569b5STao Ma * us pretend as if the blocks aren't allocated yet. 18249c3569b5STao Ma * In case of clusters, we have to handle the work 18259c3569b5STao Ma * of mapping from cluster so that the reserved space 18269c3569b5STao Ma * is calculated properly. 18279c3569b5STao Ma */ 18289c3569b5STao Ma if ((EXT4_SB(inode->i_sb)->s_cluster_ratio > 1) && 18299c3569b5STao Ma ext4_find_delalloc_cluster(inode, map->m_lblk)) 18309c3569b5STao Ma map->m_flags |= EXT4_MAP_FROM_CLUSTER; 18319c3569b5STao Ma retval = 0; 18329c3569b5STao Ma } else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 1833*d100eef2SZheng Liu retval = ext4_ext_map_blocks(NULL, inode, map, 1834*d100eef2SZheng Liu EXT4_GET_BLOCKS_NO_PUT_HOLE); 18355356f261SAditya Kali else 1836*d100eef2SZheng Liu retval = ext4_ind_map_blocks(NULL, inode, map, 1837*d100eef2SZheng Liu EXT4_GET_BLOCKS_NO_PUT_HOLE); 18385356f261SAditya Kali 1839*d100eef2SZheng Liu add_delayed: 18405356f261SAditya Kali if (retval == 0) { 1841f7fec032SZheng Liu int ret; 18425356f261SAditya Kali /* 18435356f261SAditya Kali * XXX: __block_prepare_write() unmaps passed block, 18445356f261SAditya Kali * is it OK? 18455356f261SAditya Kali */ 18465356f261SAditya Kali /* If the block was allocated from previously allocated cluster, 18475356f261SAditya Kali * then we dont need to reserve it again. */ 18485356f261SAditya Kali if (!(map->m_flags & EXT4_MAP_FROM_CLUSTER)) { 1849f7fec032SZheng Liu ret = ext4_da_reserve_space(inode, iblock); 1850f7fec032SZheng Liu if (ret) { 18515356f261SAditya Kali /* not enough space to reserve */ 1852f7fec032SZheng Liu retval = ret; 18535356f261SAditya Kali goto out_unlock; 18545356f261SAditya Kali } 1855f7fec032SZheng Liu } 18565356f261SAditya Kali 1857f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 1858fdc0212eSZheng Liu ~0, EXTENT_STATUS_DELAYED); 1859f7fec032SZheng Liu if (ret) { 1860f7fec032SZheng Liu retval = ret; 186151865fdaSZheng Liu goto out_unlock; 1862f7fec032SZheng Liu } 186351865fdaSZheng Liu 18645356f261SAditya Kali /* Clear EXT4_MAP_FROM_CLUSTER flag since its purpose is served 18655356f261SAditya Kali * and it should not appear on the bh->b_state. 18665356f261SAditya Kali */ 18675356f261SAditya Kali map->m_flags &= ~EXT4_MAP_FROM_CLUSTER; 18685356f261SAditya Kali 18695356f261SAditya Kali map_bh(bh, inode->i_sb, invalid_block); 18705356f261SAditya Kali set_buffer_new(bh); 18715356f261SAditya Kali set_buffer_delay(bh); 1872f7fec032SZheng Liu } else if (retval > 0) { 1873f7fec032SZheng Liu int ret; 1874f7fec032SZheng Liu unsigned long long status; 1875f7fec032SZheng Liu 1876f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 1877f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 1878f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 1879f7fec032SZheng Liu map->m_pblk, status); 1880f7fec032SZheng Liu if (ret != 0) 1881f7fec032SZheng Liu retval = ret; 18825356f261SAditya Kali } 18835356f261SAditya Kali 18845356f261SAditya Kali out_unlock: 18855356f261SAditya Kali up_read((&EXT4_I(inode)->i_data_sem)); 18865356f261SAditya Kali 18875356f261SAditya Kali return retval; 18885356f261SAditya Kali } 18895356f261SAditya Kali 18905356f261SAditya Kali /* 1891b920c755STheodore Ts'o * This is a special get_blocks_t callback which is used by 1892b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 1893b920c755STheodore Ts'o * reserve space for a single block. 189429fa89d0SAneesh Kumar K.V * 189529fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 189629fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 189729fa89d0SAneesh Kumar K.V * 189829fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 189929fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 190029fa89d0SAneesh Kumar K.V * initialized properly. 190164769240SAlex Tomas */ 19029c3569b5STao Ma int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 19032ed88685STheodore Ts'o struct buffer_head *bh, int create) 190464769240SAlex Tomas { 19052ed88685STheodore Ts'o struct ext4_map_blocks map; 190664769240SAlex Tomas int ret = 0; 190764769240SAlex Tomas 190864769240SAlex Tomas BUG_ON(create == 0); 19092ed88685STheodore Ts'o BUG_ON(bh->b_size != inode->i_sb->s_blocksize); 19102ed88685STheodore Ts'o 19112ed88685STheodore Ts'o map.m_lblk = iblock; 19122ed88685STheodore Ts'o map.m_len = 1; 191364769240SAlex Tomas 191464769240SAlex Tomas /* 191564769240SAlex Tomas * first, we need to know whether the block is allocated already 191664769240SAlex Tomas * preallocated blocks are unmapped but should treated 191764769240SAlex Tomas * the same as allocated blocks. 191864769240SAlex Tomas */ 19195356f261SAditya Kali ret = ext4_da_map_blocks(inode, iblock, &map, bh); 19205356f261SAditya Kali if (ret <= 0) 19212ed88685STheodore Ts'o return ret; 192264769240SAlex Tomas 19232ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 19242ed88685STheodore Ts'o bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; 19252ed88685STheodore Ts'o 19262ed88685STheodore Ts'o if (buffer_unwritten(bh)) { 19272ed88685STheodore Ts'o /* A delayed write to unwritten bh should be marked 19282ed88685STheodore Ts'o * new and mapped. Mapped ensures that we don't do 19292ed88685STheodore Ts'o * get_block multiple times when we write to the same 19302ed88685STheodore Ts'o * offset and new ensures that we do proper zero out 19312ed88685STheodore Ts'o * for partial write. 19322ed88685STheodore Ts'o */ 19332ed88685STheodore Ts'o set_buffer_new(bh); 1934c8205636STheodore Ts'o set_buffer_mapped(bh); 19352ed88685STheodore Ts'o } 19362ed88685STheodore Ts'o return 0; 193764769240SAlex Tomas } 193861628a3fSMingming Cao 193962e086beSAneesh Kumar K.V static int bget_one(handle_t *handle, struct buffer_head *bh) 194062e086beSAneesh Kumar K.V { 194162e086beSAneesh Kumar K.V get_bh(bh); 194262e086beSAneesh Kumar K.V return 0; 194362e086beSAneesh Kumar K.V } 194462e086beSAneesh Kumar K.V 194562e086beSAneesh Kumar K.V static int bput_one(handle_t *handle, struct buffer_head *bh) 194662e086beSAneesh Kumar K.V { 194762e086beSAneesh Kumar K.V put_bh(bh); 194862e086beSAneesh Kumar K.V return 0; 194962e086beSAneesh Kumar K.V } 195062e086beSAneesh Kumar K.V 195162e086beSAneesh Kumar K.V static int __ext4_journalled_writepage(struct page *page, 195262e086beSAneesh Kumar K.V unsigned int len) 195362e086beSAneesh Kumar K.V { 195462e086beSAneesh Kumar K.V struct address_space *mapping = page->mapping; 195562e086beSAneesh Kumar K.V struct inode *inode = mapping->host; 19563fdcfb66STao Ma struct buffer_head *page_bufs = NULL; 195762e086beSAneesh Kumar K.V handle_t *handle = NULL; 19583fdcfb66STao Ma int ret = 0, err = 0; 19593fdcfb66STao Ma int inline_data = ext4_has_inline_data(inode); 19603fdcfb66STao Ma struct buffer_head *inode_bh = NULL; 196162e086beSAneesh Kumar K.V 1962cb20d518STheodore Ts'o ClearPageChecked(page); 19633fdcfb66STao Ma 19643fdcfb66STao Ma if (inline_data) { 19653fdcfb66STao Ma BUG_ON(page->index != 0); 19663fdcfb66STao Ma BUG_ON(len > ext4_get_max_inline_size(inode)); 19673fdcfb66STao Ma inode_bh = ext4_journalled_write_inline_data(inode, len, page); 19683fdcfb66STao Ma if (inode_bh == NULL) 19693fdcfb66STao Ma goto out; 19703fdcfb66STao Ma } else { 197162e086beSAneesh Kumar K.V page_bufs = page_buffers(page); 19723fdcfb66STao Ma if (!page_bufs) { 19733fdcfb66STao Ma BUG(); 19743fdcfb66STao Ma goto out; 19753fdcfb66STao Ma } 19763fdcfb66STao Ma ext4_walk_page_buffers(handle, page_bufs, 0, len, 19773fdcfb66STao Ma NULL, bget_one); 19783fdcfb66STao Ma } 197962e086beSAneesh Kumar K.V /* As soon as we unlock the page, it can go away, but we have 198062e086beSAneesh Kumar K.V * references to buffers so we are safe */ 198162e086beSAneesh Kumar K.V unlock_page(page); 198262e086beSAneesh Kumar K.V 19839924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 19849924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 198562e086beSAneesh Kumar K.V if (IS_ERR(handle)) { 198662e086beSAneesh Kumar K.V ret = PTR_ERR(handle); 198762e086beSAneesh Kumar K.V goto out; 198862e086beSAneesh Kumar K.V } 198962e086beSAneesh Kumar K.V 1990441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1991441c8508SCurt Wohlgemuth 19923fdcfb66STao Ma if (inline_data) { 19933fdcfb66STao Ma ret = ext4_journal_get_write_access(handle, inode_bh); 19943fdcfb66STao Ma 19953fdcfb66STao Ma err = ext4_handle_dirty_metadata(handle, inode, inode_bh); 19963fdcfb66STao Ma 19973fdcfb66STao Ma } else { 1998f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 199962e086beSAneesh Kumar K.V do_journal_get_write_access); 200062e086beSAneesh Kumar K.V 2001f19d5870STao Ma err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 200262e086beSAneesh Kumar K.V write_end_fn); 20033fdcfb66STao Ma } 200462e086beSAneesh Kumar K.V if (ret == 0) 200562e086beSAneesh Kumar K.V ret = err; 20062d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 200762e086beSAneesh Kumar K.V err = ext4_journal_stop(handle); 200862e086beSAneesh Kumar K.V if (!ret) 200962e086beSAneesh Kumar K.V ret = err; 201062e086beSAneesh Kumar K.V 20113fdcfb66STao Ma if (!ext4_has_inline_data(inode)) 20123fdcfb66STao Ma ext4_walk_page_buffers(handle, page_bufs, 0, len, 20133fdcfb66STao Ma NULL, bput_one); 201419f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 201562e086beSAneesh Kumar K.V out: 20163fdcfb66STao Ma brelse(inode_bh); 201762e086beSAneesh Kumar K.V return ret; 201862e086beSAneesh Kumar K.V } 201962e086beSAneesh Kumar K.V 202061628a3fSMingming Cao /* 202143ce1d23SAneesh Kumar K.V * Note that we don't need to start a transaction unless we're journaling data 202243ce1d23SAneesh Kumar K.V * because we should have holes filled from ext4_page_mkwrite(). We even don't 202343ce1d23SAneesh Kumar K.V * need to file the inode to the transaction's list in ordered mode because if 202443ce1d23SAneesh Kumar K.V * we are writing back data added by write(), the inode is already there and if 202543ce1d23SAneesh Kumar K.V * we are writing back data modified via mmap(), no one guarantees in which 202643ce1d23SAneesh Kumar K.V * transaction the data will hit the disk. In case we are journaling data, we 202743ce1d23SAneesh Kumar K.V * cannot start transaction directly because transaction start ranks above page 202843ce1d23SAneesh Kumar K.V * lock so we have to do some magic. 202943ce1d23SAneesh Kumar K.V * 2030b920c755STheodore Ts'o * This function can get called via... 2031b920c755STheodore Ts'o * - ext4_da_writepages after taking page lock (have journal handle) 2032b920c755STheodore Ts'o * - journal_submit_inode_data_buffers (no journal handle) 2033f6463b0dSArtem Bityutskiy * - shrink_page_list via the kswapd/direct reclaim (no journal handle) 2034b920c755STheodore Ts'o * - grab_page_cache when doing write_begin (have journal handle) 203543ce1d23SAneesh Kumar K.V * 203643ce1d23SAneesh Kumar K.V * We don't do any block allocation in this function. If we have page with 203743ce1d23SAneesh Kumar K.V * multiple blocks we need to write those buffer_heads that are mapped. This 203843ce1d23SAneesh Kumar K.V * is important for mmaped based write. So if we do with blocksize 1K 203943ce1d23SAneesh Kumar K.V * truncate(f, 1024); 204043ce1d23SAneesh Kumar K.V * a = mmap(f, 0, 4096); 204143ce1d23SAneesh Kumar K.V * a[0] = 'a'; 204243ce1d23SAneesh Kumar K.V * truncate(f, 4096); 204343ce1d23SAneesh Kumar K.V * we have in the page first buffer_head mapped via page_mkwrite call back 204490802ed9SPaul Bolle * but other buffer_heads would be unmapped but dirty (dirty done via the 204543ce1d23SAneesh Kumar K.V * do_wp_page). So writepage should write the first block. If we modify 204643ce1d23SAneesh Kumar K.V * the mmap area beyond 1024 we will again get a page_fault and the 204743ce1d23SAneesh Kumar K.V * page_mkwrite callback will do the block allocation and mark the 204843ce1d23SAneesh Kumar K.V * buffer_heads mapped. 204943ce1d23SAneesh Kumar K.V * 205043ce1d23SAneesh Kumar K.V * We redirty the page if we have any buffer_heads that is either delay or 205143ce1d23SAneesh Kumar K.V * unwritten in the page. 205243ce1d23SAneesh Kumar K.V * 205343ce1d23SAneesh Kumar K.V * We can get recursively called as show below. 205443ce1d23SAneesh Kumar K.V * 205543ce1d23SAneesh Kumar K.V * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> 205643ce1d23SAneesh Kumar K.V * ext4_writepage() 205743ce1d23SAneesh Kumar K.V * 205843ce1d23SAneesh Kumar K.V * But since we don't do any block allocation we should not deadlock. 205943ce1d23SAneesh Kumar K.V * Page also have the dirty flag cleared so we don't get recurive page_lock. 206061628a3fSMingming Cao */ 206143ce1d23SAneesh Kumar K.V static int ext4_writepage(struct page *page, 206264769240SAlex Tomas struct writeback_control *wbc) 206364769240SAlex Tomas { 2064f8bec370SJan Kara int ret = 0; 206561628a3fSMingming Cao loff_t size; 2066498e5f24STheodore Ts'o unsigned int len; 2067744692dcSJiaying Zhang struct buffer_head *page_bufs = NULL; 206861628a3fSMingming Cao struct inode *inode = page->mapping->host; 206936ade451SJan Kara struct ext4_io_submit io_submit; 207064769240SAlex Tomas 2071a9c667f8SLukas Czerner trace_ext4_writepage(page); 207261628a3fSMingming Cao size = i_size_read(inode); 207361628a3fSMingming Cao if (page->index == size >> PAGE_CACHE_SHIFT) 207461628a3fSMingming Cao len = size & ~PAGE_CACHE_MASK; 207561628a3fSMingming Cao else 207661628a3fSMingming Cao len = PAGE_CACHE_SIZE; 207761628a3fSMingming Cao 2078f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 2079fe386132SJan Kara /* 2080fe386132SJan Kara * We cannot do block allocation or other extent handling in this 2081fe386132SJan Kara * function. If there are buffers needing that, we have to redirty 2082fe386132SJan Kara * the page. But we may reach here when we do a journal commit via 2083fe386132SJan Kara * journal_submit_inode_data_buffers() and in that case we must write 2084fe386132SJan Kara * allocated buffers to achieve data=ordered mode guarantees. 2085fe386132SJan Kara */ 2086f19d5870STao Ma if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2087c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 2088f8bec370SJan Kara redirty_page_for_writepage(wbc, page); 2089fe386132SJan Kara if (current->flags & PF_MEMALLOC) { 2090fe386132SJan Kara /* 2091fe386132SJan Kara * For memory cleaning there's no point in writing only 2092fe386132SJan Kara * some buffers. So just bail out. Warn if we came here 2093fe386132SJan Kara * from direct reclaim. 2094fe386132SJan Kara */ 2095fe386132SJan Kara WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) 2096fe386132SJan Kara == PF_MEMALLOC); 2097f8bec370SJan Kara unlock_page(page); 2098f8bec370SJan Kara return 0; 2099f0e6c985SAneesh Kumar K.V } 2100fe386132SJan Kara } 210164769240SAlex Tomas 2102cb20d518STheodore Ts'o if (PageChecked(page) && ext4_should_journal_data(inode)) 210343ce1d23SAneesh Kumar K.V /* 210443ce1d23SAneesh Kumar K.V * It's mmapped pagecache. Add buffers and journal it. There 210543ce1d23SAneesh Kumar K.V * doesn't seem much point in redirtying the page here. 210643ce1d23SAneesh Kumar K.V */ 21073f0ca309SWu Fengguang return __ext4_journalled_writepage(page, len); 210843ce1d23SAneesh Kumar K.V 210936ade451SJan Kara memset(&io_submit, 0, sizeof(io_submit)); 211036ade451SJan Kara ret = ext4_bio_write_page(&io_submit, page, len, wbc); 211136ade451SJan Kara ext4_io_submit(&io_submit); 211264769240SAlex Tomas return ret; 211364769240SAlex Tomas } 211464769240SAlex Tomas 211561628a3fSMingming Cao /* 2116525f4ed8SMingming Cao * This is called via ext4_da_writepages() to 211725985edcSLucas De Marchi * calculate the total number of credits to reserve to fit 2118525f4ed8SMingming Cao * a single extent allocation into a single transaction, 2119525f4ed8SMingming Cao * ext4_da_writpeages() will loop calling this before 2120525f4ed8SMingming Cao * the block allocation. 212161628a3fSMingming Cao */ 2122525f4ed8SMingming Cao 2123525f4ed8SMingming Cao static int ext4_da_writepages_trans_blocks(struct inode *inode) 2124525f4ed8SMingming Cao { 2125525f4ed8SMingming Cao int max_blocks = EXT4_I(inode)->i_reserved_data_blocks; 2126525f4ed8SMingming Cao 2127525f4ed8SMingming Cao /* 2128525f4ed8SMingming Cao * With non-extent format the journal credit needed to 2129525f4ed8SMingming Cao * insert nrblocks contiguous block is dependent on 2130525f4ed8SMingming Cao * number of contiguous block. So we will limit 2131525f4ed8SMingming Cao * number of contiguous block to a sane value 2132525f4ed8SMingming Cao */ 213312e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) && 2134525f4ed8SMingming Cao (max_blocks > EXT4_MAX_TRANS_DATA)) 2135525f4ed8SMingming Cao max_blocks = EXT4_MAX_TRANS_DATA; 2136525f4ed8SMingming Cao 2137525f4ed8SMingming Cao return ext4_chunk_trans_blocks(inode, max_blocks); 2138525f4ed8SMingming Cao } 213961628a3fSMingming Cao 21408e48dcfbSTheodore Ts'o /* 21418e48dcfbSTheodore Ts'o * write_cache_pages_da - walk the list of dirty pages of the given 21428eb9e5ceSTheodore Ts'o * address space and accumulate pages that need writing, and call 2143168fc022STheodore Ts'o * mpage_da_map_and_submit to map a single contiguous memory region 2144168fc022STheodore Ts'o * and then write them. 21458e48dcfbSTheodore Ts'o */ 21469c3569b5STao Ma static int write_cache_pages_da(handle_t *handle, 21479c3569b5STao Ma struct address_space *mapping, 21488e48dcfbSTheodore Ts'o struct writeback_control *wbc, 214972f84e65SEric Sandeen struct mpage_da_data *mpd, 215072f84e65SEric Sandeen pgoff_t *done_index) 21518e48dcfbSTheodore Ts'o { 21528eb9e5ceSTheodore Ts'o struct buffer_head *bh, *head; 2153168fc022STheodore Ts'o struct inode *inode = mapping->host; 21548e48dcfbSTheodore Ts'o struct pagevec pvec; 21554f01b02cSTheodore Ts'o unsigned int nr_pages; 21564f01b02cSTheodore Ts'o sector_t logical; 21574f01b02cSTheodore Ts'o pgoff_t index, end; 21588e48dcfbSTheodore Ts'o long nr_to_write = wbc->nr_to_write; 21594f01b02cSTheodore Ts'o int i, tag, ret = 0; 21608e48dcfbSTheodore Ts'o 2161168fc022STheodore Ts'o memset(mpd, 0, sizeof(struct mpage_da_data)); 2162168fc022STheodore Ts'o mpd->wbc = wbc; 2163168fc022STheodore Ts'o mpd->inode = inode; 21648e48dcfbSTheodore Ts'o pagevec_init(&pvec, 0); 21658e48dcfbSTheodore Ts'o index = wbc->range_start >> PAGE_CACHE_SHIFT; 21668e48dcfbSTheodore Ts'o end = wbc->range_end >> PAGE_CACHE_SHIFT; 21678e48dcfbSTheodore Ts'o 21686e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 21695b41d924SEric Sandeen tag = PAGECACHE_TAG_TOWRITE; 21705b41d924SEric Sandeen else 21715b41d924SEric Sandeen tag = PAGECACHE_TAG_DIRTY; 21725b41d924SEric Sandeen 217372f84e65SEric Sandeen *done_index = index; 21744f01b02cSTheodore Ts'o while (index <= end) { 21755b41d924SEric Sandeen nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, 21768e48dcfbSTheodore Ts'o min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); 21778e48dcfbSTheodore Ts'o if (nr_pages == 0) 21784f01b02cSTheodore Ts'o return 0; 21798e48dcfbSTheodore Ts'o 21808e48dcfbSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 21818e48dcfbSTheodore Ts'o struct page *page = pvec.pages[i]; 21828e48dcfbSTheodore Ts'o 21838e48dcfbSTheodore Ts'o /* 21848e48dcfbSTheodore Ts'o * At this point, the page may be truncated or 21858e48dcfbSTheodore Ts'o * invalidated (changing page->mapping to NULL), or 21868e48dcfbSTheodore Ts'o * even swizzled back from swapper_space to tmpfs file 21878e48dcfbSTheodore Ts'o * mapping. However, page->index will not change 21888e48dcfbSTheodore Ts'o * because we have a reference on the page. 21898e48dcfbSTheodore Ts'o */ 21904f01b02cSTheodore Ts'o if (page->index > end) 21914f01b02cSTheodore Ts'o goto out; 21928e48dcfbSTheodore Ts'o 219372f84e65SEric Sandeen *done_index = page->index + 1; 219472f84e65SEric Sandeen 219578aaced3STheodore Ts'o /* 219678aaced3STheodore Ts'o * If we can't merge this page, and we have 219778aaced3STheodore Ts'o * accumulated an contiguous region, write it 219878aaced3STheodore Ts'o */ 219978aaced3STheodore Ts'o if ((mpd->next_page != page->index) && 220078aaced3STheodore Ts'o (mpd->next_page != mpd->first_page)) { 220178aaced3STheodore Ts'o mpage_da_map_and_submit(mpd); 220278aaced3STheodore Ts'o goto ret_extent_tail; 220378aaced3STheodore Ts'o } 220478aaced3STheodore Ts'o 22058e48dcfbSTheodore Ts'o lock_page(page); 22068e48dcfbSTheodore Ts'o 22078e48dcfbSTheodore Ts'o /* 22084f01b02cSTheodore Ts'o * If the page is no longer dirty, or its 22094f01b02cSTheodore Ts'o * mapping no longer corresponds to inode we 22104f01b02cSTheodore Ts'o * are writing (which means it has been 22114f01b02cSTheodore Ts'o * truncated or invalidated), or the page is 22124f01b02cSTheodore Ts'o * already under writeback and we are not 22134f01b02cSTheodore Ts'o * doing a data integrity writeback, skip the page 22148e48dcfbSTheodore Ts'o */ 22154f01b02cSTheodore Ts'o if (!PageDirty(page) || 22164f01b02cSTheodore Ts'o (PageWriteback(page) && 22174f01b02cSTheodore Ts'o (wbc->sync_mode == WB_SYNC_NONE)) || 22184f01b02cSTheodore Ts'o unlikely(page->mapping != mapping)) { 22198e48dcfbSTheodore Ts'o unlock_page(page); 22208e48dcfbSTheodore Ts'o continue; 22218e48dcfbSTheodore Ts'o } 22228e48dcfbSTheodore Ts'o 22238e48dcfbSTheodore Ts'o wait_on_page_writeback(page); 22248e48dcfbSTheodore Ts'o BUG_ON(PageWriteback(page)); 22258e48dcfbSTheodore Ts'o 22269c3569b5STao Ma /* 22279c3569b5STao Ma * If we have inline data and arrive here, it means that 22289c3569b5STao Ma * we will soon create the block for the 1st page, so 22299c3569b5STao Ma * we'd better clear the inline data here. 22309c3569b5STao Ma */ 22319c3569b5STao Ma if (ext4_has_inline_data(inode)) { 22329c3569b5STao Ma BUG_ON(ext4_test_inode_state(inode, 22339c3569b5STao Ma EXT4_STATE_MAY_INLINE_DATA)); 22349c3569b5STao Ma ext4_destroy_inline_data(handle, inode); 22359c3569b5STao Ma } 22369c3569b5STao Ma 2237168fc022STheodore Ts'o if (mpd->next_page != page->index) 22388eb9e5ceSTheodore Ts'o mpd->first_page = page->index; 22398eb9e5ceSTheodore Ts'o mpd->next_page = page->index + 1; 22408eb9e5ceSTheodore Ts'o logical = (sector_t) page->index << 22418eb9e5ceSTheodore Ts'o (PAGE_CACHE_SHIFT - inode->i_blkbits); 22428eb9e5ceSTheodore Ts'o 2243f8bec370SJan Kara /* Add all dirty buffers to mpd */ 22448eb9e5ceSTheodore Ts'o head = page_buffers(page); 22458eb9e5ceSTheodore Ts'o bh = head; 22468eb9e5ceSTheodore Ts'o do { 22478eb9e5ceSTheodore Ts'o BUG_ON(buffer_locked(bh)); 22488eb9e5ceSTheodore Ts'o /* 2249f8bec370SJan Kara * We need to try to allocate unmapped blocks 2250f8bec370SJan Kara * in the same page. Otherwise we won't make 2251f8bec370SJan Kara * progress with the page in ext4_writepage 22528eb9e5ceSTheodore Ts'o */ 22538eb9e5ceSTheodore Ts'o if (ext4_bh_delay_or_unwritten(NULL, bh)) { 22548eb9e5ceSTheodore Ts'o mpage_add_bh_to_extent(mpd, logical, 22558eb9e5ceSTheodore Ts'o bh->b_state); 22564f01b02cSTheodore Ts'o if (mpd->io_done) 22574f01b02cSTheodore Ts'o goto ret_extent_tail; 2258f8bec370SJan Kara } else if (buffer_dirty(bh) && 2259f8bec370SJan Kara buffer_mapped(bh)) { 22608eb9e5ceSTheodore Ts'o /* 2261f8bec370SJan Kara * mapped dirty buffer. We need to 2262f8bec370SJan Kara * update the b_state because we look 2263f8bec370SJan Kara * at b_state in mpage_da_map_blocks. 2264f8bec370SJan Kara * We don't update b_size because if we 2265f8bec370SJan Kara * find an unmapped buffer_head later 2266f8bec370SJan Kara * we need to use the b_state flag of 2267f8bec370SJan Kara * that buffer_head. 22688eb9e5ceSTheodore Ts'o */ 22698eb9e5ceSTheodore Ts'o if (mpd->b_size == 0) 2270f8bec370SJan Kara mpd->b_state = 2271f8bec370SJan Kara bh->b_state & BH_FLAGS; 22728e48dcfbSTheodore Ts'o } 22738eb9e5ceSTheodore Ts'o logical++; 22748eb9e5ceSTheodore Ts'o } while ((bh = bh->b_this_page) != head); 22758e48dcfbSTheodore Ts'o 22768e48dcfbSTheodore Ts'o if (nr_to_write > 0) { 22778e48dcfbSTheodore Ts'o nr_to_write--; 22788e48dcfbSTheodore Ts'o if (nr_to_write == 0 && 22794f01b02cSTheodore Ts'o wbc->sync_mode == WB_SYNC_NONE) 22808e48dcfbSTheodore Ts'o /* 22818e48dcfbSTheodore Ts'o * We stop writing back only if we are 22828e48dcfbSTheodore Ts'o * not doing integrity sync. In case of 22838e48dcfbSTheodore Ts'o * integrity sync we have to keep going 22848e48dcfbSTheodore Ts'o * because someone may be concurrently 22858e48dcfbSTheodore Ts'o * dirtying pages, and we might have 22868e48dcfbSTheodore Ts'o * synced a lot of newly appeared dirty 22878e48dcfbSTheodore Ts'o * pages, but have not synced all of the 22888e48dcfbSTheodore Ts'o * old dirty pages. 22898e48dcfbSTheodore Ts'o */ 22904f01b02cSTheodore Ts'o goto out; 22918e48dcfbSTheodore Ts'o } 22928e48dcfbSTheodore Ts'o } 22938e48dcfbSTheodore Ts'o pagevec_release(&pvec); 22948e48dcfbSTheodore Ts'o cond_resched(); 22958e48dcfbSTheodore Ts'o } 22964f01b02cSTheodore Ts'o return 0; 22974f01b02cSTheodore Ts'o ret_extent_tail: 22984f01b02cSTheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 22998eb9e5ceSTheodore Ts'o out: 23008eb9e5ceSTheodore Ts'o pagevec_release(&pvec); 23018eb9e5ceSTheodore Ts'o cond_resched(); 23028e48dcfbSTheodore Ts'o return ret; 23038e48dcfbSTheodore Ts'o } 23048e48dcfbSTheodore Ts'o 23058e48dcfbSTheodore Ts'o 230664769240SAlex Tomas static int ext4_da_writepages(struct address_space *mapping, 230764769240SAlex Tomas struct writeback_control *wbc) 230864769240SAlex Tomas { 230922208dedSAneesh Kumar K.V pgoff_t index; 231022208dedSAneesh Kumar K.V int range_whole = 0; 231161628a3fSMingming Cao handle_t *handle = NULL; 2312df22291fSAneesh Kumar K.V struct mpage_da_data mpd; 23135e745b04SAneesh Kumar K.V struct inode *inode = mapping->host; 2314498e5f24STheodore Ts'o int pages_written = 0; 231555138e0bSTheodore Ts'o unsigned int max_pages; 23162acf2c26SAneesh Kumar K.V int range_cyclic, cycled = 1, io_done = 0; 231755138e0bSTheodore Ts'o int needed_blocks, ret = 0; 231855138e0bSTheodore Ts'o long desired_nr_to_write, nr_to_writebump = 0; 2319de89de6eSTheodore Ts'o loff_t range_start = wbc->range_start; 23205e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 232172f84e65SEric Sandeen pgoff_t done_index = 0; 23225b41d924SEric Sandeen pgoff_t end; 23231bce63d1SShaohua Li struct blk_plug plug; 232461628a3fSMingming Cao 23259bffad1eSTheodore Ts'o trace_ext4_da_writepages(inode, wbc); 2326ba80b101STheodore Ts'o 232761628a3fSMingming Cao /* 232861628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 232961628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 233061628a3fSMingming Cao * because that could violate lock ordering on umount 233161628a3fSMingming Cao */ 2332a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 233361628a3fSMingming Cao return 0; 23342a21e37eSTheodore Ts'o 23352a21e37eSTheodore Ts'o /* 23362a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 23372a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 23382a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 23394ab2f15bSTheodore Ts'o * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because 23402a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 23412a21e37eSTheodore Ts'o * read-only, and in that case, ext4_da_writepages should 23422a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 23432a21e37eSTheodore Ts'o * the stack trace. 23442a21e37eSTheodore Ts'o */ 23454ab2f15bSTheodore Ts'o if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) 23462a21e37eSTheodore Ts'o return -EROFS; 23472a21e37eSTheodore Ts'o 234822208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 234922208dedSAneesh Kumar K.V range_whole = 1; 235061628a3fSMingming Cao 23512acf2c26SAneesh Kumar K.V range_cyclic = wbc->range_cyclic; 23522acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 235322208dedSAneesh Kumar K.V index = mapping->writeback_index; 23542acf2c26SAneesh Kumar K.V if (index) 23552acf2c26SAneesh Kumar K.V cycled = 0; 23562acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 23572acf2c26SAneesh Kumar K.V wbc->range_end = LLONG_MAX; 23582acf2c26SAneesh Kumar K.V wbc->range_cyclic = 0; 23595b41d924SEric Sandeen end = -1; 23605b41d924SEric Sandeen } else { 236122208dedSAneesh Kumar K.V index = wbc->range_start >> PAGE_CACHE_SHIFT; 23625b41d924SEric Sandeen end = wbc->range_end >> PAGE_CACHE_SHIFT; 23635b41d924SEric Sandeen } 2364a1d6cc56SAneesh Kumar K.V 236555138e0bSTheodore Ts'o /* 236655138e0bSTheodore Ts'o * This works around two forms of stupidity. The first is in 236755138e0bSTheodore Ts'o * the writeback code, which caps the maximum number of pages 236855138e0bSTheodore Ts'o * written to be 1024 pages. This is wrong on multiple 236955138e0bSTheodore Ts'o * levels; different architectues have a different page size, 237055138e0bSTheodore Ts'o * which changes the maximum amount of data which gets 237155138e0bSTheodore Ts'o * written. Secondly, 4 megabytes is way too small. XFS 237255138e0bSTheodore Ts'o * forces this value to be 16 megabytes by multiplying 237355138e0bSTheodore Ts'o * nr_to_write parameter by four, and then relies on its 237455138e0bSTheodore Ts'o * allocator to allocate larger extents to make them 237555138e0bSTheodore Ts'o * contiguous. Unfortunately this brings us to the second 237655138e0bSTheodore Ts'o * stupidity, which is that ext4's mballoc code only allocates 237755138e0bSTheodore Ts'o * at most 2048 blocks. So we force contiguous writes up to 237855138e0bSTheodore Ts'o * the number of dirty blocks in the inode, or 237955138e0bSTheodore Ts'o * sbi->max_writeback_mb_bump whichever is smaller. 238055138e0bSTheodore Ts'o */ 238155138e0bSTheodore Ts'o max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT); 2382b443e733SEric Sandeen if (!range_cyclic && range_whole) { 2383b443e733SEric Sandeen if (wbc->nr_to_write == LONG_MAX) 2384b443e733SEric Sandeen desired_nr_to_write = wbc->nr_to_write; 238555138e0bSTheodore Ts'o else 2386b443e733SEric Sandeen desired_nr_to_write = wbc->nr_to_write * 8; 2387b443e733SEric Sandeen } else 238855138e0bSTheodore Ts'o desired_nr_to_write = ext4_num_dirty_pages(inode, index, 238955138e0bSTheodore Ts'o max_pages); 239055138e0bSTheodore Ts'o if (desired_nr_to_write > max_pages) 239155138e0bSTheodore Ts'o desired_nr_to_write = max_pages; 239255138e0bSTheodore Ts'o 239355138e0bSTheodore Ts'o if (wbc->nr_to_write < desired_nr_to_write) { 239455138e0bSTheodore Ts'o nr_to_writebump = desired_nr_to_write - wbc->nr_to_write; 239555138e0bSTheodore Ts'o wbc->nr_to_write = desired_nr_to_write; 239655138e0bSTheodore Ts'o } 239755138e0bSTheodore Ts'o 23982acf2c26SAneesh Kumar K.V retry: 23996e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 24005b41d924SEric Sandeen tag_pages_for_writeback(mapping, index, end); 24015b41d924SEric Sandeen 24021bce63d1SShaohua Li blk_start_plug(&plug); 240322208dedSAneesh Kumar K.V while (!ret && wbc->nr_to_write > 0) { 2404a1d6cc56SAneesh Kumar K.V 2405a1d6cc56SAneesh Kumar K.V /* 2406a1d6cc56SAneesh Kumar K.V * we insert one extent at a time. So we need 2407a1d6cc56SAneesh Kumar K.V * credit needed for single extent allocation. 2408a1d6cc56SAneesh Kumar K.V * journalled mode is currently not supported 2409a1d6cc56SAneesh Kumar K.V * by delalloc 2410a1d6cc56SAneesh Kumar K.V */ 2411a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 2412525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 2413a1d6cc56SAneesh Kumar K.V 241461628a3fSMingming Cao /* start a new transaction*/ 24159924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 24169924a92aSTheodore Ts'o needed_blocks); 241761628a3fSMingming Cao if (IS_ERR(handle)) { 241861628a3fSMingming Cao ret = PTR_ERR(handle); 24191693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2420fbe845ddSCurt Wohlgemuth "%ld pages, ino %lu; err %d", __func__, 2421a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 24223c1fcb2cSNamjae Jeon blk_finish_plug(&plug); 242361628a3fSMingming Cao goto out_writepages; 242461628a3fSMingming Cao } 2425f63e6005STheodore Ts'o 2426f63e6005STheodore Ts'o /* 24278eb9e5ceSTheodore Ts'o * Now call write_cache_pages_da() to find the next 2428f63e6005STheodore Ts'o * contiguous region of logical blocks that need 24298eb9e5ceSTheodore Ts'o * blocks to be allocated by ext4 and submit them. 2430f63e6005STheodore Ts'o */ 24319c3569b5STao Ma ret = write_cache_pages_da(handle, mapping, 24329c3569b5STao Ma wbc, &mpd, &done_index); 2433f63e6005STheodore Ts'o /* 2434af901ca1SAndré Goddard Rosa * If we have a contiguous extent of pages and we 2435f63e6005STheodore Ts'o * haven't done the I/O yet, map the blocks and submit 2436f63e6005STheodore Ts'o * them for I/O. 2437f63e6005STheodore Ts'o */ 2438f63e6005STheodore Ts'o if (!mpd.io_done && mpd.next_page != mpd.first_page) { 24395a87b7a5STheodore Ts'o mpage_da_map_and_submit(&mpd); 2440f63e6005STheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 2441f63e6005STheodore Ts'o } 2442b3a3ca8cSTheodore Ts'o trace_ext4_da_write_pages(inode, &mpd); 2443f63e6005STheodore Ts'o wbc->nr_to_write -= mpd.pages_written; 2444df22291fSAneesh Kumar K.V 244561628a3fSMingming Cao ext4_journal_stop(handle); 2446df22291fSAneesh Kumar K.V 24478f64b32eSEric Sandeen if ((mpd.retval == -ENOSPC) && sbi->s_journal) { 244822208dedSAneesh Kumar K.V /* commit the transaction which would 244922208dedSAneesh Kumar K.V * free blocks released in the transaction 245022208dedSAneesh Kumar K.V * and try again 245122208dedSAneesh Kumar K.V */ 2452df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 245322208dedSAneesh Kumar K.V ret = 0; 245422208dedSAneesh Kumar K.V } else if (ret == MPAGE_DA_EXTENT_TAIL) { 2455a1d6cc56SAneesh Kumar K.V /* 24568de49e67SKazuya Mio * Got one extent now try with rest of the pages. 24578de49e67SKazuya Mio * If mpd.retval is set -EIO, journal is aborted. 24588de49e67SKazuya Mio * So we don't need to write any more. 2459a1d6cc56SAneesh Kumar K.V */ 246022208dedSAneesh Kumar K.V pages_written += mpd.pages_written; 24618de49e67SKazuya Mio ret = mpd.retval; 24622acf2c26SAneesh Kumar K.V io_done = 1; 246322208dedSAneesh Kumar K.V } else if (wbc->nr_to_write) 246461628a3fSMingming Cao /* 246561628a3fSMingming Cao * There is no more writeout needed 246661628a3fSMingming Cao * or we requested for a noblocking writeout 246761628a3fSMingming Cao * and we found the device congested 246861628a3fSMingming Cao */ 246961628a3fSMingming Cao break; 247061628a3fSMingming Cao } 24711bce63d1SShaohua Li blk_finish_plug(&plug); 24722acf2c26SAneesh Kumar K.V if (!io_done && !cycled) { 24732acf2c26SAneesh Kumar K.V cycled = 1; 24742acf2c26SAneesh Kumar K.V index = 0; 24752acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 24762acf2c26SAneesh Kumar K.V wbc->range_end = mapping->writeback_index - 1; 24772acf2c26SAneesh Kumar K.V goto retry; 24782acf2c26SAneesh Kumar K.V } 247961628a3fSMingming Cao 248022208dedSAneesh Kumar K.V /* Update index */ 24812acf2c26SAneesh Kumar K.V wbc->range_cyclic = range_cyclic; 248222208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 248322208dedSAneesh Kumar K.V /* 248422208dedSAneesh Kumar K.V * set the writeback_index so that range_cyclic 248522208dedSAneesh Kumar K.V * mode will write it back later 248622208dedSAneesh Kumar K.V */ 248772f84e65SEric Sandeen mapping->writeback_index = done_index; 2488a1d6cc56SAneesh Kumar K.V 248961628a3fSMingming Cao out_writepages: 249022208dedSAneesh Kumar K.V wbc->nr_to_write -= nr_to_writebump; 2491de89de6eSTheodore Ts'o wbc->range_start = range_start; 24929bffad1eSTheodore Ts'o trace_ext4_da_writepages_result(inode, wbc, ret, pages_written); 249361628a3fSMingming Cao return ret; 249464769240SAlex Tomas } 249564769240SAlex Tomas 249679f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 249779f0be8dSAneesh Kumar K.V { 249879f0be8dSAneesh Kumar K.V s64 free_blocks, dirty_blocks; 249979f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 250079f0be8dSAneesh Kumar K.V 250179f0be8dSAneesh Kumar K.V /* 250279f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 250379f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 2504179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 250579f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 250679f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 250779f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 250879f0be8dSAneesh Kumar K.V */ 250957042651STheodore Ts'o free_blocks = EXT4_C2B(sbi, 251057042651STheodore Ts'o percpu_counter_read_positive(&sbi->s_freeclusters_counter)); 251157042651STheodore Ts'o dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyclusters_counter); 251200d4e736STheodore Ts'o /* 251300d4e736STheodore Ts'o * Start pushing delalloc when 1/2 of free blocks are dirty. 251400d4e736STheodore Ts'o */ 251500d4e736STheodore Ts'o if (dirty_blocks && (free_blocks < 2 * dirty_blocks) && 251600d4e736STheodore Ts'o !writeback_in_progress(sb->s_bdi) && 251700d4e736STheodore Ts'o down_read_trylock(&sb->s_umount)) { 251800d4e736STheodore Ts'o writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE); 251900d4e736STheodore Ts'o up_read(&sb->s_umount); 252000d4e736STheodore Ts'o } 252100d4e736STheodore Ts'o 252279f0be8dSAneesh Kumar K.V if (2 * free_blocks < 3 * dirty_blocks || 2523df55c99dSTheodore Ts'o free_blocks < (dirty_blocks + EXT4_FREECLUSTERS_WATERMARK)) { 252479f0be8dSAneesh Kumar K.V /* 2525c8afb446SEric Sandeen * free block count is less than 150% of dirty blocks 2526c8afb446SEric Sandeen * or free blocks is less than watermark 252779f0be8dSAneesh Kumar K.V */ 252879f0be8dSAneesh Kumar K.V return 1; 252979f0be8dSAneesh Kumar K.V } 253079f0be8dSAneesh Kumar K.V return 0; 253179f0be8dSAneesh Kumar K.V } 253279f0be8dSAneesh Kumar K.V 253364769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 253464769240SAlex Tomas loff_t pos, unsigned len, unsigned flags, 253564769240SAlex Tomas struct page **pagep, void **fsdata) 253664769240SAlex Tomas { 253772b8ab9dSEric Sandeen int ret, retries = 0; 253864769240SAlex Tomas struct page *page; 253964769240SAlex Tomas pgoff_t index; 254064769240SAlex Tomas struct inode *inode = mapping->host; 254164769240SAlex Tomas handle_t *handle; 254264769240SAlex Tomas 254364769240SAlex Tomas index = pos >> PAGE_CACHE_SHIFT; 254479f0be8dSAneesh Kumar K.V 254579f0be8dSAneesh Kumar K.V if (ext4_nonda_switch(inode->i_sb)) { 254679f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 254779f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 254879f0be8dSAneesh Kumar K.V len, flags, pagep, fsdata); 254979f0be8dSAneesh Kumar K.V } 255079f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 25519bffad1eSTheodore Ts'o trace_ext4_da_write_begin(inode, pos, len, flags); 25529c3569b5STao Ma 25539c3569b5STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 25549c3569b5STao Ma ret = ext4_da_write_inline_data_begin(mapping, inode, 25559c3569b5STao Ma pos, len, flags, 25569c3569b5STao Ma pagep, fsdata); 25579c3569b5STao Ma if (ret < 0) 255847564bfbSTheodore Ts'o return ret; 255947564bfbSTheodore Ts'o if (ret == 1) 256047564bfbSTheodore Ts'o return 0; 25619c3569b5STao Ma } 25629c3569b5STao Ma 256347564bfbSTheodore Ts'o /* 256447564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 256547564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 256647564bfbSTheodore Ts'o * is being written back. So grab it first before we start 256747564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 256847564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 256947564bfbSTheodore Ts'o */ 257047564bfbSTheodore Ts'o retry_grab: 257147564bfbSTheodore Ts'o page = grab_cache_page_write_begin(mapping, index, flags); 257247564bfbSTheodore Ts'o if (!page) 257347564bfbSTheodore Ts'o return -ENOMEM; 257447564bfbSTheodore Ts'o unlock_page(page); 257547564bfbSTheodore Ts'o 257664769240SAlex Tomas /* 257764769240SAlex Tomas * With delayed allocation, we don't log the i_disksize update 257864769240SAlex Tomas * if there is delayed block allocation. But we still need 257964769240SAlex Tomas * to journalling the i_disksize update if writes to the end 258064769240SAlex Tomas * of file which has an already mapped buffer. 258164769240SAlex Tomas */ 258247564bfbSTheodore Ts'o retry_journal: 25839924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 1); 258464769240SAlex Tomas if (IS_ERR(handle)) { 258547564bfbSTheodore Ts'o page_cache_release(page); 258647564bfbSTheodore Ts'o return PTR_ERR(handle); 258764769240SAlex Tomas } 258864769240SAlex Tomas 258947564bfbSTheodore Ts'o lock_page(page); 259047564bfbSTheodore Ts'o if (page->mapping != mapping) { 259147564bfbSTheodore Ts'o /* The page got truncated from under us */ 259247564bfbSTheodore Ts'o unlock_page(page); 259347564bfbSTheodore Ts'o page_cache_release(page); 2594d5a0d4f7SEric Sandeen ext4_journal_stop(handle); 259547564bfbSTheodore Ts'o goto retry_grab; 2596d5a0d4f7SEric Sandeen } 259747564bfbSTheodore Ts'o /* In case writeback began while the page was unlocked */ 259847564bfbSTheodore Ts'o wait_on_page_writeback(page); 259964769240SAlex Tomas 26006e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep); 260164769240SAlex Tomas if (ret < 0) { 260264769240SAlex Tomas unlock_page(page); 260364769240SAlex Tomas ext4_journal_stop(handle); 2604ae4d5372SAneesh Kumar K.V /* 2605ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 2606ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 2607ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 2608ae4d5372SAneesh Kumar K.V */ 2609ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 2610b9a4207dSJan Kara ext4_truncate_failed_write(inode); 261147564bfbSTheodore Ts'o 261247564bfbSTheodore Ts'o if (ret == -ENOSPC && 261347564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 261447564bfbSTheodore Ts'o goto retry_journal; 261547564bfbSTheodore Ts'o 261647564bfbSTheodore Ts'o page_cache_release(page); 261747564bfbSTheodore Ts'o return ret; 261864769240SAlex Tomas } 261964769240SAlex Tomas 262047564bfbSTheodore Ts'o *pagep = page; 262164769240SAlex Tomas return ret; 262264769240SAlex Tomas } 262364769240SAlex Tomas 2624632eaeabSMingming Cao /* 2625632eaeabSMingming Cao * Check if we should update i_disksize 2626632eaeabSMingming Cao * when write to the end of file but not require block allocation 2627632eaeabSMingming Cao */ 2628632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page, 2629632eaeabSMingming Cao unsigned long offset) 2630632eaeabSMingming Cao { 2631632eaeabSMingming Cao struct buffer_head *bh; 2632632eaeabSMingming Cao struct inode *inode = page->mapping->host; 2633632eaeabSMingming Cao unsigned int idx; 2634632eaeabSMingming Cao int i; 2635632eaeabSMingming Cao 2636632eaeabSMingming Cao bh = page_buffers(page); 2637632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 2638632eaeabSMingming Cao 2639632eaeabSMingming Cao for (i = 0; i < idx; i++) 2640632eaeabSMingming Cao bh = bh->b_this_page; 2641632eaeabSMingming Cao 264229fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 2643632eaeabSMingming Cao return 0; 2644632eaeabSMingming Cao return 1; 2645632eaeabSMingming Cao } 2646632eaeabSMingming Cao 264764769240SAlex Tomas static int ext4_da_write_end(struct file *file, 264864769240SAlex Tomas struct address_space *mapping, 264964769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 265064769240SAlex Tomas struct page *page, void *fsdata) 265164769240SAlex Tomas { 265264769240SAlex Tomas struct inode *inode = mapping->host; 265364769240SAlex Tomas int ret = 0, ret2; 265464769240SAlex Tomas handle_t *handle = ext4_journal_current_handle(); 265564769240SAlex Tomas loff_t new_i_size; 2656632eaeabSMingming Cao unsigned long start, end; 265779f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 265879f0be8dSAneesh Kumar K.V 265979f0be8dSAneesh Kumar K.V if (write_mode == FALL_BACK_TO_NONDELALLOC) { 26603d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 26613d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 266279f0be8dSAneesh Kumar K.V return ext4_ordered_write_end(file, mapping, pos, 266379f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 26643d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 266579f0be8dSAneesh Kumar K.V return ext4_writeback_write_end(file, mapping, pos, 266679f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 26673d2b1582SLukas Czerner default: 266879f0be8dSAneesh Kumar K.V BUG(); 266979f0be8dSAneesh Kumar K.V } 267079f0be8dSAneesh Kumar K.V } 2671632eaeabSMingming Cao 26729bffad1eSTheodore Ts'o trace_ext4_da_write_end(inode, pos, len, copied); 2673632eaeabSMingming Cao start = pos & (PAGE_CACHE_SIZE - 1); 2674632eaeabSMingming Cao end = start + copied - 1; 267564769240SAlex Tomas 267664769240SAlex Tomas /* 267764769240SAlex Tomas * generic_write_end() will run mark_inode_dirty() if i_size 267864769240SAlex Tomas * changes. So let's piggyback the i_disksize mark_inode_dirty 267964769240SAlex Tomas * into that. 268064769240SAlex Tomas */ 268164769240SAlex Tomas new_i_size = pos + copied; 2682ea51d132SAndrea Arcangeli if (copied && new_i_size > EXT4_I(inode)->i_disksize) { 26839c3569b5STao Ma if (ext4_has_inline_data(inode) || 26849c3569b5STao Ma ext4_da_should_update_i_disksize(page, end)) { 2685632eaeabSMingming Cao down_write(&EXT4_I(inode)->i_data_sem); 2686f3b59291STheodore Ts'o if (new_i_size > EXT4_I(inode)->i_disksize) 268764769240SAlex Tomas EXT4_I(inode)->i_disksize = new_i_size; 2688632eaeabSMingming Cao up_write(&EXT4_I(inode)->i_data_sem); 2689cf17fea6SAneesh Kumar K.V /* We need to mark inode dirty even if 2690cf17fea6SAneesh Kumar K.V * new_i_size is less that inode->i_size 2691cf17fea6SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 2692cf17fea6SAneesh Kumar K.V */ 2693cf17fea6SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 2694632eaeabSMingming Cao } 2695632eaeabSMingming Cao } 26969c3569b5STao Ma 26979c3569b5STao Ma if (write_mode != CONVERT_INLINE_DATA && 26989c3569b5STao Ma ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) && 26999c3569b5STao Ma ext4_has_inline_data(inode)) 27009c3569b5STao Ma ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied, 27019c3569b5STao Ma page); 27029c3569b5STao Ma else 270364769240SAlex Tomas ret2 = generic_write_end(file, mapping, pos, len, copied, 270464769240SAlex Tomas page, fsdata); 27059c3569b5STao Ma 270664769240SAlex Tomas copied = ret2; 270764769240SAlex Tomas if (ret2 < 0) 270864769240SAlex Tomas ret = ret2; 270964769240SAlex Tomas ret2 = ext4_journal_stop(handle); 271064769240SAlex Tomas if (!ret) 271164769240SAlex Tomas ret = ret2; 271264769240SAlex Tomas 271364769240SAlex Tomas return ret ? ret : copied; 271464769240SAlex Tomas } 271564769240SAlex Tomas 271664769240SAlex Tomas static void ext4_da_invalidatepage(struct page *page, unsigned long offset) 271764769240SAlex Tomas { 271864769240SAlex Tomas /* 271964769240SAlex Tomas * Drop reserved blocks 272064769240SAlex Tomas */ 272164769240SAlex Tomas BUG_ON(!PageLocked(page)); 272264769240SAlex Tomas if (!page_has_buffers(page)) 272364769240SAlex Tomas goto out; 272464769240SAlex Tomas 2725d2a17637SMingming Cao ext4_da_page_release_reservation(page, offset); 272664769240SAlex Tomas 272764769240SAlex Tomas out: 272864769240SAlex Tomas ext4_invalidatepage(page, offset); 272964769240SAlex Tomas 273064769240SAlex Tomas return; 273164769240SAlex Tomas } 273264769240SAlex Tomas 2733ccd2506bSTheodore Ts'o /* 2734ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 2735ccd2506bSTheodore Ts'o */ 2736ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 2737ccd2506bSTheodore Ts'o { 2738fb40ba0dSTheodore Ts'o trace_ext4_alloc_da_blocks(inode); 2739fb40ba0dSTheodore Ts'o 2740ccd2506bSTheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks && 2741ccd2506bSTheodore Ts'o !EXT4_I(inode)->i_reserved_meta_blocks) 2742ccd2506bSTheodore Ts'o return 0; 2743ccd2506bSTheodore Ts'o 2744ccd2506bSTheodore Ts'o /* 2745ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 2746ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 2747ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 2748ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 2749ccd2506bSTheodore Ts'o * would require replicating code paths in: 2750ccd2506bSTheodore Ts'o * 2751ccd2506bSTheodore Ts'o * ext4_da_writepages() -> 2752ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 2753ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 2754ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 2755ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 2756ccd2506bSTheodore Ts'o * 2757ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 2758ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 2759ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 2760ccd2506bSTheodore Ts'o * doing I/O at all. 2761ccd2506bSTheodore Ts'o * 2762ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 2763380cf090SWu Fengguang * the pages by calling redirty_page_for_writepage() but that 2764ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 2765ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 276625985edcSLucas De Marchi * simplifying them because we wouldn't actually intend to 2767ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 2768ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 2769ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 2770ccd2506bSTheodore Ts'o * 2771ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 2772ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 2773ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 2774ccd2506bSTheodore Ts'o */ 2775ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 2776ccd2506bSTheodore Ts'o } 277764769240SAlex Tomas 277864769240SAlex Tomas /* 2779ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 2780ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 2781ac27a0ecSDave Kleikamp * 2782ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 2783617ba13bSMingming Cao * journal. If somebody makes a swapfile on an ext4 data-journaling 2784ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 2785ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 2786ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 2787ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 2788ac27a0ecSDave Kleikamp * 2789ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 2790ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 2791ac27a0ecSDave Kleikamp */ 2792617ba13bSMingming Cao static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 2793ac27a0ecSDave Kleikamp { 2794ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 2795ac27a0ecSDave Kleikamp journal_t *journal; 2796ac27a0ecSDave Kleikamp int err; 2797ac27a0ecSDave Kleikamp 279846c7f254STao Ma /* 279946c7f254STao Ma * We can get here for an inline file via the FIBMAP ioctl 280046c7f254STao Ma */ 280146c7f254STao Ma if (ext4_has_inline_data(inode)) 280246c7f254STao Ma return 0; 280346c7f254STao Ma 280464769240SAlex Tomas if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 280564769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) { 280664769240SAlex Tomas /* 280764769240SAlex Tomas * With delalloc we want to sync the file 280864769240SAlex Tomas * so that we can make sure we allocate 280964769240SAlex Tomas * blocks for file 281064769240SAlex Tomas */ 281164769240SAlex Tomas filemap_write_and_wait(mapping); 281264769240SAlex Tomas } 281364769240SAlex Tomas 281419f5fb7aSTheodore Ts'o if (EXT4_JOURNAL(inode) && 281519f5fb7aSTheodore Ts'o ext4_test_inode_state(inode, EXT4_STATE_JDATA)) { 2816ac27a0ecSDave Kleikamp /* 2817ac27a0ecSDave Kleikamp * This is a REALLY heavyweight approach, but the use of 2818ac27a0ecSDave Kleikamp * bmap on dirty files is expected to be extremely rare: 2819ac27a0ecSDave Kleikamp * only if we run lilo or swapon on a freshly made file 2820ac27a0ecSDave Kleikamp * do we expect this to happen. 2821ac27a0ecSDave Kleikamp * 2822ac27a0ecSDave Kleikamp * (bmap requires CAP_SYS_RAWIO so this does not 2823ac27a0ecSDave Kleikamp * represent an unprivileged user DOS attack --- we'd be 2824ac27a0ecSDave Kleikamp * in trouble if mortal users could trigger this path at 2825ac27a0ecSDave Kleikamp * will.) 2826ac27a0ecSDave Kleikamp * 2827617ba13bSMingming Cao * NB. EXT4_STATE_JDATA is not set on files other than 2828ac27a0ecSDave Kleikamp * regular files. If somebody wants to bmap a directory 2829ac27a0ecSDave Kleikamp * or symlink and gets confused because the buffer 2830ac27a0ecSDave Kleikamp * hasn't yet been flushed to disk, they deserve 2831ac27a0ecSDave Kleikamp * everything they get. 2832ac27a0ecSDave Kleikamp */ 2833ac27a0ecSDave Kleikamp 283419f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_JDATA); 2835617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 2836dab291afSMingming Cao jbd2_journal_lock_updates(journal); 2837dab291afSMingming Cao err = jbd2_journal_flush(journal); 2838dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 2839ac27a0ecSDave Kleikamp 2840ac27a0ecSDave Kleikamp if (err) 2841ac27a0ecSDave Kleikamp return 0; 2842ac27a0ecSDave Kleikamp } 2843ac27a0ecSDave Kleikamp 2844617ba13bSMingming Cao return generic_block_bmap(mapping, block, ext4_get_block); 2845ac27a0ecSDave Kleikamp } 2846ac27a0ecSDave Kleikamp 2847617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page) 2848ac27a0ecSDave Kleikamp { 284946c7f254STao Ma int ret = -EAGAIN; 285046c7f254STao Ma struct inode *inode = page->mapping->host; 285146c7f254STao Ma 28520562e0baSJiaying Zhang trace_ext4_readpage(page); 285346c7f254STao Ma 285446c7f254STao Ma if (ext4_has_inline_data(inode)) 285546c7f254STao Ma ret = ext4_readpage_inline(inode, page); 285646c7f254STao Ma 285746c7f254STao Ma if (ret == -EAGAIN) 2858617ba13bSMingming Cao return mpage_readpage(page, ext4_get_block); 285946c7f254STao Ma 286046c7f254STao Ma return ret; 2861ac27a0ecSDave Kleikamp } 2862ac27a0ecSDave Kleikamp 2863ac27a0ecSDave Kleikamp static int 2864617ba13bSMingming Cao ext4_readpages(struct file *file, struct address_space *mapping, 2865ac27a0ecSDave Kleikamp struct list_head *pages, unsigned nr_pages) 2866ac27a0ecSDave Kleikamp { 286746c7f254STao Ma struct inode *inode = mapping->host; 286846c7f254STao Ma 286946c7f254STao Ma /* If the file has inline data, no need to do readpages. */ 287046c7f254STao Ma if (ext4_has_inline_data(inode)) 287146c7f254STao Ma return 0; 287246c7f254STao Ma 2873617ba13bSMingming Cao return mpage_readpages(mapping, pages, nr_pages, ext4_get_block); 2874ac27a0ecSDave Kleikamp } 2875ac27a0ecSDave Kleikamp 2876617ba13bSMingming Cao static void ext4_invalidatepage(struct page *page, unsigned long offset) 2877ac27a0ecSDave Kleikamp { 28780562e0baSJiaying Zhang trace_ext4_invalidatepage(page, offset); 28790562e0baSJiaying Zhang 28804520fb3cSJan Kara /* No journalling happens on data buffers when this function is used */ 28814520fb3cSJan Kara WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page))); 28824520fb3cSJan Kara 28834520fb3cSJan Kara block_invalidatepage(page, offset); 28844520fb3cSJan Kara } 28854520fb3cSJan Kara 288653e87268SJan Kara static int __ext4_journalled_invalidatepage(struct page *page, 28874520fb3cSJan Kara unsigned long offset) 28884520fb3cSJan Kara { 28894520fb3cSJan Kara journal_t *journal = EXT4_JOURNAL(page->mapping->host); 28904520fb3cSJan Kara 28914520fb3cSJan Kara trace_ext4_journalled_invalidatepage(page, offset); 28924520fb3cSJan Kara 2893744692dcSJiaying Zhang /* 2894ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 2895ac27a0ecSDave Kleikamp */ 2896ac27a0ecSDave Kleikamp if (offset == 0) 2897ac27a0ecSDave Kleikamp ClearPageChecked(page); 2898ac27a0ecSDave Kleikamp 289953e87268SJan Kara return jbd2_journal_invalidatepage(journal, page, offset); 290053e87268SJan Kara } 290153e87268SJan Kara 290253e87268SJan Kara /* Wrapper for aops... */ 290353e87268SJan Kara static void ext4_journalled_invalidatepage(struct page *page, 290453e87268SJan Kara unsigned long offset) 290553e87268SJan Kara { 290653e87268SJan Kara WARN_ON(__ext4_journalled_invalidatepage(page, offset) < 0); 2907ac27a0ecSDave Kleikamp } 2908ac27a0ecSDave Kleikamp 2909617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait) 2910ac27a0ecSDave Kleikamp { 2911617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 2912ac27a0ecSDave Kleikamp 29130562e0baSJiaying Zhang trace_ext4_releasepage(page); 29140562e0baSJiaying Zhang 2915ac27a0ecSDave Kleikamp WARN_ON(PageChecked(page)); 2916ac27a0ecSDave Kleikamp if (!page_has_buffers(page)) 2917ac27a0ecSDave Kleikamp return 0; 29180390131bSFrank Mayhar if (journal) 2919dab291afSMingming Cao return jbd2_journal_try_to_free_buffers(journal, page, wait); 29200390131bSFrank Mayhar else 29210390131bSFrank Mayhar return try_to_free_buffers(page); 2922ac27a0ecSDave Kleikamp } 2923ac27a0ecSDave Kleikamp 2924ac27a0ecSDave Kleikamp /* 29252ed88685STheodore Ts'o * ext4_get_block used when preparing for a DIO write or buffer write. 29262ed88685STheodore Ts'o * We allocate an uinitialized extent if blocks haven't been allocated. 29272ed88685STheodore Ts'o * The extent will be converted to initialized after the IO is complete. 29282ed88685STheodore Ts'o */ 2929f19d5870STao Ma int ext4_get_block_write(struct inode *inode, sector_t iblock, 29304c0425ffSMingming Cao struct buffer_head *bh_result, int create) 29314c0425ffSMingming Cao { 2932c7064ef1SJiaying Zhang ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n", 29338d5d02e6SMingming Cao inode->i_ino, create); 29342ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh_result, 29352ed88685STheodore Ts'o EXT4_GET_BLOCKS_IO_CREATE_EXT); 29364c0425ffSMingming Cao } 29374c0425ffSMingming Cao 2938729f52c6SZheng Liu static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock, 29398b0f165fSAnatol Pomozov struct buffer_head *bh_result, int create) 2940729f52c6SZheng Liu { 29418b0f165fSAnatol Pomozov ext4_debug("ext4_get_block_write_nolock: inode %lu, create flag %d\n", 29428b0f165fSAnatol Pomozov inode->i_ino, create); 29438b0f165fSAnatol Pomozov return _ext4_get_block(inode, iblock, bh_result, 29448b0f165fSAnatol Pomozov EXT4_GET_BLOCKS_NO_LOCK); 2945729f52c6SZheng Liu } 2946729f52c6SZheng Liu 29474c0425ffSMingming Cao static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset, 2948552ef802SChristoph Hellwig ssize_t size, void *private, int ret, 2949552ef802SChristoph Hellwig bool is_async) 29504c0425ffSMingming Cao { 295172c5052dSChristoph Hellwig struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode; 29524c0425ffSMingming Cao ext4_io_end_t *io_end = iocb->private; 29534c0425ffSMingming Cao 29544b70df18SMingming /* if not async direct IO or dio with 0 bytes write, just return */ 29554b70df18SMingming if (!io_end || !size) 2956552ef802SChristoph Hellwig goto out; 29574b70df18SMingming 29588d5d02e6SMingming Cao ext_debug("ext4_end_io_dio(): io_end 0x%p " 2959ace36ad4SJoe Perches "for inode %lu, iocb 0x%p, offset %llu, size %zd\n", 29608d5d02e6SMingming Cao iocb->private, io_end->inode->i_ino, iocb, offset, 29618d5d02e6SMingming Cao size); 29628d5d02e6SMingming Cao 2963b5a7e970STheodore Ts'o iocb->private = NULL; 2964b5a7e970STheodore Ts'o 29658d5d02e6SMingming Cao /* if not aio dio with unwritten extents, just free io and return */ 2966bd2d0210STheodore Ts'o if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) { 29678d5d02e6SMingming Cao ext4_free_io_end(io_end); 29685b3ff237Sjiayingz@google.com (Jiaying Zhang) out: 2969091e26dfSJan Kara inode_dio_done(inode); 29705b3ff237Sjiayingz@google.com (Jiaying Zhang) if (is_async) 29715b3ff237Sjiayingz@google.com (Jiaying Zhang) aio_complete(iocb, ret, 0); 29725b3ff237Sjiayingz@google.com (Jiaying Zhang) return; 29738d5d02e6SMingming Cao } 29748d5d02e6SMingming Cao 29754c0425ffSMingming Cao io_end->offset = offset; 29764c0425ffSMingming Cao io_end->size = size; 29775b3ff237Sjiayingz@google.com (Jiaying Zhang) if (is_async) { 29785b3ff237Sjiayingz@google.com (Jiaying Zhang) io_end->iocb = iocb; 29795b3ff237Sjiayingz@google.com (Jiaying Zhang) io_end->result = ret; 29805b3ff237Sjiayingz@google.com (Jiaying Zhang) } 29814c0425ffSMingming Cao 298228a535f9SDmitry Monakhov ext4_add_complete_io(io_end); 29834c0425ffSMingming Cao } 2984c7064ef1SJiaying Zhang 29854c0425ffSMingming Cao /* 29864c0425ffSMingming Cao * For ext4 extent files, ext4 will do direct-io write to holes, 29874c0425ffSMingming Cao * preallocated extents, and those write extend the file, no need to 29884c0425ffSMingming Cao * fall back to buffered IO. 29894c0425ffSMingming Cao * 2990b595076aSUwe Kleine-König * For holes, we fallocate those blocks, mark them as uninitialized 299169c499d1STheodore Ts'o * If those blocks were preallocated, we mark sure they are split, but 2992b595076aSUwe Kleine-König * still keep the range to write as uninitialized. 29934c0425ffSMingming Cao * 299469c499d1STheodore Ts'o * The unwritten extents will be converted to written when DIO is completed. 29958d5d02e6SMingming Cao * For async direct IO, since the IO may still pending when return, we 299625985edcSLucas De Marchi * set up an end_io call back function, which will do the conversion 29978d5d02e6SMingming Cao * when async direct IO completed. 29984c0425ffSMingming Cao * 29994c0425ffSMingming Cao * If the O_DIRECT write will extend the file then add this inode to the 30004c0425ffSMingming Cao * orphan list. So recovery will truncate it back to the original size 30014c0425ffSMingming Cao * if the machine crashes during the write. 30024c0425ffSMingming Cao * 30034c0425ffSMingming Cao */ 30044c0425ffSMingming Cao static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb, 30054c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 30064c0425ffSMingming Cao unsigned long nr_segs) 30074c0425ffSMingming Cao { 30084c0425ffSMingming Cao struct file *file = iocb->ki_filp; 30094c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 30104c0425ffSMingming Cao ssize_t ret; 30114c0425ffSMingming Cao size_t count = iov_length(iov, nr_segs); 3012729f52c6SZheng Liu int overwrite = 0; 30138b0f165fSAnatol Pomozov get_block_t *get_block_func = NULL; 30148b0f165fSAnatol Pomozov int dio_flags = 0; 301569c499d1STheodore Ts'o loff_t final_size = offset + count; 301669c499d1STheodore Ts'o 301769c499d1STheodore Ts'o /* Use the old path for reads and writes beyond i_size. */ 301869c499d1STheodore Ts'o if (rw != WRITE || final_size > inode->i_size) 301969c499d1STheodore Ts'o return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 3020729f52c6SZheng Liu 30214bd809dbSZheng Liu BUG_ON(iocb->private == NULL); 30224bd809dbSZheng Liu 30234bd809dbSZheng Liu /* If we do a overwrite dio, i_mutex locking can be released */ 30244bd809dbSZheng Liu overwrite = *((int *)iocb->private); 30254bd809dbSZheng Liu 30264bd809dbSZheng Liu if (overwrite) { 30271f555cfaSDmitry Monakhov atomic_inc(&inode->i_dio_count); 30284bd809dbSZheng Liu down_read(&EXT4_I(inode)->i_data_sem); 30294bd809dbSZheng Liu mutex_unlock(&inode->i_mutex); 30304bd809dbSZheng Liu } 30314bd809dbSZheng Liu 30324c0425ffSMingming Cao /* 30338d5d02e6SMingming Cao * We could direct write to holes and fallocate. 30348d5d02e6SMingming Cao * 303569c499d1STheodore Ts'o * Allocated blocks to fill the hole are marked as 303669c499d1STheodore Ts'o * uninitialized to prevent parallel buffered read to expose 303769c499d1STheodore Ts'o * the stale data before DIO complete the data IO. 30388d5d02e6SMingming Cao * 303969c499d1STheodore Ts'o * As to previously fallocated extents, ext4 get_block will 304069c499d1STheodore Ts'o * just simply mark the buffer mapped but still keep the 304169c499d1STheodore Ts'o * extents uninitialized. 30424c0425ffSMingming Cao * 304369c499d1STheodore Ts'o * For non AIO case, we will convert those unwritten extents 30448d5d02e6SMingming Cao * to written after return back from blockdev_direct_IO. 30454c0425ffSMingming Cao * 304669c499d1STheodore Ts'o * For async DIO, the conversion needs to be deferred when the 304769c499d1STheodore Ts'o * IO is completed. The ext4 end_io callback function will be 304869c499d1STheodore Ts'o * called to take care of the conversion work. Here for async 304969c499d1STheodore Ts'o * case, we allocate an io_end structure to hook to the iocb. 30504c0425ffSMingming Cao */ 30518d5d02e6SMingming Cao iocb->private = NULL; 3052f45ee3a1SDmitry Monakhov ext4_inode_aio_set(inode, NULL); 30538d5d02e6SMingming Cao if (!is_sync_kiocb(iocb)) { 305469c499d1STheodore Ts'o ext4_io_end_t *io_end = ext4_init_io_end(inode, GFP_NOFS); 30554bd809dbSZheng Liu if (!io_end) { 30564bd809dbSZheng Liu ret = -ENOMEM; 30574bd809dbSZheng Liu goto retake_lock; 30584bd809dbSZheng Liu } 3059266991b1SJeff Moyer io_end->flag |= EXT4_IO_END_DIRECT; 3060266991b1SJeff Moyer iocb->private = io_end; 30618d5d02e6SMingming Cao /* 306269c499d1STheodore Ts'o * we save the io structure for current async direct 306369c499d1STheodore Ts'o * IO, so that later ext4_map_blocks() could flag the 306469c499d1STheodore Ts'o * io structure whether there is a unwritten extents 306569c499d1STheodore Ts'o * needs to be converted when IO is completed. 30668d5d02e6SMingming Cao */ 3067f45ee3a1SDmitry Monakhov ext4_inode_aio_set(inode, io_end); 30688d5d02e6SMingming Cao } 30698d5d02e6SMingming Cao 30708b0f165fSAnatol Pomozov if (overwrite) { 30718b0f165fSAnatol Pomozov get_block_func = ext4_get_block_write_nolock; 30728b0f165fSAnatol Pomozov } else { 30738b0f165fSAnatol Pomozov get_block_func = ext4_get_block_write; 30748b0f165fSAnatol Pomozov dio_flags = DIO_LOCKING; 30758b0f165fSAnatol Pomozov } 3076729f52c6SZheng Liu ret = __blockdev_direct_IO(rw, iocb, inode, 3077729f52c6SZheng Liu inode->i_sb->s_bdev, iov, 3078729f52c6SZheng Liu offset, nr_segs, 30798b0f165fSAnatol Pomozov get_block_func, 3080729f52c6SZheng Liu ext4_end_io_dio, 3081729f52c6SZheng Liu NULL, 30828b0f165fSAnatol Pomozov dio_flags); 30838b0f165fSAnatol Pomozov 30848d5d02e6SMingming Cao if (iocb->private) 3085f45ee3a1SDmitry Monakhov ext4_inode_aio_set(inode, NULL); 30868d5d02e6SMingming Cao /* 308769c499d1STheodore Ts'o * The io_end structure takes a reference to the inode, that 308869c499d1STheodore Ts'o * structure needs to be destroyed and the reference to the 308969c499d1STheodore Ts'o * inode need to be dropped, when IO is complete, even with 0 309069c499d1STheodore Ts'o * byte write, or failed. 30918d5d02e6SMingming Cao * 309269c499d1STheodore Ts'o * In the successful AIO DIO case, the io_end structure will 309369c499d1STheodore Ts'o * be destroyed and the reference to the inode will be dropped 30948d5d02e6SMingming Cao * after the end_io call back function is called. 30958d5d02e6SMingming Cao * 309669c499d1STheodore Ts'o * In the case there is 0 byte write, or error case, since VFS 309769c499d1STheodore Ts'o * direct IO won't invoke the end_io call back function, we 309869c499d1STheodore Ts'o * need to free the end_io structure here. 30998d5d02e6SMingming Cao */ 31008d5d02e6SMingming Cao if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) { 31018d5d02e6SMingming Cao ext4_free_io_end(iocb->private); 31028d5d02e6SMingming Cao iocb->private = NULL; 3103729f52c6SZheng Liu } else if (ret > 0 && !overwrite && ext4_test_inode_state(inode, 31045f524950SMingming EXT4_STATE_DIO_UNWRITTEN)) { 3105109f5565SMingming int err; 31068d5d02e6SMingming Cao /* 31078d5d02e6SMingming Cao * for non AIO case, since the IO is already 310825985edcSLucas De Marchi * completed, we could do the conversion right here 31098d5d02e6SMingming Cao */ 3110109f5565SMingming err = ext4_convert_unwritten_extents(inode, 31118d5d02e6SMingming Cao offset, ret); 3112109f5565SMingming if (err < 0) 3113109f5565SMingming ret = err; 311419f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN); 3115109f5565SMingming } 31164bd809dbSZheng Liu 31174bd809dbSZheng Liu retake_lock: 31184bd809dbSZheng Liu /* take i_mutex locking again if we do a ovewrite dio */ 31194bd809dbSZheng Liu if (overwrite) { 31201f555cfaSDmitry Monakhov inode_dio_done(inode); 31214bd809dbSZheng Liu up_read(&EXT4_I(inode)->i_data_sem); 31224bd809dbSZheng Liu mutex_lock(&inode->i_mutex); 31234bd809dbSZheng Liu } 31244bd809dbSZheng Liu 31254c0425ffSMingming Cao return ret; 31264c0425ffSMingming Cao } 31278d5d02e6SMingming Cao 31284c0425ffSMingming Cao static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb, 31294c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 31304c0425ffSMingming Cao unsigned long nr_segs) 31314c0425ffSMingming Cao { 31324c0425ffSMingming Cao struct file *file = iocb->ki_filp; 31334c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 31340562e0baSJiaying Zhang ssize_t ret; 31354c0425ffSMingming Cao 313684ebd795STheodore Ts'o /* 313784ebd795STheodore Ts'o * If we are doing data journalling we don't support O_DIRECT 313884ebd795STheodore Ts'o */ 313984ebd795STheodore Ts'o if (ext4_should_journal_data(inode)) 314084ebd795STheodore Ts'o return 0; 314184ebd795STheodore Ts'o 314246c7f254STao Ma /* Let buffer I/O handle the inline data case. */ 314346c7f254STao Ma if (ext4_has_inline_data(inode)) 314446c7f254STao Ma return 0; 314546c7f254STao Ma 31460562e0baSJiaying Zhang trace_ext4_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw); 314712e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 31480562e0baSJiaying Zhang ret = ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs); 31490562e0baSJiaying Zhang else 31500562e0baSJiaying Zhang ret = ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 31510562e0baSJiaying Zhang trace_ext4_direct_IO_exit(inode, offset, 31520562e0baSJiaying Zhang iov_length(iov, nr_segs), rw, ret); 31530562e0baSJiaying Zhang return ret; 31544c0425ffSMingming Cao } 31554c0425ffSMingming Cao 3156ac27a0ecSDave Kleikamp /* 3157617ba13bSMingming Cao * Pages can be marked dirty completely asynchronously from ext4's journalling 3158ac27a0ecSDave Kleikamp * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do 3159ac27a0ecSDave Kleikamp * much here because ->set_page_dirty is called under VFS locks. The page is 3160ac27a0ecSDave Kleikamp * not necessarily locked. 3161ac27a0ecSDave Kleikamp * 3162ac27a0ecSDave Kleikamp * We cannot just dirty the page and leave attached buffers clean, because the 3163ac27a0ecSDave Kleikamp * buffers' dirty state is "definitive". We cannot just set the buffers dirty 3164ac27a0ecSDave Kleikamp * or jbddirty because all the journalling code will explode. 3165ac27a0ecSDave Kleikamp * 3166ac27a0ecSDave Kleikamp * So what we do is to mark the page "pending dirty" and next time writepage 3167ac27a0ecSDave Kleikamp * is called, propagate that into the buffers appropriately. 3168ac27a0ecSDave Kleikamp */ 3169617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page) 3170ac27a0ecSDave Kleikamp { 3171ac27a0ecSDave Kleikamp SetPageChecked(page); 3172ac27a0ecSDave Kleikamp return __set_page_dirty_nobuffers(page); 3173ac27a0ecSDave Kleikamp } 3174ac27a0ecSDave Kleikamp 3175617ba13bSMingming Cao static const struct address_space_operations ext4_ordered_aops = { 3176617ba13bSMingming Cao .readpage = ext4_readpage, 3177617ba13bSMingming Cao .readpages = ext4_readpages, 317843ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3179bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3180bfc1af65SNick Piggin .write_end = ext4_ordered_write_end, 3181617ba13bSMingming Cao .bmap = ext4_bmap, 3182617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3183617ba13bSMingming Cao .releasepage = ext4_releasepage, 3184617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 3185ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 31868ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3187aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3188ac27a0ecSDave Kleikamp }; 3189ac27a0ecSDave Kleikamp 3190617ba13bSMingming Cao static const struct address_space_operations ext4_writeback_aops = { 3191617ba13bSMingming Cao .readpage = ext4_readpage, 3192617ba13bSMingming Cao .readpages = ext4_readpages, 319343ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3194bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3195bfc1af65SNick Piggin .write_end = ext4_writeback_write_end, 3196617ba13bSMingming Cao .bmap = ext4_bmap, 3197617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3198617ba13bSMingming Cao .releasepage = ext4_releasepage, 3199617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 3200ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 32018ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3202aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3203ac27a0ecSDave Kleikamp }; 3204ac27a0ecSDave Kleikamp 3205617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 3206617ba13bSMingming Cao .readpage = ext4_readpage, 3207617ba13bSMingming Cao .readpages = ext4_readpages, 320843ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3209bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3210bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 3211617ba13bSMingming Cao .set_page_dirty = ext4_journalled_set_page_dirty, 3212617ba13bSMingming Cao .bmap = ext4_bmap, 32134520fb3cSJan Kara .invalidatepage = ext4_journalled_invalidatepage, 3214617ba13bSMingming Cao .releasepage = ext4_releasepage, 321584ebd795STheodore Ts'o .direct_IO = ext4_direct_IO, 32168ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3217aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3218ac27a0ecSDave Kleikamp }; 3219ac27a0ecSDave Kleikamp 322064769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 322164769240SAlex Tomas .readpage = ext4_readpage, 322264769240SAlex Tomas .readpages = ext4_readpages, 322343ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 322464769240SAlex Tomas .writepages = ext4_da_writepages, 322564769240SAlex Tomas .write_begin = ext4_da_write_begin, 322664769240SAlex Tomas .write_end = ext4_da_write_end, 322764769240SAlex Tomas .bmap = ext4_bmap, 322864769240SAlex Tomas .invalidatepage = ext4_da_invalidatepage, 322964769240SAlex Tomas .releasepage = ext4_releasepage, 323064769240SAlex Tomas .direct_IO = ext4_direct_IO, 323164769240SAlex Tomas .migratepage = buffer_migrate_page, 32328ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3233aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 323464769240SAlex Tomas }; 323564769240SAlex Tomas 3236617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 3237ac27a0ecSDave Kleikamp { 32383d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 32393d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 32403d2b1582SLukas Czerner if (test_opt(inode->i_sb, DELALLOC)) 3241cd1aac32SAneesh Kumar K.V inode->i_mapping->a_ops = &ext4_da_aops; 3242ac27a0ecSDave Kleikamp else 32433d2b1582SLukas Czerner inode->i_mapping->a_ops = &ext4_ordered_aops; 32443d2b1582SLukas Czerner break; 32453d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 32463d2b1582SLukas Czerner if (test_opt(inode->i_sb, DELALLOC)) 32473d2b1582SLukas Czerner inode->i_mapping->a_ops = &ext4_da_aops; 32483d2b1582SLukas Czerner else 32493d2b1582SLukas Czerner inode->i_mapping->a_ops = &ext4_writeback_aops; 32503d2b1582SLukas Czerner break; 32513d2b1582SLukas Czerner case EXT4_INODE_JOURNAL_DATA_MODE: 3252617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 32533d2b1582SLukas Czerner break; 32543d2b1582SLukas Czerner default: 32553d2b1582SLukas Czerner BUG(); 32563d2b1582SLukas Czerner } 3257ac27a0ecSDave Kleikamp } 3258ac27a0ecSDave Kleikamp 32594e96b2dbSAllison Henderson 32604e96b2dbSAllison Henderson /* 32614e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers() 32624e96b2dbSAllison Henderson * Wrapper function for ext4_discard_partial_page_buffers_no_lock. 32634e96b2dbSAllison Henderson * This function finds and locks the page containing the offset 32644e96b2dbSAllison Henderson * "from" and passes it to ext4_discard_partial_page_buffers_no_lock. 32654e96b2dbSAllison Henderson * Calling functions that already have the page locked should call 32664e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers_no_lock directly. 32674e96b2dbSAllison Henderson */ 32684e96b2dbSAllison Henderson int ext4_discard_partial_page_buffers(handle_t *handle, 32694e96b2dbSAllison Henderson struct address_space *mapping, loff_t from, 32704e96b2dbSAllison Henderson loff_t length, int flags) 32714e96b2dbSAllison Henderson { 32724e96b2dbSAllison Henderson struct inode *inode = mapping->host; 32734e96b2dbSAllison Henderson struct page *page; 32744e96b2dbSAllison Henderson int err = 0; 32754e96b2dbSAllison Henderson 32764e96b2dbSAllison Henderson page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT, 32774e96b2dbSAllison Henderson mapping_gfp_mask(mapping) & ~__GFP_FS); 32784e96b2dbSAllison Henderson if (!page) 32795129d05fSYongqiang Yang return -ENOMEM; 32804e96b2dbSAllison Henderson 32814e96b2dbSAllison Henderson err = ext4_discard_partial_page_buffers_no_lock(handle, inode, page, 32824e96b2dbSAllison Henderson from, length, flags); 32834e96b2dbSAllison Henderson 32844e96b2dbSAllison Henderson unlock_page(page); 32854e96b2dbSAllison Henderson page_cache_release(page); 32864e96b2dbSAllison Henderson return err; 32874e96b2dbSAllison Henderson } 32884e96b2dbSAllison Henderson 32894e96b2dbSAllison Henderson /* 32904e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers_no_lock() 32914e96b2dbSAllison Henderson * Zeros a page range of length 'length' starting from offset 'from'. 32924e96b2dbSAllison Henderson * Buffer heads that correspond to the block aligned regions of the 32934e96b2dbSAllison Henderson * zeroed range will be unmapped. Unblock aligned regions 32944e96b2dbSAllison Henderson * will have the corresponding buffer head mapped if needed so that 32954e96b2dbSAllison Henderson * that region of the page can be updated with the partial zero out. 32964e96b2dbSAllison Henderson * 32974e96b2dbSAllison Henderson * This function assumes that the page has already been locked. The 32984e96b2dbSAllison Henderson * The range to be discarded must be contained with in the given page. 32994e96b2dbSAllison Henderson * If the specified range exceeds the end of the page it will be shortened 33004e96b2dbSAllison Henderson * to the end of the page that corresponds to 'from'. This function is 33014e96b2dbSAllison Henderson * appropriate for updating a page and it buffer heads to be unmapped and 33024e96b2dbSAllison Henderson * zeroed for blocks that have been either released, or are going to be 33034e96b2dbSAllison Henderson * released. 33044e96b2dbSAllison Henderson * 33054e96b2dbSAllison Henderson * handle: The journal handle 33064e96b2dbSAllison Henderson * inode: The files inode 33074e96b2dbSAllison Henderson * page: A locked page that contains the offset "from" 33084907cb7bSAnatol Pomozov * from: The starting byte offset (from the beginning of the file) 33094e96b2dbSAllison Henderson * to begin discarding 33104e96b2dbSAllison Henderson * len: The length of bytes to discard 33114e96b2dbSAllison Henderson * flags: Optional flags that may be used: 33124e96b2dbSAllison Henderson * 33134e96b2dbSAllison Henderson * EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED 33144e96b2dbSAllison Henderson * Only zero the regions of the page whose buffer heads 33154e96b2dbSAllison Henderson * have already been unmapped. This flag is appropriate 33164907cb7bSAnatol Pomozov * for updating the contents of a page whose blocks may 33174e96b2dbSAllison Henderson * have already been released, and we only want to zero 33184e96b2dbSAllison Henderson * out the regions that correspond to those released blocks. 33194e96b2dbSAllison Henderson * 33204907cb7bSAnatol Pomozov * Returns zero on success or negative on failure. 33214e96b2dbSAllison Henderson */ 33225f163cc7SEric Sandeen static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle, 33234e96b2dbSAllison Henderson struct inode *inode, struct page *page, loff_t from, 33244e96b2dbSAllison Henderson loff_t length, int flags) 33254e96b2dbSAllison Henderson { 33264e96b2dbSAllison Henderson ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; 33274e96b2dbSAllison Henderson unsigned int offset = from & (PAGE_CACHE_SIZE-1); 33284e96b2dbSAllison Henderson unsigned int blocksize, max, pos; 33294e96b2dbSAllison Henderson ext4_lblk_t iblock; 33304e96b2dbSAllison Henderson struct buffer_head *bh; 33314e96b2dbSAllison Henderson int err = 0; 33324e96b2dbSAllison Henderson 33334e96b2dbSAllison Henderson blocksize = inode->i_sb->s_blocksize; 33344e96b2dbSAllison Henderson max = PAGE_CACHE_SIZE - offset; 33354e96b2dbSAllison Henderson 33364e96b2dbSAllison Henderson if (index != page->index) 33374e96b2dbSAllison Henderson return -EINVAL; 33384e96b2dbSAllison Henderson 33394e96b2dbSAllison Henderson /* 33404e96b2dbSAllison Henderson * correct length if it does not fall between 33414e96b2dbSAllison Henderson * 'from' and the end of the page 33424e96b2dbSAllison Henderson */ 33434e96b2dbSAllison Henderson if (length > max || length < 0) 33444e96b2dbSAllison Henderson length = max; 33454e96b2dbSAllison Henderson 33464e96b2dbSAllison Henderson iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); 33474e96b2dbSAllison Henderson 3348093e6e36SYongqiang Yang if (!page_has_buffers(page)) 33494e96b2dbSAllison Henderson create_empty_buffers(page, blocksize, 0); 33504e96b2dbSAllison Henderson 33514e96b2dbSAllison Henderson /* Find the buffer that contains "offset" */ 33524e96b2dbSAllison Henderson bh = page_buffers(page); 33534e96b2dbSAllison Henderson pos = blocksize; 33544e96b2dbSAllison Henderson while (offset >= pos) { 33554e96b2dbSAllison Henderson bh = bh->b_this_page; 33564e96b2dbSAllison Henderson iblock++; 33574e96b2dbSAllison Henderson pos += blocksize; 33584e96b2dbSAllison Henderson } 33594e96b2dbSAllison Henderson 33604e96b2dbSAllison Henderson pos = offset; 33614e96b2dbSAllison Henderson while (pos < offset + length) { 3362e260daf2SYongqiang Yang unsigned int end_of_block, range_to_discard; 3363e260daf2SYongqiang Yang 33644e96b2dbSAllison Henderson err = 0; 33654e96b2dbSAllison Henderson 33664e96b2dbSAllison Henderson /* The length of space left to zero and unmap */ 33674e96b2dbSAllison Henderson range_to_discard = offset + length - pos; 33684e96b2dbSAllison Henderson 33694e96b2dbSAllison Henderson /* The length of space until the end of the block */ 33704e96b2dbSAllison Henderson end_of_block = blocksize - (pos & (blocksize-1)); 33714e96b2dbSAllison Henderson 33724e96b2dbSAllison Henderson /* 33734e96b2dbSAllison Henderson * Do not unmap or zero past end of block 33744e96b2dbSAllison Henderson * for this buffer head 33754e96b2dbSAllison Henderson */ 33764e96b2dbSAllison Henderson if (range_to_discard > end_of_block) 33774e96b2dbSAllison Henderson range_to_discard = end_of_block; 33784e96b2dbSAllison Henderson 33794e96b2dbSAllison Henderson 33804e96b2dbSAllison Henderson /* 33814e96b2dbSAllison Henderson * Skip this buffer head if we are only zeroing unampped 33824e96b2dbSAllison Henderson * regions of the page 33834e96b2dbSAllison Henderson */ 33844e96b2dbSAllison Henderson if (flags & EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED && 33854e96b2dbSAllison Henderson buffer_mapped(bh)) 33864e96b2dbSAllison Henderson goto next; 33874e96b2dbSAllison Henderson 33884e96b2dbSAllison Henderson /* If the range is block aligned, unmap */ 33894e96b2dbSAllison Henderson if (range_to_discard == blocksize) { 33904e96b2dbSAllison Henderson clear_buffer_dirty(bh); 33914e96b2dbSAllison Henderson bh->b_bdev = NULL; 33924e96b2dbSAllison Henderson clear_buffer_mapped(bh); 33934e96b2dbSAllison Henderson clear_buffer_req(bh); 33944e96b2dbSAllison Henderson clear_buffer_new(bh); 33954e96b2dbSAllison Henderson clear_buffer_delay(bh); 33964e96b2dbSAllison Henderson clear_buffer_unwritten(bh); 33974e96b2dbSAllison Henderson clear_buffer_uptodate(bh); 33984e96b2dbSAllison Henderson zero_user(page, pos, range_to_discard); 33994e96b2dbSAllison Henderson BUFFER_TRACE(bh, "Buffer discarded"); 34004e96b2dbSAllison Henderson goto next; 34014e96b2dbSAllison Henderson } 34024e96b2dbSAllison Henderson 34034e96b2dbSAllison Henderson /* 34044e96b2dbSAllison Henderson * If this block is not completely contained in the range 34054e96b2dbSAllison Henderson * to be discarded, then it is not going to be released. Because 34064e96b2dbSAllison Henderson * we need to keep this block, we need to make sure this part 34074e96b2dbSAllison Henderson * of the page is uptodate before we modify it by writeing 34084e96b2dbSAllison Henderson * partial zeros on it. 34094e96b2dbSAllison Henderson */ 34104e96b2dbSAllison Henderson if (!buffer_mapped(bh)) { 34114e96b2dbSAllison Henderson /* 34124e96b2dbSAllison Henderson * Buffer head must be mapped before we can read 34134e96b2dbSAllison Henderson * from the block 34144e96b2dbSAllison Henderson */ 34154e96b2dbSAllison Henderson BUFFER_TRACE(bh, "unmapped"); 34164e96b2dbSAllison Henderson ext4_get_block(inode, iblock, bh, 0); 34174e96b2dbSAllison Henderson /* unmapped? It's a hole - nothing to do */ 34184e96b2dbSAllison Henderson if (!buffer_mapped(bh)) { 34194e96b2dbSAllison Henderson BUFFER_TRACE(bh, "still unmapped"); 34204e96b2dbSAllison Henderson goto next; 34214e96b2dbSAllison Henderson } 34224e96b2dbSAllison Henderson } 34234e96b2dbSAllison Henderson 34244e96b2dbSAllison Henderson /* Ok, it's mapped. Make sure it's up-to-date */ 34254e96b2dbSAllison Henderson if (PageUptodate(page)) 34264e96b2dbSAllison Henderson set_buffer_uptodate(bh); 34274e96b2dbSAllison Henderson 34284e96b2dbSAllison Henderson if (!buffer_uptodate(bh)) { 34294e96b2dbSAllison Henderson err = -EIO; 34304e96b2dbSAllison Henderson ll_rw_block(READ, 1, &bh); 34314e96b2dbSAllison Henderson wait_on_buffer(bh); 34324e96b2dbSAllison Henderson /* Uhhuh. Read error. Complain and punt.*/ 34334e96b2dbSAllison Henderson if (!buffer_uptodate(bh)) 34344e96b2dbSAllison Henderson goto next; 34354e96b2dbSAllison Henderson } 34364e96b2dbSAllison Henderson 34374e96b2dbSAllison Henderson if (ext4_should_journal_data(inode)) { 34384e96b2dbSAllison Henderson BUFFER_TRACE(bh, "get write access"); 34394e96b2dbSAllison Henderson err = ext4_journal_get_write_access(handle, bh); 34404e96b2dbSAllison Henderson if (err) 34414e96b2dbSAllison Henderson goto next; 34424e96b2dbSAllison Henderson } 34434e96b2dbSAllison Henderson 34444e96b2dbSAllison Henderson zero_user(page, pos, range_to_discard); 34454e96b2dbSAllison Henderson 34464e96b2dbSAllison Henderson err = 0; 34474e96b2dbSAllison Henderson if (ext4_should_journal_data(inode)) { 34484e96b2dbSAllison Henderson err = ext4_handle_dirty_metadata(handle, inode, bh); 3449decbd919STheodore Ts'o } else 34504e96b2dbSAllison Henderson mark_buffer_dirty(bh); 34514e96b2dbSAllison Henderson 34524e96b2dbSAllison Henderson BUFFER_TRACE(bh, "Partial buffer zeroed"); 34534e96b2dbSAllison Henderson next: 34544e96b2dbSAllison Henderson bh = bh->b_this_page; 34554e96b2dbSAllison Henderson iblock++; 34564e96b2dbSAllison Henderson pos += range_to_discard; 34574e96b2dbSAllison Henderson } 34584e96b2dbSAllison Henderson 34594e96b2dbSAllison Henderson return err; 34604e96b2dbSAllison Henderson } 34614e96b2dbSAllison Henderson 346291ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 346391ef4cafSDuane Griffin { 346491ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 346591ef4cafSDuane Griffin return 1; 346691ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 346791ef4cafSDuane Griffin return 1; 346891ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 346991ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 347091ef4cafSDuane Griffin return 0; 347191ef4cafSDuane Griffin } 347291ef4cafSDuane Griffin 3473ac27a0ecSDave Kleikamp /* 3474a4bb6b64SAllison Henderson * ext4_punch_hole: punches a hole in a file by releaseing the blocks 3475a4bb6b64SAllison Henderson * associated with the given offset and length 3476a4bb6b64SAllison Henderson * 3477a4bb6b64SAllison Henderson * @inode: File inode 3478a4bb6b64SAllison Henderson * @offset: The offset where the hole will begin 3479a4bb6b64SAllison Henderson * @len: The length of the hole 3480a4bb6b64SAllison Henderson * 34814907cb7bSAnatol Pomozov * Returns: 0 on success or negative on failure 3482a4bb6b64SAllison Henderson */ 3483a4bb6b64SAllison Henderson 3484a4bb6b64SAllison Henderson int ext4_punch_hole(struct file *file, loff_t offset, loff_t length) 3485a4bb6b64SAllison Henderson { 3486a4bb6b64SAllison Henderson struct inode *inode = file->f_path.dentry->d_inode; 3487a4bb6b64SAllison Henderson if (!S_ISREG(inode->i_mode)) 348873355192SAllison Henderson return -EOPNOTSUPP; 3489a4bb6b64SAllison Henderson 34908bad6fc8SZheng Liu if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 34918bad6fc8SZheng Liu return ext4_ind_punch_hole(file, offset, length); 3492a4bb6b64SAllison Henderson 3493bab08ab9STheodore Ts'o if (EXT4_SB(inode->i_sb)->s_cluster_ratio > 1) { 3494bab08ab9STheodore Ts'o /* TODO: Add support for bigalloc file systems */ 349573355192SAllison Henderson return -EOPNOTSUPP; 3496bab08ab9STheodore Ts'o } 3497bab08ab9STheodore Ts'o 3498aaddea81SZheng Liu trace_ext4_punch_hole(inode, offset, length); 3499aaddea81SZheng Liu 3500a4bb6b64SAllison Henderson return ext4_ext_punch_hole(file, offset, length); 3501a4bb6b64SAllison Henderson } 3502a4bb6b64SAllison Henderson 3503a4bb6b64SAllison Henderson /* 3504617ba13bSMingming Cao * ext4_truncate() 3505ac27a0ecSDave Kleikamp * 3506617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 3507617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 3508ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 3509ac27a0ecSDave Kleikamp * 351042b2aa86SJustin P. Mattock * As we work through the truncate and commit bits of it to the journal there 3511ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 3512ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 3513ac27a0ecSDave Kleikamp * 3514ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 3515ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 3516ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 3517ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 3518ac27a0ecSDave Kleikamp * left-to-right works OK too). 3519ac27a0ecSDave Kleikamp * 3520ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 3521ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 3522ac27a0ecSDave Kleikamp * 3523ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 3524617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 3525ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 3526617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 3527617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 3528ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 3529617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 3530ac27a0ecSDave Kleikamp */ 3531617ba13bSMingming Cao void ext4_truncate(struct inode *inode) 3532ac27a0ecSDave Kleikamp { 35330562e0baSJiaying Zhang trace_ext4_truncate_enter(inode); 35340562e0baSJiaying Zhang 353591ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 3536ac27a0ecSDave Kleikamp return; 3537ac27a0ecSDave Kleikamp 353812e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS); 3539c8d46e41SJiaying Zhang 35405534fb5bSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 354119f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE); 35427d8f9f7dSTheodore Ts'o 3543aef1c851STao Ma if (ext4_has_inline_data(inode)) { 3544aef1c851STao Ma int has_inline = 1; 3545aef1c851STao Ma 3546aef1c851STao Ma ext4_inline_data_truncate(inode, &has_inline); 3547aef1c851STao Ma if (has_inline) 3548aef1c851STao Ma return; 3549aef1c851STao Ma } 3550aef1c851STao Ma 3551ff9893dcSAmir Goldstein if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3552cf108bcaSJan Kara ext4_ext_truncate(inode); 3553ff9893dcSAmir Goldstein else 3554ff9893dcSAmir Goldstein ext4_ind_truncate(inode); 3555a86c6181SAlex Tomas 35560562e0baSJiaying Zhang trace_ext4_truncate_exit(inode); 3557ac27a0ecSDave Kleikamp } 3558ac27a0ecSDave Kleikamp 3559ac27a0ecSDave Kleikamp /* 3560617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 3561ac27a0ecSDave Kleikamp * underlying buffer_head on success. If 'in_mem' is true, we have all 3562ac27a0ecSDave Kleikamp * data in memory that is needed to recreate the on-disk version of this 3563ac27a0ecSDave Kleikamp * inode. 3564ac27a0ecSDave Kleikamp */ 3565617ba13bSMingming Cao static int __ext4_get_inode_loc(struct inode *inode, 3566617ba13bSMingming Cao struct ext4_iloc *iloc, int in_mem) 3567ac27a0ecSDave Kleikamp { 3568240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 3569ac27a0ecSDave Kleikamp struct buffer_head *bh; 3570240799cdSTheodore Ts'o struct super_block *sb = inode->i_sb; 3571240799cdSTheodore Ts'o ext4_fsblk_t block; 3572240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 3573ac27a0ecSDave Kleikamp 35743a06d778SAneesh Kumar K.V iloc->bh = NULL; 3575240799cdSTheodore Ts'o if (!ext4_valid_inum(sb, inode->i_ino)) 3576ac27a0ecSDave Kleikamp return -EIO; 3577ac27a0ecSDave Kleikamp 3578240799cdSTheodore Ts'o iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb); 3579240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 3580240799cdSTheodore Ts'o if (!gdp) 3581240799cdSTheodore Ts'o return -EIO; 3582240799cdSTheodore Ts'o 3583240799cdSTheodore Ts'o /* 3584240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 3585240799cdSTheodore Ts'o */ 358600d09882STao Ma inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 3587240799cdSTheodore Ts'o inode_offset = ((inode->i_ino - 1) % 3588240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 3589240799cdSTheodore Ts'o block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); 3590240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 3591240799cdSTheodore Ts'o 3592240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 3593aebf0243SWang Shilong if (unlikely(!bh)) 3594860d21e2STheodore Ts'o return -ENOMEM; 3595ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 3596ac27a0ecSDave Kleikamp lock_buffer(bh); 35979c83a923SHidehiro Kawai 35989c83a923SHidehiro Kawai /* 35999c83a923SHidehiro Kawai * If the buffer has the write error flag, we have failed 36009c83a923SHidehiro Kawai * to write out another inode in the same block. In this 36019c83a923SHidehiro Kawai * case, we don't have to read the block because we may 36029c83a923SHidehiro Kawai * read the old inode data successfully. 36039c83a923SHidehiro Kawai */ 36049c83a923SHidehiro Kawai if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) 36059c83a923SHidehiro Kawai set_buffer_uptodate(bh); 36069c83a923SHidehiro Kawai 3607ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) { 3608ac27a0ecSDave Kleikamp /* someone brought it uptodate while we waited */ 3609ac27a0ecSDave Kleikamp unlock_buffer(bh); 3610ac27a0ecSDave Kleikamp goto has_buffer; 3611ac27a0ecSDave Kleikamp } 3612ac27a0ecSDave Kleikamp 3613ac27a0ecSDave Kleikamp /* 3614ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 3615ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 3616ac27a0ecSDave Kleikamp * block. 3617ac27a0ecSDave Kleikamp */ 3618ac27a0ecSDave Kleikamp if (in_mem) { 3619ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 3620240799cdSTheodore Ts'o int i, start; 3621ac27a0ecSDave Kleikamp 3622240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 3623ac27a0ecSDave Kleikamp 3624ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 3625240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 3626aebf0243SWang Shilong if (unlikely(!bitmap_bh)) 3627ac27a0ecSDave Kleikamp goto make_io; 3628ac27a0ecSDave Kleikamp 3629ac27a0ecSDave Kleikamp /* 3630ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 3631ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 3632ac27a0ecSDave Kleikamp * of one, so skip it. 3633ac27a0ecSDave Kleikamp */ 3634ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 3635ac27a0ecSDave Kleikamp brelse(bitmap_bh); 3636ac27a0ecSDave Kleikamp goto make_io; 3637ac27a0ecSDave Kleikamp } 3638240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 3639ac27a0ecSDave Kleikamp if (i == inode_offset) 3640ac27a0ecSDave Kleikamp continue; 3641617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 3642ac27a0ecSDave Kleikamp break; 3643ac27a0ecSDave Kleikamp } 3644ac27a0ecSDave Kleikamp brelse(bitmap_bh); 3645240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 3646ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 3647ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 3648ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 3649ac27a0ecSDave Kleikamp unlock_buffer(bh); 3650ac27a0ecSDave Kleikamp goto has_buffer; 3651ac27a0ecSDave Kleikamp } 3652ac27a0ecSDave Kleikamp } 3653ac27a0ecSDave Kleikamp 3654ac27a0ecSDave Kleikamp make_io: 3655ac27a0ecSDave Kleikamp /* 3656240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 3657240799cdSTheodore Ts'o * blocks from the inode table. 3658240799cdSTheodore Ts'o */ 3659240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 3660240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 3661240799cdSTheodore Ts'o unsigned num; 3662240799cdSTheodore Ts'o 3663240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 3664b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 3665240799cdSTheodore Ts'o b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1); 3666240799cdSTheodore Ts'o if (table > b) 3667240799cdSTheodore Ts'o b = table; 3668240799cdSTheodore Ts'o end = b + EXT4_SB(sb)->s_inode_readahead_blks; 3669240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 3670feb0ab32SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) 3671560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 3672240799cdSTheodore Ts'o table += num / inodes_per_block; 3673240799cdSTheodore Ts'o if (end > table) 3674240799cdSTheodore Ts'o end = table; 3675240799cdSTheodore Ts'o while (b <= end) 3676240799cdSTheodore Ts'o sb_breadahead(sb, b++); 3677240799cdSTheodore Ts'o } 3678240799cdSTheodore Ts'o 3679240799cdSTheodore Ts'o /* 3680ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 3681ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 3682ac27a0ecSDave Kleikamp * Read the block from disk. 3683ac27a0ecSDave Kleikamp */ 36840562e0baSJiaying Zhang trace_ext4_load_inode(inode); 3685ac27a0ecSDave Kleikamp get_bh(bh); 3686ac27a0ecSDave Kleikamp bh->b_end_io = end_buffer_read_sync; 368765299a3bSChristoph Hellwig submit_bh(READ | REQ_META | REQ_PRIO, bh); 3688ac27a0ecSDave Kleikamp wait_on_buffer(bh); 3689ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 3690c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, block, 3691c398eda0STheodore Ts'o "unable to read itable block"); 3692ac27a0ecSDave Kleikamp brelse(bh); 3693ac27a0ecSDave Kleikamp return -EIO; 3694ac27a0ecSDave Kleikamp } 3695ac27a0ecSDave Kleikamp } 3696ac27a0ecSDave Kleikamp has_buffer: 3697ac27a0ecSDave Kleikamp iloc->bh = bh; 3698ac27a0ecSDave Kleikamp return 0; 3699ac27a0ecSDave Kleikamp } 3700ac27a0ecSDave Kleikamp 3701617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 3702ac27a0ecSDave Kleikamp { 3703ac27a0ecSDave Kleikamp /* We have all inode data except xattrs in memory here. */ 3704617ba13bSMingming Cao return __ext4_get_inode_loc(inode, iloc, 370519f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_XATTR)); 3706ac27a0ecSDave Kleikamp } 3707ac27a0ecSDave Kleikamp 3708617ba13bSMingming Cao void ext4_set_inode_flags(struct inode *inode) 3709ac27a0ecSDave Kleikamp { 3710617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 3711ac27a0ecSDave Kleikamp 3712ac27a0ecSDave Kleikamp inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); 3713617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 3714ac27a0ecSDave Kleikamp inode->i_flags |= S_SYNC; 3715617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 3716ac27a0ecSDave Kleikamp inode->i_flags |= S_APPEND; 3717617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 3718ac27a0ecSDave Kleikamp inode->i_flags |= S_IMMUTABLE; 3719617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 3720ac27a0ecSDave Kleikamp inode->i_flags |= S_NOATIME; 3721617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 3722ac27a0ecSDave Kleikamp inode->i_flags |= S_DIRSYNC; 3723ac27a0ecSDave Kleikamp } 3724ac27a0ecSDave Kleikamp 3725ff9ddf7eSJan Kara /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */ 3726ff9ddf7eSJan Kara void ext4_get_inode_flags(struct ext4_inode_info *ei) 3727ff9ddf7eSJan Kara { 372884a8dce2SDmitry Monakhov unsigned int vfs_fl; 372984a8dce2SDmitry Monakhov unsigned long old_fl, new_fl; 3730ff9ddf7eSJan Kara 373184a8dce2SDmitry Monakhov do { 373284a8dce2SDmitry Monakhov vfs_fl = ei->vfs_inode.i_flags; 373384a8dce2SDmitry Monakhov old_fl = ei->i_flags; 373484a8dce2SDmitry Monakhov new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL| 373584a8dce2SDmitry Monakhov EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL| 373684a8dce2SDmitry Monakhov EXT4_DIRSYNC_FL); 373784a8dce2SDmitry Monakhov if (vfs_fl & S_SYNC) 373884a8dce2SDmitry Monakhov new_fl |= EXT4_SYNC_FL; 373984a8dce2SDmitry Monakhov if (vfs_fl & S_APPEND) 374084a8dce2SDmitry Monakhov new_fl |= EXT4_APPEND_FL; 374184a8dce2SDmitry Monakhov if (vfs_fl & S_IMMUTABLE) 374284a8dce2SDmitry Monakhov new_fl |= EXT4_IMMUTABLE_FL; 374384a8dce2SDmitry Monakhov if (vfs_fl & S_NOATIME) 374484a8dce2SDmitry Monakhov new_fl |= EXT4_NOATIME_FL; 374584a8dce2SDmitry Monakhov if (vfs_fl & S_DIRSYNC) 374684a8dce2SDmitry Monakhov new_fl |= EXT4_DIRSYNC_FL; 374784a8dce2SDmitry Monakhov } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl); 3748ff9ddf7eSJan Kara } 3749de9a55b8STheodore Ts'o 37500fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 37510fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 37520fc1b451SAneesh Kumar K.V { 37530fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 37548180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 37558180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 37560fc1b451SAneesh Kumar K.V 37570fc1b451SAneesh Kumar K.V if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 37580fc1b451SAneesh Kumar K.V EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) { 37590fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 37600fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 37610fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 376207a03824STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) { 37638180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 37648180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 37658180a562SAneesh Kumar K.V } else { 37660fc1b451SAneesh Kumar K.V return i_blocks; 37678180a562SAneesh Kumar K.V } 37680fc1b451SAneesh Kumar K.V } else { 37690fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 37700fc1b451SAneesh Kumar K.V } 37710fc1b451SAneesh Kumar K.V } 3772ff9ddf7eSJan Kara 3773152a7b0aSTao Ma static inline void ext4_iget_extra_inode(struct inode *inode, 3774152a7b0aSTao Ma struct ext4_inode *raw_inode, 3775152a7b0aSTao Ma struct ext4_inode_info *ei) 3776152a7b0aSTao Ma { 3777152a7b0aSTao Ma __le32 *magic = (void *)raw_inode + 3778152a7b0aSTao Ma EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize; 377967cf5b09STao Ma if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) { 3780152a7b0aSTao Ma ext4_set_inode_state(inode, EXT4_STATE_XATTR); 378167cf5b09STao Ma ext4_find_inline_data_nolock(inode); 3782f19d5870STao Ma } else 3783f19d5870STao Ma EXT4_I(inode)->i_inline_off = 0; 3784152a7b0aSTao Ma } 3785152a7b0aSTao Ma 37861d1fe1eeSDavid Howells struct inode *ext4_iget(struct super_block *sb, unsigned long ino) 3787ac27a0ecSDave Kleikamp { 3788617ba13bSMingming Cao struct ext4_iloc iloc; 3789617ba13bSMingming Cao struct ext4_inode *raw_inode; 37901d1fe1eeSDavid Howells struct ext4_inode_info *ei; 37911d1fe1eeSDavid Howells struct inode *inode; 3792b436b9beSJan Kara journal_t *journal = EXT4_SB(sb)->s_journal; 37931d1fe1eeSDavid Howells long ret; 3794ac27a0ecSDave Kleikamp int block; 379508cefc7aSEric W. Biederman uid_t i_uid; 379608cefc7aSEric W. Biederman gid_t i_gid; 3797ac27a0ecSDave Kleikamp 37981d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 37991d1fe1eeSDavid Howells if (!inode) 38001d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 38011d1fe1eeSDavid Howells if (!(inode->i_state & I_NEW)) 38021d1fe1eeSDavid Howells return inode; 38031d1fe1eeSDavid Howells 38041d1fe1eeSDavid Howells ei = EXT4_I(inode); 38057dc57615SPeter Huewe iloc.bh = NULL; 3806ac27a0ecSDave Kleikamp 38071d1fe1eeSDavid Howells ret = __ext4_get_inode_loc(inode, &iloc, 0); 38081d1fe1eeSDavid Howells if (ret < 0) 3809ac27a0ecSDave Kleikamp goto bad_inode; 3810617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 3811814525f4SDarrick J. Wong 3812814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 3813814525f4SDarrick J. Wong ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 3814814525f4SDarrick J. Wong if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 3815814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)) { 3816814525f4SDarrick J. Wong EXT4_ERROR_INODE(inode, "bad extra_isize (%u != %u)", 3817814525f4SDarrick J. Wong EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize, 3818814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)); 3819814525f4SDarrick J. Wong ret = -EIO; 3820814525f4SDarrick J. Wong goto bad_inode; 3821814525f4SDarrick J. Wong } 3822814525f4SDarrick J. Wong } else 3823814525f4SDarrick J. Wong ei->i_extra_isize = 0; 3824814525f4SDarrick J. Wong 3825814525f4SDarrick J. Wong /* Precompute checksum seed for inode metadata */ 3826814525f4SDarrick J. Wong if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 3827814525f4SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) { 3828814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3829814525f4SDarrick J. Wong __u32 csum; 3830814525f4SDarrick J. Wong __le32 inum = cpu_to_le32(inode->i_ino); 3831814525f4SDarrick J. Wong __le32 gen = raw_inode->i_generation; 3832814525f4SDarrick J. Wong csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, 3833814525f4SDarrick J. Wong sizeof(inum)); 3834814525f4SDarrick J. Wong ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, 3835814525f4SDarrick J. Wong sizeof(gen)); 3836814525f4SDarrick J. Wong } 3837814525f4SDarrick J. Wong 3838814525f4SDarrick J. Wong if (!ext4_inode_csum_verify(inode, raw_inode, ei)) { 3839814525f4SDarrick J. Wong EXT4_ERROR_INODE(inode, "checksum invalid"); 3840814525f4SDarrick J. Wong ret = -EIO; 3841814525f4SDarrick J. Wong goto bad_inode; 3842814525f4SDarrick J. Wong } 3843814525f4SDarrick J. Wong 3844ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 384508cefc7aSEric W. Biederman i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 384608cefc7aSEric W. Biederman i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 3847ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 384808cefc7aSEric W. Biederman i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 384908cefc7aSEric W. Biederman i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 3850ac27a0ecSDave Kleikamp } 385108cefc7aSEric W. Biederman i_uid_write(inode, i_uid); 385208cefc7aSEric W. Biederman i_gid_write(inode, i_gid); 3853bfe86848SMiklos Szeredi set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); 3854ac27a0ecSDave Kleikamp 3855353eb83cSTheodore Ts'o ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 385667cf5b09STao Ma ei->i_inline_off = 0; 3857ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 3858ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 3859ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 3860ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 3861ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 3862ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 3863ac27a0ecSDave Kleikamp */ 3864ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 3865ac27a0ecSDave Kleikamp if (inode->i_mode == 0 || 3866617ba13bSMingming Cao !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) { 3867ac27a0ecSDave Kleikamp /* this inode is deleted */ 38681d1fe1eeSDavid Howells ret = -ESTALE; 3869ac27a0ecSDave Kleikamp goto bad_inode; 3870ac27a0ecSDave Kleikamp } 3871ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 3872ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 3873ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 3874ac27a0ecSDave Kleikamp * the process of deleting those. */ 3875ac27a0ecSDave Kleikamp } 3876ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 38770fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 38787973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 3879a9e81742STheodore Ts'o if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) 3880a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 3881a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 3882a48380f7SAneesh Kumar K.V inode->i_size = ext4_isize(raw_inode); 3883ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 3884a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 3885a9e7f447SDmitry Monakhov ei->i_reserved_quota = 0; 3886a9e7f447SDmitry Monakhov #endif 3887ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 3888ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 3889a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 3890ac27a0ecSDave Kleikamp /* 3891ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 3892ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 3893ac27a0ecSDave Kleikamp */ 3894617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 3895ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 3896ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 3897ac27a0ecSDave Kleikamp 3898b436b9beSJan Kara /* 3899b436b9beSJan Kara * Set transaction id's of transactions that have to be committed 3900b436b9beSJan Kara * to finish f[data]sync. We set them to currently running transaction 3901b436b9beSJan Kara * as we cannot be sure that the inode or some of its metadata isn't 3902b436b9beSJan Kara * part of the transaction - the inode could have been reclaimed and 3903b436b9beSJan Kara * now it is reread from disk. 3904b436b9beSJan Kara */ 3905b436b9beSJan Kara if (journal) { 3906b436b9beSJan Kara transaction_t *transaction; 3907b436b9beSJan Kara tid_t tid; 3908b436b9beSJan Kara 3909a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 3910b436b9beSJan Kara if (journal->j_running_transaction) 3911b436b9beSJan Kara transaction = journal->j_running_transaction; 3912b436b9beSJan Kara else 3913b436b9beSJan Kara transaction = journal->j_committing_transaction; 3914b436b9beSJan Kara if (transaction) 3915b436b9beSJan Kara tid = transaction->t_tid; 3916b436b9beSJan Kara else 3917b436b9beSJan Kara tid = journal->j_commit_sequence; 3918a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 3919b436b9beSJan Kara ei->i_sync_tid = tid; 3920b436b9beSJan Kara ei->i_datasync_tid = tid; 3921b436b9beSJan Kara } 3922b436b9beSJan Kara 39230040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 3924ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 3925ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 3926617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 3927617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 3928ac27a0ecSDave Kleikamp } else { 3929152a7b0aSTao Ma ext4_iget_extra_inode(inode, raw_inode, ei); 3930ac27a0ecSDave Kleikamp } 3931814525f4SDarrick J. Wong } 3932ac27a0ecSDave Kleikamp 3933ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 3934ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 3935ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 3936ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 3937ef7f3835SKalpak Shah 393825ec56b5SJean Noel Cordenner inode->i_version = le32_to_cpu(raw_inode->i_disk_version); 393925ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 394025ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 394125ec56b5SJean Noel Cordenner inode->i_version |= 394225ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 394325ec56b5SJean Noel Cordenner } 394425ec56b5SJean Noel Cordenner 3945c4b5a614STheodore Ts'o ret = 0; 3946485c26ecSTheodore Ts'o if (ei->i_file_acl && 39471032988cSTheodore Ts'o !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) { 394824676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "bad extended attribute block %llu", 394924676da4STheodore Ts'o ei->i_file_acl); 3950485c26ecSTheodore Ts'o ret = -EIO; 3951485c26ecSTheodore Ts'o goto bad_inode; 3952f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 3953f19d5870STao Ma if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 3954f19d5870STao Ma if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 3955c4b5a614STheodore Ts'o (S_ISLNK(inode->i_mode) && 3956f19d5870STao Ma !ext4_inode_is_fast_symlink(inode)))) 39577a262f7cSAneesh Kumar K.V /* Validate extent which is part of inode */ 39587a262f7cSAneesh Kumar K.V ret = ext4_ext_check_inode(inode); 3959fe2c8191SThiemo Nagel } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 3960fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 3961fe2c8191SThiemo Nagel !ext4_inode_is_fast_symlink(inode))) { 3962fe2c8191SThiemo Nagel /* Validate block references which are part of inode */ 39631f7d1e77STheodore Ts'o ret = ext4_ind_check_inode(inode); 3964fe2c8191SThiemo Nagel } 3965f19d5870STao Ma } 3966567f3e9aSTheodore Ts'o if (ret) 39677a262f7cSAneesh Kumar K.V goto bad_inode; 39687a262f7cSAneesh Kumar K.V 3969ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 3970617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 3971617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 3972617ba13bSMingming Cao ext4_set_aops(inode); 3973ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 3974617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 3975617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 3976ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 3977e83c1397SDuane Griffin if (ext4_inode_is_fast_symlink(inode)) { 3978617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 3979e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 3980e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 3981e83c1397SDuane Griffin } else { 3982617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 3983617ba13bSMingming Cao ext4_set_aops(inode); 3984ac27a0ecSDave Kleikamp } 3985563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 3986563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 3987617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 3988ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 3989ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 3990ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 3991ac27a0ecSDave Kleikamp else 3992ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 3993ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 3994563bdd61STheodore Ts'o } else { 3995563bdd61STheodore Ts'o ret = -EIO; 399624676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode); 3997563bdd61STheodore Ts'o goto bad_inode; 3998ac27a0ecSDave Kleikamp } 3999ac27a0ecSDave Kleikamp brelse(iloc.bh); 4000617ba13bSMingming Cao ext4_set_inode_flags(inode); 40011d1fe1eeSDavid Howells unlock_new_inode(inode); 40021d1fe1eeSDavid Howells return inode; 4003ac27a0ecSDave Kleikamp 4004ac27a0ecSDave Kleikamp bad_inode: 4005567f3e9aSTheodore Ts'o brelse(iloc.bh); 40061d1fe1eeSDavid Howells iget_failed(inode); 40071d1fe1eeSDavid Howells return ERR_PTR(ret); 4008ac27a0ecSDave Kleikamp } 4009ac27a0ecSDave Kleikamp 40100fc1b451SAneesh Kumar K.V static int ext4_inode_blocks_set(handle_t *handle, 40110fc1b451SAneesh Kumar K.V struct ext4_inode *raw_inode, 40120fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 40130fc1b451SAneesh Kumar K.V { 40140fc1b451SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 40150fc1b451SAneesh Kumar K.V u64 i_blocks = inode->i_blocks; 40160fc1b451SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 40170fc1b451SAneesh Kumar K.V 40180fc1b451SAneesh Kumar K.V if (i_blocks <= ~0U) { 40190fc1b451SAneesh Kumar K.V /* 40204907cb7bSAnatol Pomozov * i_blocks can be represented in a 32 bit variable 40210fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 40220fc1b451SAneesh Kumar K.V */ 40238180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 40240fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = 0; 402584a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 4026f287a1a5STheodore Ts'o return 0; 4027f287a1a5STheodore Ts'o } 4028f287a1a5STheodore Ts'o if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) 4029f287a1a5STheodore Ts'o return -EFBIG; 4030f287a1a5STheodore Ts'o 4031f287a1a5STheodore Ts'o if (i_blocks <= 0xffffffffffffULL) { 40320fc1b451SAneesh Kumar K.V /* 40330fc1b451SAneesh Kumar K.V * i_blocks can be represented in a 48 bit variable 40340fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 40350fc1b451SAneesh Kumar K.V */ 40368180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 40370fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 403884a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 40390fc1b451SAneesh Kumar K.V } else { 404084a8dce2SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE); 40418180a562SAneesh Kumar K.V /* i_block is stored in file system block size */ 40428180a562SAneesh Kumar K.V i_blocks = i_blocks >> (inode->i_blkbits - 9); 40438180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 40448180a562SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 40450fc1b451SAneesh Kumar K.V } 4046f287a1a5STheodore Ts'o return 0; 40470fc1b451SAneesh Kumar K.V } 40480fc1b451SAneesh Kumar K.V 4049ac27a0ecSDave Kleikamp /* 4050ac27a0ecSDave Kleikamp * Post the struct inode info into an on-disk inode location in the 4051ac27a0ecSDave Kleikamp * buffer-cache. This gobbles the caller's reference to the 4052ac27a0ecSDave Kleikamp * buffer_head in the inode location struct. 4053ac27a0ecSDave Kleikamp * 4054ac27a0ecSDave Kleikamp * The caller must have write access to iloc->bh. 4055ac27a0ecSDave Kleikamp */ 4056617ba13bSMingming Cao static int ext4_do_update_inode(handle_t *handle, 4057ac27a0ecSDave Kleikamp struct inode *inode, 4058830156c7SFrank Mayhar struct ext4_iloc *iloc) 4059ac27a0ecSDave Kleikamp { 4060617ba13bSMingming Cao struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 4061617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 4062ac27a0ecSDave Kleikamp struct buffer_head *bh = iloc->bh; 4063ac27a0ecSDave Kleikamp int err = 0, rc, block; 4064b71fc079SJan Kara int need_datasync = 0; 406508cefc7aSEric W. Biederman uid_t i_uid; 406608cefc7aSEric W. Biederman gid_t i_gid; 4067ac27a0ecSDave Kleikamp 4068ac27a0ecSDave Kleikamp /* For fields not not tracking in the in-memory inode, 4069ac27a0ecSDave Kleikamp * initialise them to zero for new inodes. */ 407019f5fb7aSTheodore Ts'o if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) 4071617ba13bSMingming Cao memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 4072ac27a0ecSDave Kleikamp 4073ff9ddf7eSJan Kara ext4_get_inode_flags(ei); 4074ac27a0ecSDave Kleikamp raw_inode->i_mode = cpu_to_le16(inode->i_mode); 407508cefc7aSEric W. Biederman i_uid = i_uid_read(inode); 407608cefc7aSEric W. Biederman i_gid = i_gid_read(inode); 4077ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 407808cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid)); 407908cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid)); 4080ac27a0ecSDave Kleikamp /* 4081ac27a0ecSDave Kleikamp * Fix up interoperability with old kernels. Otherwise, old inodes get 4082ac27a0ecSDave Kleikamp * re-used with the upper 16 bits of the uid/gid intact 4083ac27a0ecSDave Kleikamp */ 4084ac27a0ecSDave Kleikamp if (!ei->i_dtime) { 4085ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 408608cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_uid)); 4087ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 408808cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_gid)); 4089ac27a0ecSDave Kleikamp } else { 4090ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4091ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4092ac27a0ecSDave Kleikamp } 4093ac27a0ecSDave Kleikamp } else { 409408cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid)); 409508cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid)); 4096ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4097ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4098ac27a0ecSDave Kleikamp } 4099ac27a0ecSDave Kleikamp raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 4100ef7f3835SKalpak Shah 4101ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 4102ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 4103ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 4104ef7f3835SKalpak Shah EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 4105ef7f3835SKalpak Shah 41060fc1b451SAneesh Kumar K.V if (ext4_inode_blocks_set(handle, raw_inode, ei)) 41070fc1b451SAneesh Kumar K.V goto out_brelse; 4108ac27a0ecSDave Kleikamp raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 4109353eb83cSTheodore Ts'o raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF); 41109b8f1f01SMingming Cao if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 41119b8f1f01SMingming Cao cpu_to_le32(EXT4_OS_HURD)) 4112a1ddeb7eSBadari Pulavarty raw_inode->i_file_acl_high = 4113a1ddeb7eSBadari Pulavarty cpu_to_le16(ei->i_file_acl >> 32); 41147973c0c1SAneesh Kumar K.V raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 4115b71fc079SJan Kara if (ei->i_disksize != ext4_isize(raw_inode)) { 4116a48380f7SAneesh Kumar K.V ext4_isize_set(raw_inode, ei->i_disksize); 4117b71fc079SJan Kara need_datasync = 1; 4118b71fc079SJan Kara } 4119ac27a0ecSDave Kleikamp if (ei->i_disksize > 0x7fffffffULL) { 4120ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 4121617ba13bSMingming Cao if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, 4122617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE) || 4123617ba13bSMingming Cao EXT4_SB(sb)->s_es->s_rev_level == 4124617ba13bSMingming Cao cpu_to_le32(EXT4_GOOD_OLD_REV)) { 4125ac27a0ecSDave Kleikamp /* If this is the first large file 4126ac27a0ecSDave Kleikamp * created, add a flag to the superblock. 4127ac27a0ecSDave Kleikamp */ 4128617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, 4129617ba13bSMingming Cao EXT4_SB(sb)->s_sbh); 4130ac27a0ecSDave Kleikamp if (err) 4131ac27a0ecSDave Kleikamp goto out_brelse; 4132617ba13bSMingming Cao ext4_update_dynamic_rev(sb); 4133617ba13bSMingming Cao EXT4_SET_RO_COMPAT_FEATURE(sb, 4134617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE); 41350390131bSFrank Mayhar ext4_handle_sync(handle); 4136b50924c2SArtem Bityutskiy err = ext4_handle_dirty_super(handle, sb); 4137ac27a0ecSDave Kleikamp } 4138ac27a0ecSDave Kleikamp } 4139ac27a0ecSDave Kleikamp raw_inode->i_generation = cpu_to_le32(inode->i_generation); 4140ac27a0ecSDave Kleikamp if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 4141ac27a0ecSDave Kleikamp if (old_valid_dev(inode->i_rdev)) { 4142ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 4143ac27a0ecSDave Kleikamp cpu_to_le32(old_encode_dev(inode->i_rdev)); 4144ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 0; 4145ac27a0ecSDave Kleikamp } else { 4146ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 0; 4147ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 4148ac27a0ecSDave Kleikamp cpu_to_le32(new_encode_dev(inode->i_rdev)); 4149ac27a0ecSDave Kleikamp raw_inode->i_block[2] = 0; 4150ac27a0ecSDave Kleikamp } 4151f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 4152de9a55b8STheodore Ts'o for (block = 0; block < EXT4_N_BLOCKS; block++) 4153ac27a0ecSDave Kleikamp raw_inode->i_block[block] = ei->i_data[block]; 4154f19d5870STao Ma } 4155ac27a0ecSDave Kleikamp 415625ec56b5SJean Noel Cordenner raw_inode->i_disk_version = cpu_to_le32(inode->i_version); 415725ec56b5SJean Noel Cordenner if (ei->i_extra_isize) { 415825ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 415925ec56b5SJean Noel Cordenner raw_inode->i_version_hi = 416025ec56b5SJean Noel Cordenner cpu_to_le32(inode->i_version >> 32); 4161ac27a0ecSDave Kleikamp raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize); 416225ec56b5SJean Noel Cordenner } 416325ec56b5SJean Noel Cordenner 4164814525f4SDarrick J. Wong ext4_inode_csum_set(inode, raw_inode, ei); 4165814525f4SDarrick J. Wong 41660390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 416773b50c1cSCurt Wohlgemuth rc = ext4_handle_dirty_metadata(handle, NULL, bh); 4168ac27a0ecSDave Kleikamp if (!err) 4169ac27a0ecSDave Kleikamp err = rc; 417019f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_NEW); 4171ac27a0ecSDave Kleikamp 4172b71fc079SJan Kara ext4_update_inode_fsync_trans(handle, inode, need_datasync); 4173ac27a0ecSDave Kleikamp out_brelse: 4174ac27a0ecSDave Kleikamp brelse(bh); 4175617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4176ac27a0ecSDave Kleikamp return err; 4177ac27a0ecSDave Kleikamp } 4178ac27a0ecSDave Kleikamp 4179ac27a0ecSDave Kleikamp /* 4180617ba13bSMingming Cao * ext4_write_inode() 4181ac27a0ecSDave Kleikamp * 4182ac27a0ecSDave Kleikamp * We are called from a few places: 4183ac27a0ecSDave Kleikamp * 4184ac27a0ecSDave Kleikamp * - Within generic_file_write() for O_SYNC files. 4185ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 41864907cb7bSAnatol Pomozov * transaction to commit. 4187ac27a0ecSDave Kleikamp * 4188ac27a0ecSDave Kleikamp * - Within sys_sync(), kupdate and such. 4189ac27a0ecSDave Kleikamp * We wait on commit, if tol to. 4190ac27a0ecSDave Kleikamp * 4191ac27a0ecSDave Kleikamp * - Within prune_icache() (PF_MEMALLOC == true) 4192ac27a0ecSDave Kleikamp * Here we simply return. We can't afford to block kswapd on the 4193ac27a0ecSDave Kleikamp * journal commit. 4194ac27a0ecSDave Kleikamp * 4195ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 4196ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 4197617ba13bSMingming Cao * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for 4198ac27a0ecSDave Kleikamp * knfsd. 4199ac27a0ecSDave Kleikamp * 4200ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 4201ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 4202ac27a0ecSDave Kleikamp * which we are interested. 4203ac27a0ecSDave Kleikamp * 4204ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 4205ac27a0ecSDave Kleikamp * 4206ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 4207ac27a0ecSDave Kleikamp * stuff(); 4208ac27a0ecSDave Kleikamp * inode->i_size = expr; 4209ac27a0ecSDave Kleikamp * 4210ac27a0ecSDave Kleikamp * is in error because a kswapd-driven write_inode() could occur while 4211ac27a0ecSDave Kleikamp * `stuff()' is running, and the new i_size will be lost. Plus the inode 4212ac27a0ecSDave Kleikamp * will no longer be on the superblock's dirty inode list. 4213ac27a0ecSDave Kleikamp */ 4214a9185b41SChristoph Hellwig int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) 4215ac27a0ecSDave Kleikamp { 421691ac6f43SFrank Mayhar int err; 421791ac6f43SFrank Mayhar 4218ac27a0ecSDave Kleikamp if (current->flags & PF_MEMALLOC) 4219ac27a0ecSDave Kleikamp return 0; 4220ac27a0ecSDave Kleikamp 422191ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal) { 4222617ba13bSMingming Cao if (ext4_journal_current_handle()) { 4223b38bd33aSMingming Cao jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); 4224ac27a0ecSDave Kleikamp dump_stack(); 4225ac27a0ecSDave Kleikamp return -EIO; 4226ac27a0ecSDave Kleikamp } 4227ac27a0ecSDave Kleikamp 4228a9185b41SChristoph Hellwig if (wbc->sync_mode != WB_SYNC_ALL) 4229ac27a0ecSDave Kleikamp return 0; 4230ac27a0ecSDave Kleikamp 423191ac6f43SFrank Mayhar err = ext4_force_commit(inode->i_sb); 423291ac6f43SFrank Mayhar } else { 423391ac6f43SFrank Mayhar struct ext4_iloc iloc; 423491ac6f43SFrank Mayhar 42358b472d73SCurt Wohlgemuth err = __ext4_get_inode_loc(inode, &iloc, 0); 423691ac6f43SFrank Mayhar if (err) 423791ac6f43SFrank Mayhar return err; 4238a9185b41SChristoph Hellwig if (wbc->sync_mode == WB_SYNC_ALL) 4239830156c7SFrank Mayhar sync_dirty_buffer(iloc.bh); 4240830156c7SFrank Mayhar if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { 4241c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr, 4242c398eda0STheodore Ts'o "IO error syncing inode"); 4243830156c7SFrank Mayhar err = -EIO; 4244830156c7SFrank Mayhar } 4245fd2dd9fbSCurt Wohlgemuth brelse(iloc.bh); 424691ac6f43SFrank Mayhar } 424791ac6f43SFrank Mayhar return err; 4248ac27a0ecSDave Kleikamp } 4249ac27a0ecSDave Kleikamp 4250ac27a0ecSDave Kleikamp /* 425153e87268SJan Kara * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate 425253e87268SJan Kara * buffers that are attached to a page stradding i_size and are undergoing 425353e87268SJan Kara * commit. In that case we have to wait for commit to finish and try again. 425453e87268SJan Kara */ 425553e87268SJan Kara static void ext4_wait_for_tail_page_commit(struct inode *inode) 425653e87268SJan Kara { 425753e87268SJan Kara struct page *page; 425853e87268SJan Kara unsigned offset; 425953e87268SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 426053e87268SJan Kara tid_t commit_tid = 0; 426153e87268SJan Kara int ret; 426253e87268SJan Kara 426353e87268SJan Kara offset = inode->i_size & (PAGE_CACHE_SIZE - 1); 426453e87268SJan Kara /* 426553e87268SJan Kara * All buffers in the last page remain valid? Then there's nothing to 426653e87268SJan Kara * do. We do the check mainly to optimize the common PAGE_CACHE_SIZE == 426753e87268SJan Kara * blocksize case 426853e87268SJan Kara */ 426953e87268SJan Kara if (offset > PAGE_CACHE_SIZE - (1 << inode->i_blkbits)) 427053e87268SJan Kara return; 427153e87268SJan Kara while (1) { 427253e87268SJan Kara page = find_lock_page(inode->i_mapping, 427353e87268SJan Kara inode->i_size >> PAGE_CACHE_SHIFT); 427453e87268SJan Kara if (!page) 427553e87268SJan Kara return; 427653e87268SJan Kara ret = __ext4_journalled_invalidatepage(page, offset); 427753e87268SJan Kara unlock_page(page); 427853e87268SJan Kara page_cache_release(page); 427953e87268SJan Kara if (ret != -EBUSY) 428053e87268SJan Kara return; 428153e87268SJan Kara commit_tid = 0; 428253e87268SJan Kara read_lock(&journal->j_state_lock); 428353e87268SJan Kara if (journal->j_committing_transaction) 428453e87268SJan Kara commit_tid = journal->j_committing_transaction->t_tid; 428553e87268SJan Kara read_unlock(&journal->j_state_lock); 428653e87268SJan Kara if (commit_tid) 428753e87268SJan Kara jbd2_log_wait_commit(journal, commit_tid); 428853e87268SJan Kara } 428953e87268SJan Kara } 429053e87268SJan Kara 429153e87268SJan Kara /* 4292617ba13bSMingming Cao * ext4_setattr() 4293ac27a0ecSDave Kleikamp * 4294ac27a0ecSDave Kleikamp * Called from notify_change. 4295ac27a0ecSDave Kleikamp * 4296ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 4297ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 4298ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 4299ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 4300ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 4301ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 4302ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 4303ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 4304ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 4305ac27a0ecSDave Kleikamp * 4306678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 4307678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 4308678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 4309678aaf48SJan Kara * This way we are sure that all the data written in the previous 4310678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 4311678aaf48SJan Kara * writeback). 4312678aaf48SJan Kara * 4313678aaf48SJan Kara * Called with inode->i_mutex down. 4314ac27a0ecSDave Kleikamp */ 4315617ba13bSMingming Cao int ext4_setattr(struct dentry *dentry, struct iattr *attr) 4316ac27a0ecSDave Kleikamp { 4317ac27a0ecSDave Kleikamp struct inode *inode = dentry->d_inode; 4318ac27a0ecSDave Kleikamp int error, rc = 0; 43193d287de3SDmitry Monakhov int orphan = 0; 4320ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 4321ac27a0ecSDave Kleikamp 4322ac27a0ecSDave Kleikamp error = inode_change_ok(inode, attr); 4323ac27a0ecSDave Kleikamp if (error) 4324ac27a0ecSDave Kleikamp return error; 4325ac27a0ecSDave Kleikamp 432612755627SDmitry Monakhov if (is_quota_modification(inode, attr)) 4327871a2931SChristoph Hellwig dquot_initialize(inode); 432808cefc7aSEric W. Biederman if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) || 432908cefc7aSEric W. Biederman (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) { 4330ac27a0ecSDave Kleikamp handle_t *handle; 4331ac27a0ecSDave Kleikamp 4332ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 4333ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 43349924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 43359924a92aSTheodore Ts'o (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) + 4336194074acSDmitry Monakhov EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3); 4337ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 4338ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 4339ac27a0ecSDave Kleikamp goto err_out; 4340ac27a0ecSDave Kleikamp } 4341b43fa828SChristoph Hellwig error = dquot_transfer(inode, attr); 4342ac27a0ecSDave Kleikamp if (error) { 4343617ba13bSMingming Cao ext4_journal_stop(handle); 4344ac27a0ecSDave Kleikamp return error; 4345ac27a0ecSDave Kleikamp } 4346ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 4347ac27a0ecSDave Kleikamp * one transaction */ 4348ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_UID) 4349ac27a0ecSDave Kleikamp inode->i_uid = attr->ia_uid; 4350ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_GID) 4351ac27a0ecSDave Kleikamp inode->i_gid = attr->ia_gid; 4352617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 4353617ba13bSMingming Cao ext4_journal_stop(handle); 4354ac27a0ecSDave Kleikamp } 4355ac27a0ecSDave Kleikamp 4356e2b46574SEric Sandeen if (attr->ia_valid & ATTR_SIZE) { 4357562c72aaSChristoph Hellwig 435812e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 4359e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4360e2b46574SEric Sandeen 43610c095c7fSTheodore Ts'o if (attr->ia_size > sbi->s_bitmap_maxbytes) 43620c095c7fSTheodore Ts'o return -EFBIG; 4363e2b46574SEric Sandeen } 4364e2b46574SEric Sandeen } 4365e2b46574SEric Sandeen 4366ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode) && 4367c8d46e41SJiaying Zhang attr->ia_valid & ATTR_SIZE && 4368072bd7eaSTheodore Ts'o (attr->ia_size < inode->i_size)) { 4369ac27a0ecSDave Kleikamp handle_t *handle; 4370ac27a0ecSDave Kleikamp 43719924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 3); 4372ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 4373ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 4374ac27a0ecSDave Kleikamp goto err_out; 4375ac27a0ecSDave Kleikamp } 43763d287de3SDmitry Monakhov if (ext4_handle_valid(handle)) { 4377617ba13bSMingming Cao error = ext4_orphan_add(handle, inode); 43783d287de3SDmitry Monakhov orphan = 1; 43793d287de3SDmitry Monakhov } 4380617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 4381617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 4382ac27a0ecSDave Kleikamp if (!error) 4383ac27a0ecSDave Kleikamp error = rc; 4384617ba13bSMingming Cao ext4_journal_stop(handle); 4385678aaf48SJan Kara 4386678aaf48SJan Kara if (ext4_should_order_data(inode)) { 4387678aaf48SJan Kara error = ext4_begin_ordered_truncate(inode, 4388678aaf48SJan Kara attr->ia_size); 4389678aaf48SJan Kara if (error) { 4390678aaf48SJan Kara /* Do as much error cleanup as possible */ 43919924a92aSTheodore Ts'o handle = ext4_journal_start(inode, 43929924a92aSTheodore Ts'o EXT4_HT_INODE, 3); 4393678aaf48SJan Kara if (IS_ERR(handle)) { 4394678aaf48SJan Kara ext4_orphan_del(NULL, inode); 4395678aaf48SJan Kara goto err_out; 4396678aaf48SJan Kara } 4397678aaf48SJan Kara ext4_orphan_del(handle, inode); 43983d287de3SDmitry Monakhov orphan = 0; 4399678aaf48SJan Kara ext4_journal_stop(handle); 4400678aaf48SJan Kara goto err_out; 4401678aaf48SJan Kara } 4402678aaf48SJan Kara } 4403ac27a0ecSDave Kleikamp } 4404ac27a0ecSDave Kleikamp 4405072bd7eaSTheodore Ts'o if (attr->ia_valid & ATTR_SIZE) { 440653e87268SJan Kara if (attr->ia_size != inode->i_size) { 440753e87268SJan Kara loff_t oldsize = inode->i_size; 440853e87268SJan Kara 440953e87268SJan Kara i_size_write(inode, attr->ia_size); 441053e87268SJan Kara /* 441153e87268SJan Kara * Blocks are going to be removed from the inode. Wait 441253e87268SJan Kara * for dio in flight. Temporarily disable 441353e87268SJan Kara * dioread_nolock to prevent livelock. 441453e87268SJan Kara */ 44151b65007eSDmitry Monakhov if (orphan) { 441653e87268SJan Kara if (!ext4_should_journal_data(inode)) { 44171b65007eSDmitry Monakhov ext4_inode_block_unlocked_dio(inode); 44181c9114f9SDmitry Monakhov inode_dio_wait(inode); 44191b65007eSDmitry Monakhov ext4_inode_resume_unlocked_dio(inode); 442053e87268SJan Kara } else 442153e87268SJan Kara ext4_wait_for_tail_page_commit(inode); 44221b65007eSDmitry Monakhov } 442353e87268SJan Kara /* 442453e87268SJan Kara * Truncate pagecache after we've waited for commit 442553e87268SJan Kara * in data=journal mode to make pages freeable. 442653e87268SJan Kara */ 442753e87268SJan Kara truncate_pagecache(inode, oldsize, inode->i_size); 44281c9114f9SDmitry Monakhov } 4429072bd7eaSTheodore Ts'o ext4_truncate(inode); 4430072bd7eaSTheodore Ts'o } 4431ac27a0ecSDave Kleikamp 44321025774cSChristoph Hellwig if (!rc) { 44331025774cSChristoph Hellwig setattr_copy(inode, attr); 44341025774cSChristoph Hellwig mark_inode_dirty(inode); 44351025774cSChristoph Hellwig } 44361025774cSChristoph Hellwig 44371025774cSChristoph Hellwig /* 44381025774cSChristoph Hellwig * If the call to ext4_truncate failed to get a transaction handle at 44391025774cSChristoph Hellwig * all, we need to clean up the in-core orphan list manually. 44401025774cSChristoph Hellwig */ 44413d287de3SDmitry Monakhov if (orphan && inode->i_nlink) 4442617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 4443ac27a0ecSDave Kleikamp 4444ac27a0ecSDave Kleikamp if (!rc && (ia_valid & ATTR_MODE)) 4445617ba13bSMingming Cao rc = ext4_acl_chmod(inode); 4446ac27a0ecSDave Kleikamp 4447ac27a0ecSDave Kleikamp err_out: 4448617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 4449ac27a0ecSDave Kleikamp if (!error) 4450ac27a0ecSDave Kleikamp error = rc; 4451ac27a0ecSDave Kleikamp return error; 4452ac27a0ecSDave Kleikamp } 4453ac27a0ecSDave Kleikamp 44543e3398a0SMingming Cao int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry, 44553e3398a0SMingming Cao struct kstat *stat) 44563e3398a0SMingming Cao { 44573e3398a0SMingming Cao struct inode *inode; 44583e3398a0SMingming Cao unsigned long delalloc_blocks; 44593e3398a0SMingming Cao 44603e3398a0SMingming Cao inode = dentry->d_inode; 44613e3398a0SMingming Cao generic_fillattr(inode, stat); 44623e3398a0SMingming Cao 44633e3398a0SMingming Cao /* 44643e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 44653e3398a0SMingming Cao * otherwise in the case of system crash before the real block 44663e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 44673e3398a0SMingming Cao * on-disk file blocks. 44683e3398a0SMingming Cao * We always keep i_blocks updated together with real 44693e3398a0SMingming Cao * allocation. But to not confuse with user, stat 44703e3398a0SMingming Cao * will return the blocks that include the delayed allocation 44713e3398a0SMingming Cao * blocks for this file. 44723e3398a0SMingming Cao */ 447396607551STao Ma delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb), 447496607551STao Ma EXT4_I(inode)->i_reserved_data_blocks); 44753e3398a0SMingming Cao 44763e3398a0SMingming Cao stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9; 44773e3398a0SMingming Cao return 0; 44783e3398a0SMingming Cao } 4479ac27a0ecSDave Kleikamp 4480a02908f1SMingming Cao static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4481a02908f1SMingming Cao { 448212e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 44838bb2b247SAmir Goldstein return ext4_ind_trans_blocks(inode, nrblocks, chunk); 4484ac51d837STheodore Ts'o return ext4_ext_index_trans_blocks(inode, nrblocks, chunk); 4485a02908f1SMingming Cao } 4486ac51d837STheodore Ts'o 4487a02908f1SMingming Cao /* 4488a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 4489a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 4490a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 4491a02908f1SMingming Cao * 4492a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 44934907cb7bSAnatol Pomozov * different block groups too. If they are contiguous, with flexbg, 4494a02908f1SMingming Cao * they could still across block group boundary. 4495a02908f1SMingming Cao * 4496a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 4497a02908f1SMingming Cao */ 44981f109d5aSTheodore Ts'o static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4499a02908f1SMingming Cao { 45008df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 45018df9675fSTheodore Ts'o int gdpblocks; 4502a02908f1SMingming Cao int idxblocks; 4503a02908f1SMingming Cao int ret = 0; 4504a02908f1SMingming Cao 4505a02908f1SMingming Cao /* 4506a02908f1SMingming Cao * How many index blocks need to touch to modify nrblocks? 4507a02908f1SMingming Cao * The "Chunk" flag indicating whether the nrblocks is 4508a02908f1SMingming Cao * physically contiguous on disk 4509a02908f1SMingming Cao * 4510a02908f1SMingming Cao * For Direct IO and fallocate, they calls get_block to allocate 4511a02908f1SMingming Cao * one single extent at a time, so they could set the "Chunk" flag 4512a02908f1SMingming Cao */ 4513a02908f1SMingming Cao idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk); 4514a02908f1SMingming Cao 4515a02908f1SMingming Cao ret = idxblocks; 4516a02908f1SMingming Cao 4517a02908f1SMingming Cao /* 4518a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 4519a02908f1SMingming Cao * to account 4520a02908f1SMingming Cao */ 4521a02908f1SMingming Cao groups = idxblocks; 4522a02908f1SMingming Cao if (chunk) 4523a02908f1SMingming Cao groups += 1; 4524ac27a0ecSDave Kleikamp else 4525a02908f1SMingming Cao groups += nrblocks; 4526ac27a0ecSDave Kleikamp 4527a02908f1SMingming Cao gdpblocks = groups; 45288df9675fSTheodore Ts'o if (groups > ngroups) 45298df9675fSTheodore Ts'o groups = ngroups; 4530a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 4531a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 4532a02908f1SMingming Cao 4533a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 4534a02908f1SMingming Cao ret += groups + gdpblocks; 4535a02908f1SMingming Cao 4536a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 4537a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 4538ac27a0ecSDave Kleikamp 4539ac27a0ecSDave Kleikamp return ret; 4540ac27a0ecSDave Kleikamp } 4541ac27a0ecSDave Kleikamp 4542ac27a0ecSDave Kleikamp /* 454325985edcSLucas De Marchi * Calculate the total number of credits to reserve to fit 4544f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 4545f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 4546a02908f1SMingming Cao * 4547525f4ed8SMingming Cao * This could be called via ext4_write_begin() 4548a02908f1SMingming Cao * 4549525f4ed8SMingming Cao * We need to consider the worse case, when 4550a02908f1SMingming Cao * one new block per extent. 4551a02908f1SMingming Cao */ 4552a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 4553a02908f1SMingming Cao { 4554a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 4555a02908f1SMingming Cao int ret; 4556a02908f1SMingming Cao 4557a02908f1SMingming Cao ret = ext4_meta_trans_blocks(inode, bpp, 0); 4558a02908f1SMingming Cao 4559a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 4560a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 4561a02908f1SMingming Cao ret += bpp; 4562a02908f1SMingming Cao return ret; 4563a02908f1SMingming Cao } 4564f3bd1f3fSMingming Cao 4565f3bd1f3fSMingming Cao /* 4566f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 4567f3bd1f3fSMingming Cao * 4568f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 456979e83036SEric Sandeen * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks. 4570f3bd1f3fSMingming Cao * 4571f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 4572f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 4573f3bd1f3fSMingming Cao */ 4574f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 4575f3bd1f3fSMingming Cao { 4576f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 4577f3bd1f3fSMingming Cao } 4578f3bd1f3fSMingming Cao 4579a02908f1SMingming Cao /* 4580617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 4581ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 4582ac27a0ecSDave Kleikamp */ 4583617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 4584617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 4585ac27a0ecSDave Kleikamp { 4586ac27a0ecSDave Kleikamp int err = 0; 4587ac27a0ecSDave Kleikamp 4588c64db50eSTheodore Ts'o if (IS_I_VERSION(inode)) 458925ec56b5SJean Noel Cordenner inode_inc_iversion(inode); 459025ec56b5SJean Noel Cordenner 4591ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 4592ac27a0ecSDave Kleikamp get_bh(iloc->bh); 4593ac27a0ecSDave Kleikamp 4594dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 4595830156c7SFrank Mayhar err = ext4_do_update_inode(handle, inode, iloc); 4596ac27a0ecSDave Kleikamp put_bh(iloc->bh); 4597ac27a0ecSDave Kleikamp return err; 4598ac27a0ecSDave Kleikamp } 4599ac27a0ecSDave Kleikamp 4600ac27a0ecSDave Kleikamp /* 4601ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 4602ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 4603ac27a0ecSDave Kleikamp */ 4604ac27a0ecSDave Kleikamp 4605ac27a0ecSDave Kleikamp int 4606617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 4607617ba13bSMingming Cao struct ext4_iloc *iloc) 4608ac27a0ecSDave Kleikamp { 46090390131bSFrank Mayhar int err; 46100390131bSFrank Mayhar 4611617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 4612ac27a0ecSDave Kleikamp if (!err) { 4613ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 4614617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, iloc->bh); 4615ac27a0ecSDave Kleikamp if (err) { 4616ac27a0ecSDave Kleikamp brelse(iloc->bh); 4617ac27a0ecSDave Kleikamp iloc->bh = NULL; 4618ac27a0ecSDave Kleikamp } 4619ac27a0ecSDave Kleikamp } 4620617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4621ac27a0ecSDave Kleikamp return err; 4622ac27a0ecSDave Kleikamp } 4623ac27a0ecSDave Kleikamp 4624ac27a0ecSDave Kleikamp /* 46256dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 46266dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 46276dd4ee7cSKalpak Shah */ 46281d03ec98SAneesh Kumar K.V static int ext4_expand_extra_isize(struct inode *inode, 46291d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 46301d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 46311d03ec98SAneesh Kumar K.V handle_t *handle) 46326dd4ee7cSKalpak Shah { 46336dd4ee7cSKalpak Shah struct ext4_inode *raw_inode; 46346dd4ee7cSKalpak Shah struct ext4_xattr_ibody_header *header; 46356dd4ee7cSKalpak Shah 46366dd4ee7cSKalpak Shah if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) 46376dd4ee7cSKalpak Shah return 0; 46386dd4ee7cSKalpak Shah 46396dd4ee7cSKalpak Shah raw_inode = ext4_raw_inode(&iloc); 46406dd4ee7cSKalpak Shah 46416dd4ee7cSKalpak Shah header = IHDR(inode, raw_inode); 46426dd4ee7cSKalpak Shah 46436dd4ee7cSKalpak Shah /* No extended attributes present */ 464419f5fb7aSTheodore Ts'o if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) || 46456dd4ee7cSKalpak Shah header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 46466dd4ee7cSKalpak Shah memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0, 46476dd4ee7cSKalpak Shah new_extra_isize); 46486dd4ee7cSKalpak Shah EXT4_I(inode)->i_extra_isize = new_extra_isize; 46496dd4ee7cSKalpak Shah return 0; 46506dd4ee7cSKalpak Shah } 46516dd4ee7cSKalpak Shah 46526dd4ee7cSKalpak Shah /* try to expand with EAs present */ 46536dd4ee7cSKalpak Shah return ext4_expand_extra_isize_ea(inode, new_extra_isize, 46546dd4ee7cSKalpak Shah raw_inode, handle); 46556dd4ee7cSKalpak Shah } 46566dd4ee7cSKalpak Shah 46576dd4ee7cSKalpak Shah /* 4658ac27a0ecSDave Kleikamp * What we do here is to mark the in-core inode as clean with respect to inode 4659ac27a0ecSDave Kleikamp * dirtiness (it may still be data-dirty). 4660ac27a0ecSDave Kleikamp * This means that the in-core inode may be reaped by prune_icache 4661ac27a0ecSDave Kleikamp * without having to perform any I/O. This is a very good thing, 4662ac27a0ecSDave Kleikamp * because *any* task may call prune_icache - even ones which 4663ac27a0ecSDave Kleikamp * have a transaction open against a different journal. 4664ac27a0ecSDave Kleikamp * 4665ac27a0ecSDave Kleikamp * Is this cheating? Not really. Sure, we haven't written the 4666ac27a0ecSDave Kleikamp * inode out, but prune_icache isn't a user-visible syncing function. 4667ac27a0ecSDave Kleikamp * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 4668ac27a0ecSDave Kleikamp * we start and wait on commits. 4669ac27a0ecSDave Kleikamp */ 4670617ba13bSMingming Cao int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode) 4671ac27a0ecSDave Kleikamp { 4672617ba13bSMingming Cao struct ext4_iloc iloc; 46736dd4ee7cSKalpak Shah struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 46746dd4ee7cSKalpak Shah static unsigned int mnt_count; 46756dd4ee7cSKalpak Shah int err, ret; 4676ac27a0ecSDave Kleikamp 4677ac27a0ecSDave Kleikamp might_sleep(); 46787ff9c073STheodore Ts'o trace_ext4_mark_inode_dirty(inode, _RET_IP_); 4679617ba13bSMingming Cao err = ext4_reserve_inode_write(handle, inode, &iloc); 46800390131bSFrank Mayhar if (ext4_handle_valid(handle) && 46810390131bSFrank Mayhar EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize && 468219f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) { 46836dd4ee7cSKalpak Shah /* 46846dd4ee7cSKalpak Shah * We need extra buffer credits since we may write into EA block 46856dd4ee7cSKalpak Shah * with this same handle. If journal_extend fails, then it will 46866dd4ee7cSKalpak Shah * only result in a minor loss of functionality for that inode. 46876dd4ee7cSKalpak Shah * If this is felt to be critical, then e2fsck should be run to 46886dd4ee7cSKalpak Shah * force a large enough s_min_extra_isize. 46896dd4ee7cSKalpak Shah */ 46906dd4ee7cSKalpak Shah if ((jbd2_journal_extend(handle, 46916dd4ee7cSKalpak Shah EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) { 46926dd4ee7cSKalpak Shah ret = ext4_expand_extra_isize(inode, 46936dd4ee7cSKalpak Shah sbi->s_want_extra_isize, 46946dd4ee7cSKalpak Shah iloc, handle); 46956dd4ee7cSKalpak Shah if (ret) { 469619f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, 469719f5fb7aSTheodore Ts'o EXT4_STATE_NO_EXPAND); 4698c1bddad9SAneesh Kumar K.V if (mnt_count != 4699c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count)) { 470012062dddSEric Sandeen ext4_warning(inode->i_sb, 47016dd4ee7cSKalpak Shah "Unable to expand inode %lu. Delete" 47026dd4ee7cSKalpak Shah " some EAs or run e2fsck.", 47036dd4ee7cSKalpak Shah inode->i_ino); 4704c1bddad9SAneesh Kumar K.V mnt_count = 4705c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count); 47066dd4ee7cSKalpak Shah } 47076dd4ee7cSKalpak Shah } 47086dd4ee7cSKalpak Shah } 47096dd4ee7cSKalpak Shah } 4710ac27a0ecSDave Kleikamp if (!err) 4711617ba13bSMingming Cao err = ext4_mark_iloc_dirty(handle, inode, &iloc); 4712ac27a0ecSDave Kleikamp return err; 4713ac27a0ecSDave Kleikamp } 4714ac27a0ecSDave Kleikamp 4715ac27a0ecSDave Kleikamp /* 4716617ba13bSMingming Cao * ext4_dirty_inode() is called from __mark_inode_dirty() 4717ac27a0ecSDave Kleikamp * 4718ac27a0ecSDave Kleikamp * We're really interested in the case where a file is being extended. 4719ac27a0ecSDave Kleikamp * i_size has been changed by generic_commit_write() and we thus need 4720ac27a0ecSDave Kleikamp * to include the updated inode in the current transaction. 4721ac27a0ecSDave Kleikamp * 47225dd4056dSChristoph Hellwig * Also, dquot_alloc_block() will always dirty the inode when blocks 4723ac27a0ecSDave Kleikamp * are allocated to the file. 4724ac27a0ecSDave Kleikamp * 4725ac27a0ecSDave Kleikamp * If the inode is marked synchronous, we don't honour that here - doing 4726ac27a0ecSDave Kleikamp * so would cause a commit on atime updates, which we don't bother doing. 4727ac27a0ecSDave Kleikamp * We handle synchronous inodes at the highest possible level. 4728ac27a0ecSDave Kleikamp */ 4729aa385729SChristoph Hellwig void ext4_dirty_inode(struct inode *inode, int flags) 4730ac27a0ecSDave Kleikamp { 4731ac27a0ecSDave Kleikamp handle_t *handle; 4732ac27a0ecSDave Kleikamp 47339924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 2); 4734ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 4735ac27a0ecSDave Kleikamp goto out; 4736f3dc272fSCurt Wohlgemuth 4737617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 4738f3dc272fSCurt Wohlgemuth 4739617ba13bSMingming Cao ext4_journal_stop(handle); 4740ac27a0ecSDave Kleikamp out: 4741ac27a0ecSDave Kleikamp return; 4742ac27a0ecSDave Kleikamp } 4743ac27a0ecSDave Kleikamp 4744ac27a0ecSDave Kleikamp #if 0 4745ac27a0ecSDave Kleikamp /* 4746ac27a0ecSDave Kleikamp * Bind an inode's backing buffer_head into this transaction, to prevent 4747ac27a0ecSDave Kleikamp * it from being flushed to disk early. Unlike 4748617ba13bSMingming Cao * ext4_reserve_inode_write, this leaves behind no bh reference and 4749ac27a0ecSDave Kleikamp * returns no iloc structure, so the caller needs to repeat the iloc 4750ac27a0ecSDave Kleikamp * lookup to mark the inode dirty later. 4751ac27a0ecSDave Kleikamp */ 4752617ba13bSMingming Cao static int ext4_pin_inode(handle_t *handle, struct inode *inode) 4753ac27a0ecSDave Kleikamp { 4754617ba13bSMingming Cao struct ext4_iloc iloc; 4755ac27a0ecSDave Kleikamp 4756ac27a0ecSDave Kleikamp int err = 0; 4757ac27a0ecSDave Kleikamp if (handle) { 4758617ba13bSMingming Cao err = ext4_get_inode_loc(inode, &iloc); 4759ac27a0ecSDave Kleikamp if (!err) { 4760ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc.bh, "get_write_access"); 4761dab291afSMingming Cao err = jbd2_journal_get_write_access(handle, iloc.bh); 4762ac27a0ecSDave Kleikamp if (!err) 47630390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, 476473b50c1cSCurt Wohlgemuth NULL, 4765ac27a0ecSDave Kleikamp iloc.bh); 4766ac27a0ecSDave Kleikamp brelse(iloc.bh); 4767ac27a0ecSDave Kleikamp } 4768ac27a0ecSDave Kleikamp } 4769617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4770ac27a0ecSDave Kleikamp return err; 4771ac27a0ecSDave Kleikamp } 4772ac27a0ecSDave Kleikamp #endif 4773ac27a0ecSDave Kleikamp 4774617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val) 4775ac27a0ecSDave Kleikamp { 4776ac27a0ecSDave Kleikamp journal_t *journal; 4777ac27a0ecSDave Kleikamp handle_t *handle; 4778ac27a0ecSDave Kleikamp int err; 4779ac27a0ecSDave Kleikamp 4780ac27a0ecSDave Kleikamp /* 4781ac27a0ecSDave Kleikamp * We have to be very careful here: changing a data block's 4782ac27a0ecSDave Kleikamp * journaling status dynamically is dangerous. If we write a 4783ac27a0ecSDave Kleikamp * data block to the journal, change the status and then delete 4784ac27a0ecSDave Kleikamp * that block, we risk forgetting to revoke the old log record 4785ac27a0ecSDave Kleikamp * from the journal and so a subsequent replay can corrupt data. 4786ac27a0ecSDave Kleikamp * So, first we make sure that the journal is empty and that 4787ac27a0ecSDave Kleikamp * nobody is changing anything. 4788ac27a0ecSDave Kleikamp */ 4789ac27a0ecSDave Kleikamp 4790617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 47910390131bSFrank Mayhar if (!journal) 47920390131bSFrank Mayhar return 0; 4793d699594dSDave Hansen if (is_journal_aborted(journal)) 4794ac27a0ecSDave Kleikamp return -EROFS; 47952aff57b0SYongqiang Yang /* We have to allocate physical blocks for delalloc blocks 47962aff57b0SYongqiang Yang * before flushing journal. otherwise delalloc blocks can not 47972aff57b0SYongqiang Yang * be allocated any more. even more truncate on delalloc blocks 47982aff57b0SYongqiang Yang * could trigger BUG by flushing delalloc blocks in journal. 47992aff57b0SYongqiang Yang * There is no delalloc block in non-journal data mode. 48002aff57b0SYongqiang Yang */ 48012aff57b0SYongqiang Yang if (val && test_opt(inode->i_sb, DELALLOC)) { 48022aff57b0SYongqiang Yang err = ext4_alloc_da_blocks(inode); 48032aff57b0SYongqiang Yang if (err < 0) 48042aff57b0SYongqiang Yang return err; 48052aff57b0SYongqiang Yang } 4806ac27a0ecSDave Kleikamp 480717335dccSDmitry Monakhov /* Wait for all existing dio workers */ 480817335dccSDmitry Monakhov ext4_inode_block_unlocked_dio(inode); 480917335dccSDmitry Monakhov inode_dio_wait(inode); 481017335dccSDmitry Monakhov 4811dab291afSMingming Cao jbd2_journal_lock_updates(journal); 4812ac27a0ecSDave Kleikamp 4813ac27a0ecSDave Kleikamp /* 4814ac27a0ecSDave Kleikamp * OK, there are no updates running now, and all cached data is 4815ac27a0ecSDave Kleikamp * synced to disk. We are now in a completely consistent state 4816ac27a0ecSDave Kleikamp * which doesn't have anything in the journal, and we know that 4817ac27a0ecSDave Kleikamp * no filesystem updates are running, so it is safe to modify 4818ac27a0ecSDave Kleikamp * the inode's in-core data-journaling state flag now. 4819ac27a0ecSDave Kleikamp */ 4820ac27a0ecSDave Kleikamp 4821ac27a0ecSDave Kleikamp if (val) 482212e9b892SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 48235872ddaaSYongqiang Yang else { 48245872ddaaSYongqiang Yang jbd2_journal_flush(journal); 482512e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 48265872ddaaSYongqiang Yang } 4827617ba13bSMingming Cao ext4_set_aops(inode); 4828ac27a0ecSDave Kleikamp 4829dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 483017335dccSDmitry Monakhov ext4_inode_resume_unlocked_dio(inode); 4831ac27a0ecSDave Kleikamp 4832ac27a0ecSDave Kleikamp /* Finally we can mark the inode as dirty. */ 4833ac27a0ecSDave Kleikamp 48349924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 4835ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 4836ac27a0ecSDave Kleikamp return PTR_ERR(handle); 4837ac27a0ecSDave Kleikamp 4838617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 48390390131bSFrank Mayhar ext4_handle_sync(handle); 4840617ba13bSMingming Cao ext4_journal_stop(handle); 4841617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4842ac27a0ecSDave Kleikamp 4843ac27a0ecSDave Kleikamp return err; 4844ac27a0ecSDave Kleikamp } 48452e9ee850SAneesh Kumar K.V 48462e9ee850SAneesh Kumar K.V static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) 48472e9ee850SAneesh Kumar K.V { 48482e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 48492e9ee850SAneesh Kumar K.V } 48502e9ee850SAneesh Kumar K.V 4851c2ec175cSNick Piggin int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) 48522e9ee850SAneesh Kumar K.V { 4853c2ec175cSNick Piggin struct page *page = vmf->page; 48542e9ee850SAneesh Kumar K.V loff_t size; 48552e9ee850SAneesh Kumar K.V unsigned long len; 48569ea7df53SJan Kara int ret; 48572e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 48582e9ee850SAneesh Kumar K.V struct inode *inode = file->f_path.dentry->d_inode; 48592e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 48609ea7df53SJan Kara handle_t *handle; 48619ea7df53SJan Kara get_block_t *get_block; 48629ea7df53SJan Kara int retries = 0; 48632e9ee850SAneesh Kumar K.V 48648e8ad8a5SJan Kara sb_start_pagefault(inode->i_sb); 4865041bbb6dSTheodore Ts'o file_update_time(vma->vm_file); 48669ea7df53SJan Kara /* Delalloc case is easy... */ 48679ea7df53SJan Kara if (test_opt(inode->i_sb, DELALLOC) && 48689ea7df53SJan Kara !ext4_should_journal_data(inode) && 48699ea7df53SJan Kara !ext4_nonda_switch(inode->i_sb)) { 48709ea7df53SJan Kara do { 48719ea7df53SJan Kara ret = __block_page_mkwrite(vma, vmf, 48729ea7df53SJan Kara ext4_da_get_block_prep); 48739ea7df53SJan Kara } while (ret == -ENOSPC && 48749ea7df53SJan Kara ext4_should_retry_alloc(inode->i_sb, &retries)); 48759ea7df53SJan Kara goto out_ret; 48762e9ee850SAneesh Kumar K.V } 48770e499890SDarrick J. Wong 48780e499890SDarrick J. Wong lock_page(page); 48799ea7df53SJan Kara size = i_size_read(inode); 48809ea7df53SJan Kara /* Page got truncated from under us? */ 48819ea7df53SJan Kara if (page->mapping != mapping || page_offset(page) > size) { 48829ea7df53SJan Kara unlock_page(page); 48839ea7df53SJan Kara ret = VM_FAULT_NOPAGE; 48849ea7df53SJan Kara goto out; 48850e499890SDarrick J. Wong } 48862e9ee850SAneesh Kumar K.V 48872e9ee850SAneesh Kumar K.V if (page->index == size >> PAGE_CACHE_SHIFT) 48882e9ee850SAneesh Kumar K.V len = size & ~PAGE_CACHE_MASK; 48892e9ee850SAneesh Kumar K.V else 48902e9ee850SAneesh Kumar K.V len = PAGE_CACHE_SIZE; 4891a827eaffSAneesh Kumar K.V /* 48929ea7df53SJan Kara * Return if we have all the buffers mapped. This avoids the need to do 48939ea7df53SJan Kara * journal_start/journal_stop which can block and take a long time 4894a827eaffSAneesh Kumar K.V */ 48952e9ee850SAneesh Kumar K.V if (page_has_buffers(page)) { 4896f19d5870STao Ma if (!ext4_walk_page_buffers(NULL, page_buffers(page), 4897f19d5870STao Ma 0, len, NULL, 4898a827eaffSAneesh Kumar K.V ext4_bh_unmapped)) { 48999ea7df53SJan Kara /* Wait so that we don't change page under IO */ 49009ea7df53SJan Kara wait_on_page_writeback(page); 49019ea7df53SJan Kara ret = VM_FAULT_LOCKED; 49029ea7df53SJan Kara goto out; 49032e9ee850SAneesh Kumar K.V } 4904a827eaffSAneesh Kumar K.V } 4905a827eaffSAneesh Kumar K.V unlock_page(page); 49069ea7df53SJan Kara /* OK, we need to fill the hole... */ 49079ea7df53SJan Kara if (ext4_should_dioread_nolock(inode)) 49089ea7df53SJan Kara get_block = ext4_get_block_write; 49099ea7df53SJan Kara else 49109ea7df53SJan Kara get_block = ext4_get_block; 49119ea7df53SJan Kara retry_alloc: 49129924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 49139924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 49149ea7df53SJan Kara if (IS_ERR(handle)) { 4915c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 49169ea7df53SJan Kara goto out; 49179ea7df53SJan Kara } 49189ea7df53SJan Kara ret = __block_page_mkwrite(vma, vmf, get_block); 49199ea7df53SJan Kara if (!ret && ext4_should_journal_data(inode)) { 4920f19d5870STao Ma if (ext4_walk_page_buffers(handle, page_buffers(page), 0, 49219ea7df53SJan Kara PAGE_CACHE_SIZE, NULL, do_journal_get_write_access)) { 49229ea7df53SJan Kara unlock_page(page); 49239ea7df53SJan Kara ret = VM_FAULT_SIGBUS; 4924fcbb5515SYongqiang Yang ext4_journal_stop(handle); 49259ea7df53SJan Kara goto out; 49269ea7df53SJan Kara } 49279ea7df53SJan Kara ext4_set_inode_state(inode, EXT4_STATE_JDATA); 49289ea7df53SJan Kara } 49299ea7df53SJan Kara ext4_journal_stop(handle); 49309ea7df53SJan Kara if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 49319ea7df53SJan Kara goto retry_alloc; 49329ea7df53SJan Kara out_ret: 49339ea7df53SJan Kara ret = block_page_mkwrite_return(ret); 49349ea7df53SJan Kara out: 49358e8ad8a5SJan Kara sb_end_pagefault(inode->i_sb); 49362e9ee850SAneesh Kumar K.V return ret; 49372e9ee850SAneesh Kumar K.V } 4938