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> 40*a27bb332SKent Overstreet #include <linux/aio.h> 419bffad1eSTheodore Ts'o 423dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 43ac27a0ecSDave Kleikamp #include "xattr.h" 44ac27a0ecSDave Kleikamp #include "acl.h" 459f125d64STheodore Ts'o #include "truncate.h" 46ac27a0ecSDave Kleikamp 479bffad1eSTheodore Ts'o #include <trace/events/ext4.h> 489bffad1eSTheodore Ts'o 49a1d6cc56SAneesh Kumar K.V #define MPAGE_DA_EXTENT_TAIL 0x01 50a1d6cc56SAneesh Kumar K.V 51814525f4SDarrick J. Wong static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw, 52814525f4SDarrick J. Wong struct ext4_inode_info *ei) 53814525f4SDarrick J. Wong { 54814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 55814525f4SDarrick J. Wong __u16 csum_lo; 56814525f4SDarrick J. Wong __u16 csum_hi = 0; 57814525f4SDarrick J. Wong __u32 csum; 58814525f4SDarrick J. Wong 59171a7f21SDmitry Monakhov csum_lo = le16_to_cpu(raw->i_checksum_lo); 60814525f4SDarrick J. Wong raw->i_checksum_lo = 0; 61814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 62814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) { 63171a7f21SDmitry Monakhov csum_hi = le16_to_cpu(raw->i_checksum_hi); 64814525f4SDarrick J. Wong raw->i_checksum_hi = 0; 65814525f4SDarrick J. Wong } 66814525f4SDarrick J. Wong 67814525f4SDarrick J. Wong csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw, 68814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)); 69814525f4SDarrick J. Wong 70171a7f21SDmitry Monakhov raw->i_checksum_lo = cpu_to_le16(csum_lo); 71814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 72814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 73171a7f21SDmitry Monakhov raw->i_checksum_hi = cpu_to_le16(csum_hi); 74814525f4SDarrick J. Wong 75814525f4SDarrick J. Wong return csum; 76814525f4SDarrick J. Wong } 77814525f4SDarrick J. Wong 78814525f4SDarrick J. Wong static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw, 79814525f4SDarrick J. Wong struct ext4_inode_info *ei) 80814525f4SDarrick J. Wong { 81814525f4SDarrick J. Wong __u32 provided, calculated; 82814525f4SDarrick J. Wong 83814525f4SDarrick J. Wong if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 84814525f4SDarrick J. Wong cpu_to_le32(EXT4_OS_LINUX) || 85814525f4SDarrick J. Wong !EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, 86814525f4SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) 87814525f4SDarrick J. Wong return 1; 88814525f4SDarrick J. Wong 89814525f4SDarrick J. Wong provided = le16_to_cpu(raw->i_checksum_lo); 90814525f4SDarrick J. Wong calculated = ext4_inode_csum(inode, raw, ei); 91814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 92814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 93814525f4SDarrick J. Wong provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16; 94814525f4SDarrick J. Wong else 95814525f4SDarrick J. Wong calculated &= 0xFFFF; 96814525f4SDarrick J. Wong 97814525f4SDarrick J. Wong return provided == calculated; 98814525f4SDarrick J. Wong } 99814525f4SDarrick J. Wong 100814525f4SDarrick J. Wong static void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw, 101814525f4SDarrick J. Wong struct ext4_inode_info *ei) 102814525f4SDarrick J. Wong { 103814525f4SDarrick J. Wong __u32 csum; 104814525f4SDarrick J. Wong 105814525f4SDarrick J. Wong if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 106814525f4SDarrick J. Wong cpu_to_le32(EXT4_OS_LINUX) || 107814525f4SDarrick J. Wong !EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, 108814525f4SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) 109814525f4SDarrick J. Wong return; 110814525f4SDarrick J. Wong 111814525f4SDarrick J. Wong csum = ext4_inode_csum(inode, raw, ei); 112814525f4SDarrick J. Wong raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF); 113814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 114814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 115814525f4SDarrick J. Wong raw->i_checksum_hi = cpu_to_le16(csum >> 16); 116814525f4SDarrick J. Wong } 117814525f4SDarrick J. Wong 118678aaf48SJan Kara static inline int ext4_begin_ordered_truncate(struct inode *inode, 119678aaf48SJan Kara loff_t new_size) 120678aaf48SJan Kara { 1217ff9c073STheodore Ts'o trace_ext4_begin_ordered_truncate(inode, new_size); 1228aefcd55STheodore Ts'o /* 1238aefcd55STheodore Ts'o * If jinode is zero, then we never opened the file for 1248aefcd55STheodore Ts'o * writing, so there's no need to call 1258aefcd55STheodore Ts'o * jbd2_journal_begin_ordered_truncate() since there's no 1268aefcd55STheodore Ts'o * outstanding writes we need to flush. 1278aefcd55STheodore Ts'o */ 1288aefcd55STheodore Ts'o if (!EXT4_I(inode)->jinode) 1298aefcd55STheodore Ts'o return 0; 1308aefcd55STheodore Ts'o return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode), 1318aefcd55STheodore Ts'o EXT4_I(inode)->jinode, 132678aaf48SJan Kara new_size); 133678aaf48SJan Kara } 134678aaf48SJan Kara 13564769240SAlex Tomas static void ext4_invalidatepage(struct page *page, unsigned long offset); 136cb20d518STheodore Ts'o static int __ext4_journalled_writepage(struct page *page, unsigned int len); 137cb20d518STheodore Ts'o static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh); 1385f163cc7SEric Sandeen static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle, 1395f163cc7SEric Sandeen struct inode *inode, struct page *page, loff_t from, 1405f163cc7SEric Sandeen loff_t length, int flags); 14164769240SAlex Tomas 142ac27a0ecSDave Kleikamp /* 143ac27a0ecSDave Kleikamp * Test whether an inode is a fast symlink. 144ac27a0ecSDave Kleikamp */ 145617ba13bSMingming Cao static int ext4_inode_is_fast_symlink(struct inode *inode) 146ac27a0ecSDave Kleikamp { 147617ba13bSMingming Cao int ea_blocks = EXT4_I(inode)->i_file_acl ? 148ac27a0ecSDave Kleikamp (inode->i_sb->s_blocksize >> 9) : 0; 149ac27a0ecSDave Kleikamp 150ac27a0ecSDave Kleikamp return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0); 151ac27a0ecSDave Kleikamp } 152ac27a0ecSDave Kleikamp 153ac27a0ecSDave Kleikamp /* 154ac27a0ecSDave Kleikamp * Restart the transaction associated with *handle. This does a commit, 155ac27a0ecSDave Kleikamp * so before we call here everything must be consistently dirtied against 156ac27a0ecSDave Kleikamp * this transaction. 157ac27a0ecSDave Kleikamp */ 158487caeefSJan Kara int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode, 159487caeefSJan Kara int nblocks) 160ac27a0ecSDave Kleikamp { 161487caeefSJan Kara int ret; 162487caeefSJan Kara 163487caeefSJan Kara /* 164e35fd660STheodore Ts'o * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this 165487caeefSJan Kara * moment, get_block can be called only for blocks inside i_size since 166487caeefSJan Kara * page cache has been already dropped and writes are blocked by 167487caeefSJan Kara * i_mutex. So we can safely drop the i_data_sem here. 168487caeefSJan Kara */ 1690390131bSFrank Mayhar BUG_ON(EXT4_JOURNAL(inode) == NULL); 170ac27a0ecSDave Kleikamp jbd_debug(2, "restarting handle %p\n", handle); 171487caeefSJan Kara up_write(&EXT4_I(inode)->i_data_sem); 1728e8eaabeSAmir Goldstein ret = ext4_journal_restart(handle, nblocks); 173487caeefSJan Kara down_write(&EXT4_I(inode)->i_data_sem); 174fa5d1113SAneesh Kumar K.V ext4_discard_preallocations(inode); 175487caeefSJan Kara 176487caeefSJan Kara return ret; 177ac27a0ecSDave Kleikamp } 178ac27a0ecSDave Kleikamp 179ac27a0ecSDave Kleikamp /* 180ac27a0ecSDave Kleikamp * Called at the last iput() if i_nlink is zero. 181ac27a0ecSDave Kleikamp */ 1820930fcc1SAl Viro void ext4_evict_inode(struct inode *inode) 183ac27a0ecSDave Kleikamp { 184ac27a0ecSDave Kleikamp handle_t *handle; 185bc965ab3STheodore Ts'o int err; 186ac27a0ecSDave Kleikamp 1877ff9c073STheodore Ts'o trace_ext4_evict_inode(inode); 1882581fdc8SJiaying Zhang 1890930fcc1SAl Viro if (inode->i_nlink) { 1902d859db3SJan Kara /* 1912d859db3SJan Kara * When journalling data dirty buffers are tracked only in the 1922d859db3SJan Kara * journal. So although mm thinks everything is clean and 1932d859db3SJan Kara * ready for reaping the inode might still have some pages to 1942d859db3SJan Kara * write in the running transaction or waiting to be 1952d859db3SJan Kara * checkpointed. Thus calling jbd2_journal_invalidatepage() 1962d859db3SJan Kara * (via truncate_inode_pages()) to discard these buffers can 1972d859db3SJan Kara * cause data loss. Also even if we did not discard these 1982d859db3SJan Kara * buffers, we would have no way to find them after the inode 1992d859db3SJan Kara * is reaped and thus user could see stale data if he tries to 2002d859db3SJan Kara * read them before the transaction is checkpointed. So be 2012d859db3SJan Kara * careful and force everything to disk here... We use 2022d859db3SJan Kara * ei->i_datasync_tid to store the newest transaction 2032d859db3SJan Kara * containing inode's data. 2042d859db3SJan Kara * 2052d859db3SJan Kara * Note that directories do not have this problem because they 2062d859db3SJan Kara * don't use page cache. 2072d859db3SJan Kara */ 2082d859db3SJan Kara if (ext4_should_journal_data(inode) && 2092b405bfaSTheodore Ts'o (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode)) && 2102b405bfaSTheodore Ts'o inode->i_ino != EXT4_JOURNAL_INO) { 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 214d76a3a77STheodore Ts'o jbd2_complete_transaction(journal, commit_tid); 2152d859db3SJan Kara filemap_write_and_wait(&inode->i_data); 2162d859db3SJan Kara } 2170930fcc1SAl Viro truncate_inode_pages(&inode->i_data, 0); 2181ada47d9STheodore Ts'o ext4_ioend_shutdown(inode); 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); 2281ada47d9STheodore Ts'o ext4_ioend_shutdown(inode); 229ac27a0ecSDave Kleikamp 230ac27a0ecSDave Kleikamp if (is_bad_inode(inode)) 231ac27a0ecSDave Kleikamp goto no_delete; 232ac27a0ecSDave Kleikamp 2338e8ad8a5SJan Kara /* 2348e8ad8a5SJan Kara * Protect us against freezing - iput() caller didn't have to have any 2358e8ad8a5SJan Kara * protection against it 2368e8ad8a5SJan Kara */ 2378e8ad8a5SJan Kara sb_start_intwrite(inode->i_sb); 2389924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, 2399924a92aSTheodore Ts'o ext4_blocks_for_truncate(inode)+3); 240ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 241bc965ab3STheodore Ts'o ext4_std_error(inode->i_sb, PTR_ERR(handle)); 242ac27a0ecSDave Kleikamp /* 243ac27a0ecSDave Kleikamp * If we're going to skip the normal cleanup, we still need to 244ac27a0ecSDave Kleikamp * make sure that the in-core orphan linked list is properly 245ac27a0ecSDave Kleikamp * cleaned up. 246ac27a0ecSDave Kleikamp */ 247617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 2488e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 249ac27a0ecSDave Kleikamp goto no_delete; 250ac27a0ecSDave Kleikamp } 251ac27a0ecSDave Kleikamp 252ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 2530390131bSFrank Mayhar ext4_handle_sync(handle); 254ac27a0ecSDave Kleikamp inode->i_size = 0; 255bc965ab3STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 256bc965ab3STheodore Ts'o if (err) { 25712062dddSEric Sandeen ext4_warning(inode->i_sb, 258bc965ab3STheodore Ts'o "couldn't mark inode dirty (err %d)", err); 259bc965ab3STheodore Ts'o goto stop_handle; 260bc965ab3STheodore Ts'o } 261ac27a0ecSDave Kleikamp if (inode->i_blocks) 262617ba13bSMingming Cao ext4_truncate(inode); 263bc965ab3STheodore Ts'o 264bc965ab3STheodore Ts'o /* 265bc965ab3STheodore Ts'o * ext4_ext_truncate() doesn't reserve any slop when it 266bc965ab3STheodore Ts'o * restarts journal transactions; therefore there may not be 267bc965ab3STheodore Ts'o * enough credits left in the handle to remove the inode from 268bc965ab3STheodore Ts'o * the orphan list and set the dtime field. 269bc965ab3STheodore Ts'o */ 2700390131bSFrank Mayhar if (!ext4_handle_has_enough_credits(handle, 3)) { 271bc965ab3STheodore Ts'o err = ext4_journal_extend(handle, 3); 272bc965ab3STheodore Ts'o if (err > 0) 273bc965ab3STheodore Ts'o err = ext4_journal_restart(handle, 3); 274bc965ab3STheodore Ts'o if (err != 0) { 27512062dddSEric Sandeen ext4_warning(inode->i_sb, 276bc965ab3STheodore Ts'o "couldn't extend journal (err %d)", err); 277bc965ab3STheodore Ts'o stop_handle: 278bc965ab3STheodore Ts'o ext4_journal_stop(handle); 27945388219STheodore Ts'o ext4_orphan_del(NULL, inode); 2808e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 281bc965ab3STheodore Ts'o goto no_delete; 282bc965ab3STheodore Ts'o } 283bc965ab3STheodore Ts'o } 284bc965ab3STheodore Ts'o 285ac27a0ecSDave Kleikamp /* 286617ba13bSMingming Cao * Kill off the orphan record which ext4_truncate created. 287ac27a0ecSDave Kleikamp * AKPM: I think this can be inside the above `if'. 288617ba13bSMingming Cao * Note that ext4_orphan_del() has to be able to cope with the 289ac27a0ecSDave Kleikamp * deletion of a non-existent orphan - this is because we don't 290617ba13bSMingming Cao * know if ext4_truncate() actually created an orphan record. 291ac27a0ecSDave Kleikamp * (Well, we could do this if we need to, but heck - it works) 292ac27a0ecSDave Kleikamp */ 293617ba13bSMingming Cao ext4_orphan_del(handle, inode); 294617ba13bSMingming Cao EXT4_I(inode)->i_dtime = get_seconds(); 295ac27a0ecSDave Kleikamp 296ac27a0ecSDave Kleikamp /* 297ac27a0ecSDave Kleikamp * One subtle ordering requirement: if anything has gone wrong 298ac27a0ecSDave Kleikamp * (transaction abort, IO errors, whatever), then we can still 299ac27a0ecSDave Kleikamp * do these next steps (the fs will already have been marked as 300ac27a0ecSDave Kleikamp * having errors), but we can't free the inode if the mark_dirty 301ac27a0ecSDave Kleikamp * fails. 302ac27a0ecSDave Kleikamp */ 303617ba13bSMingming Cao if (ext4_mark_inode_dirty(handle, inode)) 304ac27a0ecSDave Kleikamp /* If that failed, just do the required in-core inode clear. */ 3050930fcc1SAl Viro ext4_clear_inode(inode); 306ac27a0ecSDave Kleikamp else 307617ba13bSMingming Cao ext4_free_inode(handle, inode); 308617ba13bSMingming Cao ext4_journal_stop(handle); 3098e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 310ac27a0ecSDave Kleikamp return; 311ac27a0ecSDave Kleikamp no_delete: 3120930fcc1SAl Viro ext4_clear_inode(inode); /* We must guarantee clearing of inode... */ 313ac27a0ecSDave Kleikamp } 314ac27a0ecSDave Kleikamp 315a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 316a9e7f447SDmitry Monakhov qsize_t *ext4_get_reserved_space(struct inode *inode) 31760e58e0fSMingming Cao { 318a9e7f447SDmitry Monakhov return &EXT4_I(inode)->i_reserved_quota; 31960e58e0fSMingming Cao } 320a9e7f447SDmitry Monakhov #endif 3219d0be502STheodore Ts'o 32212219aeaSAneesh Kumar K.V /* 32312219aeaSAneesh Kumar K.V * Calculate the number of metadata blocks need to reserve 3249d0be502STheodore Ts'o * to allocate a block located at @lblock 32512219aeaSAneesh Kumar K.V */ 32601f49d0bSTheodore Ts'o static int ext4_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock) 32712219aeaSAneesh Kumar K.V { 32812e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3299d0be502STheodore Ts'o return ext4_ext_calc_metadata_amount(inode, lblock); 33012219aeaSAneesh Kumar K.V 3318bb2b247SAmir Goldstein return ext4_ind_calc_metadata_amount(inode, lblock); 33212219aeaSAneesh Kumar K.V } 33312219aeaSAneesh Kumar K.V 3340637c6f4STheodore Ts'o /* 3350637c6f4STheodore Ts'o * Called with i_data_sem down, which is important since we can call 3360637c6f4STheodore Ts'o * ext4_discard_preallocations() from here. 3370637c6f4STheodore Ts'o */ 3385f634d06SAneesh Kumar K.V void ext4_da_update_reserve_space(struct inode *inode, 3395f634d06SAneesh Kumar K.V int used, int quota_claim) 34012219aeaSAneesh Kumar K.V { 34112219aeaSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3420637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 34312219aeaSAneesh Kumar K.V 3440637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 345d8990240SAditya Kali trace_ext4_da_update_reserve_space(inode, used, quota_claim); 3460637c6f4STheodore Ts'o if (unlikely(used > ei->i_reserved_data_blocks)) { 3478de5c325STheodore Ts'o ext4_warning(inode->i_sb, "%s: ino %lu, used %d " 3481084f252STheodore Ts'o "with only %d reserved data blocks", 3490637c6f4STheodore Ts'o __func__, inode->i_ino, used, 3500637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 3510637c6f4STheodore Ts'o WARN_ON(1); 3520637c6f4STheodore Ts'o used = ei->i_reserved_data_blocks; 3536bc6e63fSAneesh Kumar K.V } 35412219aeaSAneesh Kumar K.V 35597795d2aSBrian Foster if (unlikely(ei->i_allocated_meta_blocks > ei->i_reserved_meta_blocks)) { 35601a523ebSTheodore Ts'o ext4_warning(inode->i_sb, "ino %lu, allocated %d " 35701a523ebSTheodore Ts'o "with only %d reserved metadata blocks " 35801a523ebSTheodore Ts'o "(releasing %d blocks with reserved %d data blocks)", 35997795d2aSBrian Foster inode->i_ino, ei->i_allocated_meta_blocks, 36001a523ebSTheodore Ts'o ei->i_reserved_meta_blocks, used, 36101a523ebSTheodore Ts'o ei->i_reserved_data_blocks); 36297795d2aSBrian Foster WARN_ON(1); 36397795d2aSBrian Foster ei->i_allocated_meta_blocks = ei->i_reserved_meta_blocks; 36497795d2aSBrian Foster } 36597795d2aSBrian Foster 3660637c6f4STheodore Ts'o /* Update per-inode reservations */ 3670637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= used; 3680637c6f4STheodore Ts'o ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks; 36957042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 37072b8ab9dSEric Sandeen used + ei->i_allocated_meta_blocks); 3710637c6f4STheodore Ts'o ei->i_allocated_meta_blocks = 0; 3720637c6f4STheodore Ts'o 3730637c6f4STheodore Ts'o if (ei->i_reserved_data_blocks == 0) { 3740637c6f4STheodore Ts'o /* 3750637c6f4STheodore Ts'o * We can release all of the reserved metadata blocks 3760637c6f4STheodore Ts'o * only when we have written all of the delayed 3770637c6f4STheodore Ts'o * allocation blocks. 3780637c6f4STheodore Ts'o */ 37957042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 38072b8ab9dSEric Sandeen ei->i_reserved_meta_blocks); 381ee5f4d9cSTheodore Ts'o ei->i_reserved_meta_blocks = 0; 3829d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 3830637c6f4STheodore Ts'o } 38412219aeaSAneesh Kumar K.V spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 38560e58e0fSMingming Cao 38672b8ab9dSEric Sandeen /* Update quota subsystem for data blocks */ 38772b8ab9dSEric Sandeen if (quota_claim) 3887b415bf6SAditya Kali dquot_claim_block(inode, EXT4_C2B(sbi, used)); 38972b8ab9dSEric Sandeen else { 3905f634d06SAneesh Kumar K.V /* 3915f634d06SAneesh Kumar K.V * We did fallocate with an offset that is already delayed 3925f634d06SAneesh Kumar K.V * allocated. So on delayed allocated writeback we should 39372b8ab9dSEric Sandeen * not re-claim the quota for fallocated blocks. 3945f634d06SAneesh Kumar K.V */ 3957b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, used)); 3965f634d06SAneesh Kumar K.V } 397d6014301SAneesh Kumar K.V 398d6014301SAneesh Kumar K.V /* 399d6014301SAneesh Kumar K.V * If we have done all the pending block allocations and if 400d6014301SAneesh Kumar K.V * there aren't any writers on the inode, we can discard the 401d6014301SAneesh Kumar K.V * inode's preallocations. 402d6014301SAneesh Kumar K.V */ 4030637c6f4STheodore Ts'o if ((ei->i_reserved_data_blocks == 0) && 4040637c6f4STheodore Ts'o (atomic_read(&inode->i_writecount) == 0)) 405d6014301SAneesh Kumar K.V ext4_discard_preallocations(inode); 40612219aeaSAneesh Kumar K.V } 40712219aeaSAneesh Kumar K.V 408e29136f8STheodore Ts'o static int __check_block_validity(struct inode *inode, const char *func, 409c398eda0STheodore Ts'o unsigned int line, 41024676da4STheodore Ts'o struct ext4_map_blocks *map) 4116fd058f7STheodore Ts'o { 41224676da4STheodore Ts'o if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk, 41324676da4STheodore Ts'o map->m_len)) { 414c398eda0STheodore Ts'o ext4_error_inode(inode, func, line, map->m_pblk, 415c398eda0STheodore Ts'o "lblock %lu mapped to illegal pblock " 41624676da4STheodore Ts'o "(length %d)", (unsigned long) map->m_lblk, 417c398eda0STheodore Ts'o map->m_len); 4186fd058f7STheodore Ts'o return -EIO; 4196fd058f7STheodore Ts'o } 4206fd058f7STheodore Ts'o return 0; 4216fd058f7STheodore Ts'o } 4226fd058f7STheodore Ts'o 423e29136f8STheodore Ts'o #define check_block_validity(inode, map) \ 424c398eda0STheodore Ts'o __check_block_validity((inode), __func__, __LINE__, (map)) 425e29136f8STheodore Ts'o 426f5ab0d1fSMingming Cao /* 4271f94533dSTheodore Ts'o * Return the number of contiguous dirty pages in a given inode 4281f94533dSTheodore Ts'o * starting at page frame idx. 42955138e0bSTheodore Ts'o */ 43055138e0bSTheodore Ts'o static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx, 43155138e0bSTheodore Ts'o unsigned int max_pages) 43255138e0bSTheodore Ts'o { 43355138e0bSTheodore Ts'o struct address_space *mapping = inode->i_mapping; 43455138e0bSTheodore Ts'o pgoff_t index; 43555138e0bSTheodore Ts'o struct pagevec pvec; 43655138e0bSTheodore Ts'o pgoff_t num = 0; 43755138e0bSTheodore Ts'o int i, nr_pages, done = 0; 43855138e0bSTheodore Ts'o 43955138e0bSTheodore Ts'o if (max_pages == 0) 44055138e0bSTheodore Ts'o return 0; 44155138e0bSTheodore Ts'o pagevec_init(&pvec, 0); 44255138e0bSTheodore Ts'o while (!done) { 44355138e0bSTheodore Ts'o index = idx; 44455138e0bSTheodore Ts'o nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 44555138e0bSTheodore Ts'o PAGECACHE_TAG_DIRTY, 44655138e0bSTheodore Ts'o (pgoff_t)PAGEVEC_SIZE); 44755138e0bSTheodore Ts'o if (nr_pages == 0) 44855138e0bSTheodore Ts'o break; 44955138e0bSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 45055138e0bSTheodore Ts'o struct page *page = pvec.pages[i]; 45155138e0bSTheodore Ts'o struct buffer_head *bh, *head; 45255138e0bSTheodore Ts'o 45355138e0bSTheodore Ts'o lock_page(page); 45455138e0bSTheodore Ts'o if (unlikely(page->mapping != mapping) || 45555138e0bSTheodore Ts'o !PageDirty(page) || 45655138e0bSTheodore Ts'o PageWriteback(page) || 45755138e0bSTheodore Ts'o page->index != idx) { 45855138e0bSTheodore Ts'o done = 1; 45955138e0bSTheodore Ts'o unlock_page(page); 46055138e0bSTheodore Ts'o break; 46155138e0bSTheodore Ts'o } 4621f94533dSTheodore Ts'o if (page_has_buffers(page)) { 4631f94533dSTheodore Ts'o bh = head = page_buffers(page); 46455138e0bSTheodore Ts'o do { 46555138e0bSTheodore Ts'o if (!buffer_delay(bh) && 4661f94533dSTheodore Ts'o !buffer_unwritten(bh)) 46755138e0bSTheodore Ts'o done = 1; 4681f94533dSTheodore Ts'o bh = bh->b_this_page; 4691f94533dSTheodore Ts'o } while (!done && (bh != head)); 47055138e0bSTheodore Ts'o } 47155138e0bSTheodore Ts'o unlock_page(page); 47255138e0bSTheodore Ts'o if (done) 47355138e0bSTheodore Ts'o break; 47455138e0bSTheodore Ts'o idx++; 47555138e0bSTheodore Ts'o num++; 476659c6009SEric Sandeen if (num >= max_pages) { 477659c6009SEric Sandeen done = 1; 47855138e0bSTheodore Ts'o break; 47955138e0bSTheodore Ts'o } 480659c6009SEric Sandeen } 48155138e0bSTheodore Ts'o pagevec_release(&pvec); 48255138e0bSTheodore Ts'o } 48355138e0bSTheodore Ts'o return num; 48455138e0bSTheodore Ts'o } 48555138e0bSTheodore Ts'o 486921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 487921f266bSDmitry Monakhov static void ext4_map_blocks_es_recheck(handle_t *handle, 488921f266bSDmitry Monakhov struct inode *inode, 489921f266bSDmitry Monakhov struct ext4_map_blocks *es_map, 490921f266bSDmitry Monakhov struct ext4_map_blocks *map, 491921f266bSDmitry Monakhov int flags) 492921f266bSDmitry Monakhov { 493921f266bSDmitry Monakhov int retval; 494921f266bSDmitry Monakhov 495921f266bSDmitry Monakhov map->m_flags = 0; 496921f266bSDmitry Monakhov /* 497921f266bSDmitry Monakhov * There is a race window that the result is not the same. 498921f266bSDmitry Monakhov * e.g. xfstests #223 when dioread_nolock enables. The reason 499921f266bSDmitry Monakhov * is that we lookup a block mapping in extent status tree with 500921f266bSDmitry Monakhov * out taking i_data_sem. So at the time the unwritten extent 501921f266bSDmitry Monakhov * could be converted. 502921f266bSDmitry Monakhov */ 503921f266bSDmitry Monakhov if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 504921f266bSDmitry Monakhov down_read((&EXT4_I(inode)->i_data_sem)); 505921f266bSDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 506921f266bSDmitry Monakhov retval = ext4_ext_map_blocks(handle, inode, map, flags & 507921f266bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 508921f266bSDmitry Monakhov } else { 509921f266bSDmitry Monakhov retval = ext4_ind_map_blocks(handle, inode, map, flags & 510921f266bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 511921f266bSDmitry Monakhov } 512921f266bSDmitry Monakhov if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 513921f266bSDmitry Monakhov up_read((&EXT4_I(inode)->i_data_sem)); 514921f266bSDmitry Monakhov /* 515921f266bSDmitry Monakhov * Clear EXT4_MAP_FROM_CLUSTER and EXT4_MAP_BOUNDARY flag 516921f266bSDmitry Monakhov * because it shouldn't be marked in es_map->m_flags. 517921f266bSDmitry Monakhov */ 518921f266bSDmitry Monakhov map->m_flags &= ~(EXT4_MAP_FROM_CLUSTER | EXT4_MAP_BOUNDARY); 519921f266bSDmitry Monakhov 520921f266bSDmitry Monakhov /* 521921f266bSDmitry Monakhov * We don't check m_len because extent will be collpased in status 522921f266bSDmitry Monakhov * tree. So the m_len might not equal. 523921f266bSDmitry Monakhov */ 524921f266bSDmitry Monakhov if (es_map->m_lblk != map->m_lblk || 525921f266bSDmitry Monakhov es_map->m_flags != map->m_flags || 526921f266bSDmitry Monakhov es_map->m_pblk != map->m_pblk) { 527921f266bSDmitry Monakhov printk("ES cache assertation failed for inode: %lu " 528921f266bSDmitry Monakhov "es_cached ex [%d/%d/%llu/%x] != " 529921f266bSDmitry Monakhov "found ex [%d/%d/%llu/%x] retval %d flags %x\n", 530921f266bSDmitry Monakhov inode->i_ino, es_map->m_lblk, es_map->m_len, 531921f266bSDmitry Monakhov es_map->m_pblk, es_map->m_flags, map->m_lblk, 532921f266bSDmitry Monakhov map->m_len, map->m_pblk, map->m_flags, 533921f266bSDmitry Monakhov retval, flags); 534921f266bSDmitry Monakhov } 535921f266bSDmitry Monakhov } 536921f266bSDmitry Monakhov #endif /* ES_AGGRESSIVE_TEST */ 537921f266bSDmitry Monakhov 53855138e0bSTheodore Ts'o /* 539e35fd660STheodore Ts'o * The ext4_map_blocks() function tries to look up the requested blocks, 5402b2d6d01STheodore Ts'o * and returns if the blocks are already mapped. 541f5ab0d1fSMingming Cao * 542f5ab0d1fSMingming Cao * Otherwise it takes the write lock of the i_data_sem and allocate blocks 543f5ab0d1fSMingming Cao * and store the allocated blocks in the result buffer head and mark it 544f5ab0d1fSMingming Cao * mapped. 545f5ab0d1fSMingming Cao * 546e35fd660STheodore Ts'o * If file type is extents based, it will call ext4_ext_map_blocks(), 547e35fd660STheodore Ts'o * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping 548f5ab0d1fSMingming Cao * based files 549f5ab0d1fSMingming Cao * 550f5ab0d1fSMingming Cao * On success, it returns the number of blocks being mapped or allocate. 551f5ab0d1fSMingming Cao * if create==0 and the blocks are pre-allocated and uninitialized block, 552f5ab0d1fSMingming Cao * the result buffer head is unmapped. If the create ==1, it will make sure 553f5ab0d1fSMingming Cao * the buffer head is mapped. 554f5ab0d1fSMingming Cao * 555f5ab0d1fSMingming Cao * It returns 0 if plain look up failed (blocks have not been allocated), in 556df3ab170STao Ma * that case, buffer head is unmapped 557f5ab0d1fSMingming Cao * 558f5ab0d1fSMingming Cao * It returns the error in case of allocation failure. 559f5ab0d1fSMingming Cao */ 560e35fd660STheodore Ts'o int ext4_map_blocks(handle_t *handle, struct inode *inode, 561e35fd660STheodore Ts'o struct ext4_map_blocks *map, int flags) 5620e855ac8SAneesh Kumar K.V { 563d100eef2SZheng Liu struct extent_status es; 5640e855ac8SAneesh Kumar K.V int retval; 565921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 566921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 567921f266bSDmitry Monakhov 568921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 569921f266bSDmitry Monakhov #endif 570f5ab0d1fSMingming Cao 571e35fd660STheodore Ts'o map->m_flags = 0; 572e35fd660STheodore Ts'o ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u," 573e35fd660STheodore Ts'o "logical block %lu\n", inode->i_ino, flags, map->m_len, 574e35fd660STheodore Ts'o (unsigned long) map->m_lblk); 575d100eef2SZheng Liu 576d100eef2SZheng Liu /* Lookup extent status tree firstly */ 577d100eef2SZheng Liu if (ext4_es_lookup_extent(inode, map->m_lblk, &es)) { 578d100eef2SZheng Liu if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) { 579d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + 580d100eef2SZheng Liu map->m_lblk - es.es_lblk; 581d100eef2SZheng Liu map->m_flags |= ext4_es_is_written(&es) ? 582d100eef2SZheng Liu EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN; 583d100eef2SZheng Liu retval = es.es_len - (map->m_lblk - es.es_lblk); 584d100eef2SZheng Liu if (retval > map->m_len) 585d100eef2SZheng Liu retval = map->m_len; 586d100eef2SZheng Liu map->m_len = retval; 587d100eef2SZheng Liu } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) { 588d100eef2SZheng Liu retval = 0; 589d100eef2SZheng Liu } else { 590d100eef2SZheng Liu BUG_ON(1); 591d100eef2SZheng Liu } 592921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 593921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(handle, inode, map, 594921f266bSDmitry Monakhov &orig_map, flags); 595921f266bSDmitry Monakhov #endif 596d100eef2SZheng Liu goto found; 597d100eef2SZheng Liu } 598d100eef2SZheng Liu 5994df3d265SAneesh Kumar K.V /* 600b920c755STheodore Ts'o * Try to see if we can get the block without requesting a new 601b920c755STheodore Ts'o * file system block. 6024df3d265SAneesh Kumar K.V */ 603729f52c6SZheng Liu if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 6040e855ac8SAneesh Kumar K.V down_read((&EXT4_I(inode)->i_data_sem)); 60512e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 606a4e5d88bSDmitry Monakhov retval = ext4_ext_map_blocks(handle, inode, map, flags & 607a4e5d88bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 6084df3d265SAneesh Kumar K.V } else { 609a4e5d88bSDmitry Monakhov retval = ext4_ind_map_blocks(handle, inode, map, flags & 610a4e5d88bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 6110e855ac8SAneesh Kumar K.V } 612f7fec032SZheng Liu if (retval > 0) { 613f7fec032SZheng Liu int ret; 614f7fec032SZheng Liu unsigned long long status; 615f7fec032SZheng Liu 616921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 617921f266bSDmitry Monakhov if (retval != map->m_len) { 618921f266bSDmitry Monakhov printk("ES len assertation failed for inode: %lu " 619921f266bSDmitry Monakhov "retval %d != map->m_len %d " 620921f266bSDmitry Monakhov "in %s (lookup)\n", inode->i_ino, retval, 621921f266bSDmitry Monakhov map->m_len, __func__); 622921f266bSDmitry Monakhov } 623921f266bSDmitry Monakhov #endif 624921f266bSDmitry Monakhov 625f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 626f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 627f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 628f7fec032SZheng Liu ext4_find_delalloc_range(inode, map->m_lblk, 629f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 630f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 631f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, 632f7fec032SZheng Liu map->m_len, map->m_pblk, status); 633f7fec032SZheng Liu if (ret < 0) 634f7fec032SZheng Liu retval = ret; 635f7fec032SZheng Liu } 636729f52c6SZheng Liu if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 6374df3d265SAneesh Kumar K.V up_read((&EXT4_I(inode)->i_data_sem)); 638f5ab0d1fSMingming Cao 639d100eef2SZheng Liu found: 640e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 641f7fec032SZheng Liu int ret = check_block_validity(inode, map); 6426fd058f7STheodore Ts'o if (ret != 0) 6436fd058f7STheodore Ts'o return ret; 6446fd058f7STheodore Ts'o } 6456fd058f7STheodore Ts'o 646f5ab0d1fSMingming Cao /* If it is only a block(s) look up */ 647c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 6484df3d265SAneesh Kumar K.V return retval; 6494df3d265SAneesh Kumar K.V 6504df3d265SAneesh Kumar K.V /* 651f5ab0d1fSMingming Cao * Returns if the blocks have already allocated 652f5ab0d1fSMingming Cao * 653f5ab0d1fSMingming Cao * Note that if blocks have been preallocated 654df3ab170STao Ma * ext4_ext_get_block() returns the create = 0 655f5ab0d1fSMingming Cao * with buffer head unmapped. 656f5ab0d1fSMingming Cao */ 657e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) 658f5ab0d1fSMingming Cao return retval; 659f5ab0d1fSMingming Cao 660f5ab0d1fSMingming Cao /* 661a25a4e1aSZheng Liu * Here we clear m_flags because after allocating an new extent, 662a25a4e1aSZheng Liu * it will be set again. 6632a8964d6SAneesh Kumar K.V */ 664a25a4e1aSZheng Liu map->m_flags &= ~EXT4_MAP_FLAGS; 6652a8964d6SAneesh Kumar K.V 6662a8964d6SAneesh Kumar K.V /* 667f5ab0d1fSMingming Cao * New blocks allocate and/or writing to uninitialized extent 668f5ab0d1fSMingming Cao * will possibly result in updating i_data, so we take 669f5ab0d1fSMingming Cao * the write lock of i_data_sem, and call get_blocks() 670f5ab0d1fSMingming Cao * with create == 1 flag. 6714df3d265SAneesh Kumar K.V */ 6724df3d265SAneesh Kumar K.V down_write((&EXT4_I(inode)->i_data_sem)); 673d2a17637SMingming Cao 674d2a17637SMingming Cao /* 675d2a17637SMingming Cao * if the caller is from delayed allocation writeout path 676d2a17637SMingming Cao * we have already reserved fs blocks for allocation 677d2a17637SMingming Cao * let the underlying get_block() function know to 678d2a17637SMingming Cao * avoid double accounting 679d2a17637SMingming Cao */ 680c2177057STheodore Ts'o if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 681f2321097STheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED); 6824df3d265SAneesh Kumar K.V /* 6834df3d265SAneesh Kumar K.V * We need to check for EXT4 here because migrate 6844df3d265SAneesh Kumar K.V * could have changed the inode type in between 6854df3d265SAneesh Kumar K.V */ 68612e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 687e35fd660STheodore Ts'o retval = ext4_ext_map_blocks(handle, inode, map, flags); 6880e855ac8SAneesh Kumar K.V } else { 689e35fd660STheodore Ts'o retval = ext4_ind_map_blocks(handle, inode, map, flags); 690267e4db9SAneesh Kumar K.V 691e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_NEW) { 692267e4db9SAneesh Kumar K.V /* 693267e4db9SAneesh Kumar K.V * We allocated new blocks which will result in 694267e4db9SAneesh Kumar K.V * i_data's format changing. Force the migrate 695267e4db9SAneesh Kumar K.V * to fail by clearing migrate flags 696267e4db9SAneesh Kumar K.V */ 69719f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE); 698267e4db9SAneesh Kumar K.V } 6992ac3b6e0STheodore Ts'o 700d2a17637SMingming Cao /* 7012ac3b6e0STheodore Ts'o * Update reserved blocks/metadata blocks after successful 7025f634d06SAneesh Kumar K.V * block allocation which had been deferred till now. We don't 7035f634d06SAneesh Kumar K.V * support fallocate for non extent files. So we can update 7045f634d06SAneesh Kumar K.V * reserve space here. 705d2a17637SMingming Cao */ 7065f634d06SAneesh Kumar K.V if ((retval > 0) && 7071296cc85SAneesh Kumar K.V (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)) 7085f634d06SAneesh Kumar K.V ext4_da_update_reserve_space(inode, retval, 1); 7095f634d06SAneesh Kumar K.V } 710f7fec032SZheng Liu if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 711f2321097STheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED); 712d2a17637SMingming Cao 713f7fec032SZheng Liu if (retval > 0) { 71451865fdaSZheng Liu int ret; 715f7fec032SZheng Liu unsigned long long status; 716f7fec032SZheng Liu 717921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 718921f266bSDmitry Monakhov if (retval != map->m_len) { 719921f266bSDmitry Monakhov printk("ES len assertation failed for inode: %lu " 720921f266bSDmitry Monakhov "retval %d != map->m_len %d " 721921f266bSDmitry Monakhov "in %s (allocation)\n", inode->i_ino, retval, 722921f266bSDmitry Monakhov map->m_len, __func__); 723921f266bSDmitry Monakhov } 724921f266bSDmitry Monakhov #endif 725921f266bSDmitry Monakhov 726adb23551SZheng Liu /* 727adb23551SZheng Liu * If the extent has been zeroed out, we don't need to update 728adb23551SZheng Liu * extent status tree. 729adb23551SZheng Liu */ 730adb23551SZheng Liu if ((flags & EXT4_GET_BLOCKS_PRE_IO) && 731adb23551SZheng Liu ext4_es_lookup_extent(inode, map->m_lblk, &es)) { 732adb23551SZheng Liu if (ext4_es_is_written(&es)) 733adb23551SZheng Liu goto has_zeroout; 734adb23551SZheng Liu } 735f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 736f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 737f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 738f7fec032SZheng Liu ext4_find_delalloc_range(inode, map->m_lblk, 739f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 740f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 741f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 742f7fec032SZheng Liu map->m_pblk, status); 74351865fdaSZheng Liu if (ret < 0) 74451865fdaSZheng Liu retval = ret; 74551865fdaSZheng Liu } 7465356f261SAditya Kali 747adb23551SZheng Liu has_zeroout: 7480e855ac8SAneesh Kumar K.V up_write((&EXT4_I(inode)->i_data_sem)); 749e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 750e29136f8STheodore Ts'o int ret = check_block_validity(inode, map); 7516fd058f7STheodore Ts'o if (ret != 0) 7526fd058f7STheodore Ts'o return ret; 7536fd058f7STheodore Ts'o } 7540e855ac8SAneesh Kumar K.V return retval; 7550e855ac8SAneesh Kumar K.V } 7560e855ac8SAneesh Kumar K.V 757f3bd1f3fSMingming Cao /* Maximum number of blocks we map for direct IO at once. */ 758f3bd1f3fSMingming Cao #define DIO_MAX_BLOCKS 4096 759f3bd1f3fSMingming Cao 7602ed88685STheodore Ts'o static int _ext4_get_block(struct inode *inode, sector_t iblock, 7612ed88685STheodore Ts'o struct buffer_head *bh, int flags) 762ac27a0ecSDave Kleikamp { 7633e4fdaf8SDmitriy Monakhov handle_t *handle = ext4_journal_current_handle(); 7642ed88685STheodore Ts'o struct ext4_map_blocks map; 7657fb5409dSJan Kara int ret = 0, started = 0; 766f3bd1f3fSMingming Cao int dio_credits; 767ac27a0ecSDave Kleikamp 76846c7f254STao Ma if (ext4_has_inline_data(inode)) 76946c7f254STao Ma return -ERANGE; 77046c7f254STao Ma 7712ed88685STheodore Ts'o map.m_lblk = iblock; 7722ed88685STheodore Ts'o map.m_len = bh->b_size >> inode->i_blkbits; 7732ed88685STheodore Ts'o 7748b0f165fSAnatol Pomozov if (flags && !(flags & EXT4_GET_BLOCKS_NO_LOCK) && !handle) { 7757fb5409dSJan Kara /* Direct IO write... */ 7762ed88685STheodore Ts'o if (map.m_len > DIO_MAX_BLOCKS) 7772ed88685STheodore Ts'o map.m_len = DIO_MAX_BLOCKS; 7782ed88685STheodore Ts'o dio_credits = ext4_chunk_trans_blocks(inode, map.m_len); 7799924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, 7809924a92aSTheodore Ts'o dio_credits); 7817fb5409dSJan Kara if (IS_ERR(handle)) { 782ac27a0ecSDave Kleikamp ret = PTR_ERR(handle); 7832ed88685STheodore Ts'o return ret; 7847fb5409dSJan Kara } 7857fb5409dSJan Kara started = 1; 786ac27a0ecSDave Kleikamp } 787ac27a0ecSDave Kleikamp 7882ed88685STheodore Ts'o ret = ext4_map_blocks(handle, inode, &map, flags); 789ac27a0ecSDave Kleikamp if (ret > 0) { 7902ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 7912ed88685STheodore Ts'o bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; 7922ed88685STheodore Ts'o bh->b_size = inode->i_sb->s_blocksize * map.m_len; 793ac27a0ecSDave Kleikamp ret = 0; 794ac27a0ecSDave Kleikamp } 7957fb5409dSJan Kara if (started) 7967fb5409dSJan Kara ext4_journal_stop(handle); 797ac27a0ecSDave Kleikamp return ret; 798ac27a0ecSDave Kleikamp } 799ac27a0ecSDave Kleikamp 8002ed88685STheodore Ts'o int ext4_get_block(struct inode *inode, sector_t iblock, 8012ed88685STheodore Ts'o struct buffer_head *bh, int create) 8022ed88685STheodore Ts'o { 8032ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh, 8042ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 8052ed88685STheodore Ts'o } 8062ed88685STheodore Ts'o 807ac27a0ecSDave Kleikamp /* 808ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 809ac27a0ecSDave Kleikamp */ 810617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 811725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *errp) 812ac27a0ecSDave Kleikamp { 8132ed88685STheodore Ts'o struct ext4_map_blocks map; 8142ed88685STheodore Ts'o struct buffer_head *bh; 815ac27a0ecSDave Kleikamp int fatal = 0, err; 816ac27a0ecSDave Kleikamp 817ac27a0ecSDave Kleikamp J_ASSERT(handle != NULL || create == 0); 818ac27a0ecSDave Kleikamp 8192ed88685STheodore Ts'o map.m_lblk = block; 8202ed88685STheodore Ts'o map.m_len = 1; 8212ed88685STheodore Ts'o err = ext4_map_blocks(handle, inode, &map, 8222ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 8232ed88685STheodore Ts'o 82490b0a973SCarlos Maiolino /* ensure we send some value back into *errp */ 82590b0a973SCarlos Maiolino *errp = 0; 82690b0a973SCarlos Maiolino 8270f70b406STheodore Ts'o if (create && err == 0) 8280f70b406STheodore Ts'o err = -ENOSPC; /* should never happen */ 8292ed88685STheodore Ts'o if (err < 0) 830ac27a0ecSDave Kleikamp *errp = err; 8312ed88685STheodore Ts'o if (err <= 0) 8322ed88685STheodore Ts'o return NULL; 8332ed88685STheodore Ts'o 8342ed88685STheodore Ts'o bh = sb_getblk(inode->i_sb, map.m_pblk); 835aebf0243SWang Shilong if (unlikely(!bh)) { 836860d21e2STheodore Ts'o *errp = -ENOMEM; 8372ed88685STheodore Ts'o return NULL; 838ac27a0ecSDave Kleikamp } 8392ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 840ac27a0ecSDave Kleikamp J_ASSERT(create != 0); 841ac39849dSAneesh Kumar K.V J_ASSERT(handle != NULL); 842ac27a0ecSDave Kleikamp 843ac27a0ecSDave Kleikamp /* 844ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 845ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 846ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 847617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 848ac27a0ecSDave Kleikamp * problem. 849ac27a0ecSDave Kleikamp */ 850ac27a0ecSDave Kleikamp lock_buffer(bh); 851ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 852617ba13bSMingming Cao fatal = ext4_journal_get_create_access(handle, bh); 853ac27a0ecSDave Kleikamp if (!fatal && !buffer_uptodate(bh)) { 854ac27a0ecSDave Kleikamp memset(bh->b_data, 0, inode->i_sb->s_blocksize); 855ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 856ac27a0ecSDave Kleikamp } 857ac27a0ecSDave Kleikamp unlock_buffer(bh); 8580390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 8590390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 860ac27a0ecSDave Kleikamp if (!fatal) 861ac27a0ecSDave Kleikamp fatal = err; 862ac27a0ecSDave Kleikamp } else { 863ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "not a new buffer"); 864ac27a0ecSDave Kleikamp } 865ac27a0ecSDave Kleikamp if (fatal) { 866ac27a0ecSDave Kleikamp *errp = fatal; 867ac27a0ecSDave Kleikamp brelse(bh); 868ac27a0ecSDave Kleikamp bh = NULL; 869ac27a0ecSDave Kleikamp } 870ac27a0ecSDave Kleikamp return bh; 871ac27a0ecSDave Kleikamp } 872ac27a0ecSDave Kleikamp 873617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 874725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *err) 875ac27a0ecSDave Kleikamp { 876ac27a0ecSDave Kleikamp struct buffer_head *bh; 877ac27a0ecSDave Kleikamp 878617ba13bSMingming Cao bh = ext4_getblk(handle, inode, block, create, err); 879ac27a0ecSDave Kleikamp if (!bh) 880ac27a0ecSDave Kleikamp return bh; 881ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 882ac27a0ecSDave Kleikamp return bh; 88365299a3bSChristoph Hellwig ll_rw_block(READ | REQ_META | REQ_PRIO, 1, &bh); 884ac27a0ecSDave Kleikamp wait_on_buffer(bh); 885ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 886ac27a0ecSDave Kleikamp return bh; 887ac27a0ecSDave Kleikamp put_bh(bh); 888ac27a0ecSDave Kleikamp *err = -EIO; 889ac27a0ecSDave Kleikamp return NULL; 890ac27a0ecSDave Kleikamp } 891ac27a0ecSDave Kleikamp 892f19d5870STao Ma int ext4_walk_page_buffers(handle_t *handle, 893ac27a0ecSDave Kleikamp struct buffer_head *head, 894ac27a0ecSDave Kleikamp unsigned from, 895ac27a0ecSDave Kleikamp unsigned to, 896ac27a0ecSDave Kleikamp int *partial, 897ac27a0ecSDave Kleikamp int (*fn)(handle_t *handle, 898ac27a0ecSDave Kleikamp struct buffer_head *bh)) 899ac27a0ecSDave Kleikamp { 900ac27a0ecSDave Kleikamp struct buffer_head *bh; 901ac27a0ecSDave Kleikamp unsigned block_start, block_end; 902ac27a0ecSDave Kleikamp unsigned blocksize = head->b_size; 903ac27a0ecSDave Kleikamp int err, ret = 0; 904ac27a0ecSDave Kleikamp struct buffer_head *next; 905ac27a0ecSDave Kleikamp 906ac27a0ecSDave Kleikamp for (bh = head, block_start = 0; 907ac27a0ecSDave Kleikamp ret == 0 && (bh != head || !block_start); 908de9a55b8STheodore Ts'o block_start = block_end, bh = next) { 909ac27a0ecSDave Kleikamp next = bh->b_this_page; 910ac27a0ecSDave Kleikamp block_end = block_start + blocksize; 911ac27a0ecSDave Kleikamp if (block_end <= from || block_start >= to) { 912ac27a0ecSDave Kleikamp if (partial && !buffer_uptodate(bh)) 913ac27a0ecSDave Kleikamp *partial = 1; 914ac27a0ecSDave Kleikamp continue; 915ac27a0ecSDave Kleikamp } 916ac27a0ecSDave Kleikamp err = (*fn)(handle, bh); 917ac27a0ecSDave Kleikamp if (!ret) 918ac27a0ecSDave Kleikamp ret = err; 919ac27a0ecSDave Kleikamp } 920ac27a0ecSDave Kleikamp return ret; 921ac27a0ecSDave Kleikamp } 922ac27a0ecSDave Kleikamp 923ac27a0ecSDave Kleikamp /* 924ac27a0ecSDave Kleikamp * To preserve ordering, it is essential that the hole instantiation and 925ac27a0ecSDave Kleikamp * the data write be encapsulated in a single transaction. We cannot 926617ba13bSMingming Cao * close off a transaction and start a new one between the ext4_get_block() 927dab291afSMingming Cao * and the commit_write(). So doing the jbd2_journal_start at the start of 928ac27a0ecSDave Kleikamp * prepare_write() is the right place. 929ac27a0ecSDave Kleikamp * 93036ade451SJan Kara * Also, this function can nest inside ext4_writepage(). In that case, we 93136ade451SJan Kara * *know* that ext4_writepage() has generated enough buffer credits to do the 93236ade451SJan Kara * whole page. So we won't block on the journal in that case, which is good, 93336ade451SJan Kara * because the caller may be PF_MEMALLOC. 934ac27a0ecSDave Kleikamp * 935617ba13bSMingming Cao * By accident, ext4 can be reentered when a transaction is open via 936ac27a0ecSDave Kleikamp * quota file writes. If we were to commit the transaction while thus 937ac27a0ecSDave Kleikamp * reentered, there can be a deadlock - we would be holding a quota 938ac27a0ecSDave Kleikamp * lock, and the commit would never complete if another thread had a 939ac27a0ecSDave Kleikamp * transaction open and was blocking on the quota lock - a ranking 940ac27a0ecSDave Kleikamp * violation. 941ac27a0ecSDave Kleikamp * 942dab291afSMingming Cao * So what we do is to rely on the fact that jbd2_journal_stop/journal_start 943ac27a0ecSDave Kleikamp * will _not_ run commit under these circumstances because handle->h_ref 944ac27a0ecSDave Kleikamp * is elevated. We'll still have enough credits for the tiny quotafile 945ac27a0ecSDave Kleikamp * write. 946ac27a0ecSDave Kleikamp */ 947f19d5870STao Ma int do_journal_get_write_access(handle_t *handle, 948ac27a0ecSDave Kleikamp struct buffer_head *bh) 949ac27a0ecSDave Kleikamp { 95056d35a4cSJan Kara int dirty = buffer_dirty(bh); 95156d35a4cSJan Kara int ret; 95256d35a4cSJan Kara 953ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 954ac27a0ecSDave Kleikamp return 0; 95556d35a4cSJan Kara /* 956ebdec241SChristoph Hellwig * __block_write_begin() could have dirtied some buffers. Clean 95756d35a4cSJan Kara * the dirty bit as jbd2_journal_get_write_access() could complain 95856d35a4cSJan Kara * otherwise about fs integrity issues. Setting of the dirty bit 959ebdec241SChristoph Hellwig * by __block_write_begin() isn't a real problem here as we clear 96056d35a4cSJan Kara * the bit before releasing a page lock and thus writeback cannot 96156d35a4cSJan Kara * ever write the buffer. 96256d35a4cSJan Kara */ 96356d35a4cSJan Kara if (dirty) 96456d35a4cSJan Kara clear_buffer_dirty(bh); 96556d35a4cSJan Kara ret = ext4_journal_get_write_access(handle, bh); 96656d35a4cSJan Kara if (!ret && dirty) 96756d35a4cSJan Kara ret = ext4_handle_dirty_metadata(handle, NULL, bh); 96856d35a4cSJan Kara return ret; 969ac27a0ecSDave Kleikamp } 970ac27a0ecSDave Kleikamp 9718b0f165fSAnatol Pomozov static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock, 9728b0f165fSAnatol Pomozov struct buffer_head *bh_result, int create); 973bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 974bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned flags, 975bfc1af65SNick Piggin struct page **pagep, void **fsdata) 976ac27a0ecSDave Kleikamp { 977bfc1af65SNick Piggin struct inode *inode = mapping->host; 9781938a150SAneesh Kumar K.V int ret, needed_blocks; 979ac27a0ecSDave Kleikamp handle_t *handle; 980ac27a0ecSDave Kleikamp int retries = 0; 981bfc1af65SNick Piggin struct page *page; 982bfc1af65SNick Piggin pgoff_t index; 983bfc1af65SNick Piggin unsigned from, to; 984bfc1af65SNick Piggin 9859bffad1eSTheodore Ts'o trace_ext4_write_begin(inode, pos, len, flags); 9861938a150SAneesh Kumar K.V /* 9871938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 9881938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 9891938a150SAneesh Kumar K.V */ 9901938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 991bfc1af65SNick Piggin index = pos >> PAGE_CACHE_SHIFT; 992bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 993bfc1af65SNick Piggin to = from + len; 994ac27a0ecSDave Kleikamp 995f19d5870STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 996f19d5870STao Ma ret = ext4_try_to_write_inline_data(mapping, inode, pos, len, 997f19d5870STao Ma flags, pagep); 998f19d5870STao Ma if (ret < 0) 99947564bfbSTheodore Ts'o return ret; 100047564bfbSTheodore Ts'o if (ret == 1) 100147564bfbSTheodore Ts'o return 0; 1002f19d5870STao Ma } 1003f19d5870STao Ma 100447564bfbSTheodore Ts'o /* 100547564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 100647564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 100747564bfbSTheodore Ts'o * is being written back. So grab it first before we start 100847564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 100947564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 101047564bfbSTheodore Ts'o */ 101147564bfbSTheodore Ts'o retry_grab: 101254566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 101347564bfbSTheodore Ts'o if (!page) 101447564bfbSTheodore Ts'o return -ENOMEM; 101547564bfbSTheodore Ts'o unlock_page(page); 101647564bfbSTheodore Ts'o 101747564bfbSTheodore Ts'o retry_journal: 10189924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks); 1019ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 102047564bfbSTheodore Ts'o page_cache_release(page); 102147564bfbSTheodore Ts'o return PTR_ERR(handle); 1022cf108bcaSJan Kara } 1023f19d5870STao Ma 102447564bfbSTheodore Ts'o lock_page(page); 102547564bfbSTheodore Ts'o if (page->mapping != mapping) { 102647564bfbSTheodore Ts'o /* The page got truncated from under us */ 102747564bfbSTheodore Ts'o unlock_page(page); 102847564bfbSTheodore Ts'o page_cache_release(page); 1029cf108bcaSJan Kara ext4_journal_stop(handle); 103047564bfbSTheodore Ts'o goto retry_grab; 1031cf108bcaSJan Kara } 103247564bfbSTheodore Ts'o wait_on_page_writeback(page); 1033cf108bcaSJan Kara 1034744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 10356e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block_write); 1036744692dcSJiaying Zhang else 10376e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block); 1038bfc1af65SNick Piggin 1039bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 1040f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_buffers(page), 1041f19d5870STao Ma from, to, NULL, 1042f19d5870STao Ma do_journal_get_write_access); 1043b46be050SAndrey Savochkin } 1044bfc1af65SNick Piggin 1045bfc1af65SNick Piggin if (ret) { 1046bfc1af65SNick Piggin unlock_page(page); 1047ae4d5372SAneesh Kumar K.V /* 10486e1db88dSChristoph Hellwig * __block_write_begin may have instantiated a few blocks 1049ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 1050ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 10511938a150SAneesh Kumar K.V * 10521938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 10531938a150SAneesh Kumar K.V * truncate finishes 1054ae4d5372SAneesh Kumar K.V */ 1055ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 10561938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 10571938a150SAneesh Kumar K.V 10581938a150SAneesh Kumar K.V ext4_journal_stop(handle); 10591938a150SAneesh Kumar K.V if (pos + len > inode->i_size) { 1060b9a4207dSJan Kara ext4_truncate_failed_write(inode); 10611938a150SAneesh Kumar K.V /* 1062ffacfa7aSJan Kara * If truncate failed early the inode might 10631938a150SAneesh Kumar K.V * still be on the orphan list; we need to 10641938a150SAneesh Kumar K.V * make sure the inode is removed from the 10651938a150SAneesh Kumar K.V * orphan list in that case. 10661938a150SAneesh Kumar K.V */ 10671938a150SAneesh Kumar K.V if (inode->i_nlink) 10681938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 10691938a150SAneesh Kumar K.V } 1070bfc1af65SNick Piggin 107147564bfbSTheodore Ts'o if (ret == -ENOSPC && 107247564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 107347564bfbSTheodore Ts'o goto retry_journal; 107447564bfbSTheodore Ts'o page_cache_release(page); 107547564bfbSTheodore Ts'o return ret; 107647564bfbSTheodore Ts'o } 107747564bfbSTheodore Ts'o *pagep = page; 1078ac27a0ecSDave Kleikamp return ret; 1079ac27a0ecSDave Kleikamp } 1080ac27a0ecSDave Kleikamp 1081bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 1082bfc1af65SNick Piggin static int write_end_fn(handle_t *handle, struct buffer_head *bh) 1083ac27a0ecSDave Kleikamp { 108413fca323STheodore Ts'o int ret; 1085ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1086ac27a0ecSDave Kleikamp return 0; 1087ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 108813fca323STheodore Ts'o ret = ext4_handle_dirty_metadata(handle, NULL, bh); 108913fca323STheodore Ts'o clear_buffer_meta(bh); 109013fca323STheodore Ts'o clear_buffer_prio(bh); 109113fca323STheodore Ts'o return ret; 1092ac27a0ecSDave Kleikamp } 1093ac27a0ecSDave Kleikamp 1094eed4333fSZheng Liu /* 1095eed4333fSZheng Liu * We need to pick up the new inode size which generic_commit_write gave us 1096eed4333fSZheng Liu * `file' can be NULL - eg, when called from page_symlink(). 1097eed4333fSZheng Liu * 1098eed4333fSZheng Liu * ext4 never places buffers on inode->i_mapping->private_list. metadata 1099eed4333fSZheng Liu * buffers are managed internally. 1100eed4333fSZheng Liu */ 1101eed4333fSZheng Liu static int ext4_write_end(struct file *file, 1102f8514083SAneesh Kumar K.V struct address_space *mapping, 1103f8514083SAneesh Kumar K.V loff_t pos, unsigned len, unsigned copied, 1104f8514083SAneesh Kumar K.V struct page *page, void *fsdata) 1105f8514083SAneesh Kumar K.V { 1106f8514083SAneesh Kumar K.V handle_t *handle = ext4_journal_current_handle(); 1107eed4333fSZheng Liu struct inode *inode = mapping->host; 1108eed4333fSZheng Liu int ret = 0, ret2; 1109eed4333fSZheng Liu int i_size_changed = 0; 1110eed4333fSZheng Liu 1111eed4333fSZheng Liu trace_ext4_write_end(inode, pos, len, copied); 1112eed4333fSZheng Liu if (ext4_test_inode_state(inode, EXT4_STATE_ORDERED_MODE)) { 1113eed4333fSZheng Liu ret = ext4_jbd2_file_inode(handle, inode); 1114eed4333fSZheng Liu if (ret) { 1115eed4333fSZheng Liu unlock_page(page); 1116eed4333fSZheng Liu page_cache_release(page); 1117eed4333fSZheng Liu goto errout; 1118eed4333fSZheng Liu } 1119eed4333fSZheng Liu } 1120f8514083SAneesh Kumar K.V 1121f19d5870STao Ma if (ext4_has_inline_data(inode)) 1122f19d5870STao Ma copied = ext4_write_inline_data_end(inode, pos, len, 1123f19d5870STao Ma copied, page); 1124f19d5870STao Ma else 1125f19d5870STao Ma copied = block_write_end(file, mapping, pos, 1126f19d5870STao Ma len, copied, page, fsdata); 1127f8514083SAneesh Kumar K.V 1128f8514083SAneesh Kumar K.V /* 1129f8514083SAneesh Kumar K.V * No need to use i_size_read() here, the i_size 1130eed4333fSZheng Liu * cannot change under us because we hole i_mutex. 1131f8514083SAneesh Kumar K.V * 1132f8514083SAneesh Kumar K.V * But it's important to update i_size while still holding page lock: 1133f8514083SAneesh Kumar K.V * page writeout could otherwise come in and zero beyond i_size. 1134f8514083SAneesh Kumar K.V */ 1135f8514083SAneesh Kumar K.V if (pos + copied > inode->i_size) { 1136f8514083SAneesh Kumar K.V i_size_write(inode, pos + copied); 1137f8514083SAneesh Kumar K.V i_size_changed = 1; 1138f8514083SAneesh Kumar K.V } 1139f8514083SAneesh Kumar K.V 1140f8514083SAneesh Kumar K.V if (pos + copied > EXT4_I(inode)->i_disksize) { 1141f8514083SAneesh Kumar K.V /* We need to mark inode dirty even if 1142f8514083SAneesh Kumar K.V * new_i_size is less that inode->i_size 1143eed4333fSZheng Liu * but greater than i_disksize. (hint delalloc) 1144f8514083SAneesh Kumar K.V */ 1145f8514083SAneesh Kumar K.V ext4_update_i_disksize(inode, (pos + copied)); 1146f8514083SAneesh Kumar K.V i_size_changed = 1; 1147f8514083SAneesh Kumar K.V } 1148f8514083SAneesh Kumar K.V unlock_page(page); 1149f8514083SAneesh Kumar K.V page_cache_release(page); 1150f8514083SAneesh Kumar K.V 1151f8514083SAneesh Kumar K.V /* 1152f8514083SAneesh Kumar K.V * Don't mark the inode dirty under page lock. First, it unnecessarily 1153f8514083SAneesh Kumar K.V * makes the holding time of page lock longer. Second, it forces lock 1154f8514083SAneesh Kumar K.V * ordering of page lock and transaction start for journaling 1155f8514083SAneesh Kumar K.V * filesystems. 1156f8514083SAneesh Kumar K.V */ 1157f8514083SAneesh Kumar K.V if (i_size_changed) 1158f8514083SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 1159f8514083SAneesh Kumar K.V 116074d553aaSTheodore Ts'o if (copied < 0) 116174d553aaSTheodore Ts'o ret = copied; 1162ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1163f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1164f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1165f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1166f8514083SAneesh Kumar K.V */ 1167f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 116874d553aaSTheodore Ts'o errout: 1169617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1170ac27a0ecSDave Kleikamp if (!ret) 1171ac27a0ecSDave Kleikamp ret = ret2; 1172bfc1af65SNick Piggin 1173f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1174b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1175f8514083SAneesh Kumar K.V /* 1176ffacfa7aSJan Kara * If truncate failed early the inode might still be 1177f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1178f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1179f8514083SAneesh Kumar K.V */ 1180f8514083SAneesh Kumar K.V if (inode->i_nlink) 1181f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1182f8514083SAneesh Kumar K.V } 1183f8514083SAneesh Kumar K.V 1184bfc1af65SNick Piggin return ret ? ret : copied; 1185ac27a0ecSDave Kleikamp } 1186ac27a0ecSDave Kleikamp 1187bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 1188bfc1af65SNick Piggin struct address_space *mapping, 1189bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1190bfc1af65SNick Piggin struct page *page, void *fsdata) 1191ac27a0ecSDave Kleikamp { 1192617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1193bfc1af65SNick Piggin struct inode *inode = mapping->host; 1194ac27a0ecSDave Kleikamp int ret = 0, ret2; 1195ac27a0ecSDave Kleikamp int partial = 0; 1196bfc1af65SNick Piggin unsigned from, to; 1197cf17fea6SAneesh Kumar K.V loff_t new_i_size; 1198ac27a0ecSDave Kleikamp 11999bffad1eSTheodore Ts'o trace_ext4_journalled_write_end(inode, pos, len, copied); 1200bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 1201bfc1af65SNick Piggin to = from + len; 1202bfc1af65SNick Piggin 1203441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1204441c8508SCurt Wohlgemuth 12053fdcfb66STao Ma if (ext4_has_inline_data(inode)) 12063fdcfb66STao Ma copied = ext4_write_inline_data_end(inode, pos, len, 12073fdcfb66STao Ma copied, page); 12083fdcfb66STao Ma else { 1209bfc1af65SNick Piggin if (copied < len) { 1210bfc1af65SNick Piggin if (!PageUptodate(page)) 1211bfc1af65SNick Piggin copied = 0; 1212bfc1af65SNick Piggin page_zero_new_buffers(page, from+copied, to); 1213bfc1af65SNick Piggin } 1214ac27a0ecSDave Kleikamp 1215f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_buffers(page), from, 1216bfc1af65SNick Piggin to, &partial, write_end_fn); 1217ac27a0ecSDave Kleikamp if (!partial) 1218ac27a0ecSDave Kleikamp SetPageUptodate(page); 12193fdcfb66STao Ma } 1220cf17fea6SAneesh Kumar K.V new_i_size = pos + copied; 1221cf17fea6SAneesh Kumar K.V if (new_i_size > inode->i_size) 1222bfc1af65SNick Piggin i_size_write(inode, pos+copied); 122319f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 12242d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 1225cf17fea6SAneesh Kumar K.V if (new_i_size > EXT4_I(inode)->i_disksize) { 1226cf17fea6SAneesh Kumar K.V ext4_update_i_disksize(inode, new_i_size); 1227617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1228ac27a0ecSDave Kleikamp if (!ret) 1229ac27a0ecSDave Kleikamp ret = ret2; 1230ac27a0ecSDave Kleikamp } 1231bfc1af65SNick Piggin 1232cf108bcaSJan Kara unlock_page(page); 1233f8514083SAneesh Kumar K.V page_cache_release(page); 1234ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1235f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1236f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1237f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1238f8514083SAneesh Kumar K.V */ 1239f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1240f8514083SAneesh Kumar K.V 1241617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1242ac27a0ecSDave Kleikamp if (!ret) 1243ac27a0ecSDave Kleikamp ret = ret2; 1244f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1245b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1246f8514083SAneesh Kumar K.V /* 1247ffacfa7aSJan Kara * If truncate failed early the inode might still be 1248f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1249f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1250f8514083SAneesh Kumar K.V */ 1251f8514083SAneesh Kumar K.V if (inode->i_nlink) 1252f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1253f8514083SAneesh Kumar K.V } 1254bfc1af65SNick Piggin 1255bfc1af65SNick Piggin return ret ? ret : copied; 1256ac27a0ecSDave Kleikamp } 1257d2a17637SMingming Cao 12589d0be502STheodore Ts'o /* 1259386ad67cSLukas Czerner * Reserve a metadata for a single block located at lblock 1260386ad67cSLukas Czerner */ 1261386ad67cSLukas Czerner static int ext4_da_reserve_metadata(struct inode *inode, ext4_lblk_t lblock) 1262386ad67cSLukas Czerner { 1263386ad67cSLukas Czerner int retries = 0; 1264386ad67cSLukas Czerner struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 1265386ad67cSLukas Czerner struct ext4_inode_info *ei = EXT4_I(inode); 1266386ad67cSLukas Czerner unsigned int md_needed; 1267386ad67cSLukas Czerner ext4_lblk_t save_last_lblock; 1268386ad67cSLukas Czerner int save_len; 1269386ad67cSLukas Czerner 1270386ad67cSLukas Czerner /* 1271386ad67cSLukas Czerner * recalculate the amount of metadata blocks to reserve 1272386ad67cSLukas Czerner * in order to allocate nrblocks 1273386ad67cSLukas Czerner * worse case is one extent per block 1274386ad67cSLukas Czerner */ 1275386ad67cSLukas Czerner repeat: 1276386ad67cSLukas Czerner spin_lock(&ei->i_block_reservation_lock); 1277386ad67cSLukas Czerner /* 1278386ad67cSLukas Czerner * ext4_calc_metadata_amount() has side effects, which we have 1279386ad67cSLukas Czerner * to be prepared undo if we fail to claim space. 1280386ad67cSLukas Czerner */ 1281386ad67cSLukas Czerner save_len = ei->i_da_metadata_calc_len; 1282386ad67cSLukas Czerner save_last_lblock = ei->i_da_metadata_calc_last_lblock; 1283386ad67cSLukas Czerner md_needed = EXT4_NUM_B2C(sbi, 1284386ad67cSLukas Czerner ext4_calc_metadata_amount(inode, lblock)); 1285386ad67cSLukas Czerner trace_ext4_da_reserve_space(inode, md_needed); 1286386ad67cSLukas Czerner 1287386ad67cSLukas Czerner /* 1288386ad67cSLukas Czerner * We do still charge estimated metadata to the sb though; 1289386ad67cSLukas Czerner * we cannot afford to run out of free blocks. 1290386ad67cSLukas Czerner */ 1291386ad67cSLukas Czerner if (ext4_claim_free_clusters(sbi, md_needed, 0)) { 1292386ad67cSLukas Czerner ei->i_da_metadata_calc_len = save_len; 1293386ad67cSLukas Czerner ei->i_da_metadata_calc_last_lblock = save_last_lblock; 1294386ad67cSLukas Czerner spin_unlock(&ei->i_block_reservation_lock); 1295386ad67cSLukas Czerner if (ext4_should_retry_alloc(inode->i_sb, &retries)) { 1296386ad67cSLukas Czerner cond_resched(); 1297386ad67cSLukas Czerner goto repeat; 1298386ad67cSLukas Czerner } 1299386ad67cSLukas Czerner return -ENOSPC; 1300386ad67cSLukas Czerner } 1301386ad67cSLukas Czerner ei->i_reserved_meta_blocks += md_needed; 1302386ad67cSLukas Czerner spin_unlock(&ei->i_block_reservation_lock); 1303386ad67cSLukas Czerner 1304386ad67cSLukas Czerner return 0; /* success */ 1305386ad67cSLukas Czerner } 1306386ad67cSLukas Czerner 1307386ad67cSLukas Czerner /* 13087b415bf6SAditya Kali * Reserve a single cluster located at lblock 13099d0be502STheodore Ts'o */ 131001f49d0bSTheodore Ts'o static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock) 1311d2a17637SMingming Cao { 1312030ba6bcSAneesh Kumar K.V int retries = 0; 1313d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 13140637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 13157b415bf6SAditya Kali unsigned int md_needed; 13165dd4056dSChristoph Hellwig int ret; 131703179fe9STheodore Ts'o ext4_lblk_t save_last_lblock; 131803179fe9STheodore Ts'o int save_len; 1319d2a17637SMingming Cao 132060e58e0fSMingming Cao /* 132172b8ab9dSEric Sandeen * We will charge metadata quota at writeout time; this saves 132272b8ab9dSEric Sandeen * us from metadata over-estimation, though we may go over by 132372b8ab9dSEric Sandeen * a small amount in the end. Here we just reserve for data. 132460e58e0fSMingming Cao */ 13257b415bf6SAditya Kali ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1)); 13265dd4056dSChristoph Hellwig if (ret) 13275dd4056dSChristoph Hellwig return ret; 132803179fe9STheodore Ts'o 132903179fe9STheodore Ts'o /* 133003179fe9STheodore Ts'o * recalculate the amount of metadata blocks to reserve 133103179fe9STheodore Ts'o * in order to allocate nrblocks 133203179fe9STheodore Ts'o * worse case is one extent per block 133303179fe9STheodore Ts'o */ 133403179fe9STheodore Ts'o repeat: 133503179fe9STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 133603179fe9STheodore Ts'o /* 133703179fe9STheodore Ts'o * ext4_calc_metadata_amount() has side effects, which we have 133803179fe9STheodore Ts'o * to be prepared undo if we fail to claim space. 133903179fe9STheodore Ts'o */ 134003179fe9STheodore Ts'o save_len = ei->i_da_metadata_calc_len; 134103179fe9STheodore Ts'o save_last_lblock = ei->i_da_metadata_calc_last_lblock; 134203179fe9STheodore Ts'o md_needed = EXT4_NUM_B2C(sbi, 134303179fe9STheodore Ts'o ext4_calc_metadata_amount(inode, lblock)); 134403179fe9STheodore Ts'o trace_ext4_da_reserve_space(inode, md_needed); 134503179fe9STheodore Ts'o 134672b8ab9dSEric Sandeen /* 134772b8ab9dSEric Sandeen * We do still charge estimated metadata to the sb though; 134872b8ab9dSEric Sandeen * we cannot afford to run out of free blocks. 134972b8ab9dSEric Sandeen */ 1350e7d5f315STheodore Ts'o if (ext4_claim_free_clusters(sbi, md_needed + 1, 0)) { 135103179fe9STheodore Ts'o ei->i_da_metadata_calc_len = save_len; 135203179fe9STheodore Ts'o ei->i_da_metadata_calc_last_lblock = save_last_lblock; 135303179fe9STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 1354030ba6bcSAneesh Kumar K.V if (ext4_should_retry_alloc(inode->i_sb, &retries)) { 1355bb8b20edSLukas Czerner cond_resched(); 1356030ba6bcSAneesh Kumar K.V goto repeat; 1357030ba6bcSAneesh Kumar K.V } 135803179fe9STheodore Ts'o dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1)); 1359d2a17637SMingming Cao return -ENOSPC; 1360d2a17637SMingming Cao } 13619d0be502STheodore Ts'o ei->i_reserved_data_blocks++; 13620637c6f4STheodore Ts'o ei->i_reserved_meta_blocks += md_needed; 13630637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 136439bc680aSDmitry Monakhov 1365d2a17637SMingming Cao return 0; /* success */ 1366d2a17637SMingming Cao } 1367d2a17637SMingming Cao 136812219aeaSAneesh Kumar K.V static void ext4_da_release_space(struct inode *inode, int to_free) 1369d2a17637SMingming Cao { 1370d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 13710637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 1372d2a17637SMingming Cao 1373cd213226SMingming Cao if (!to_free) 1374cd213226SMingming Cao return; /* Nothing to release, exit */ 1375cd213226SMingming Cao 1376d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1377cd213226SMingming Cao 13785a58ec87SLi Zefan trace_ext4_da_release_space(inode, to_free); 13790637c6f4STheodore Ts'o if (unlikely(to_free > ei->i_reserved_data_blocks)) { 1380cd213226SMingming Cao /* 13810637c6f4STheodore Ts'o * if there aren't enough reserved blocks, then the 13820637c6f4STheodore Ts'o * counter is messed up somewhere. Since this 13830637c6f4STheodore Ts'o * function is called from invalidate page, it's 13840637c6f4STheodore Ts'o * harmless to return without any action. 1385cd213226SMingming Cao */ 13868de5c325STheodore Ts'o ext4_warning(inode->i_sb, "ext4_da_release_space: " 13870637c6f4STheodore Ts'o "ino %lu, to_free %d with only %d reserved " 13881084f252STheodore Ts'o "data blocks", inode->i_ino, to_free, 13890637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 13900637c6f4STheodore Ts'o WARN_ON(1); 13910637c6f4STheodore Ts'o to_free = ei->i_reserved_data_blocks; 13920637c6f4STheodore Ts'o } 13930637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= to_free; 13940637c6f4STheodore Ts'o 13950637c6f4STheodore Ts'o if (ei->i_reserved_data_blocks == 0) { 13960637c6f4STheodore Ts'o /* 13970637c6f4STheodore Ts'o * We can release all of the reserved metadata blocks 13980637c6f4STheodore Ts'o * only when we have written all of the delayed 13990637c6f4STheodore Ts'o * allocation blocks. 14007b415bf6SAditya Kali * Note that in case of bigalloc, i_reserved_meta_blocks, 14017b415bf6SAditya Kali * i_reserved_data_blocks, etc. refer to number of clusters. 14020637c6f4STheodore Ts'o */ 140357042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 140472b8ab9dSEric Sandeen ei->i_reserved_meta_blocks); 1405ee5f4d9cSTheodore Ts'o ei->i_reserved_meta_blocks = 0; 14069d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 1407cd213226SMingming Cao } 1408cd213226SMingming Cao 140972b8ab9dSEric Sandeen /* update fs dirty data blocks counter */ 141057042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free); 1411d2a17637SMingming Cao 1412d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 141360e58e0fSMingming Cao 14147b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free)); 1415d2a17637SMingming Cao } 1416d2a17637SMingming Cao 1417d2a17637SMingming Cao static void ext4_da_page_release_reservation(struct page *page, 1418d2a17637SMingming Cao unsigned long offset) 1419d2a17637SMingming Cao { 1420d2a17637SMingming Cao int to_release = 0; 1421d2a17637SMingming Cao struct buffer_head *head, *bh; 1422d2a17637SMingming Cao unsigned int curr_off = 0; 14237b415bf6SAditya Kali struct inode *inode = page->mapping->host; 14247b415bf6SAditya Kali struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 14257b415bf6SAditya Kali int num_clusters; 142651865fdaSZheng Liu ext4_fsblk_t lblk; 1427d2a17637SMingming Cao 1428d2a17637SMingming Cao head = page_buffers(page); 1429d2a17637SMingming Cao bh = head; 1430d2a17637SMingming Cao do { 1431d2a17637SMingming Cao unsigned int next_off = curr_off + bh->b_size; 1432d2a17637SMingming Cao 1433d2a17637SMingming Cao if ((offset <= curr_off) && (buffer_delay(bh))) { 1434d2a17637SMingming Cao to_release++; 1435d2a17637SMingming Cao clear_buffer_delay(bh); 1436d2a17637SMingming Cao } 1437d2a17637SMingming Cao curr_off = next_off; 1438d2a17637SMingming Cao } while ((bh = bh->b_this_page) != head); 14397b415bf6SAditya Kali 144051865fdaSZheng Liu if (to_release) { 144151865fdaSZheng Liu lblk = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits); 144251865fdaSZheng Liu ext4_es_remove_extent(inode, lblk, to_release); 144351865fdaSZheng Liu } 144451865fdaSZheng Liu 14457b415bf6SAditya Kali /* If we have released all the blocks belonging to a cluster, then we 14467b415bf6SAditya Kali * need to release the reserved space for that cluster. */ 14477b415bf6SAditya Kali num_clusters = EXT4_NUM_B2C(sbi, to_release); 14487b415bf6SAditya Kali while (num_clusters > 0) { 14497b415bf6SAditya Kali lblk = (page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits)) + 14507b415bf6SAditya Kali ((num_clusters - 1) << sbi->s_cluster_bits); 14517b415bf6SAditya Kali if (sbi->s_cluster_ratio == 1 || 14527d1b1fbcSZheng Liu !ext4_find_delalloc_cluster(inode, lblk)) 14537b415bf6SAditya Kali ext4_da_release_space(inode, 1); 14547b415bf6SAditya Kali 14557b415bf6SAditya Kali num_clusters--; 14567b415bf6SAditya Kali } 1457d2a17637SMingming Cao } 1458ac27a0ecSDave Kleikamp 1459ac27a0ecSDave Kleikamp /* 146064769240SAlex Tomas * Delayed allocation stuff 146164769240SAlex Tomas */ 146264769240SAlex Tomas 146364769240SAlex Tomas /* 146464769240SAlex Tomas * mpage_da_submit_io - walks through extent of pages and try to write 1465a1d6cc56SAneesh Kumar K.V * them with writepage() call back 146664769240SAlex Tomas * 146764769240SAlex Tomas * @mpd->inode: inode 146864769240SAlex Tomas * @mpd->first_page: first page of the extent 146964769240SAlex Tomas * @mpd->next_page: page after the last page of the extent 147064769240SAlex Tomas * 147164769240SAlex Tomas * By the time mpage_da_submit_io() is called we expect all blocks 147264769240SAlex Tomas * to be allocated. this may be wrong if allocation failed. 147364769240SAlex Tomas * 147464769240SAlex Tomas * As pages are already locked by write_cache_pages(), we can't use it 147564769240SAlex Tomas */ 14761de3e3dfSTheodore Ts'o static int mpage_da_submit_io(struct mpage_da_data *mpd, 14771de3e3dfSTheodore Ts'o struct ext4_map_blocks *map) 147864769240SAlex Tomas { 1479791b7f08SAneesh Kumar K.V struct pagevec pvec; 1480791b7f08SAneesh Kumar K.V unsigned long index, end; 1481791b7f08SAneesh Kumar K.V int ret = 0, err, nr_pages, i; 1482791b7f08SAneesh Kumar K.V struct inode *inode = mpd->inode; 1483791b7f08SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 1484cb20d518STheodore Ts'o loff_t size = i_size_read(inode); 14853ecdb3a1STheodore Ts'o unsigned int len, block_start; 14863ecdb3a1STheodore Ts'o struct buffer_head *bh, *page_bufs = NULL; 14871de3e3dfSTheodore Ts'o sector_t pblock = 0, cur_logical = 0; 1488bd2d0210STheodore Ts'o struct ext4_io_submit io_submit; 148964769240SAlex Tomas 149064769240SAlex Tomas BUG_ON(mpd->next_page <= mpd->first_page); 14914eec708dSJan Kara ext4_io_submit_init(&io_submit, mpd->wbc); 14924eec708dSJan Kara io_submit.io_end = ext4_init_io_end(inode, GFP_NOFS); 14934eec708dSJan Kara if (!io_submit.io_end) 14944eec708dSJan Kara return -ENOMEM; 1495791b7f08SAneesh Kumar K.V /* 1496791b7f08SAneesh Kumar K.V * We need to start from the first_page to the next_page - 1 1497791b7f08SAneesh Kumar K.V * to make sure we also write the mapped dirty buffer_heads. 14988dc207c0STheodore Ts'o * If we look at mpd->b_blocknr we would only be looking 1499791b7f08SAneesh Kumar K.V * at the currently mapped buffer_heads. 1500791b7f08SAneesh Kumar K.V */ 150164769240SAlex Tomas index = mpd->first_page; 150264769240SAlex Tomas end = mpd->next_page - 1; 150364769240SAlex Tomas 1504791b7f08SAneesh Kumar K.V pagevec_init(&pvec, 0); 150564769240SAlex Tomas while (index <= end) { 1506791b7f08SAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 150764769240SAlex Tomas if (nr_pages == 0) 150864769240SAlex Tomas break; 150964769240SAlex Tomas for (i = 0; i < nr_pages; i++) { 1510f8bec370SJan Kara int skip_page = 0; 151164769240SAlex Tomas struct page *page = pvec.pages[i]; 151264769240SAlex Tomas 1513791b7f08SAneesh Kumar K.V index = page->index; 1514791b7f08SAneesh Kumar K.V if (index > end) 1515791b7f08SAneesh Kumar K.V break; 1516cb20d518STheodore Ts'o 1517cb20d518STheodore Ts'o if (index == size >> PAGE_CACHE_SHIFT) 1518cb20d518STheodore Ts'o len = size & ~PAGE_CACHE_MASK; 1519cb20d518STheodore Ts'o else 1520cb20d518STheodore Ts'o len = PAGE_CACHE_SIZE; 15211de3e3dfSTheodore Ts'o if (map) { 15221de3e3dfSTheodore Ts'o cur_logical = index << (PAGE_CACHE_SHIFT - 15231de3e3dfSTheodore Ts'o inode->i_blkbits); 15241de3e3dfSTheodore Ts'o pblock = map->m_pblk + (cur_logical - 15251de3e3dfSTheodore Ts'o map->m_lblk); 15261de3e3dfSTheodore Ts'o } 1527791b7f08SAneesh Kumar K.V index++; 1528791b7f08SAneesh Kumar K.V 1529791b7f08SAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1530791b7f08SAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1531791b7f08SAneesh Kumar K.V 15323ecdb3a1STheodore Ts'o bh = page_bufs = page_buffers(page); 15333ecdb3a1STheodore Ts'o block_start = 0; 15343ecdb3a1STheodore Ts'o do { 15351de3e3dfSTheodore Ts'o if (map && (cur_logical >= map->m_lblk) && 15361de3e3dfSTheodore Ts'o (cur_logical <= (map->m_lblk + 15371de3e3dfSTheodore Ts'o (map->m_len - 1)))) { 15381de3e3dfSTheodore Ts'o if (buffer_delay(bh)) { 15391de3e3dfSTheodore Ts'o clear_buffer_delay(bh); 15401de3e3dfSTheodore Ts'o bh->b_blocknr = pblock; 15411de3e3dfSTheodore Ts'o } 15421de3e3dfSTheodore Ts'o if (buffer_unwritten(bh) || 15431de3e3dfSTheodore Ts'o buffer_mapped(bh)) 15441de3e3dfSTheodore Ts'o BUG_ON(bh->b_blocknr != pblock); 15451de3e3dfSTheodore Ts'o if (map->m_flags & EXT4_MAP_UNINIT) 15461de3e3dfSTheodore Ts'o set_buffer_uninit(bh); 15471de3e3dfSTheodore Ts'o clear_buffer_unwritten(bh); 15481de3e3dfSTheodore Ts'o } 15491de3e3dfSTheodore Ts'o 155013a79a47SYongqiang Yang /* 155113a79a47SYongqiang Yang * skip page if block allocation undone and 155213a79a47SYongqiang Yang * block is dirty 155313a79a47SYongqiang Yang */ 155413a79a47SYongqiang Yang if (ext4_bh_delay_or_unwritten(NULL, bh)) 155597498956STheodore Ts'o skip_page = 1; 15563ecdb3a1STheodore Ts'o bh = bh->b_this_page; 15573ecdb3a1STheodore Ts'o block_start += bh->b_size; 15581de3e3dfSTheodore Ts'o cur_logical++; 15591de3e3dfSTheodore Ts'o pblock++; 15601de3e3dfSTheodore Ts'o } while (bh != page_bufs); 15611de3e3dfSTheodore Ts'o 1562f8bec370SJan Kara if (skip_page) { 1563f8bec370SJan Kara unlock_page(page); 1564f8bec370SJan Kara continue; 1565f8bec370SJan Kara } 1566cb20d518STheodore Ts'o 156797498956STheodore Ts'o clear_page_dirty_for_io(page); 1568fe089c77SJan Kara err = ext4_bio_write_page(&io_submit, page, len, 1569fe089c77SJan Kara mpd->wbc); 1570cb20d518STheodore Ts'o if (!err) 1571a1d6cc56SAneesh Kumar K.V mpd->pages_written++; 157264769240SAlex Tomas /* 157364769240SAlex Tomas * In error case, we have to continue because 157464769240SAlex Tomas * remaining pages are still locked 157564769240SAlex Tomas */ 157664769240SAlex Tomas if (ret == 0) 157764769240SAlex Tomas ret = err; 157864769240SAlex Tomas } 157964769240SAlex Tomas pagevec_release(&pvec); 158064769240SAlex Tomas } 1581bd2d0210STheodore Ts'o ext4_io_submit(&io_submit); 15824eec708dSJan Kara /* Drop io_end reference we got from init */ 15834eec708dSJan Kara ext4_put_io_end_defer(io_submit.io_end); 158464769240SAlex Tomas return ret; 158564769240SAlex Tomas } 158664769240SAlex Tomas 1587c7f5938aSCurt Wohlgemuth static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd) 1588c4a0c46eSAneesh Kumar K.V { 1589c4a0c46eSAneesh Kumar K.V int nr_pages, i; 1590c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 1591c4a0c46eSAneesh Kumar K.V struct pagevec pvec; 1592c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 1593c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 159451865fdaSZheng Liu ext4_lblk_t start, last; 1595c4a0c46eSAneesh Kumar K.V 1596c7f5938aSCurt Wohlgemuth index = mpd->first_page; 1597c7f5938aSCurt Wohlgemuth end = mpd->next_page - 1; 159851865fdaSZheng Liu 159951865fdaSZheng Liu start = index << (PAGE_CACHE_SHIFT - inode->i_blkbits); 160051865fdaSZheng Liu last = end << (PAGE_CACHE_SHIFT - inode->i_blkbits); 160151865fdaSZheng Liu ext4_es_remove_extent(inode, start, last - start + 1); 160251865fdaSZheng Liu 160366bea92cSEric Sandeen pagevec_init(&pvec, 0); 1604c4a0c46eSAneesh Kumar K.V while (index <= end) { 1605c4a0c46eSAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 1606c4a0c46eSAneesh Kumar K.V if (nr_pages == 0) 1607c4a0c46eSAneesh Kumar K.V break; 1608c4a0c46eSAneesh Kumar K.V for (i = 0; i < nr_pages; i++) { 1609c4a0c46eSAneesh Kumar K.V struct page *page = pvec.pages[i]; 16109b1d0998SJan Kara if (page->index > end) 1611c4a0c46eSAneesh Kumar K.V break; 1612c4a0c46eSAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1613c4a0c46eSAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1614c4a0c46eSAneesh Kumar K.V block_invalidatepage(page, 0); 1615c4a0c46eSAneesh Kumar K.V ClearPageUptodate(page); 1616c4a0c46eSAneesh Kumar K.V unlock_page(page); 1617c4a0c46eSAneesh Kumar K.V } 16189b1d0998SJan Kara index = pvec.pages[nr_pages - 1]->index + 1; 16199b1d0998SJan Kara pagevec_release(&pvec); 1620c4a0c46eSAneesh Kumar K.V } 1621c4a0c46eSAneesh Kumar K.V return; 1622c4a0c46eSAneesh Kumar K.V } 1623c4a0c46eSAneesh Kumar K.V 1624df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 1625df22291fSAneesh Kumar K.V { 1626df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 162792b97816STheodore Ts'o struct super_block *sb = inode->i_sb; 1628f78ee70dSLukas Czerner struct ext4_inode_info *ei = EXT4_I(inode); 162992b97816STheodore Ts'o 163092b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld", 16315dee5437STheodore Ts'o EXT4_C2B(EXT4_SB(inode->i_sb), 1632f78ee70dSLukas Czerner ext4_count_free_clusters(sb))); 163392b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Free/Dirty block details"); 163492b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "free_blocks=%lld", 1635f78ee70dSLukas Czerner (long long) EXT4_C2B(EXT4_SB(sb), 163657042651STheodore Ts'o percpu_counter_sum(&sbi->s_freeclusters_counter))); 163792b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld", 1638f78ee70dSLukas Czerner (long long) EXT4_C2B(EXT4_SB(sb), 16397b415bf6SAditya Kali percpu_counter_sum(&sbi->s_dirtyclusters_counter))); 164092b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Block reservation details"); 164192b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u", 1642f78ee70dSLukas Czerner ei->i_reserved_data_blocks); 164392b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_meta_blocks=%u", 1644f78ee70dSLukas Czerner ei->i_reserved_meta_blocks); 1645f78ee70dSLukas Czerner ext4_msg(sb, KERN_CRIT, "i_allocated_meta_blocks=%u", 1646f78ee70dSLukas Czerner ei->i_allocated_meta_blocks); 1647df22291fSAneesh Kumar K.V return; 1648df22291fSAneesh Kumar K.V } 1649df22291fSAneesh Kumar K.V 1650b920c755STheodore Ts'o /* 16515a87b7a5STheodore Ts'o * mpage_da_map_and_submit - go through given space, map them 16525a87b7a5STheodore Ts'o * if necessary, and then submit them for I/O 165364769240SAlex Tomas * 16548dc207c0STheodore Ts'o * @mpd - bh describing space 165564769240SAlex Tomas * 165664769240SAlex Tomas * The function skips space we know is already mapped to disk blocks. 165764769240SAlex Tomas * 165864769240SAlex Tomas */ 16595a87b7a5STheodore Ts'o static void mpage_da_map_and_submit(struct mpage_da_data *mpd) 166064769240SAlex Tomas { 16612ac3b6e0STheodore Ts'o int err, blks, get_blocks_flags; 16621de3e3dfSTheodore Ts'o struct ext4_map_blocks map, *mapp = NULL; 16632fa3cdfbSTheodore Ts'o sector_t next = mpd->b_blocknr; 16642fa3cdfbSTheodore Ts'o unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits; 16652fa3cdfbSTheodore Ts'o loff_t disksize = EXT4_I(mpd->inode)->i_disksize; 16662fa3cdfbSTheodore Ts'o handle_t *handle = NULL; 166764769240SAlex Tomas 166864769240SAlex Tomas /* 16695a87b7a5STheodore Ts'o * If the blocks are mapped already, or we couldn't accumulate 16705a87b7a5STheodore Ts'o * any blocks, then proceed immediately to the submission stage. 167164769240SAlex Tomas */ 16725a87b7a5STheodore Ts'o if ((mpd->b_size == 0) || 16735a87b7a5STheodore Ts'o ((mpd->b_state & (1 << BH_Mapped)) && 167429fa89d0SAneesh Kumar K.V !(mpd->b_state & (1 << BH_Delay)) && 16755a87b7a5STheodore Ts'o !(mpd->b_state & (1 << BH_Unwritten)))) 16765a87b7a5STheodore Ts'o goto submit_io; 16772fa3cdfbSTheodore Ts'o 16782fa3cdfbSTheodore Ts'o handle = ext4_journal_current_handle(); 16792fa3cdfbSTheodore Ts'o BUG_ON(!handle); 16802fa3cdfbSTheodore Ts'o 168179ffab34SAneesh Kumar K.V /* 168279e83036SEric Sandeen * Call ext4_map_blocks() to allocate any delayed allocation 16832ac3b6e0STheodore Ts'o * blocks, or to convert an uninitialized extent to be 16842ac3b6e0STheodore Ts'o * initialized (in the case where we have written into 16852ac3b6e0STheodore Ts'o * one or more preallocated blocks). 16862ac3b6e0STheodore Ts'o * 16872ac3b6e0STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to 16882ac3b6e0STheodore Ts'o * indicate that we are on the delayed allocation path. This 16892ac3b6e0STheodore Ts'o * affects functions in many different parts of the allocation 16902ac3b6e0STheodore Ts'o * call path. This flag exists primarily because we don't 169179e83036SEric Sandeen * want to change *many* call functions, so ext4_map_blocks() 1692f2321097STheodore Ts'o * will set the EXT4_STATE_DELALLOC_RESERVED flag once the 16932ac3b6e0STheodore Ts'o * inode's allocation semaphore is taken. 16942ac3b6e0STheodore Ts'o * 16952ac3b6e0STheodore Ts'o * If the blocks in questions were delalloc blocks, set 16962ac3b6e0STheodore Ts'o * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting 16972ac3b6e0STheodore Ts'o * variables are updated after the blocks have been allocated. 169879ffab34SAneesh Kumar K.V */ 16992ed88685STheodore Ts'o map.m_lblk = next; 17002ed88685STheodore Ts'o map.m_len = max_blocks; 170127dd4385SLukas Czerner /* 170227dd4385SLukas Czerner * We're in delalloc path and it is possible that we're going to 170327dd4385SLukas Czerner * need more metadata blocks than previously reserved. However 170427dd4385SLukas Czerner * we must not fail because we're in writeback and there is 170527dd4385SLukas Czerner * nothing we can do about it so it might result in data loss. 170627dd4385SLukas Czerner * So use reserved blocks to allocate metadata if possible. 170727dd4385SLukas Czerner */ 170827dd4385SLukas Czerner get_blocks_flags = EXT4_GET_BLOCKS_CREATE | 170927dd4385SLukas Czerner EXT4_GET_BLOCKS_METADATA_NOFAIL; 1710744692dcSJiaying Zhang if (ext4_should_dioread_nolock(mpd->inode)) 1711744692dcSJiaying Zhang get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; 17122ac3b6e0STheodore Ts'o if (mpd->b_state & (1 << BH_Delay)) 17131296cc85SAneesh Kumar K.V get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; 17141296cc85SAneesh Kumar K.V 171527dd4385SLukas Czerner 17162ed88685STheodore Ts'o blks = ext4_map_blocks(handle, mpd->inode, &map, get_blocks_flags); 17172fa3cdfbSTheodore Ts'o if (blks < 0) { 1718e3570639SEric Sandeen struct super_block *sb = mpd->inode->i_sb; 1719e3570639SEric Sandeen 17202fa3cdfbSTheodore Ts'o err = blks; 1721ed5bde0bSTheodore Ts'o /* 17225a87b7a5STheodore Ts'o * If get block returns EAGAIN or ENOSPC and there 172397498956STheodore Ts'o * appears to be free blocks we will just let 172497498956STheodore Ts'o * mpage_da_submit_io() unlock all of the pages. 1725c4a0c46eSAneesh Kumar K.V */ 1726c4a0c46eSAneesh Kumar K.V if (err == -EAGAIN) 17275a87b7a5STheodore Ts'o goto submit_io; 1728df22291fSAneesh Kumar K.V 17295dee5437STheodore Ts'o if (err == -ENOSPC && ext4_count_free_clusters(sb)) { 1730df22291fSAneesh Kumar K.V mpd->retval = err; 17315a87b7a5STheodore Ts'o goto submit_io; 1732df22291fSAneesh Kumar K.V } 1733df22291fSAneesh Kumar K.V 1734c4a0c46eSAneesh Kumar K.V /* 1735ed5bde0bSTheodore Ts'o * get block failure will cause us to loop in 1736ed5bde0bSTheodore Ts'o * writepages, because a_ops->writepage won't be able 1737ed5bde0bSTheodore Ts'o * to make progress. The page will be redirtied by 1738ed5bde0bSTheodore Ts'o * writepage and writepages will again try to write 1739ed5bde0bSTheodore Ts'o * the same. 1740c4a0c46eSAneesh Kumar K.V */ 1741e3570639SEric Sandeen if (!(EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) { 1742e3570639SEric Sandeen ext4_msg(sb, KERN_CRIT, 1743e3570639SEric Sandeen "delayed block allocation failed for inode %lu " 1744e3570639SEric Sandeen "at logical offset %llu with max blocks %zd " 1745e3570639SEric Sandeen "with error %d", mpd->inode->i_ino, 1746c4a0c46eSAneesh Kumar K.V (unsigned long long) next, 17478dc207c0STheodore Ts'o mpd->b_size >> mpd->inode->i_blkbits, err); 1748e3570639SEric Sandeen ext4_msg(sb, KERN_CRIT, 174901a523ebSTheodore Ts'o "This should not happen!! Data will be lost"); 1750e3570639SEric Sandeen if (err == -ENOSPC) 1751df22291fSAneesh Kumar K.V ext4_print_free_blocks(mpd->inode); 1752030ba6bcSAneesh Kumar K.V } 17532fa3cdfbSTheodore Ts'o /* invalidate all the pages */ 1754c7f5938aSCurt Wohlgemuth ext4_da_block_invalidatepages(mpd); 1755e0fd9b90SCurt Wohlgemuth 1756e0fd9b90SCurt Wohlgemuth /* Mark this page range as having been completed */ 1757e0fd9b90SCurt Wohlgemuth mpd->io_done = 1; 17585a87b7a5STheodore Ts'o return; 1759c4a0c46eSAneesh Kumar K.V } 17602fa3cdfbSTheodore Ts'o BUG_ON(blks == 0); 17612fa3cdfbSTheodore Ts'o 17621de3e3dfSTheodore Ts'o mapp = ↦ 17632ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 17642ed88685STheodore Ts'o struct block_device *bdev = mpd->inode->i_sb->s_bdev; 17652ed88685STheodore Ts'o int i; 176664769240SAlex Tomas 17672ed88685STheodore Ts'o for (i = 0; i < map.m_len; i++) 17682ed88685STheodore Ts'o unmap_underlying_metadata(bdev, map.m_pblk + i); 17692fa3cdfbSTheodore Ts'o } 17702fa3cdfbSTheodore Ts'o 17712fa3cdfbSTheodore Ts'o /* 177203f5d8bcSJan Kara * Update on-disk size along with block allocation. 17732fa3cdfbSTheodore Ts'o */ 17742fa3cdfbSTheodore Ts'o disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits; 17752fa3cdfbSTheodore Ts'o if (disksize > i_size_read(mpd->inode)) 17762fa3cdfbSTheodore Ts'o disksize = i_size_read(mpd->inode); 17772fa3cdfbSTheodore Ts'o if (disksize > EXT4_I(mpd->inode)->i_disksize) { 17782fa3cdfbSTheodore Ts'o ext4_update_i_disksize(mpd->inode, disksize); 17795a87b7a5STheodore Ts'o err = ext4_mark_inode_dirty(handle, mpd->inode); 17805a87b7a5STheodore Ts'o if (err) 17815a87b7a5STheodore Ts'o ext4_error(mpd->inode->i_sb, 17825a87b7a5STheodore Ts'o "Failed to mark inode %lu dirty", 17835a87b7a5STheodore Ts'o mpd->inode->i_ino); 17842fa3cdfbSTheodore Ts'o } 17852fa3cdfbSTheodore Ts'o 17865a87b7a5STheodore Ts'o submit_io: 17871de3e3dfSTheodore Ts'o mpage_da_submit_io(mpd, mapp); 17885a87b7a5STheodore Ts'o mpd->io_done = 1; 178964769240SAlex Tomas } 179064769240SAlex Tomas 1791bf068ee2SAneesh Kumar K.V #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \ 1792bf068ee2SAneesh Kumar K.V (1 << BH_Delay) | (1 << BH_Unwritten)) 179364769240SAlex Tomas 179464769240SAlex Tomas /* 179564769240SAlex Tomas * mpage_add_bh_to_extent - try to add one more block to extent of blocks 179664769240SAlex Tomas * 179764769240SAlex Tomas * @mpd->lbh - extent of blocks 179864769240SAlex Tomas * @logical - logical number of the block in the file 1799b6a8e62fSJan Kara * @b_state - b_state of the buffer head added 180064769240SAlex Tomas * 180164769240SAlex Tomas * the function is used to collect contig. blocks in same state 180264769240SAlex Tomas */ 1803b6a8e62fSJan Kara static void mpage_add_bh_to_extent(struct mpage_da_data *mpd, sector_t logical, 18048dc207c0STheodore Ts'o unsigned long b_state) 180564769240SAlex Tomas { 180664769240SAlex Tomas sector_t next; 1807b6a8e62fSJan Kara int blkbits = mpd->inode->i_blkbits; 1808b6a8e62fSJan Kara int nrblocks = mpd->b_size >> blkbits; 180964769240SAlex Tomas 1810c445e3e0SEric Sandeen /* 1811c445e3e0SEric Sandeen * XXX Don't go larger than mballoc is willing to allocate 1812c445e3e0SEric Sandeen * This is a stopgap solution. We eventually need to fold 1813c445e3e0SEric Sandeen * mpage_da_submit_io() into this function and then call 181479e83036SEric Sandeen * ext4_map_blocks() multiple times in a loop 1815c445e3e0SEric Sandeen */ 1816b6a8e62fSJan Kara if (nrblocks >= (8*1024*1024 >> blkbits)) 1817c445e3e0SEric Sandeen goto flush_it; 1818c445e3e0SEric Sandeen 1819525f4ed8SMingming Cao /* check if the reserved journal credits might overflow */ 1820b6a8e62fSJan Kara if (!ext4_test_inode_flag(mpd->inode, EXT4_INODE_EXTENTS)) { 1821525f4ed8SMingming Cao if (nrblocks >= EXT4_MAX_TRANS_DATA) { 1822525f4ed8SMingming Cao /* 1823525f4ed8SMingming Cao * With non-extent format we are limited by the journal 1824525f4ed8SMingming Cao * credit available. Total credit needed to insert 1825525f4ed8SMingming Cao * nrblocks contiguous blocks is dependent on the 1826525f4ed8SMingming Cao * nrblocks. So limit nrblocks. 1827525f4ed8SMingming Cao */ 1828525f4ed8SMingming Cao goto flush_it; 1829525f4ed8SMingming Cao } 1830525f4ed8SMingming Cao } 183164769240SAlex Tomas /* 183264769240SAlex Tomas * First block in the extent 183364769240SAlex Tomas */ 18348dc207c0STheodore Ts'o if (mpd->b_size == 0) { 18358dc207c0STheodore Ts'o mpd->b_blocknr = logical; 1836b6a8e62fSJan Kara mpd->b_size = 1 << blkbits; 18378dc207c0STheodore Ts'o mpd->b_state = b_state & BH_FLAGS; 183864769240SAlex Tomas return; 183964769240SAlex Tomas } 184064769240SAlex Tomas 18418dc207c0STheodore Ts'o next = mpd->b_blocknr + nrblocks; 184264769240SAlex Tomas /* 184364769240SAlex Tomas * Can we merge the block to our big extent? 184464769240SAlex Tomas */ 18458dc207c0STheodore Ts'o if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) { 1846b6a8e62fSJan Kara mpd->b_size += 1 << blkbits; 184764769240SAlex Tomas return; 184864769240SAlex Tomas } 184964769240SAlex Tomas 1850525f4ed8SMingming Cao flush_it: 185164769240SAlex Tomas /* 185264769240SAlex Tomas * We couldn't merge the block to our extent, so we 185364769240SAlex Tomas * need to flush current extent and start new one 185464769240SAlex Tomas */ 18555a87b7a5STheodore Ts'o mpage_da_map_and_submit(mpd); 1856a1d6cc56SAneesh Kumar K.V return; 185764769240SAlex Tomas } 185864769240SAlex Tomas 1859c364b22cSAneesh Kumar K.V static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh) 186029fa89d0SAneesh Kumar K.V { 1861c364b22cSAneesh Kumar K.V return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 186229fa89d0SAneesh Kumar K.V } 186329fa89d0SAneesh Kumar K.V 186464769240SAlex Tomas /* 18655356f261SAditya Kali * This function is grabs code from the very beginning of 18665356f261SAditya Kali * ext4_map_blocks, but assumes that the caller is from delayed write 18675356f261SAditya Kali * time. This function looks up the requested blocks and sets the 18685356f261SAditya Kali * buffer delay bit under the protection of i_data_sem. 18695356f261SAditya Kali */ 18705356f261SAditya Kali static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, 18715356f261SAditya Kali struct ext4_map_blocks *map, 18725356f261SAditya Kali struct buffer_head *bh) 18735356f261SAditya Kali { 1874d100eef2SZheng Liu struct extent_status es; 18755356f261SAditya Kali int retval; 18765356f261SAditya Kali sector_t invalid_block = ~((sector_t) 0xffff); 1877921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1878921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 1879921f266bSDmitry Monakhov 1880921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 1881921f266bSDmitry Monakhov #endif 18825356f261SAditya Kali 18835356f261SAditya Kali if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 18845356f261SAditya Kali invalid_block = ~0; 18855356f261SAditya Kali 18865356f261SAditya Kali map->m_flags = 0; 18875356f261SAditya Kali ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u," 18885356f261SAditya Kali "logical block %lu\n", inode->i_ino, map->m_len, 18895356f261SAditya Kali (unsigned long) map->m_lblk); 1890d100eef2SZheng Liu 1891d100eef2SZheng Liu /* Lookup extent status tree firstly */ 1892d100eef2SZheng Liu if (ext4_es_lookup_extent(inode, iblock, &es)) { 1893d100eef2SZheng Liu 1894d100eef2SZheng Liu if (ext4_es_is_hole(&es)) { 1895d100eef2SZheng Liu retval = 0; 1896d100eef2SZheng Liu down_read((&EXT4_I(inode)->i_data_sem)); 1897d100eef2SZheng Liu goto add_delayed; 1898d100eef2SZheng Liu } 1899d100eef2SZheng Liu 1900d100eef2SZheng Liu /* 1901d100eef2SZheng Liu * Delayed extent could be allocated by fallocate. 1902d100eef2SZheng Liu * So we need to check it. 1903d100eef2SZheng Liu */ 1904d100eef2SZheng Liu if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) { 1905d100eef2SZheng Liu map_bh(bh, inode->i_sb, invalid_block); 1906d100eef2SZheng Liu set_buffer_new(bh); 1907d100eef2SZheng Liu set_buffer_delay(bh); 1908d100eef2SZheng Liu return 0; 1909d100eef2SZheng Liu } 1910d100eef2SZheng Liu 1911d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk; 1912d100eef2SZheng Liu retval = es.es_len - (iblock - es.es_lblk); 1913d100eef2SZheng Liu if (retval > map->m_len) 1914d100eef2SZheng Liu retval = map->m_len; 1915d100eef2SZheng Liu map->m_len = retval; 1916d100eef2SZheng Liu if (ext4_es_is_written(&es)) 1917d100eef2SZheng Liu map->m_flags |= EXT4_MAP_MAPPED; 1918d100eef2SZheng Liu else if (ext4_es_is_unwritten(&es)) 1919d100eef2SZheng Liu map->m_flags |= EXT4_MAP_UNWRITTEN; 1920d100eef2SZheng Liu else 1921d100eef2SZheng Liu BUG_ON(1); 1922d100eef2SZheng Liu 1923921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1924921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0); 1925921f266bSDmitry Monakhov #endif 1926d100eef2SZheng Liu return retval; 1927d100eef2SZheng Liu } 1928d100eef2SZheng Liu 19295356f261SAditya Kali /* 19305356f261SAditya Kali * Try to see if we can get the block without requesting a new 19315356f261SAditya Kali * file system block. 19325356f261SAditya Kali */ 19335356f261SAditya Kali down_read((&EXT4_I(inode)->i_data_sem)); 19349c3569b5STao Ma if (ext4_has_inline_data(inode)) { 19359c3569b5STao Ma /* 19369c3569b5STao Ma * We will soon create blocks for this page, and let 19379c3569b5STao Ma * us pretend as if the blocks aren't allocated yet. 19389c3569b5STao Ma * In case of clusters, we have to handle the work 19399c3569b5STao Ma * of mapping from cluster so that the reserved space 19409c3569b5STao Ma * is calculated properly. 19419c3569b5STao Ma */ 19429c3569b5STao Ma if ((EXT4_SB(inode->i_sb)->s_cluster_ratio > 1) && 19439c3569b5STao Ma ext4_find_delalloc_cluster(inode, map->m_lblk)) 19449c3569b5STao Ma map->m_flags |= EXT4_MAP_FROM_CLUSTER; 19459c3569b5STao Ma retval = 0; 19469c3569b5STao Ma } else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 1947d100eef2SZheng Liu retval = ext4_ext_map_blocks(NULL, inode, map, 1948d100eef2SZheng Liu EXT4_GET_BLOCKS_NO_PUT_HOLE); 19495356f261SAditya Kali else 1950d100eef2SZheng Liu retval = ext4_ind_map_blocks(NULL, inode, map, 1951d100eef2SZheng Liu EXT4_GET_BLOCKS_NO_PUT_HOLE); 19525356f261SAditya Kali 1953d100eef2SZheng Liu add_delayed: 19545356f261SAditya Kali if (retval == 0) { 1955f7fec032SZheng Liu int ret; 19565356f261SAditya Kali /* 19575356f261SAditya Kali * XXX: __block_prepare_write() unmaps passed block, 19585356f261SAditya Kali * is it OK? 19595356f261SAditya Kali */ 1960386ad67cSLukas Czerner /* 1961386ad67cSLukas Czerner * If the block was allocated from previously allocated cluster, 1962386ad67cSLukas Czerner * then we don't need to reserve it again. However we still need 1963386ad67cSLukas Czerner * to reserve metadata for every block we're going to write. 1964386ad67cSLukas Czerner */ 19655356f261SAditya Kali if (!(map->m_flags & EXT4_MAP_FROM_CLUSTER)) { 1966f7fec032SZheng Liu ret = ext4_da_reserve_space(inode, iblock); 1967f7fec032SZheng Liu if (ret) { 19685356f261SAditya Kali /* not enough space to reserve */ 1969f7fec032SZheng Liu retval = ret; 19705356f261SAditya Kali goto out_unlock; 19715356f261SAditya Kali } 1972386ad67cSLukas Czerner } else { 1973386ad67cSLukas Czerner ret = ext4_da_reserve_metadata(inode, iblock); 1974386ad67cSLukas Czerner if (ret) { 1975386ad67cSLukas Czerner /* not enough space to reserve */ 1976386ad67cSLukas Czerner retval = ret; 1977386ad67cSLukas Czerner goto out_unlock; 1978386ad67cSLukas Czerner } 1979f7fec032SZheng Liu } 19805356f261SAditya Kali 1981f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 1982fdc0212eSZheng Liu ~0, EXTENT_STATUS_DELAYED); 1983f7fec032SZheng Liu if (ret) { 1984f7fec032SZheng Liu retval = ret; 198551865fdaSZheng Liu goto out_unlock; 1986f7fec032SZheng Liu } 198751865fdaSZheng Liu 19885356f261SAditya Kali /* Clear EXT4_MAP_FROM_CLUSTER flag since its purpose is served 19895356f261SAditya Kali * and it should not appear on the bh->b_state. 19905356f261SAditya Kali */ 19915356f261SAditya Kali map->m_flags &= ~EXT4_MAP_FROM_CLUSTER; 19925356f261SAditya Kali 19935356f261SAditya Kali map_bh(bh, inode->i_sb, invalid_block); 19945356f261SAditya Kali set_buffer_new(bh); 19955356f261SAditya Kali set_buffer_delay(bh); 1996f7fec032SZheng Liu } else if (retval > 0) { 1997f7fec032SZheng Liu int ret; 1998f7fec032SZheng Liu unsigned long long status; 1999f7fec032SZheng Liu 2000921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 2001921f266bSDmitry Monakhov if (retval != map->m_len) { 2002921f266bSDmitry Monakhov printk("ES len assertation failed for inode: %lu " 2003921f266bSDmitry Monakhov "retval %d != map->m_len %d " 2004921f266bSDmitry Monakhov "in %s (lookup)\n", inode->i_ino, retval, 2005921f266bSDmitry Monakhov map->m_len, __func__); 2006921f266bSDmitry Monakhov } 2007921f266bSDmitry Monakhov #endif 2008921f266bSDmitry Monakhov 2009f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 2010f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 2011f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 2012f7fec032SZheng Liu map->m_pblk, status); 2013f7fec032SZheng Liu if (ret != 0) 2014f7fec032SZheng Liu retval = ret; 20155356f261SAditya Kali } 20165356f261SAditya Kali 20175356f261SAditya Kali out_unlock: 20185356f261SAditya Kali up_read((&EXT4_I(inode)->i_data_sem)); 20195356f261SAditya Kali 20205356f261SAditya Kali return retval; 20215356f261SAditya Kali } 20225356f261SAditya Kali 20235356f261SAditya Kali /* 2024b920c755STheodore Ts'o * This is a special get_blocks_t callback which is used by 2025b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 2026b920c755STheodore Ts'o * reserve space for a single block. 202729fa89d0SAneesh Kumar K.V * 202829fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 202929fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 203029fa89d0SAneesh Kumar K.V * 203129fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 203229fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 203329fa89d0SAneesh Kumar K.V * initialized properly. 203464769240SAlex Tomas */ 20359c3569b5STao Ma int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 20362ed88685STheodore Ts'o struct buffer_head *bh, int create) 203764769240SAlex Tomas { 20382ed88685STheodore Ts'o struct ext4_map_blocks map; 203964769240SAlex Tomas int ret = 0; 204064769240SAlex Tomas 204164769240SAlex Tomas BUG_ON(create == 0); 20422ed88685STheodore Ts'o BUG_ON(bh->b_size != inode->i_sb->s_blocksize); 20432ed88685STheodore Ts'o 20442ed88685STheodore Ts'o map.m_lblk = iblock; 20452ed88685STheodore Ts'o map.m_len = 1; 204664769240SAlex Tomas 204764769240SAlex Tomas /* 204864769240SAlex Tomas * first, we need to know whether the block is allocated already 204964769240SAlex Tomas * preallocated blocks are unmapped but should treated 205064769240SAlex Tomas * the same as allocated blocks. 205164769240SAlex Tomas */ 20525356f261SAditya Kali ret = ext4_da_map_blocks(inode, iblock, &map, bh); 20535356f261SAditya Kali if (ret <= 0) 20542ed88685STheodore Ts'o return ret; 205564769240SAlex Tomas 20562ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 20572ed88685STheodore Ts'o bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; 20582ed88685STheodore Ts'o 20592ed88685STheodore Ts'o if (buffer_unwritten(bh)) { 20602ed88685STheodore Ts'o /* A delayed write to unwritten bh should be marked 20612ed88685STheodore Ts'o * new and mapped. Mapped ensures that we don't do 20622ed88685STheodore Ts'o * get_block multiple times when we write to the same 20632ed88685STheodore Ts'o * offset and new ensures that we do proper zero out 20642ed88685STheodore Ts'o * for partial write. 20652ed88685STheodore Ts'o */ 20662ed88685STheodore Ts'o set_buffer_new(bh); 2067c8205636STheodore Ts'o set_buffer_mapped(bh); 20682ed88685STheodore Ts'o } 20692ed88685STheodore Ts'o return 0; 207064769240SAlex Tomas } 207161628a3fSMingming Cao 207262e086beSAneesh Kumar K.V static int bget_one(handle_t *handle, struct buffer_head *bh) 207362e086beSAneesh Kumar K.V { 207462e086beSAneesh Kumar K.V get_bh(bh); 207562e086beSAneesh Kumar K.V return 0; 207662e086beSAneesh Kumar K.V } 207762e086beSAneesh Kumar K.V 207862e086beSAneesh Kumar K.V static int bput_one(handle_t *handle, struct buffer_head *bh) 207962e086beSAneesh Kumar K.V { 208062e086beSAneesh Kumar K.V put_bh(bh); 208162e086beSAneesh Kumar K.V return 0; 208262e086beSAneesh Kumar K.V } 208362e086beSAneesh Kumar K.V 208462e086beSAneesh Kumar K.V static int __ext4_journalled_writepage(struct page *page, 208562e086beSAneesh Kumar K.V unsigned int len) 208662e086beSAneesh Kumar K.V { 208762e086beSAneesh Kumar K.V struct address_space *mapping = page->mapping; 208862e086beSAneesh Kumar K.V struct inode *inode = mapping->host; 20893fdcfb66STao Ma struct buffer_head *page_bufs = NULL; 209062e086beSAneesh Kumar K.V handle_t *handle = NULL; 20913fdcfb66STao Ma int ret = 0, err = 0; 20923fdcfb66STao Ma int inline_data = ext4_has_inline_data(inode); 20933fdcfb66STao Ma struct buffer_head *inode_bh = NULL; 209462e086beSAneesh Kumar K.V 2095cb20d518STheodore Ts'o ClearPageChecked(page); 20963fdcfb66STao Ma 20973fdcfb66STao Ma if (inline_data) { 20983fdcfb66STao Ma BUG_ON(page->index != 0); 20993fdcfb66STao Ma BUG_ON(len > ext4_get_max_inline_size(inode)); 21003fdcfb66STao Ma inode_bh = ext4_journalled_write_inline_data(inode, len, page); 21013fdcfb66STao Ma if (inode_bh == NULL) 21023fdcfb66STao Ma goto out; 21033fdcfb66STao Ma } else { 210462e086beSAneesh Kumar K.V page_bufs = page_buffers(page); 21053fdcfb66STao Ma if (!page_bufs) { 21063fdcfb66STao Ma BUG(); 21073fdcfb66STao Ma goto out; 21083fdcfb66STao Ma } 21093fdcfb66STao Ma ext4_walk_page_buffers(handle, page_bufs, 0, len, 21103fdcfb66STao Ma NULL, bget_one); 21113fdcfb66STao Ma } 211262e086beSAneesh Kumar K.V /* As soon as we unlock the page, it can go away, but we have 211362e086beSAneesh Kumar K.V * references to buffers so we are safe */ 211462e086beSAneesh Kumar K.V unlock_page(page); 211562e086beSAneesh Kumar K.V 21169924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 21179924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 211862e086beSAneesh Kumar K.V if (IS_ERR(handle)) { 211962e086beSAneesh Kumar K.V ret = PTR_ERR(handle); 212062e086beSAneesh Kumar K.V goto out; 212162e086beSAneesh Kumar K.V } 212262e086beSAneesh Kumar K.V 2123441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 2124441c8508SCurt Wohlgemuth 21253fdcfb66STao Ma if (inline_data) { 21263fdcfb66STao Ma ret = ext4_journal_get_write_access(handle, inode_bh); 21273fdcfb66STao Ma 21283fdcfb66STao Ma err = ext4_handle_dirty_metadata(handle, inode, inode_bh); 21293fdcfb66STao Ma 21303fdcfb66STao Ma } else { 2131f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 213262e086beSAneesh Kumar K.V do_journal_get_write_access); 213362e086beSAneesh Kumar K.V 2134f19d5870STao Ma err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 213562e086beSAneesh Kumar K.V write_end_fn); 21363fdcfb66STao Ma } 213762e086beSAneesh Kumar K.V if (ret == 0) 213862e086beSAneesh Kumar K.V ret = err; 21392d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 214062e086beSAneesh Kumar K.V err = ext4_journal_stop(handle); 214162e086beSAneesh Kumar K.V if (!ret) 214262e086beSAneesh Kumar K.V ret = err; 214362e086beSAneesh Kumar K.V 21443fdcfb66STao Ma if (!ext4_has_inline_data(inode)) 21453fdcfb66STao Ma ext4_walk_page_buffers(handle, page_bufs, 0, len, 21463fdcfb66STao Ma NULL, bput_one); 214719f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 214862e086beSAneesh Kumar K.V out: 21493fdcfb66STao Ma brelse(inode_bh); 215062e086beSAneesh Kumar K.V return ret; 215162e086beSAneesh Kumar K.V } 215262e086beSAneesh Kumar K.V 215361628a3fSMingming Cao /* 215443ce1d23SAneesh Kumar K.V * Note that we don't need to start a transaction unless we're journaling data 215543ce1d23SAneesh Kumar K.V * because we should have holes filled from ext4_page_mkwrite(). We even don't 215643ce1d23SAneesh Kumar K.V * need to file the inode to the transaction's list in ordered mode because if 215743ce1d23SAneesh Kumar K.V * we are writing back data added by write(), the inode is already there and if 215843ce1d23SAneesh Kumar K.V * we are writing back data modified via mmap(), no one guarantees in which 215943ce1d23SAneesh Kumar K.V * transaction the data will hit the disk. In case we are journaling data, we 216043ce1d23SAneesh Kumar K.V * cannot start transaction directly because transaction start ranks above page 216143ce1d23SAneesh Kumar K.V * lock so we have to do some magic. 216243ce1d23SAneesh Kumar K.V * 2163b920c755STheodore Ts'o * This function can get called via... 2164b920c755STheodore Ts'o * - ext4_da_writepages after taking page lock (have journal handle) 2165b920c755STheodore Ts'o * - journal_submit_inode_data_buffers (no journal handle) 2166f6463b0dSArtem Bityutskiy * - shrink_page_list via the kswapd/direct reclaim (no journal handle) 2167b920c755STheodore Ts'o * - grab_page_cache when doing write_begin (have journal handle) 216843ce1d23SAneesh Kumar K.V * 216943ce1d23SAneesh Kumar K.V * We don't do any block allocation in this function. If we have page with 217043ce1d23SAneesh Kumar K.V * multiple blocks we need to write those buffer_heads that are mapped. This 217143ce1d23SAneesh Kumar K.V * is important for mmaped based write. So if we do with blocksize 1K 217243ce1d23SAneesh Kumar K.V * truncate(f, 1024); 217343ce1d23SAneesh Kumar K.V * a = mmap(f, 0, 4096); 217443ce1d23SAneesh Kumar K.V * a[0] = 'a'; 217543ce1d23SAneesh Kumar K.V * truncate(f, 4096); 217643ce1d23SAneesh Kumar K.V * we have in the page first buffer_head mapped via page_mkwrite call back 217790802ed9SPaul Bolle * but other buffer_heads would be unmapped but dirty (dirty done via the 217843ce1d23SAneesh Kumar K.V * do_wp_page). So writepage should write the first block. If we modify 217943ce1d23SAneesh Kumar K.V * the mmap area beyond 1024 we will again get a page_fault and the 218043ce1d23SAneesh Kumar K.V * page_mkwrite callback will do the block allocation and mark the 218143ce1d23SAneesh Kumar K.V * buffer_heads mapped. 218243ce1d23SAneesh Kumar K.V * 218343ce1d23SAneesh Kumar K.V * We redirty the page if we have any buffer_heads that is either delay or 218443ce1d23SAneesh Kumar K.V * unwritten in the page. 218543ce1d23SAneesh Kumar K.V * 218643ce1d23SAneesh Kumar K.V * We can get recursively called as show below. 218743ce1d23SAneesh Kumar K.V * 218843ce1d23SAneesh Kumar K.V * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> 218943ce1d23SAneesh Kumar K.V * ext4_writepage() 219043ce1d23SAneesh Kumar K.V * 219143ce1d23SAneesh Kumar K.V * But since we don't do any block allocation we should not deadlock. 219243ce1d23SAneesh Kumar K.V * Page also have the dirty flag cleared so we don't get recurive page_lock. 219361628a3fSMingming Cao */ 219443ce1d23SAneesh Kumar K.V static int ext4_writepage(struct page *page, 219564769240SAlex Tomas struct writeback_control *wbc) 219664769240SAlex Tomas { 2197f8bec370SJan Kara int ret = 0; 219861628a3fSMingming Cao loff_t size; 2199498e5f24STheodore Ts'o unsigned int len; 2200744692dcSJiaying Zhang struct buffer_head *page_bufs = NULL; 220161628a3fSMingming Cao struct inode *inode = page->mapping->host; 220236ade451SJan Kara struct ext4_io_submit io_submit; 220364769240SAlex Tomas 2204a9c667f8SLukas Czerner trace_ext4_writepage(page); 220561628a3fSMingming Cao size = i_size_read(inode); 220661628a3fSMingming Cao if (page->index == size >> PAGE_CACHE_SHIFT) 220761628a3fSMingming Cao len = size & ~PAGE_CACHE_MASK; 220861628a3fSMingming Cao else 220961628a3fSMingming Cao len = PAGE_CACHE_SIZE; 221061628a3fSMingming Cao 2211f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 221264769240SAlex Tomas /* 2213fe386132SJan Kara * We cannot do block allocation or other extent handling in this 2214fe386132SJan Kara * function. If there are buffers needing that, we have to redirty 2215fe386132SJan Kara * the page. But we may reach here when we do a journal commit via 2216fe386132SJan Kara * journal_submit_inode_data_buffers() and in that case we must write 2217fe386132SJan Kara * allocated buffers to achieve data=ordered mode guarantees. 221864769240SAlex Tomas */ 2219f19d5870STao Ma if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2220c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 222161628a3fSMingming Cao redirty_page_for_writepage(wbc, page); 2222fe386132SJan Kara if (current->flags & PF_MEMALLOC) { 2223fe386132SJan Kara /* 2224fe386132SJan Kara * For memory cleaning there's no point in writing only 2225fe386132SJan Kara * some buffers. So just bail out. Warn if we came here 2226fe386132SJan Kara * from direct reclaim. 2227fe386132SJan Kara */ 2228fe386132SJan Kara WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) 2229fe386132SJan Kara == PF_MEMALLOC); 223061628a3fSMingming Cao unlock_page(page); 223161628a3fSMingming Cao return 0; 223261628a3fSMingming Cao } 2233f0e6c985SAneesh Kumar K.V } 223464769240SAlex Tomas 2235cb20d518STheodore Ts'o if (PageChecked(page) && ext4_should_journal_data(inode)) 223643ce1d23SAneesh Kumar K.V /* 223743ce1d23SAneesh Kumar K.V * It's mmapped pagecache. Add buffers and journal it. There 223843ce1d23SAneesh Kumar K.V * doesn't seem much point in redirtying the page here. 223943ce1d23SAneesh Kumar K.V */ 22403f0ca309SWu Fengguang return __ext4_journalled_writepage(page, len); 224143ce1d23SAneesh Kumar K.V 22424eec708dSJan Kara ext4_io_submit_init(&io_submit, wbc); 22434eec708dSJan Kara io_submit.io_end = ext4_init_io_end(inode, GFP_NOFS); 22444eec708dSJan Kara if (!io_submit.io_end) { 22454eec708dSJan Kara redirty_page_for_writepage(wbc, page); 22464eec708dSJan Kara return -ENOMEM; 22474eec708dSJan Kara } 224836ade451SJan Kara ret = ext4_bio_write_page(&io_submit, page, len, wbc); 224936ade451SJan Kara ext4_io_submit(&io_submit); 22504eec708dSJan Kara /* Drop io_end reference we got from init */ 22514eec708dSJan Kara ext4_put_io_end_defer(io_submit.io_end); 225264769240SAlex Tomas return ret; 225364769240SAlex Tomas } 225464769240SAlex Tomas 225561628a3fSMingming Cao /* 2256525f4ed8SMingming Cao * This is called via ext4_da_writepages() to 225725985edcSLucas De Marchi * calculate the total number of credits to reserve to fit 2258525f4ed8SMingming Cao * a single extent allocation into a single transaction, 2259525f4ed8SMingming Cao * ext4_da_writpeages() will loop calling this before 2260525f4ed8SMingming Cao * the block allocation. 226161628a3fSMingming Cao */ 2262525f4ed8SMingming Cao 2263525f4ed8SMingming Cao static int ext4_da_writepages_trans_blocks(struct inode *inode) 2264525f4ed8SMingming Cao { 2265525f4ed8SMingming Cao int max_blocks = EXT4_I(inode)->i_reserved_data_blocks; 2266525f4ed8SMingming Cao 2267525f4ed8SMingming Cao /* 2268525f4ed8SMingming Cao * With non-extent format the journal credit needed to 2269525f4ed8SMingming Cao * insert nrblocks contiguous block is dependent on 2270525f4ed8SMingming Cao * number of contiguous block. So we will limit 2271525f4ed8SMingming Cao * number of contiguous block to a sane value 2272525f4ed8SMingming Cao */ 227312e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) && 2274525f4ed8SMingming Cao (max_blocks > EXT4_MAX_TRANS_DATA)) 2275525f4ed8SMingming Cao max_blocks = EXT4_MAX_TRANS_DATA; 2276525f4ed8SMingming Cao 2277525f4ed8SMingming Cao return ext4_chunk_trans_blocks(inode, max_blocks); 2278525f4ed8SMingming Cao } 227961628a3fSMingming Cao 22808e48dcfbSTheodore Ts'o /* 22818e48dcfbSTheodore Ts'o * write_cache_pages_da - walk the list of dirty pages of the given 22828eb9e5ceSTheodore Ts'o * address space and accumulate pages that need writing, and call 2283168fc022STheodore Ts'o * mpage_da_map_and_submit to map a single contiguous memory region 2284168fc022STheodore Ts'o * and then write them. 22858e48dcfbSTheodore Ts'o */ 22869c3569b5STao Ma static int write_cache_pages_da(handle_t *handle, 22879c3569b5STao Ma struct address_space *mapping, 22888e48dcfbSTheodore Ts'o struct writeback_control *wbc, 228972f84e65SEric Sandeen struct mpage_da_data *mpd, 229072f84e65SEric Sandeen pgoff_t *done_index) 22918e48dcfbSTheodore Ts'o { 22928eb9e5ceSTheodore Ts'o struct buffer_head *bh, *head; 2293168fc022STheodore Ts'o struct inode *inode = mapping->host; 22948e48dcfbSTheodore Ts'o struct pagevec pvec; 22954f01b02cSTheodore Ts'o unsigned int nr_pages; 22964f01b02cSTheodore Ts'o sector_t logical; 22974f01b02cSTheodore Ts'o pgoff_t index, end; 22988e48dcfbSTheodore Ts'o long nr_to_write = wbc->nr_to_write; 22994f01b02cSTheodore Ts'o int i, tag, ret = 0; 23008e48dcfbSTheodore Ts'o 2301168fc022STheodore Ts'o memset(mpd, 0, sizeof(struct mpage_da_data)); 2302168fc022STheodore Ts'o mpd->wbc = wbc; 2303168fc022STheodore Ts'o mpd->inode = inode; 23048e48dcfbSTheodore Ts'o pagevec_init(&pvec, 0); 23058e48dcfbSTheodore Ts'o index = wbc->range_start >> PAGE_CACHE_SHIFT; 23068e48dcfbSTheodore Ts'o end = wbc->range_end >> PAGE_CACHE_SHIFT; 23078e48dcfbSTheodore Ts'o 23086e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 23095b41d924SEric Sandeen tag = PAGECACHE_TAG_TOWRITE; 23105b41d924SEric Sandeen else 23115b41d924SEric Sandeen tag = PAGECACHE_TAG_DIRTY; 23125b41d924SEric Sandeen 231372f84e65SEric Sandeen *done_index = index; 23144f01b02cSTheodore Ts'o while (index <= end) { 23155b41d924SEric Sandeen nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, 23168e48dcfbSTheodore Ts'o min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); 23178e48dcfbSTheodore Ts'o if (nr_pages == 0) 23184f01b02cSTheodore Ts'o return 0; 23198e48dcfbSTheodore Ts'o 23208e48dcfbSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 23218e48dcfbSTheodore Ts'o struct page *page = pvec.pages[i]; 23228e48dcfbSTheodore Ts'o 23238e48dcfbSTheodore Ts'o /* 23248e48dcfbSTheodore Ts'o * At this point, the page may be truncated or 23258e48dcfbSTheodore Ts'o * invalidated (changing page->mapping to NULL), or 23268e48dcfbSTheodore Ts'o * even swizzled back from swapper_space to tmpfs file 23278e48dcfbSTheodore Ts'o * mapping. However, page->index will not change 23288e48dcfbSTheodore Ts'o * because we have a reference on the page. 23298e48dcfbSTheodore Ts'o */ 23304f01b02cSTheodore Ts'o if (page->index > end) 23314f01b02cSTheodore Ts'o goto out; 23328e48dcfbSTheodore Ts'o 233372f84e65SEric Sandeen *done_index = page->index + 1; 233472f84e65SEric Sandeen 233578aaced3STheodore Ts'o /* 233678aaced3STheodore Ts'o * If we can't merge this page, and we have 233778aaced3STheodore Ts'o * accumulated an contiguous region, write it 233878aaced3STheodore Ts'o */ 233978aaced3STheodore Ts'o if ((mpd->next_page != page->index) && 234078aaced3STheodore Ts'o (mpd->next_page != mpd->first_page)) { 234178aaced3STheodore Ts'o mpage_da_map_and_submit(mpd); 234278aaced3STheodore Ts'o goto ret_extent_tail; 234378aaced3STheodore Ts'o } 234478aaced3STheodore Ts'o 23458e48dcfbSTheodore Ts'o lock_page(page); 23468e48dcfbSTheodore Ts'o 23478e48dcfbSTheodore Ts'o /* 23484f01b02cSTheodore Ts'o * If the page is no longer dirty, or its 23494f01b02cSTheodore Ts'o * mapping no longer corresponds to inode we 23504f01b02cSTheodore Ts'o * are writing (which means it has been 23514f01b02cSTheodore Ts'o * truncated or invalidated), or the page is 23524f01b02cSTheodore Ts'o * already under writeback and we are not 23534f01b02cSTheodore Ts'o * doing a data integrity writeback, skip the page 23548e48dcfbSTheodore Ts'o */ 23554f01b02cSTheodore Ts'o if (!PageDirty(page) || 23564f01b02cSTheodore Ts'o (PageWriteback(page) && 23574f01b02cSTheodore Ts'o (wbc->sync_mode == WB_SYNC_NONE)) || 23584f01b02cSTheodore Ts'o unlikely(page->mapping != mapping)) { 23598e48dcfbSTheodore Ts'o unlock_page(page); 23608e48dcfbSTheodore Ts'o continue; 23618e48dcfbSTheodore Ts'o } 23628e48dcfbSTheodore Ts'o 23638e48dcfbSTheodore Ts'o wait_on_page_writeback(page); 23648e48dcfbSTheodore Ts'o BUG_ON(PageWriteback(page)); 23658e48dcfbSTheodore Ts'o 23669c3569b5STao Ma /* 23679c3569b5STao Ma * If we have inline data and arrive here, it means that 23689c3569b5STao Ma * we will soon create the block for the 1st page, so 23699c3569b5STao Ma * we'd better clear the inline data here. 23709c3569b5STao Ma */ 23719c3569b5STao Ma if (ext4_has_inline_data(inode)) { 23729c3569b5STao Ma BUG_ON(ext4_test_inode_state(inode, 23739c3569b5STao Ma EXT4_STATE_MAY_INLINE_DATA)); 23749c3569b5STao Ma ext4_destroy_inline_data(handle, inode); 23759c3569b5STao Ma } 23769c3569b5STao Ma 2377168fc022STheodore Ts'o if (mpd->next_page != page->index) 23788eb9e5ceSTheodore Ts'o mpd->first_page = page->index; 23798eb9e5ceSTheodore Ts'o mpd->next_page = page->index + 1; 23808eb9e5ceSTheodore Ts'o logical = (sector_t) page->index << 23818eb9e5ceSTheodore Ts'o (PAGE_CACHE_SHIFT - inode->i_blkbits); 23828eb9e5ceSTheodore Ts'o 2383f8bec370SJan Kara /* Add all dirty buffers to mpd */ 23848eb9e5ceSTheodore Ts'o head = page_buffers(page); 23858eb9e5ceSTheodore Ts'o bh = head; 23868eb9e5ceSTheodore Ts'o do { 23878eb9e5ceSTheodore Ts'o BUG_ON(buffer_locked(bh)); 23888eb9e5ceSTheodore Ts'o /* 2389f8bec370SJan Kara * We need to try to allocate unmapped blocks 2390f8bec370SJan Kara * in the same page. Otherwise we won't make 2391f8bec370SJan Kara * progress with the page in ext4_writepage 23928eb9e5ceSTheodore Ts'o */ 23938eb9e5ceSTheodore Ts'o if (ext4_bh_delay_or_unwritten(NULL, bh)) { 23948eb9e5ceSTheodore Ts'o mpage_add_bh_to_extent(mpd, logical, 23958eb9e5ceSTheodore Ts'o bh->b_state); 23964f01b02cSTheodore Ts'o if (mpd->io_done) 23974f01b02cSTheodore Ts'o goto ret_extent_tail; 2398f8bec370SJan Kara } else if (buffer_dirty(bh) && 2399f8bec370SJan Kara buffer_mapped(bh)) { 24008eb9e5ceSTheodore Ts'o /* 2401f8bec370SJan Kara * mapped dirty buffer. We need to 2402f8bec370SJan Kara * update the b_state because we look 2403f8bec370SJan Kara * at b_state in mpage_da_map_blocks. 2404f8bec370SJan Kara * We don't update b_size because if we 2405f8bec370SJan Kara * find an unmapped buffer_head later 2406f8bec370SJan Kara * we need to use the b_state flag of 2407f8bec370SJan Kara * that buffer_head. 24088eb9e5ceSTheodore Ts'o */ 24098eb9e5ceSTheodore Ts'o if (mpd->b_size == 0) 2410f8bec370SJan Kara mpd->b_state = 2411f8bec370SJan Kara bh->b_state & BH_FLAGS; 24128e48dcfbSTheodore Ts'o } 24138eb9e5ceSTheodore Ts'o logical++; 24148eb9e5ceSTheodore Ts'o } while ((bh = bh->b_this_page) != head); 24158e48dcfbSTheodore Ts'o 24168e48dcfbSTheodore Ts'o if (nr_to_write > 0) { 24178e48dcfbSTheodore Ts'o nr_to_write--; 24188e48dcfbSTheodore Ts'o if (nr_to_write == 0 && 24194f01b02cSTheodore Ts'o wbc->sync_mode == WB_SYNC_NONE) 24208e48dcfbSTheodore Ts'o /* 24218e48dcfbSTheodore Ts'o * We stop writing back only if we are 24228e48dcfbSTheodore Ts'o * not doing integrity sync. In case of 24238e48dcfbSTheodore Ts'o * integrity sync we have to keep going 24248e48dcfbSTheodore Ts'o * because someone may be concurrently 24258e48dcfbSTheodore Ts'o * dirtying pages, and we might have 24268e48dcfbSTheodore Ts'o * synced a lot of newly appeared dirty 24278e48dcfbSTheodore Ts'o * pages, but have not synced all of the 24288e48dcfbSTheodore Ts'o * old dirty pages. 24298e48dcfbSTheodore Ts'o */ 24304f01b02cSTheodore Ts'o goto out; 24318e48dcfbSTheodore Ts'o } 24328e48dcfbSTheodore Ts'o } 24338e48dcfbSTheodore Ts'o pagevec_release(&pvec); 24348e48dcfbSTheodore Ts'o cond_resched(); 24358e48dcfbSTheodore Ts'o } 24364f01b02cSTheodore Ts'o return 0; 24374f01b02cSTheodore Ts'o ret_extent_tail: 24384f01b02cSTheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 24398eb9e5ceSTheodore Ts'o out: 24408eb9e5ceSTheodore Ts'o pagevec_release(&pvec); 24418eb9e5ceSTheodore Ts'o cond_resched(); 24428e48dcfbSTheodore Ts'o return ret; 24438e48dcfbSTheodore Ts'o } 24448e48dcfbSTheodore Ts'o 24458e48dcfbSTheodore Ts'o 244664769240SAlex Tomas static int ext4_da_writepages(struct address_space *mapping, 244764769240SAlex Tomas struct writeback_control *wbc) 244864769240SAlex Tomas { 244922208dedSAneesh Kumar K.V pgoff_t index; 245022208dedSAneesh Kumar K.V int range_whole = 0; 245161628a3fSMingming Cao handle_t *handle = NULL; 2452df22291fSAneesh Kumar K.V struct mpage_da_data mpd; 24535e745b04SAneesh Kumar K.V struct inode *inode = mapping->host; 2454498e5f24STheodore Ts'o int pages_written = 0; 245555138e0bSTheodore Ts'o unsigned int max_pages; 24562acf2c26SAneesh Kumar K.V int range_cyclic, cycled = 1, io_done = 0; 245755138e0bSTheodore Ts'o int needed_blocks, ret = 0; 245855138e0bSTheodore Ts'o long desired_nr_to_write, nr_to_writebump = 0; 2459de89de6eSTheodore Ts'o loff_t range_start = wbc->range_start; 24605e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 246172f84e65SEric Sandeen pgoff_t done_index = 0; 24625b41d924SEric Sandeen pgoff_t end; 24631bce63d1SShaohua Li struct blk_plug plug; 246461628a3fSMingming Cao 24659bffad1eSTheodore Ts'o trace_ext4_da_writepages(inode, wbc); 2466ba80b101STheodore Ts'o 246761628a3fSMingming Cao /* 246861628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 246961628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 247061628a3fSMingming Cao * because that could violate lock ordering on umount 247161628a3fSMingming Cao */ 2472a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 247361628a3fSMingming Cao return 0; 24742a21e37eSTheodore Ts'o 24752a21e37eSTheodore Ts'o /* 24762a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 24772a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 24782a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 24794ab2f15bSTheodore Ts'o * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because 24802a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 24812a21e37eSTheodore Ts'o * read-only, and in that case, ext4_da_writepages should 24822a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 24832a21e37eSTheodore Ts'o * the stack trace. 24842a21e37eSTheodore Ts'o */ 24854ab2f15bSTheodore Ts'o if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) 24862a21e37eSTheodore Ts'o return -EROFS; 24872a21e37eSTheodore Ts'o 248822208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 248922208dedSAneesh Kumar K.V range_whole = 1; 249061628a3fSMingming Cao 24912acf2c26SAneesh Kumar K.V range_cyclic = wbc->range_cyclic; 24922acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 249322208dedSAneesh Kumar K.V index = mapping->writeback_index; 24942acf2c26SAneesh Kumar K.V if (index) 24952acf2c26SAneesh Kumar K.V cycled = 0; 24962acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 24972acf2c26SAneesh Kumar K.V wbc->range_end = LLONG_MAX; 24982acf2c26SAneesh Kumar K.V wbc->range_cyclic = 0; 24995b41d924SEric Sandeen end = -1; 25005b41d924SEric Sandeen } else { 250122208dedSAneesh Kumar K.V index = wbc->range_start >> PAGE_CACHE_SHIFT; 25025b41d924SEric Sandeen end = wbc->range_end >> PAGE_CACHE_SHIFT; 25035b41d924SEric Sandeen } 2504a1d6cc56SAneesh Kumar K.V 250555138e0bSTheodore Ts'o /* 250655138e0bSTheodore Ts'o * This works around two forms of stupidity. The first is in 250755138e0bSTheodore Ts'o * the writeback code, which caps the maximum number of pages 250855138e0bSTheodore Ts'o * written to be 1024 pages. This is wrong on multiple 250955138e0bSTheodore Ts'o * levels; different architectues have a different page size, 251055138e0bSTheodore Ts'o * which changes the maximum amount of data which gets 251155138e0bSTheodore Ts'o * written. Secondly, 4 megabytes is way too small. XFS 251255138e0bSTheodore Ts'o * forces this value to be 16 megabytes by multiplying 251355138e0bSTheodore Ts'o * nr_to_write parameter by four, and then relies on its 251455138e0bSTheodore Ts'o * allocator to allocate larger extents to make them 251555138e0bSTheodore Ts'o * contiguous. Unfortunately this brings us to the second 251655138e0bSTheodore Ts'o * stupidity, which is that ext4's mballoc code only allocates 251755138e0bSTheodore Ts'o * at most 2048 blocks. So we force contiguous writes up to 251855138e0bSTheodore Ts'o * the number of dirty blocks in the inode, or 251955138e0bSTheodore Ts'o * sbi->max_writeback_mb_bump whichever is smaller. 252055138e0bSTheodore Ts'o */ 252155138e0bSTheodore Ts'o max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT); 2522b443e733SEric Sandeen if (!range_cyclic && range_whole) { 2523b443e733SEric Sandeen if (wbc->nr_to_write == LONG_MAX) 2524b443e733SEric Sandeen desired_nr_to_write = wbc->nr_to_write; 252555138e0bSTheodore Ts'o else 2526b443e733SEric Sandeen desired_nr_to_write = wbc->nr_to_write * 8; 2527b443e733SEric Sandeen } else 252855138e0bSTheodore Ts'o desired_nr_to_write = ext4_num_dirty_pages(inode, index, 252955138e0bSTheodore Ts'o max_pages); 253055138e0bSTheodore Ts'o if (desired_nr_to_write > max_pages) 253155138e0bSTheodore Ts'o desired_nr_to_write = max_pages; 253255138e0bSTheodore Ts'o 253355138e0bSTheodore Ts'o if (wbc->nr_to_write < desired_nr_to_write) { 253455138e0bSTheodore Ts'o nr_to_writebump = desired_nr_to_write - wbc->nr_to_write; 253555138e0bSTheodore Ts'o wbc->nr_to_write = desired_nr_to_write; 253655138e0bSTheodore Ts'o } 253755138e0bSTheodore Ts'o 25382acf2c26SAneesh Kumar K.V retry: 25396e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 25405b41d924SEric Sandeen tag_pages_for_writeback(mapping, index, end); 25415b41d924SEric Sandeen 25421bce63d1SShaohua Li blk_start_plug(&plug); 254322208dedSAneesh Kumar K.V while (!ret && wbc->nr_to_write > 0) { 2544a1d6cc56SAneesh Kumar K.V 2545a1d6cc56SAneesh Kumar K.V /* 2546a1d6cc56SAneesh Kumar K.V * we insert one extent at a time. So we need 2547a1d6cc56SAneesh Kumar K.V * credit needed for single extent allocation. 2548a1d6cc56SAneesh Kumar K.V * journalled mode is currently not supported 2549a1d6cc56SAneesh Kumar K.V * by delalloc 2550a1d6cc56SAneesh Kumar K.V */ 2551a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 2552525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 2553a1d6cc56SAneesh Kumar K.V 255461628a3fSMingming Cao /* start a new transaction*/ 25559924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 25569924a92aSTheodore Ts'o needed_blocks); 255761628a3fSMingming Cao if (IS_ERR(handle)) { 255861628a3fSMingming Cao ret = PTR_ERR(handle); 25591693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2560fbe845ddSCurt Wohlgemuth "%ld pages, ino %lu; err %d", __func__, 2561a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 25623c1fcb2cSNamjae Jeon blk_finish_plug(&plug); 256361628a3fSMingming Cao goto out_writepages; 256461628a3fSMingming Cao } 2565f63e6005STheodore Ts'o 2566f63e6005STheodore Ts'o /* 25678eb9e5ceSTheodore Ts'o * Now call write_cache_pages_da() to find the next 2568f63e6005STheodore Ts'o * contiguous region of logical blocks that need 25698eb9e5ceSTheodore Ts'o * blocks to be allocated by ext4 and submit them. 2570f63e6005STheodore Ts'o */ 25719c3569b5STao Ma ret = write_cache_pages_da(handle, mapping, 25729c3569b5STao Ma wbc, &mpd, &done_index); 2573f63e6005STheodore Ts'o /* 2574af901ca1SAndré Goddard Rosa * If we have a contiguous extent of pages and we 2575f63e6005STheodore Ts'o * haven't done the I/O yet, map the blocks and submit 2576f63e6005STheodore Ts'o * them for I/O. 2577f63e6005STheodore Ts'o */ 2578f63e6005STheodore Ts'o if (!mpd.io_done && mpd.next_page != mpd.first_page) { 25795a87b7a5STheodore Ts'o mpage_da_map_and_submit(&mpd); 2580f63e6005STheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 2581f63e6005STheodore Ts'o } 2582b3a3ca8cSTheodore Ts'o trace_ext4_da_write_pages(inode, &mpd); 2583f63e6005STheodore Ts'o wbc->nr_to_write -= mpd.pages_written; 2584df22291fSAneesh Kumar K.V 258561628a3fSMingming Cao ext4_journal_stop(handle); 2586df22291fSAneesh Kumar K.V 25878f64b32eSEric Sandeen if ((mpd.retval == -ENOSPC) && sbi->s_journal) { 258822208dedSAneesh Kumar K.V /* commit the transaction which would 258922208dedSAneesh Kumar K.V * free blocks released in the transaction 259022208dedSAneesh Kumar K.V * and try again 259122208dedSAneesh Kumar K.V */ 2592df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 259322208dedSAneesh Kumar K.V ret = 0; 259422208dedSAneesh Kumar K.V } else if (ret == MPAGE_DA_EXTENT_TAIL) { 2595a1d6cc56SAneesh Kumar K.V /* 25968de49e67SKazuya Mio * Got one extent now try with rest of the pages. 25978de49e67SKazuya Mio * If mpd.retval is set -EIO, journal is aborted. 25988de49e67SKazuya Mio * So we don't need to write any more. 2599a1d6cc56SAneesh Kumar K.V */ 260022208dedSAneesh Kumar K.V pages_written += mpd.pages_written; 26018de49e67SKazuya Mio ret = mpd.retval; 26022acf2c26SAneesh Kumar K.V io_done = 1; 260322208dedSAneesh Kumar K.V } else if (wbc->nr_to_write) 260461628a3fSMingming Cao /* 260561628a3fSMingming Cao * There is no more writeout needed 260661628a3fSMingming Cao * or we requested for a noblocking writeout 260761628a3fSMingming Cao * and we found the device congested 260861628a3fSMingming Cao */ 260961628a3fSMingming Cao break; 261061628a3fSMingming Cao } 26111bce63d1SShaohua Li blk_finish_plug(&plug); 26122acf2c26SAneesh Kumar K.V if (!io_done && !cycled) { 26132acf2c26SAneesh Kumar K.V cycled = 1; 26142acf2c26SAneesh Kumar K.V index = 0; 26152acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 26162acf2c26SAneesh Kumar K.V wbc->range_end = mapping->writeback_index - 1; 26172acf2c26SAneesh Kumar K.V goto retry; 26182acf2c26SAneesh Kumar K.V } 261961628a3fSMingming Cao 262022208dedSAneesh Kumar K.V /* Update index */ 26212acf2c26SAneesh Kumar K.V wbc->range_cyclic = range_cyclic; 262222208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 262322208dedSAneesh Kumar K.V /* 262422208dedSAneesh Kumar K.V * set the writeback_index so that range_cyclic 262522208dedSAneesh Kumar K.V * mode will write it back later 262622208dedSAneesh Kumar K.V */ 262772f84e65SEric Sandeen mapping->writeback_index = done_index; 2628a1d6cc56SAneesh Kumar K.V 262961628a3fSMingming Cao out_writepages: 263022208dedSAneesh Kumar K.V wbc->nr_to_write -= nr_to_writebump; 2631de89de6eSTheodore Ts'o wbc->range_start = range_start; 26329bffad1eSTheodore Ts'o trace_ext4_da_writepages_result(inode, wbc, ret, pages_written); 263361628a3fSMingming Cao return ret; 263464769240SAlex Tomas } 263564769240SAlex Tomas 263679f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 263779f0be8dSAneesh Kumar K.V { 26385c1ff336SEric Whitney s64 free_clusters, dirty_clusters; 263979f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 264079f0be8dSAneesh Kumar K.V 264179f0be8dSAneesh Kumar K.V /* 264279f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 264379f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 2644179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 264579f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 264679f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 264779f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 264879f0be8dSAneesh Kumar K.V */ 26495c1ff336SEric Whitney free_clusters = 26505c1ff336SEric Whitney percpu_counter_read_positive(&sbi->s_freeclusters_counter); 26515c1ff336SEric Whitney dirty_clusters = 26525c1ff336SEric Whitney percpu_counter_read_positive(&sbi->s_dirtyclusters_counter); 265300d4e736STheodore Ts'o /* 265400d4e736STheodore Ts'o * Start pushing delalloc when 1/2 of free blocks are dirty. 265500d4e736STheodore Ts'o */ 26565c1ff336SEric Whitney if (dirty_clusters && (free_clusters < 2 * dirty_clusters)) 265710ee27a0SMiao Xie try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE); 265800d4e736STheodore Ts'o 26595c1ff336SEric Whitney if (2 * free_clusters < 3 * dirty_clusters || 26605c1ff336SEric Whitney free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) { 266179f0be8dSAneesh Kumar K.V /* 2662c8afb446SEric Sandeen * free block count is less than 150% of dirty blocks 2663c8afb446SEric Sandeen * or free blocks is less than watermark 266479f0be8dSAneesh Kumar K.V */ 266579f0be8dSAneesh Kumar K.V return 1; 266679f0be8dSAneesh Kumar K.V } 266779f0be8dSAneesh Kumar K.V return 0; 266879f0be8dSAneesh Kumar K.V } 266979f0be8dSAneesh Kumar K.V 267064769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 267164769240SAlex Tomas loff_t pos, unsigned len, unsigned flags, 267264769240SAlex Tomas struct page **pagep, void **fsdata) 267364769240SAlex Tomas { 267472b8ab9dSEric Sandeen int ret, retries = 0; 267564769240SAlex Tomas struct page *page; 267664769240SAlex Tomas pgoff_t index; 267764769240SAlex Tomas struct inode *inode = mapping->host; 267864769240SAlex Tomas handle_t *handle; 267964769240SAlex Tomas 268064769240SAlex Tomas index = pos >> PAGE_CACHE_SHIFT; 268179f0be8dSAneesh Kumar K.V 268279f0be8dSAneesh Kumar K.V if (ext4_nonda_switch(inode->i_sb)) { 268379f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 268479f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 268579f0be8dSAneesh Kumar K.V len, flags, pagep, fsdata); 268679f0be8dSAneesh Kumar K.V } 268779f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 26889bffad1eSTheodore Ts'o trace_ext4_da_write_begin(inode, pos, len, flags); 26899c3569b5STao Ma 26909c3569b5STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 26919c3569b5STao Ma ret = ext4_da_write_inline_data_begin(mapping, inode, 26929c3569b5STao Ma pos, len, flags, 26939c3569b5STao Ma pagep, fsdata); 26949c3569b5STao Ma if (ret < 0) 269547564bfbSTheodore Ts'o return ret; 269647564bfbSTheodore Ts'o if (ret == 1) 269747564bfbSTheodore Ts'o return 0; 26989c3569b5STao Ma } 26999c3569b5STao Ma 270047564bfbSTheodore Ts'o /* 270147564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 270247564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 270347564bfbSTheodore Ts'o * is being written back. So grab it first before we start 270447564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 270547564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 270647564bfbSTheodore Ts'o */ 270747564bfbSTheodore Ts'o retry_grab: 270847564bfbSTheodore Ts'o page = grab_cache_page_write_begin(mapping, index, flags); 270947564bfbSTheodore Ts'o if (!page) 271047564bfbSTheodore Ts'o return -ENOMEM; 271147564bfbSTheodore Ts'o unlock_page(page); 271247564bfbSTheodore Ts'o 271364769240SAlex Tomas /* 271464769240SAlex Tomas * With delayed allocation, we don't log the i_disksize update 271564769240SAlex Tomas * if there is delayed block allocation. But we still need 271664769240SAlex Tomas * to journalling the i_disksize update if writes to the end 271764769240SAlex Tomas * of file which has an already mapped buffer. 271864769240SAlex Tomas */ 271947564bfbSTheodore Ts'o retry_journal: 27209924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 1); 272164769240SAlex Tomas if (IS_ERR(handle)) { 272247564bfbSTheodore Ts'o page_cache_release(page); 272347564bfbSTheodore Ts'o return PTR_ERR(handle); 272464769240SAlex Tomas } 272564769240SAlex Tomas 272647564bfbSTheodore Ts'o lock_page(page); 272747564bfbSTheodore Ts'o if (page->mapping != mapping) { 272847564bfbSTheodore Ts'o /* The page got truncated from under us */ 272947564bfbSTheodore Ts'o unlock_page(page); 273047564bfbSTheodore Ts'o page_cache_release(page); 2731d5a0d4f7SEric Sandeen ext4_journal_stop(handle); 273247564bfbSTheodore Ts'o goto retry_grab; 2733d5a0d4f7SEric Sandeen } 273447564bfbSTheodore Ts'o /* In case writeback began while the page was unlocked */ 273547564bfbSTheodore Ts'o wait_on_page_writeback(page); 273664769240SAlex Tomas 27376e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep); 273864769240SAlex Tomas if (ret < 0) { 273964769240SAlex Tomas unlock_page(page); 274064769240SAlex Tomas ext4_journal_stop(handle); 2741ae4d5372SAneesh Kumar K.V /* 2742ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 2743ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 2744ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 2745ae4d5372SAneesh Kumar K.V */ 2746ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 2747b9a4207dSJan Kara ext4_truncate_failed_write(inode); 274847564bfbSTheodore Ts'o 274947564bfbSTheodore Ts'o if (ret == -ENOSPC && 275047564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 275147564bfbSTheodore Ts'o goto retry_journal; 275247564bfbSTheodore Ts'o 275347564bfbSTheodore Ts'o page_cache_release(page); 275447564bfbSTheodore Ts'o return ret; 275564769240SAlex Tomas } 275664769240SAlex Tomas 275747564bfbSTheodore Ts'o *pagep = page; 275864769240SAlex Tomas return ret; 275964769240SAlex Tomas } 276064769240SAlex Tomas 2761632eaeabSMingming Cao /* 2762632eaeabSMingming Cao * Check if we should update i_disksize 2763632eaeabSMingming Cao * when write to the end of file but not require block allocation 2764632eaeabSMingming Cao */ 2765632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page, 2766632eaeabSMingming Cao unsigned long offset) 2767632eaeabSMingming Cao { 2768632eaeabSMingming Cao struct buffer_head *bh; 2769632eaeabSMingming Cao struct inode *inode = page->mapping->host; 2770632eaeabSMingming Cao unsigned int idx; 2771632eaeabSMingming Cao int i; 2772632eaeabSMingming Cao 2773632eaeabSMingming Cao bh = page_buffers(page); 2774632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 2775632eaeabSMingming Cao 2776632eaeabSMingming Cao for (i = 0; i < idx; i++) 2777632eaeabSMingming Cao bh = bh->b_this_page; 2778632eaeabSMingming Cao 277929fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 2780632eaeabSMingming Cao return 0; 2781632eaeabSMingming Cao return 1; 2782632eaeabSMingming Cao } 2783632eaeabSMingming Cao 278464769240SAlex Tomas static int ext4_da_write_end(struct file *file, 278564769240SAlex Tomas struct address_space *mapping, 278664769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 278764769240SAlex Tomas struct page *page, void *fsdata) 278864769240SAlex Tomas { 278964769240SAlex Tomas struct inode *inode = mapping->host; 279064769240SAlex Tomas int ret = 0, ret2; 279164769240SAlex Tomas handle_t *handle = ext4_journal_current_handle(); 279264769240SAlex Tomas loff_t new_i_size; 2793632eaeabSMingming Cao unsigned long start, end; 279479f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 279579f0be8dSAneesh Kumar K.V 279674d553aaSTheodore Ts'o if (write_mode == FALL_BACK_TO_NONDELALLOC) 279774d553aaSTheodore Ts'o return ext4_write_end(file, mapping, pos, 279879f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 2799632eaeabSMingming Cao 28009bffad1eSTheodore Ts'o trace_ext4_da_write_end(inode, pos, len, copied); 2801632eaeabSMingming Cao start = pos & (PAGE_CACHE_SIZE - 1); 2802632eaeabSMingming Cao end = start + copied - 1; 280364769240SAlex Tomas 280464769240SAlex Tomas /* 280564769240SAlex Tomas * generic_write_end() will run mark_inode_dirty() if i_size 280664769240SAlex Tomas * changes. So let's piggyback the i_disksize mark_inode_dirty 280764769240SAlex Tomas * into that. 280864769240SAlex Tomas */ 280964769240SAlex Tomas new_i_size = pos + copied; 2810ea51d132SAndrea Arcangeli if (copied && new_i_size > EXT4_I(inode)->i_disksize) { 28119c3569b5STao Ma if (ext4_has_inline_data(inode) || 28129c3569b5STao Ma ext4_da_should_update_i_disksize(page, end)) { 2813632eaeabSMingming Cao down_write(&EXT4_I(inode)->i_data_sem); 2814f3b59291STheodore Ts'o if (new_i_size > EXT4_I(inode)->i_disksize) 281564769240SAlex Tomas EXT4_I(inode)->i_disksize = new_i_size; 2816632eaeabSMingming Cao up_write(&EXT4_I(inode)->i_data_sem); 2817cf17fea6SAneesh Kumar K.V /* We need to mark inode dirty even if 2818cf17fea6SAneesh Kumar K.V * new_i_size is less that inode->i_size 2819cf17fea6SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 2820cf17fea6SAneesh Kumar K.V */ 2821cf17fea6SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 2822632eaeabSMingming Cao } 2823632eaeabSMingming Cao } 28249c3569b5STao Ma 28259c3569b5STao Ma if (write_mode != CONVERT_INLINE_DATA && 28269c3569b5STao Ma ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) && 28279c3569b5STao Ma ext4_has_inline_data(inode)) 28289c3569b5STao Ma ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied, 28299c3569b5STao Ma page); 28309c3569b5STao Ma else 283164769240SAlex Tomas ret2 = generic_write_end(file, mapping, pos, len, copied, 283264769240SAlex Tomas page, fsdata); 28339c3569b5STao Ma 283464769240SAlex Tomas copied = ret2; 283564769240SAlex Tomas if (ret2 < 0) 283664769240SAlex Tomas ret = ret2; 283764769240SAlex Tomas ret2 = ext4_journal_stop(handle); 283864769240SAlex Tomas if (!ret) 283964769240SAlex Tomas ret = ret2; 284064769240SAlex Tomas 284164769240SAlex Tomas return ret ? ret : copied; 284264769240SAlex Tomas } 284364769240SAlex Tomas 284464769240SAlex Tomas static void ext4_da_invalidatepage(struct page *page, unsigned long offset) 284564769240SAlex Tomas { 284664769240SAlex Tomas /* 284764769240SAlex Tomas * Drop reserved blocks 284864769240SAlex Tomas */ 284964769240SAlex Tomas BUG_ON(!PageLocked(page)); 285064769240SAlex Tomas if (!page_has_buffers(page)) 285164769240SAlex Tomas goto out; 285264769240SAlex Tomas 2853d2a17637SMingming Cao ext4_da_page_release_reservation(page, offset); 285464769240SAlex Tomas 285564769240SAlex Tomas out: 285664769240SAlex Tomas ext4_invalidatepage(page, offset); 285764769240SAlex Tomas 285864769240SAlex Tomas return; 285964769240SAlex Tomas } 286064769240SAlex Tomas 2861ccd2506bSTheodore Ts'o /* 2862ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 2863ccd2506bSTheodore Ts'o */ 2864ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 2865ccd2506bSTheodore Ts'o { 2866fb40ba0dSTheodore Ts'o trace_ext4_alloc_da_blocks(inode); 2867fb40ba0dSTheodore Ts'o 2868ccd2506bSTheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks && 2869ccd2506bSTheodore Ts'o !EXT4_I(inode)->i_reserved_meta_blocks) 2870ccd2506bSTheodore Ts'o return 0; 2871ccd2506bSTheodore Ts'o 2872ccd2506bSTheodore Ts'o /* 2873ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 2874ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 2875ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 2876ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 2877ccd2506bSTheodore Ts'o * would require replicating code paths in: 2878ccd2506bSTheodore Ts'o * 2879ccd2506bSTheodore Ts'o * ext4_da_writepages() -> 2880ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 2881ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 2882ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 2883ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 2884ccd2506bSTheodore Ts'o * 2885ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 2886ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 2887ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 2888ccd2506bSTheodore Ts'o * doing I/O at all. 2889ccd2506bSTheodore Ts'o * 2890ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 2891380cf090SWu Fengguang * the pages by calling redirty_page_for_writepage() but that 2892ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 2893ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 289425985edcSLucas De Marchi * simplifying them because we wouldn't actually intend to 2895ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 2896ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 2897ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 2898ccd2506bSTheodore Ts'o * 2899ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 2900ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 2901ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 2902ccd2506bSTheodore Ts'o */ 2903ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 2904ccd2506bSTheodore Ts'o } 290564769240SAlex Tomas 290664769240SAlex Tomas /* 2907ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 2908ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 2909ac27a0ecSDave Kleikamp * 2910ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 2911617ba13bSMingming Cao * journal. If somebody makes a swapfile on an ext4 data-journaling 2912ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 2913ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 2914ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 2915ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 2916ac27a0ecSDave Kleikamp * 2917ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 2918ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 2919ac27a0ecSDave Kleikamp */ 2920617ba13bSMingming Cao static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 2921ac27a0ecSDave Kleikamp { 2922ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 2923ac27a0ecSDave Kleikamp journal_t *journal; 2924ac27a0ecSDave Kleikamp int err; 2925ac27a0ecSDave Kleikamp 292646c7f254STao Ma /* 292746c7f254STao Ma * We can get here for an inline file via the FIBMAP ioctl 292846c7f254STao Ma */ 292946c7f254STao Ma if (ext4_has_inline_data(inode)) 293046c7f254STao Ma return 0; 293146c7f254STao Ma 293264769240SAlex Tomas if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 293364769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) { 293464769240SAlex Tomas /* 293564769240SAlex Tomas * With delalloc we want to sync the file 293664769240SAlex Tomas * so that we can make sure we allocate 293764769240SAlex Tomas * blocks for file 293864769240SAlex Tomas */ 293964769240SAlex Tomas filemap_write_and_wait(mapping); 294064769240SAlex Tomas } 294164769240SAlex Tomas 294219f5fb7aSTheodore Ts'o if (EXT4_JOURNAL(inode) && 294319f5fb7aSTheodore Ts'o ext4_test_inode_state(inode, EXT4_STATE_JDATA)) { 2944ac27a0ecSDave Kleikamp /* 2945ac27a0ecSDave Kleikamp * This is a REALLY heavyweight approach, but the use of 2946ac27a0ecSDave Kleikamp * bmap on dirty files is expected to be extremely rare: 2947ac27a0ecSDave Kleikamp * only if we run lilo or swapon on a freshly made file 2948ac27a0ecSDave Kleikamp * do we expect this to happen. 2949ac27a0ecSDave Kleikamp * 2950ac27a0ecSDave Kleikamp * (bmap requires CAP_SYS_RAWIO so this does not 2951ac27a0ecSDave Kleikamp * represent an unprivileged user DOS attack --- we'd be 2952ac27a0ecSDave Kleikamp * in trouble if mortal users could trigger this path at 2953ac27a0ecSDave Kleikamp * will.) 2954ac27a0ecSDave Kleikamp * 2955617ba13bSMingming Cao * NB. EXT4_STATE_JDATA is not set on files other than 2956ac27a0ecSDave Kleikamp * regular files. If somebody wants to bmap a directory 2957ac27a0ecSDave Kleikamp * or symlink and gets confused because the buffer 2958ac27a0ecSDave Kleikamp * hasn't yet been flushed to disk, they deserve 2959ac27a0ecSDave Kleikamp * everything they get. 2960ac27a0ecSDave Kleikamp */ 2961ac27a0ecSDave Kleikamp 296219f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_JDATA); 2963617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 2964dab291afSMingming Cao jbd2_journal_lock_updates(journal); 2965dab291afSMingming Cao err = jbd2_journal_flush(journal); 2966dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 2967ac27a0ecSDave Kleikamp 2968ac27a0ecSDave Kleikamp if (err) 2969ac27a0ecSDave Kleikamp return 0; 2970ac27a0ecSDave Kleikamp } 2971ac27a0ecSDave Kleikamp 2972617ba13bSMingming Cao return generic_block_bmap(mapping, block, ext4_get_block); 2973ac27a0ecSDave Kleikamp } 2974ac27a0ecSDave Kleikamp 2975617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page) 2976ac27a0ecSDave Kleikamp { 297746c7f254STao Ma int ret = -EAGAIN; 297846c7f254STao Ma struct inode *inode = page->mapping->host; 297946c7f254STao Ma 29800562e0baSJiaying Zhang trace_ext4_readpage(page); 298146c7f254STao Ma 298246c7f254STao Ma if (ext4_has_inline_data(inode)) 298346c7f254STao Ma ret = ext4_readpage_inline(inode, page); 298446c7f254STao Ma 298546c7f254STao Ma if (ret == -EAGAIN) 2986617ba13bSMingming Cao return mpage_readpage(page, ext4_get_block); 298746c7f254STao Ma 298846c7f254STao Ma return ret; 2989ac27a0ecSDave Kleikamp } 2990ac27a0ecSDave Kleikamp 2991ac27a0ecSDave Kleikamp static int 2992617ba13bSMingming Cao ext4_readpages(struct file *file, struct address_space *mapping, 2993ac27a0ecSDave Kleikamp struct list_head *pages, unsigned nr_pages) 2994ac27a0ecSDave Kleikamp { 299546c7f254STao Ma struct inode *inode = mapping->host; 299646c7f254STao Ma 299746c7f254STao Ma /* If the file has inline data, no need to do readpages. */ 299846c7f254STao Ma if (ext4_has_inline_data(inode)) 299946c7f254STao Ma return 0; 300046c7f254STao Ma 3001617ba13bSMingming Cao return mpage_readpages(mapping, pages, nr_pages, ext4_get_block); 3002ac27a0ecSDave Kleikamp } 3003ac27a0ecSDave Kleikamp 3004617ba13bSMingming Cao static void ext4_invalidatepage(struct page *page, unsigned long offset) 3005ac27a0ecSDave Kleikamp { 30060562e0baSJiaying Zhang trace_ext4_invalidatepage(page, offset); 30070562e0baSJiaying Zhang 30084520fb3cSJan Kara /* No journalling happens on data buffers when this function is used */ 30094520fb3cSJan Kara WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page))); 30104520fb3cSJan Kara 30114520fb3cSJan Kara block_invalidatepage(page, offset); 30124520fb3cSJan Kara } 30134520fb3cSJan Kara 301453e87268SJan Kara static int __ext4_journalled_invalidatepage(struct page *page, 30154520fb3cSJan Kara unsigned long offset) 30164520fb3cSJan Kara { 30174520fb3cSJan Kara journal_t *journal = EXT4_JOURNAL(page->mapping->host); 30184520fb3cSJan Kara 30194520fb3cSJan Kara trace_ext4_journalled_invalidatepage(page, offset); 30204520fb3cSJan Kara 3021744692dcSJiaying Zhang /* 3022ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 3023ac27a0ecSDave Kleikamp */ 3024ac27a0ecSDave Kleikamp if (offset == 0) 3025ac27a0ecSDave Kleikamp ClearPageChecked(page); 3026ac27a0ecSDave Kleikamp 302753e87268SJan Kara return jbd2_journal_invalidatepage(journal, page, offset); 302853e87268SJan Kara } 302953e87268SJan Kara 303053e87268SJan Kara /* Wrapper for aops... */ 303153e87268SJan Kara static void ext4_journalled_invalidatepage(struct page *page, 303253e87268SJan Kara unsigned long offset) 303353e87268SJan Kara { 303453e87268SJan Kara WARN_ON(__ext4_journalled_invalidatepage(page, offset) < 0); 3035ac27a0ecSDave Kleikamp } 3036ac27a0ecSDave Kleikamp 3037617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait) 3038ac27a0ecSDave Kleikamp { 3039617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 3040ac27a0ecSDave Kleikamp 30410562e0baSJiaying Zhang trace_ext4_releasepage(page); 30420562e0baSJiaying Zhang 3043e1c36595SJan Kara /* Page has dirty journalled data -> cannot release */ 3044e1c36595SJan Kara if (PageChecked(page)) 3045ac27a0ecSDave Kleikamp return 0; 30460390131bSFrank Mayhar if (journal) 3047dab291afSMingming Cao return jbd2_journal_try_to_free_buffers(journal, page, wait); 30480390131bSFrank Mayhar else 30490390131bSFrank Mayhar return try_to_free_buffers(page); 3050ac27a0ecSDave Kleikamp } 3051ac27a0ecSDave Kleikamp 3052ac27a0ecSDave Kleikamp /* 30532ed88685STheodore Ts'o * ext4_get_block used when preparing for a DIO write or buffer write. 30542ed88685STheodore Ts'o * We allocate an uinitialized extent if blocks haven't been allocated. 30552ed88685STheodore Ts'o * The extent will be converted to initialized after the IO is complete. 30562ed88685STheodore Ts'o */ 3057f19d5870STao Ma int ext4_get_block_write(struct inode *inode, sector_t iblock, 30584c0425ffSMingming Cao struct buffer_head *bh_result, int create) 30594c0425ffSMingming Cao { 3060c7064ef1SJiaying Zhang ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n", 30618d5d02e6SMingming Cao inode->i_ino, create); 30622ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh_result, 30632ed88685STheodore Ts'o EXT4_GET_BLOCKS_IO_CREATE_EXT); 30644c0425ffSMingming Cao } 30654c0425ffSMingming Cao 3066729f52c6SZheng Liu static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock, 30678b0f165fSAnatol Pomozov struct buffer_head *bh_result, int create) 3068729f52c6SZheng Liu { 30698b0f165fSAnatol Pomozov ext4_debug("ext4_get_block_write_nolock: inode %lu, create flag %d\n", 30708b0f165fSAnatol Pomozov inode->i_ino, create); 30718b0f165fSAnatol Pomozov return _ext4_get_block(inode, iblock, bh_result, 30728b0f165fSAnatol Pomozov EXT4_GET_BLOCKS_NO_LOCK); 3073729f52c6SZheng Liu } 3074729f52c6SZheng Liu 30754c0425ffSMingming Cao static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset, 3076552ef802SChristoph Hellwig ssize_t size, void *private, int ret, 3077552ef802SChristoph Hellwig bool is_async) 30784c0425ffSMingming Cao { 3079496ad9aaSAl Viro struct inode *inode = file_inode(iocb->ki_filp); 30804c0425ffSMingming Cao ext4_io_end_t *io_end = iocb->private; 30814c0425ffSMingming Cao 30824eec708dSJan Kara /* if not async direct IO just return */ 30834eec708dSJan Kara if (!io_end) { 30844eec708dSJan Kara inode_dio_done(inode); 30854eec708dSJan Kara if (is_async) 30864eec708dSJan Kara aio_complete(iocb, ret, 0); 30874eec708dSJan Kara return; 30884eec708dSJan Kara } 30894b70df18SMingming 30908d5d02e6SMingming Cao ext_debug("ext4_end_io_dio(): io_end 0x%p " 3091ace36ad4SJoe Perches "for inode %lu, iocb 0x%p, offset %llu, size %zd\n", 30928d5d02e6SMingming Cao iocb->private, io_end->inode->i_ino, iocb, offset, 30938d5d02e6SMingming Cao size); 30948d5d02e6SMingming Cao 3095b5a7e970STheodore Ts'o iocb->private = NULL; 30964c0425ffSMingming Cao io_end->offset = offset; 30974c0425ffSMingming Cao io_end->size = size; 30985b3ff237Sjiayingz@google.com (Jiaying Zhang) if (is_async) { 30995b3ff237Sjiayingz@google.com (Jiaying Zhang) io_end->iocb = iocb; 31005b3ff237Sjiayingz@google.com (Jiaying Zhang) io_end->result = ret; 31015b3ff237Sjiayingz@google.com (Jiaying Zhang) } 31024eec708dSJan Kara ext4_put_io_end_defer(io_end); 31034c0425ffSMingming Cao } 3104c7064ef1SJiaying Zhang 31054c0425ffSMingming Cao /* 31064c0425ffSMingming Cao * For ext4 extent files, ext4 will do direct-io write to holes, 31074c0425ffSMingming Cao * preallocated extents, and those write extend the file, no need to 31084c0425ffSMingming Cao * fall back to buffered IO. 31094c0425ffSMingming Cao * 3110b595076aSUwe Kleine-König * For holes, we fallocate those blocks, mark them as uninitialized 311169c499d1STheodore Ts'o * If those blocks were preallocated, we mark sure they are split, but 3112b595076aSUwe Kleine-König * still keep the range to write as uninitialized. 31134c0425ffSMingming Cao * 311469c499d1STheodore Ts'o * The unwritten extents will be converted to written when DIO is completed. 31158d5d02e6SMingming Cao * For async direct IO, since the IO may still pending when return, we 311625985edcSLucas De Marchi * set up an end_io call back function, which will do the conversion 31178d5d02e6SMingming Cao * when async direct IO completed. 31184c0425ffSMingming Cao * 31194c0425ffSMingming Cao * If the O_DIRECT write will extend the file then add this inode to the 31204c0425ffSMingming Cao * orphan list. So recovery will truncate it back to the original size 31214c0425ffSMingming Cao * if the machine crashes during the write. 31224c0425ffSMingming Cao * 31234c0425ffSMingming Cao */ 31244c0425ffSMingming Cao static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb, 31254c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 31264c0425ffSMingming Cao unsigned long nr_segs) 31274c0425ffSMingming Cao { 31284c0425ffSMingming Cao struct file *file = iocb->ki_filp; 31294c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 31304c0425ffSMingming Cao ssize_t ret; 31314c0425ffSMingming Cao size_t count = iov_length(iov, nr_segs); 3132729f52c6SZheng Liu int overwrite = 0; 31338b0f165fSAnatol Pomozov get_block_t *get_block_func = NULL; 31348b0f165fSAnatol Pomozov int dio_flags = 0; 313569c499d1STheodore Ts'o loff_t final_size = offset + count; 31364eec708dSJan Kara ext4_io_end_t *io_end = NULL; 313769c499d1STheodore Ts'o 313869c499d1STheodore Ts'o /* Use the old path for reads and writes beyond i_size. */ 313969c499d1STheodore Ts'o if (rw != WRITE || final_size > inode->i_size) 314069c499d1STheodore Ts'o return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 3141729f52c6SZheng Liu 31424bd809dbSZheng Liu BUG_ON(iocb->private == NULL); 31434bd809dbSZheng Liu 31444bd809dbSZheng Liu /* If we do a overwrite dio, i_mutex locking can be released */ 31454bd809dbSZheng Liu overwrite = *((int *)iocb->private); 31464bd809dbSZheng Liu 31474bd809dbSZheng Liu if (overwrite) { 31481f555cfaSDmitry Monakhov atomic_inc(&inode->i_dio_count); 31494bd809dbSZheng Liu down_read(&EXT4_I(inode)->i_data_sem); 31504bd809dbSZheng Liu mutex_unlock(&inode->i_mutex); 31514bd809dbSZheng Liu } 31524bd809dbSZheng Liu 31534c0425ffSMingming Cao /* 31548d5d02e6SMingming Cao * We could direct write to holes and fallocate. 31558d5d02e6SMingming Cao * 315669c499d1STheodore Ts'o * Allocated blocks to fill the hole are marked as 315769c499d1STheodore Ts'o * uninitialized to prevent parallel buffered read to expose 315869c499d1STheodore Ts'o * the stale data before DIO complete the data IO. 31598d5d02e6SMingming Cao * 316069c499d1STheodore Ts'o * As to previously fallocated extents, ext4 get_block will 316169c499d1STheodore Ts'o * just simply mark the buffer mapped but still keep the 316269c499d1STheodore Ts'o * extents uninitialized. 31634c0425ffSMingming Cao * 316469c499d1STheodore Ts'o * For non AIO case, we will convert those unwritten extents 31658d5d02e6SMingming Cao * to written after return back from blockdev_direct_IO. 31664c0425ffSMingming Cao * 316769c499d1STheodore Ts'o * For async DIO, the conversion needs to be deferred when the 316869c499d1STheodore Ts'o * IO is completed. The ext4 end_io callback function will be 316969c499d1STheodore Ts'o * called to take care of the conversion work. Here for async 317069c499d1STheodore Ts'o * case, we allocate an io_end structure to hook to the iocb. 31714c0425ffSMingming Cao */ 31728d5d02e6SMingming Cao iocb->private = NULL; 3173f45ee3a1SDmitry Monakhov ext4_inode_aio_set(inode, NULL); 31748d5d02e6SMingming Cao if (!is_sync_kiocb(iocb)) { 31754eec708dSJan Kara io_end = ext4_init_io_end(inode, GFP_NOFS); 31764bd809dbSZheng Liu if (!io_end) { 31774bd809dbSZheng Liu ret = -ENOMEM; 31784bd809dbSZheng Liu goto retake_lock; 31794bd809dbSZheng Liu } 3180266991b1SJeff Moyer io_end->flag |= EXT4_IO_END_DIRECT; 31814eec708dSJan Kara /* 31824eec708dSJan Kara * Grab reference for DIO. Will be dropped in ext4_end_io_dio() 31834eec708dSJan Kara */ 31844eec708dSJan Kara iocb->private = ext4_get_io_end(io_end); 31858d5d02e6SMingming Cao /* 318669c499d1STheodore Ts'o * we save the io structure for current async direct 318769c499d1STheodore Ts'o * IO, so that later ext4_map_blocks() could flag the 318869c499d1STheodore Ts'o * io structure whether there is a unwritten extents 318969c499d1STheodore Ts'o * needs to be converted when IO is completed. 31908d5d02e6SMingming Cao */ 3191f45ee3a1SDmitry Monakhov ext4_inode_aio_set(inode, io_end); 31928d5d02e6SMingming Cao } 31938d5d02e6SMingming Cao 31948b0f165fSAnatol Pomozov if (overwrite) { 31958b0f165fSAnatol Pomozov get_block_func = ext4_get_block_write_nolock; 31968b0f165fSAnatol Pomozov } else { 31978b0f165fSAnatol Pomozov get_block_func = ext4_get_block_write; 31988b0f165fSAnatol Pomozov dio_flags = DIO_LOCKING; 31998b0f165fSAnatol Pomozov } 3200729f52c6SZheng Liu ret = __blockdev_direct_IO(rw, iocb, inode, 3201729f52c6SZheng Liu inode->i_sb->s_bdev, iov, 3202729f52c6SZheng Liu offset, nr_segs, 32038b0f165fSAnatol Pomozov get_block_func, 3204729f52c6SZheng Liu ext4_end_io_dio, 3205729f52c6SZheng Liu NULL, 32068b0f165fSAnatol Pomozov dio_flags); 32078b0f165fSAnatol Pomozov 32088d5d02e6SMingming Cao /* 32094eec708dSJan Kara * Put our reference to io_end. This can free the io_end structure e.g. 32104eec708dSJan Kara * in sync IO case or in case of error. It can even perform extent 32114eec708dSJan Kara * conversion if all bios we submitted finished before we got here. 32124eec708dSJan Kara * Note that in that case iocb->private can be already set to NULL 32134eec708dSJan Kara * here. 32148d5d02e6SMingming Cao */ 32154eec708dSJan Kara if (io_end) { 32164eec708dSJan Kara ext4_inode_aio_set(inode, NULL); 32174eec708dSJan Kara ext4_put_io_end(io_end); 32184eec708dSJan Kara /* 32194eec708dSJan Kara * In case of error or no write ext4_end_io_dio() was not 32204eec708dSJan Kara * called so we have to put iocb's reference. 32214eec708dSJan Kara */ 32224eec708dSJan Kara if (ret <= 0 && ret != -EIOCBQUEUED) { 32234eec708dSJan Kara WARN_ON(iocb->private != io_end); 32244eec708dSJan Kara ext4_put_io_end(io_end); 32258d5d02e6SMingming Cao iocb->private = NULL; 32264eec708dSJan Kara } 32274eec708dSJan Kara } 32284eec708dSJan Kara if (ret > 0 && !overwrite && ext4_test_inode_state(inode, 32295f524950SMingming EXT4_STATE_DIO_UNWRITTEN)) { 3230109f5565SMingming int err; 32318d5d02e6SMingming Cao /* 32328d5d02e6SMingming Cao * for non AIO case, since the IO is already 323325985edcSLucas De Marchi * completed, we could do the conversion right here 32348d5d02e6SMingming Cao */ 3235109f5565SMingming err = ext4_convert_unwritten_extents(inode, 32368d5d02e6SMingming Cao offset, ret); 3237109f5565SMingming if (err < 0) 3238109f5565SMingming ret = err; 323919f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN); 3240109f5565SMingming } 32414bd809dbSZheng Liu 32424bd809dbSZheng Liu retake_lock: 32434bd809dbSZheng Liu /* take i_mutex locking again if we do a ovewrite dio */ 32444bd809dbSZheng Liu if (overwrite) { 32451f555cfaSDmitry Monakhov inode_dio_done(inode); 32464bd809dbSZheng Liu up_read(&EXT4_I(inode)->i_data_sem); 32474bd809dbSZheng Liu mutex_lock(&inode->i_mutex); 32484bd809dbSZheng Liu } 32494bd809dbSZheng Liu 32504c0425ffSMingming Cao return ret; 32514c0425ffSMingming Cao } 32528d5d02e6SMingming Cao 32534c0425ffSMingming Cao static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb, 32544c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 32554c0425ffSMingming Cao unsigned long nr_segs) 32564c0425ffSMingming Cao { 32574c0425ffSMingming Cao struct file *file = iocb->ki_filp; 32584c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 32590562e0baSJiaying Zhang ssize_t ret; 32604c0425ffSMingming Cao 326184ebd795STheodore Ts'o /* 326284ebd795STheodore Ts'o * If we are doing data journalling we don't support O_DIRECT 326384ebd795STheodore Ts'o */ 326484ebd795STheodore Ts'o if (ext4_should_journal_data(inode)) 326584ebd795STheodore Ts'o return 0; 326684ebd795STheodore Ts'o 326746c7f254STao Ma /* Let buffer I/O handle the inline data case. */ 326846c7f254STao Ma if (ext4_has_inline_data(inode)) 326946c7f254STao Ma return 0; 327046c7f254STao Ma 32710562e0baSJiaying Zhang trace_ext4_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw); 327212e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 32730562e0baSJiaying Zhang ret = ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs); 32740562e0baSJiaying Zhang else 32750562e0baSJiaying Zhang ret = ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 32760562e0baSJiaying Zhang trace_ext4_direct_IO_exit(inode, offset, 32770562e0baSJiaying Zhang iov_length(iov, nr_segs), rw, ret); 32780562e0baSJiaying Zhang return ret; 32794c0425ffSMingming Cao } 32804c0425ffSMingming Cao 3281ac27a0ecSDave Kleikamp /* 3282617ba13bSMingming Cao * Pages can be marked dirty completely asynchronously from ext4's journalling 3283ac27a0ecSDave Kleikamp * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do 3284ac27a0ecSDave Kleikamp * much here because ->set_page_dirty is called under VFS locks. The page is 3285ac27a0ecSDave Kleikamp * not necessarily locked. 3286ac27a0ecSDave Kleikamp * 3287ac27a0ecSDave Kleikamp * We cannot just dirty the page and leave attached buffers clean, because the 3288ac27a0ecSDave Kleikamp * buffers' dirty state is "definitive". We cannot just set the buffers dirty 3289ac27a0ecSDave Kleikamp * or jbddirty because all the journalling code will explode. 3290ac27a0ecSDave Kleikamp * 3291ac27a0ecSDave Kleikamp * So what we do is to mark the page "pending dirty" and next time writepage 3292ac27a0ecSDave Kleikamp * is called, propagate that into the buffers appropriately. 3293ac27a0ecSDave Kleikamp */ 3294617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page) 3295ac27a0ecSDave Kleikamp { 3296ac27a0ecSDave Kleikamp SetPageChecked(page); 3297ac27a0ecSDave Kleikamp return __set_page_dirty_nobuffers(page); 3298ac27a0ecSDave Kleikamp } 3299ac27a0ecSDave Kleikamp 330074d553aaSTheodore Ts'o static const struct address_space_operations ext4_aops = { 3301617ba13bSMingming Cao .readpage = ext4_readpage, 3302617ba13bSMingming Cao .readpages = ext4_readpages, 330343ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3304bfc1af65SNick Piggin .write_begin = ext4_write_begin, 330574d553aaSTheodore Ts'o .write_end = ext4_write_end, 3306617ba13bSMingming Cao .bmap = ext4_bmap, 3307617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3308617ba13bSMingming Cao .releasepage = ext4_releasepage, 3309617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 3310ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 33118ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3312aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3313ac27a0ecSDave Kleikamp }; 3314ac27a0ecSDave Kleikamp 3315617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 3316617ba13bSMingming Cao .readpage = ext4_readpage, 3317617ba13bSMingming Cao .readpages = ext4_readpages, 331843ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3319bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3320bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 3321617ba13bSMingming Cao .set_page_dirty = ext4_journalled_set_page_dirty, 3322617ba13bSMingming Cao .bmap = ext4_bmap, 33234520fb3cSJan Kara .invalidatepage = ext4_journalled_invalidatepage, 3324617ba13bSMingming Cao .releasepage = ext4_releasepage, 332584ebd795STheodore Ts'o .direct_IO = ext4_direct_IO, 33268ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3327aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3328ac27a0ecSDave Kleikamp }; 3329ac27a0ecSDave Kleikamp 333064769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 333164769240SAlex Tomas .readpage = ext4_readpage, 333264769240SAlex Tomas .readpages = ext4_readpages, 333343ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 333464769240SAlex Tomas .writepages = ext4_da_writepages, 333564769240SAlex Tomas .write_begin = ext4_da_write_begin, 333664769240SAlex Tomas .write_end = ext4_da_write_end, 333764769240SAlex Tomas .bmap = ext4_bmap, 333864769240SAlex Tomas .invalidatepage = ext4_da_invalidatepage, 333964769240SAlex Tomas .releasepage = ext4_releasepage, 334064769240SAlex Tomas .direct_IO = ext4_direct_IO, 334164769240SAlex Tomas .migratepage = buffer_migrate_page, 33428ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3343aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 334464769240SAlex Tomas }; 334564769240SAlex Tomas 3346617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 3347ac27a0ecSDave Kleikamp { 33483d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 33493d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 335074d553aaSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_ORDERED_MODE); 33513d2b1582SLukas Czerner break; 33523d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 335374d553aaSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_ORDERED_MODE); 33543d2b1582SLukas Czerner break; 33553d2b1582SLukas Czerner case EXT4_INODE_JOURNAL_DATA_MODE: 3356617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 335774d553aaSTheodore Ts'o return; 33583d2b1582SLukas Czerner default: 33593d2b1582SLukas Czerner BUG(); 33603d2b1582SLukas Czerner } 336174d553aaSTheodore Ts'o if (test_opt(inode->i_sb, DELALLOC)) 336274d553aaSTheodore Ts'o inode->i_mapping->a_ops = &ext4_da_aops; 336374d553aaSTheodore Ts'o else 336474d553aaSTheodore Ts'o inode->i_mapping->a_ops = &ext4_aops; 3365ac27a0ecSDave Kleikamp } 3366ac27a0ecSDave Kleikamp 33674e96b2dbSAllison Henderson 33684e96b2dbSAllison Henderson /* 33694e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers() 33704e96b2dbSAllison Henderson * Wrapper function for ext4_discard_partial_page_buffers_no_lock. 33714e96b2dbSAllison Henderson * This function finds and locks the page containing the offset 33724e96b2dbSAllison Henderson * "from" and passes it to ext4_discard_partial_page_buffers_no_lock. 33734e96b2dbSAllison Henderson * Calling functions that already have the page locked should call 33744e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers_no_lock directly. 33754e96b2dbSAllison Henderson */ 33764e96b2dbSAllison Henderson int ext4_discard_partial_page_buffers(handle_t *handle, 33774e96b2dbSAllison Henderson struct address_space *mapping, loff_t from, 33784e96b2dbSAllison Henderson loff_t length, int flags) 33794e96b2dbSAllison Henderson { 33804e96b2dbSAllison Henderson struct inode *inode = mapping->host; 33814e96b2dbSAllison Henderson struct page *page; 33824e96b2dbSAllison Henderson int err = 0; 33834e96b2dbSAllison Henderson 33844e96b2dbSAllison Henderson page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT, 33854e96b2dbSAllison Henderson mapping_gfp_mask(mapping) & ~__GFP_FS); 33864e96b2dbSAllison Henderson if (!page) 33875129d05fSYongqiang Yang return -ENOMEM; 33884e96b2dbSAllison Henderson 33894e96b2dbSAllison Henderson err = ext4_discard_partial_page_buffers_no_lock(handle, inode, page, 33904e96b2dbSAllison Henderson from, length, flags); 33914e96b2dbSAllison Henderson 33924e96b2dbSAllison Henderson unlock_page(page); 33934e96b2dbSAllison Henderson page_cache_release(page); 33944e96b2dbSAllison Henderson return err; 33954e96b2dbSAllison Henderson } 33964e96b2dbSAllison Henderson 33974e96b2dbSAllison Henderson /* 33984e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers_no_lock() 33994e96b2dbSAllison Henderson * Zeros a page range of length 'length' starting from offset 'from'. 34004e96b2dbSAllison Henderson * Buffer heads that correspond to the block aligned regions of the 34014e96b2dbSAllison Henderson * zeroed range will be unmapped. Unblock aligned regions 34024e96b2dbSAllison Henderson * will have the corresponding buffer head mapped if needed so that 34034e96b2dbSAllison Henderson * that region of the page can be updated with the partial zero out. 34044e96b2dbSAllison Henderson * 34054e96b2dbSAllison Henderson * This function assumes that the page has already been locked. The 34064e96b2dbSAllison Henderson * The range to be discarded must be contained with in the given page. 34074e96b2dbSAllison Henderson * If the specified range exceeds the end of the page it will be shortened 34084e96b2dbSAllison Henderson * to the end of the page that corresponds to 'from'. This function is 34094e96b2dbSAllison Henderson * appropriate for updating a page and it buffer heads to be unmapped and 34104e96b2dbSAllison Henderson * zeroed for blocks that have been either released, or are going to be 34114e96b2dbSAllison Henderson * released. 34124e96b2dbSAllison Henderson * 34134e96b2dbSAllison Henderson * handle: The journal handle 34144e96b2dbSAllison Henderson * inode: The files inode 34154e96b2dbSAllison Henderson * page: A locked page that contains the offset "from" 34164907cb7bSAnatol Pomozov * from: The starting byte offset (from the beginning of the file) 34174e96b2dbSAllison Henderson * to begin discarding 34184e96b2dbSAllison Henderson * len: The length of bytes to discard 34194e96b2dbSAllison Henderson * flags: Optional flags that may be used: 34204e96b2dbSAllison Henderson * 34214e96b2dbSAllison Henderson * EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED 34224e96b2dbSAllison Henderson * Only zero the regions of the page whose buffer heads 34234e96b2dbSAllison Henderson * have already been unmapped. This flag is appropriate 34244907cb7bSAnatol Pomozov * for updating the contents of a page whose blocks may 34254e96b2dbSAllison Henderson * have already been released, and we only want to zero 34264e96b2dbSAllison Henderson * out the regions that correspond to those released blocks. 34274e96b2dbSAllison Henderson * 34284907cb7bSAnatol Pomozov * Returns zero on success or negative on failure. 34294e96b2dbSAllison Henderson */ 34305f163cc7SEric Sandeen static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle, 34314e96b2dbSAllison Henderson struct inode *inode, struct page *page, loff_t from, 34324e96b2dbSAllison Henderson loff_t length, int flags) 34334e96b2dbSAllison Henderson { 34344e96b2dbSAllison Henderson ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; 34354e96b2dbSAllison Henderson unsigned int offset = from & (PAGE_CACHE_SIZE-1); 34364e96b2dbSAllison Henderson unsigned int blocksize, max, pos; 34374e96b2dbSAllison Henderson ext4_lblk_t iblock; 34384e96b2dbSAllison Henderson struct buffer_head *bh; 34394e96b2dbSAllison Henderson int err = 0; 34404e96b2dbSAllison Henderson 34414e96b2dbSAllison Henderson blocksize = inode->i_sb->s_blocksize; 34424e96b2dbSAllison Henderson max = PAGE_CACHE_SIZE - offset; 34434e96b2dbSAllison Henderson 34444e96b2dbSAllison Henderson if (index != page->index) 34454e96b2dbSAllison Henderson return -EINVAL; 34464e96b2dbSAllison Henderson 34474e96b2dbSAllison Henderson /* 34484e96b2dbSAllison Henderson * correct length if it does not fall between 34494e96b2dbSAllison Henderson * 'from' and the end of the page 34504e96b2dbSAllison Henderson */ 34514e96b2dbSAllison Henderson if (length > max || length < 0) 34524e96b2dbSAllison Henderson length = max; 34534e96b2dbSAllison Henderson 34544e96b2dbSAllison Henderson iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); 34554e96b2dbSAllison Henderson 3456093e6e36SYongqiang Yang if (!page_has_buffers(page)) 34574e96b2dbSAllison Henderson create_empty_buffers(page, blocksize, 0); 34584e96b2dbSAllison Henderson 34594e96b2dbSAllison Henderson /* Find the buffer that contains "offset" */ 34604e96b2dbSAllison Henderson bh = page_buffers(page); 34614e96b2dbSAllison Henderson pos = blocksize; 34624e96b2dbSAllison Henderson while (offset >= pos) { 34634e96b2dbSAllison Henderson bh = bh->b_this_page; 34644e96b2dbSAllison Henderson iblock++; 34654e96b2dbSAllison Henderson pos += blocksize; 34664e96b2dbSAllison Henderson } 34674e96b2dbSAllison Henderson 34684e96b2dbSAllison Henderson pos = offset; 34694e96b2dbSAllison Henderson while (pos < offset + length) { 3470e260daf2SYongqiang Yang unsigned int end_of_block, range_to_discard; 3471e260daf2SYongqiang Yang 34724e96b2dbSAllison Henderson err = 0; 34734e96b2dbSAllison Henderson 34744e96b2dbSAllison Henderson /* The length of space left to zero and unmap */ 34754e96b2dbSAllison Henderson range_to_discard = offset + length - pos; 34764e96b2dbSAllison Henderson 34774e96b2dbSAllison Henderson /* The length of space until the end of the block */ 34784e96b2dbSAllison Henderson end_of_block = blocksize - (pos & (blocksize-1)); 34794e96b2dbSAllison Henderson 34804e96b2dbSAllison Henderson /* 34814e96b2dbSAllison Henderson * Do not unmap or zero past end of block 34824e96b2dbSAllison Henderson * for this buffer head 34834e96b2dbSAllison Henderson */ 34844e96b2dbSAllison Henderson if (range_to_discard > end_of_block) 34854e96b2dbSAllison Henderson range_to_discard = end_of_block; 34864e96b2dbSAllison Henderson 34874e96b2dbSAllison Henderson 34884e96b2dbSAllison Henderson /* 34894e96b2dbSAllison Henderson * Skip this buffer head if we are only zeroing unampped 34904e96b2dbSAllison Henderson * regions of the page 34914e96b2dbSAllison Henderson */ 34924e96b2dbSAllison Henderson if (flags & EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED && 34934e96b2dbSAllison Henderson buffer_mapped(bh)) 34944e96b2dbSAllison Henderson goto next; 34954e96b2dbSAllison Henderson 34964e96b2dbSAllison Henderson /* If the range is block aligned, unmap */ 34974e96b2dbSAllison Henderson if (range_to_discard == blocksize) { 34984e96b2dbSAllison Henderson clear_buffer_dirty(bh); 34994e96b2dbSAllison Henderson bh->b_bdev = NULL; 35004e96b2dbSAllison Henderson clear_buffer_mapped(bh); 35014e96b2dbSAllison Henderson clear_buffer_req(bh); 35024e96b2dbSAllison Henderson clear_buffer_new(bh); 35034e96b2dbSAllison Henderson clear_buffer_delay(bh); 35044e96b2dbSAllison Henderson clear_buffer_unwritten(bh); 35054e96b2dbSAllison Henderson clear_buffer_uptodate(bh); 35064e96b2dbSAllison Henderson zero_user(page, pos, range_to_discard); 35074e96b2dbSAllison Henderson BUFFER_TRACE(bh, "Buffer discarded"); 35084e96b2dbSAllison Henderson goto next; 35094e96b2dbSAllison Henderson } 35104e96b2dbSAllison Henderson 35114e96b2dbSAllison Henderson /* 35124e96b2dbSAllison Henderson * If this block is not completely contained in the range 35134e96b2dbSAllison Henderson * to be discarded, then it is not going to be released. Because 35144e96b2dbSAllison Henderson * we need to keep this block, we need to make sure this part 35154e96b2dbSAllison Henderson * of the page is uptodate before we modify it by writeing 35164e96b2dbSAllison Henderson * partial zeros on it. 35174e96b2dbSAllison Henderson */ 35184e96b2dbSAllison Henderson if (!buffer_mapped(bh)) { 35194e96b2dbSAllison Henderson /* 35204e96b2dbSAllison Henderson * Buffer head must be mapped before we can read 35214e96b2dbSAllison Henderson * from the block 35224e96b2dbSAllison Henderson */ 35234e96b2dbSAllison Henderson BUFFER_TRACE(bh, "unmapped"); 35244e96b2dbSAllison Henderson ext4_get_block(inode, iblock, bh, 0); 35254e96b2dbSAllison Henderson /* unmapped? It's a hole - nothing to do */ 35264e96b2dbSAllison Henderson if (!buffer_mapped(bh)) { 35274e96b2dbSAllison Henderson BUFFER_TRACE(bh, "still unmapped"); 35284e96b2dbSAllison Henderson goto next; 35294e96b2dbSAllison Henderson } 35304e96b2dbSAllison Henderson } 35314e96b2dbSAllison Henderson 35324e96b2dbSAllison Henderson /* Ok, it's mapped. Make sure it's up-to-date */ 35334e96b2dbSAllison Henderson if (PageUptodate(page)) 35344e96b2dbSAllison Henderson set_buffer_uptodate(bh); 35354e96b2dbSAllison Henderson 35364e96b2dbSAllison Henderson if (!buffer_uptodate(bh)) { 35374e96b2dbSAllison Henderson err = -EIO; 35384e96b2dbSAllison Henderson ll_rw_block(READ, 1, &bh); 35394e96b2dbSAllison Henderson wait_on_buffer(bh); 35404e96b2dbSAllison Henderson /* Uhhuh. Read error. Complain and punt.*/ 35414e96b2dbSAllison Henderson if (!buffer_uptodate(bh)) 35424e96b2dbSAllison Henderson goto next; 35434e96b2dbSAllison Henderson } 35444e96b2dbSAllison Henderson 35454e96b2dbSAllison Henderson if (ext4_should_journal_data(inode)) { 35464e96b2dbSAllison Henderson BUFFER_TRACE(bh, "get write access"); 35474e96b2dbSAllison Henderson err = ext4_journal_get_write_access(handle, bh); 35484e96b2dbSAllison Henderson if (err) 35494e96b2dbSAllison Henderson goto next; 35504e96b2dbSAllison Henderson } 35514e96b2dbSAllison Henderson 35524e96b2dbSAllison Henderson zero_user(page, pos, range_to_discard); 35534e96b2dbSAllison Henderson 35544e96b2dbSAllison Henderson err = 0; 35554e96b2dbSAllison Henderson if (ext4_should_journal_data(inode)) { 35564e96b2dbSAllison Henderson err = ext4_handle_dirty_metadata(handle, inode, bh); 3557decbd919STheodore Ts'o } else 35584e96b2dbSAllison Henderson mark_buffer_dirty(bh); 35594e96b2dbSAllison Henderson 35604e96b2dbSAllison Henderson BUFFER_TRACE(bh, "Partial buffer zeroed"); 35614e96b2dbSAllison Henderson next: 35624e96b2dbSAllison Henderson bh = bh->b_this_page; 35634e96b2dbSAllison Henderson iblock++; 35644e96b2dbSAllison Henderson pos += range_to_discard; 35654e96b2dbSAllison Henderson } 35664e96b2dbSAllison Henderson 35674e96b2dbSAllison Henderson return err; 35684e96b2dbSAllison Henderson } 35694e96b2dbSAllison Henderson 357091ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 357191ef4cafSDuane Griffin { 357291ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 357391ef4cafSDuane Griffin return 1; 357491ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 357591ef4cafSDuane Griffin return 1; 357691ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 357791ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 357891ef4cafSDuane Griffin return 0; 357991ef4cafSDuane Griffin } 358091ef4cafSDuane Griffin 3581ac27a0ecSDave Kleikamp /* 3582a4bb6b64SAllison Henderson * ext4_punch_hole: punches a hole in a file by releaseing the blocks 3583a4bb6b64SAllison Henderson * associated with the given offset and length 3584a4bb6b64SAllison Henderson * 3585a4bb6b64SAllison Henderson * @inode: File inode 3586a4bb6b64SAllison Henderson * @offset: The offset where the hole will begin 3587a4bb6b64SAllison Henderson * @len: The length of the hole 3588a4bb6b64SAllison Henderson * 35894907cb7bSAnatol Pomozov * Returns: 0 on success or negative on failure 3590a4bb6b64SAllison Henderson */ 3591a4bb6b64SAllison Henderson 3592a4bb6b64SAllison Henderson int ext4_punch_hole(struct file *file, loff_t offset, loff_t length) 3593a4bb6b64SAllison Henderson { 3594496ad9aaSAl Viro struct inode *inode = file_inode(file); 359526a4c0c6STheodore Ts'o struct super_block *sb = inode->i_sb; 359626a4c0c6STheodore Ts'o ext4_lblk_t first_block, stop_block; 359726a4c0c6STheodore Ts'o struct address_space *mapping = inode->i_mapping; 359826a4c0c6STheodore Ts'o loff_t first_page, last_page, page_len; 359926a4c0c6STheodore Ts'o loff_t first_page_offset, last_page_offset; 360026a4c0c6STheodore Ts'o handle_t *handle; 360126a4c0c6STheodore Ts'o unsigned int credits; 360226a4c0c6STheodore Ts'o int ret = 0; 360326a4c0c6STheodore Ts'o 3604a4bb6b64SAllison Henderson if (!S_ISREG(inode->i_mode)) 360573355192SAllison Henderson return -EOPNOTSUPP; 3606a4bb6b64SAllison Henderson 360726a4c0c6STheodore Ts'o if (EXT4_SB(sb)->s_cluster_ratio > 1) { 3608bab08ab9STheodore Ts'o /* TODO: Add support for bigalloc file systems */ 360973355192SAllison Henderson return -EOPNOTSUPP; 3610bab08ab9STheodore Ts'o } 3611bab08ab9STheodore Ts'o 3612aaddea81SZheng Liu trace_ext4_punch_hole(inode, offset, length); 3613aaddea81SZheng Liu 361426a4c0c6STheodore Ts'o /* 361526a4c0c6STheodore Ts'o * Write out all dirty pages to avoid race conditions 361626a4c0c6STheodore Ts'o * Then release them. 361726a4c0c6STheodore Ts'o */ 361826a4c0c6STheodore Ts'o if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) { 361926a4c0c6STheodore Ts'o ret = filemap_write_and_wait_range(mapping, offset, 362026a4c0c6STheodore Ts'o offset + length - 1); 362126a4c0c6STheodore Ts'o if (ret) 362226a4c0c6STheodore Ts'o return ret; 362326a4c0c6STheodore Ts'o } 362426a4c0c6STheodore Ts'o 362526a4c0c6STheodore Ts'o mutex_lock(&inode->i_mutex); 362626a4c0c6STheodore Ts'o /* It's not possible punch hole on append only file */ 362726a4c0c6STheodore Ts'o if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) { 362826a4c0c6STheodore Ts'o ret = -EPERM; 362926a4c0c6STheodore Ts'o goto out_mutex; 363026a4c0c6STheodore Ts'o } 363126a4c0c6STheodore Ts'o if (IS_SWAPFILE(inode)) { 363226a4c0c6STheodore Ts'o ret = -ETXTBSY; 363326a4c0c6STheodore Ts'o goto out_mutex; 363426a4c0c6STheodore Ts'o } 363526a4c0c6STheodore Ts'o 363626a4c0c6STheodore Ts'o /* No need to punch hole beyond i_size */ 363726a4c0c6STheodore Ts'o if (offset >= inode->i_size) 363826a4c0c6STheodore Ts'o goto out_mutex; 363926a4c0c6STheodore Ts'o 364026a4c0c6STheodore Ts'o /* 364126a4c0c6STheodore Ts'o * If the hole extends beyond i_size, set the hole 364226a4c0c6STheodore Ts'o * to end after the page that contains i_size 364326a4c0c6STheodore Ts'o */ 364426a4c0c6STheodore Ts'o if (offset + length > inode->i_size) { 364526a4c0c6STheodore Ts'o length = inode->i_size + 364626a4c0c6STheodore Ts'o PAGE_CACHE_SIZE - (inode->i_size & (PAGE_CACHE_SIZE - 1)) - 364726a4c0c6STheodore Ts'o offset; 364826a4c0c6STheodore Ts'o } 364926a4c0c6STheodore Ts'o 365026a4c0c6STheodore Ts'o first_page = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 365126a4c0c6STheodore Ts'o last_page = (offset + length) >> PAGE_CACHE_SHIFT; 365226a4c0c6STheodore Ts'o 365326a4c0c6STheodore Ts'o first_page_offset = first_page << PAGE_CACHE_SHIFT; 365426a4c0c6STheodore Ts'o last_page_offset = last_page << PAGE_CACHE_SHIFT; 365526a4c0c6STheodore Ts'o 365626a4c0c6STheodore Ts'o /* Now release the pages */ 365726a4c0c6STheodore Ts'o if (last_page_offset > first_page_offset) { 365826a4c0c6STheodore Ts'o truncate_pagecache_range(inode, first_page_offset, 365926a4c0c6STheodore Ts'o last_page_offset - 1); 366026a4c0c6STheodore Ts'o } 366126a4c0c6STheodore Ts'o 366226a4c0c6STheodore Ts'o /* Wait all existing dio workers, newcomers will block on i_mutex */ 366326a4c0c6STheodore Ts'o ext4_inode_block_unlocked_dio(inode); 366426a4c0c6STheodore Ts'o ret = ext4_flush_unwritten_io(inode); 366526a4c0c6STheodore Ts'o if (ret) 366626a4c0c6STheodore Ts'o goto out_dio; 366726a4c0c6STheodore Ts'o inode_dio_wait(inode); 366826a4c0c6STheodore Ts'o 366926a4c0c6STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 367026a4c0c6STheodore Ts'o credits = ext4_writepage_trans_blocks(inode); 367126a4c0c6STheodore Ts'o else 367226a4c0c6STheodore Ts'o credits = ext4_blocks_for_truncate(inode); 367326a4c0c6STheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 367426a4c0c6STheodore Ts'o if (IS_ERR(handle)) { 367526a4c0c6STheodore Ts'o ret = PTR_ERR(handle); 367626a4c0c6STheodore Ts'o ext4_std_error(sb, ret); 367726a4c0c6STheodore Ts'o goto out_dio; 367826a4c0c6STheodore Ts'o } 367926a4c0c6STheodore Ts'o 368026a4c0c6STheodore Ts'o /* 368126a4c0c6STheodore Ts'o * Now we need to zero out the non-page-aligned data in the 368226a4c0c6STheodore Ts'o * pages at the start and tail of the hole, and unmap the 368326a4c0c6STheodore Ts'o * buffer heads for the block aligned regions of the page that 368426a4c0c6STheodore Ts'o * were completely zeroed. 368526a4c0c6STheodore Ts'o */ 368626a4c0c6STheodore Ts'o if (first_page > last_page) { 368726a4c0c6STheodore Ts'o /* 368826a4c0c6STheodore Ts'o * If the file space being truncated is contained 368926a4c0c6STheodore Ts'o * within a page just zero out and unmap the middle of 369026a4c0c6STheodore Ts'o * that page 369126a4c0c6STheodore Ts'o */ 369226a4c0c6STheodore Ts'o ret = ext4_discard_partial_page_buffers(handle, 369326a4c0c6STheodore Ts'o mapping, offset, length, 0); 369426a4c0c6STheodore Ts'o 369526a4c0c6STheodore Ts'o if (ret) 369626a4c0c6STheodore Ts'o goto out_stop; 369726a4c0c6STheodore Ts'o } else { 369826a4c0c6STheodore Ts'o /* 369926a4c0c6STheodore Ts'o * zero out and unmap the partial page that contains 370026a4c0c6STheodore Ts'o * the start of the hole 370126a4c0c6STheodore Ts'o */ 370226a4c0c6STheodore Ts'o page_len = first_page_offset - offset; 370326a4c0c6STheodore Ts'o if (page_len > 0) { 370426a4c0c6STheodore Ts'o ret = ext4_discard_partial_page_buffers(handle, mapping, 370526a4c0c6STheodore Ts'o offset, page_len, 0); 370626a4c0c6STheodore Ts'o if (ret) 370726a4c0c6STheodore Ts'o goto out_stop; 370826a4c0c6STheodore Ts'o } 370926a4c0c6STheodore Ts'o 371026a4c0c6STheodore Ts'o /* 371126a4c0c6STheodore Ts'o * zero out and unmap the partial page that contains 371226a4c0c6STheodore Ts'o * the end of the hole 371326a4c0c6STheodore Ts'o */ 371426a4c0c6STheodore Ts'o page_len = offset + length - last_page_offset; 371526a4c0c6STheodore Ts'o if (page_len > 0) { 371626a4c0c6STheodore Ts'o ret = ext4_discard_partial_page_buffers(handle, mapping, 371726a4c0c6STheodore Ts'o last_page_offset, page_len, 0); 371826a4c0c6STheodore Ts'o if (ret) 371926a4c0c6STheodore Ts'o goto out_stop; 372026a4c0c6STheodore Ts'o } 372126a4c0c6STheodore Ts'o } 372226a4c0c6STheodore Ts'o 372326a4c0c6STheodore Ts'o /* 372426a4c0c6STheodore Ts'o * If i_size is contained in the last page, we need to 372526a4c0c6STheodore Ts'o * unmap and zero the partial page after i_size 372626a4c0c6STheodore Ts'o */ 372726a4c0c6STheodore Ts'o if (inode->i_size >> PAGE_CACHE_SHIFT == last_page && 372826a4c0c6STheodore Ts'o inode->i_size % PAGE_CACHE_SIZE != 0) { 372926a4c0c6STheodore Ts'o page_len = PAGE_CACHE_SIZE - 373026a4c0c6STheodore Ts'o (inode->i_size & (PAGE_CACHE_SIZE - 1)); 373126a4c0c6STheodore Ts'o 373226a4c0c6STheodore Ts'o if (page_len > 0) { 373326a4c0c6STheodore Ts'o ret = ext4_discard_partial_page_buffers(handle, 373426a4c0c6STheodore Ts'o mapping, inode->i_size, page_len, 0); 373526a4c0c6STheodore Ts'o 373626a4c0c6STheodore Ts'o if (ret) 373726a4c0c6STheodore Ts'o goto out_stop; 373826a4c0c6STheodore Ts'o } 373926a4c0c6STheodore Ts'o } 374026a4c0c6STheodore Ts'o 374126a4c0c6STheodore Ts'o first_block = (offset + sb->s_blocksize - 1) >> 374226a4c0c6STheodore Ts'o EXT4_BLOCK_SIZE_BITS(sb); 374326a4c0c6STheodore Ts'o stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb); 374426a4c0c6STheodore Ts'o 374526a4c0c6STheodore Ts'o /* If there are no blocks to remove, return now */ 374626a4c0c6STheodore Ts'o if (first_block >= stop_block) 374726a4c0c6STheodore Ts'o goto out_stop; 374826a4c0c6STheodore Ts'o 374926a4c0c6STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 375026a4c0c6STheodore Ts'o ext4_discard_preallocations(inode); 375126a4c0c6STheodore Ts'o 375226a4c0c6STheodore Ts'o ret = ext4_es_remove_extent(inode, first_block, 375326a4c0c6STheodore Ts'o stop_block - first_block); 375426a4c0c6STheodore Ts'o if (ret) { 375526a4c0c6STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 375626a4c0c6STheodore Ts'o goto out_stop; 375726a4c0c6STheodore Ts'o } 375826a4c0c6STheodore Ts'o 375926a4c0c6STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 376026a4c0c6STheodore Ts'o ret = ext4_ext_remove_space(inode, first_block, 376126a4c0c6STheodore Ts'o stop_block - 1); 376226a4c0c6STheodore Ts'o else 376326a4c0c6STheodore Ts'o ret = ext4_free_hole_blocks(handle, inode, first_block, 376426a4c0c6STheodore Ts'o stop_block); 376526a4c0c6STheodore Ts'o 376626a4c0c6STheodore Ts'o ext4_discard_preallocations(inode); 3767819c4920STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 376826a4c0c6STheodore Ts'o if (IS_SYNC(inode)) 376926a4c0c6STheodore Ts'o ext4_handle_sync(handle); 377026a4c0c6STheodore Ts'o inode->i_mtime = inode->i_ctime = ext4_current_time(inode); 377126a4c0c6STheodore Ts'o ext4_mark_inode_dirty(handle, inode); 377226a4c0c6STheodore Ts'o out_stop: 377326a4c0c6STheodore Ts'o ext4_journal_stop(handle); 377426a4c0c6STheodore Ts'o out_dio: 377526a4c0c6STheodore Ts'o ext4_inode_resume_unlocked_dio(inode); 377626a4c0c6STheodore Ts'o out_mutex: 377726a4c0c6STheodore Ts'o mutex_unlock(&inode->i_mutex); 377826a4c0c6STheodore Ts'o return ret; 3779a4bb6b64SAllison Henderson } 3780a4bb6b64SAllison Henderson 3781a4bb6b64SAllison Henderson /* 3782617ba13bSMingming Cao * ext4_truncate() 3783ac27a0ecSDave Kleikamp * 3784617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 3785617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 3786ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 3787ac27a0ecSDave Kleikamp * 378842b2aa86SJustin P. Mattock * As we work through the truncate and commit bits of it to the journal there 3789ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 3790ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 3791ac27a0ecSDave Kleikamp * 3792ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 3793ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 3794ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 3795ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 3796ac27a0ecSDave Kleikamp * left-to-right works OK too). 3797ac27a0ecSDave Kleikamp * 3798ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 3799ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 3800ac27a0ecSDave Kleikamp * 3801ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 3802617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 3803ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 3804617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 3805617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 3806ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 3807617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 3808ac27a0ecSDave Kleikamp */ 3809617ba13bSMingming Cao void ext4_truncate(struct inode *inode) 3810ac27a0ecSDave Kleikamp { 3811819c4920STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 3812819c4920STheodore Ts'o unsigned int credits; 3813819c4920STheodore Ts'o handle_t *handle; 3814819c4920STheodore Ts'o struct address_space *mapping = inode->i_mapping; 3815819c4920STheodore Ts'o loff_t page_len; 3816819c4920STheodore Ts'o 381719b5ef61STheodore Ts'o /* 381819b5ef61STheodore Ts'o * There is a possibility that we're either freeing the inode 381919b5ef61STheodore Ts'o * or it completely new indode. In those cases we might not 382019b5ef61STheodore Ts'o * have i_mutex locked because it's not necessary. 382119b5ef61STheodore Ts'o */ 382219b5ef61STheodore Ts'o if (!(inode->i_state & (I_NEW|I_FREEING))) 382319b5ef61STheodore Ts'o WARN_ON(!mutex_is_locked(&inode->i_mutex)); 38240562e0baSJiaying Zhang trace_ext4_truncate_enter(inode); 38250562e0baSJiaying Zhang 382691ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 3827ac27a0ecSDave Kleikamp return; 3828ac27a0ecSDave Kleikamp 382912e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS); 3830c8d46e41SJiaying Zhang 38315534fb5bSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 383219f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE); 38337d8f9f7dSTheodore Ts'o 3834aef1c851STao Ma if (ext4_has_inline_data(inode)) { 3835aef1c851STao Ma int has_inline = 1; 3836aef1c851STao Ma 3837aef1c851STao Ma ext4_inline_data_truncate(inode, &has_inline); 3838aef1c851STao Ma if (has_inline) 3839aef1c851STao Ma return; 3840aef1c851STao Ma } 3841aef1c851STao Ma 3842819c4920STheodore Ts'o /* 3843819c4920STheodore Ts'o * finish any pending end_io work so we won't run the risk of 3844819c4920STheodore Ts'o * converting any truncated blocks to initialized later 3845819c4920STheodore Ts'o */ 3846819c4920STheodore Ts'o ext4_flush_unwritten_io(inode); 3847819c4920STheodore Ts'o 3848ff9893dcSAmir Goldstein if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3849819c4920STheodore Ts'o credits = ext4_writepage_trans_blocks(inode); 3850ff9893dcSAmir Goldstein else 3851819c4920STheodore Ts'o credits = ext4_blocks_for_truncate(inode); 3852819c4920STheodore Ts'o 3853819c4920STheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 3854819c4920STheodore Ts'o if (IS_ERR(handle)) { 3855819c4920STheodore Ts'o ext4_std_error(inode->i_sb, PTR_ERR(handle)); 3856819c4920STheodore Ts'o return; 3857819c4920STheodore Ts'o } 3858819c4920STheodore Ts'o 3859819c4920STheodore Ts'o if (inode->i_size % PAGE_CACHE_SIZE != 0) { 3860819c4920STheodore Ts'o page_len = PAGE_CACHE_SIZE - 3861819c4920STheodore Ts'o (inode->i_size & (PAGE_CACHE_SIZE - 1)); 3862819c4920STheodore Ts'o 3863819c4920STheodore Ts'o if (ext4_discard_partial_page_buffers(handle, 3864819c4920STheodore Ts'o mapping, inode->i_size, page_len, 0)) 3865819c4920STheodore Ts'o goto out_stop; 3866819c4920STheodore Ts'o } 3867819c4920STheodore Ts'o 3868819c4920STheodore Ts'o /* 3869819c4920STheodore Ts'o * We add the inode to the orphan list, so that if this 3870819c4920STheodore Ts'o * truncate spans multiple transactions, and we crash, we will 3871819c4920STheodore Ts'o * resume the truncate when the filesystem recovers. It also 3872819c4920STheodore Ts'o * marks the inode dirty, to catch the new size. 3873819c4920STheodore Ts'o * 3874819c4920STheodore Ts'o * Implication: the file must always be in a sane, consistent 3875819c4920STheodore Ts'o * truncatable state while each transaction commits. 3876819c4920STheodore Ts'o */ 3877819c4920STheodore Ts'o if (ext4_orphan_add(handle, inode)) 3878819c4920STheodore Ts'o goto out_stop; 3879819c4920STheodore Ts'o 3880819c4920STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 3881819c4920STheodore Ts'o 3882819c4920STheodore Ts'o ext4_discard_preallocations(inode); 3883819c4920STheodore Ts'o 3884819c4920STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3885819c4920STheodore Ts'o ext4_ext_truncate(handle, inode); 3886819c4920STheodore Ts'o else 3887819c4920STheodore Ts'o ext4_ind_truncate(handle, inode); 3888819c4920STheodore Ts'o 3889819c4920STheodore Ts'o up_write(&ei->i_data_sem); 3890819c4920STheodore Ts'o 3891819c4920STheodore Ts'o if (IS_SYNC(inode)) 3892819c4920STheodore Ts'o ext4_handle_sync(handle); 3893819c4920STheodore Ts'o 3894819c4920STheodore Ts'o out_stop: 3895819c4920STheodore Ts'o /* 3896819c4920STheodore Ts'o * If this was a simple ftruncate() and the file will remain alive, 3897819c4920STheodore Ts'o * then we need to clear up the orphan record which we created above. 3898819c4920STheodore Ts'o * However, if this was a real unlink then we were called by 3899819c4920STheodore Ts'o * ext4_delete_inode(), and we allow that function to clean up the 3900819c4920STheodore Ts'o * orphan info for us. 3901819c4920STheodore Ts'o */ 3902819c4920STheodore Ts'o if (inode->i_nlink) 3903819c4920STheodore Ts'o ext4_orphan_del(handle, inode); 3904819c4920STheodore Ts'o 3905819c4920STheodore Ts'o inode->i_mtime = inode->i_ctime = ext4_current_time(inode); 3906819c4920STheodore Ts'o ext4_mark_inode_dirty(handle, inode); 3907819c4920STheodore Ts'o ext4_journal_stop(handle); 3908a86c6181SAlex Tomas 39090562e0baSJiaying Zhang trace_ext4_truncate_exit(inode); 3910ac27a0ecSDave Kleikamp } 3911ac27a0ecSDave Kleikamp 3912ac27a0ecSDave Kleikamp /* 3913617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 3914ac27a0ecSDave Kleikamp * underlying buffer_head on success. If 'in_mem' is true, we have all 3915ac27a0ecSDave Kleikamp * data in memory that is needed to recreate the on-disk version of this 3916ac27a0ecSDave Kleikamp * inode. 3917ac27a0ecSDave Kleikamp */ 3918617ba13bSMingming Cao static int __ext4_get_inode_loc(struct inode *inode, 3919617ba13bSMingming Cao struct ext4_iloc *iloc, int in_mem) 3920ac27a0ecSDave Kleikamp { 3921240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 3922ac27a0ecSDave Kleikamp struct buffer_head *bh; 3923240799cdSTheodore Ts'o struct super_block *sb = inode->i_sb; 3924240799cdSTheodore Ts'o ext4_fsblk_t block; 3925240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 3926ac27a0ecSDave Kleikamp 39273a06d778SAneesh Kumar K.V iloc->bh = NULL; 3928240799cdSTheodore Ts'o if (!ext4_valid_inum(sb, inode->i_ino)) 3929ac27a0ecSDave Kleikamp return -EIO; 3930ac27a0ecSDave Kleikamp 3931240799cdSTheodore Ts'o iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb); 3932240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 3933240799cdSTheodore Ts'o if (!gdp) 3934240799cdSTheodore Ts'o return -EIO; 3935240799cdSTheodore Ts'o 3936240799cdSTheodore Ts'o /* 3937240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 3938240799cdSTheodore Ts'o */ 393900d09882STao Ma inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 3940240799cdSTheodore Ts'o inode_offset = ((inode->i_ino - 1) % 3941240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 3942240799cdSTheodore Ts'o block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); 3943240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 3944240799cdSTheodore Ts'o 3945240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 3946aebf0243SWang Shilong if (unlikely(!bh)) 3947860d21e2STheodore Ts'o return -ENOMEM; 3948ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 3949ac27a0ecSDave Kleikamp lock_buffer(bh); 39509c83a923SHidehiro Kawai 39519c83a923SHidehiro Kawai /* 39529c83a923SHidehiro Kawai * If the buffer has the write error flag, we have failed 39539c83a923SHidehiro Kawai * to write out another inode in the same block. In this 39549c83a923SHidehiro Kawai * case, we don't have to read the block because we may 39559c83a923SHidehiro Kawai * read the old inode data successfully. 39569c83a923SHidehiro Kawai */ 39579c83a923SHidehiro Kawai if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) 39589c83a923SHidehiro Kawai set_buffer_uptodate(bh); 39599c83a923SHidehiro Kawai 3960ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) { 3961ac27a0ecSDave Kleikamp /* someone brought it uptodate while we waited */ 3962ac27a0ecSDave Kleikamp unlock_buffer(bh); 3963ac27a0ecSDave Kleikamp goto has_buffer; 3964ac27a0ecSDave Kleikamp } 3965ac27a0ecSDave Kleikamp 3966ac27a0ecSDave Kleikamp /* 3967ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 3968ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 3969ac27a0ecSDave Kleikamp * block. 3970ac27a0ecSDave Kleikamp */ 3971ac27a0ecSDave Kleikamp if (in_mem) { 3972ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 3973240799cdSTheodore Ts'o int i, start; 3974ac27a0ecSDave Kleikamp 3975240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 3976ac27a0ecSDave Kleikamp 3977ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 3978240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 3979aebf0243SWang Shilong if (unlikely(!bitmap_bh)) 3980ac27a0ecSDave Kleikamp goto make_io; 3981ac27a0ecSDave Kleikamp 3982ac27a0ecSDave Kleikamp /* 3983ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 3984ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 3985ac27a0ecSDave Kleikamp * of one, so skip it. 3986ac27a0ecSDave Kleikamp */ 3987ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 3988ac27a0ecSDave Kleikamp brelse(bitmap_bh); 3989ac27a0ecSDave Kleikamp goto make_io; 3990ac27a0ecSDave Kleikamp } 3991240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 3992ac27a0ecSDave Kleikamp if (i == inode_offset) 3993ac27a0ecSDave Kleikamp continue; 3994617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 3995ac27a0ecSDave Kleikamp break; 3996ac27a0ecSDave Kleikamp } 3997ac27a0ecSDave Kleikamp brelse(bitmap_bh); 3998240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 3999ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 4000ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 4001ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 4002ac27a0ecSDave Kleikamp unlock_buffer(bh); 4003ac27a0ecSDave Kleikamp goto has_buffer; 4004ac27a0ecSDave Kleikamp } 4005ac27a0ecSDave Kleikamp } 4006ac27a0ecSDave Kleikamp 4007ac27a0ecSDave Kleikamp make_io: 4008ac27a0ecSDave Kleikamp /* 4009240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 4010240799cdSTheodore Ts'o * blocks from the inode table. 4011240799cdSTheodore Ts'o */ 4012240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 4013240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 4014240799cdSTheodore Ts'o unsigned num; 40150d606e2cSTheodore Ts'o __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks; 4016240799cdSTheodore Ts'o 4017240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 4018b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 40190d606e2cSTheodore Ts'o b = block & ~((ext4_fsblk_t) ra_blks - 1); 4020240799cdSTheodore Ts'o if (table > b) 4021240799cdSTheodore Ts'o b = table; 40220d606e2cSTheodore Ts'o end = b + ra_blks; 4023240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 4024feb0ab32SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) 4025560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 4026240799cdSTheodore Ts'o table += num / inodes_per_block; 4027240799cdSTheodore Ts'o if (end > table) 4028240799cdSTheodore Ts'o end = table; 4029240799cdSTheodore Ts'o while (b <= end) 4030240799cdSTheodore Ts'o sb_breadahead(sb, b++); 4031240799cdSTheodore Ts'o } 4032240799cdSTheodore Ts'o 4033240799cdSTheodore Ts'o /* 4034ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 4035ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 4036ac27a0ecSDave Kleikamp * Read the block from disk. 4037ac27a0ecSDave Kleikamp */ 40380562e0baSJiaying Zhang trace_ext4_load_inode(inode); 4039ac27a0ecSDave Kleikamp get_bh(bh); 4040ac27a0ecSDave Kleikamp bh->b_end_io = end_buffer_read_sync; 404165299a3bSChristoph Hellwig submit_bh(READ | REQ_META | REQ_PRIO, bh); 4042ac27a0ecSDave Kleikamp wait_on_buffer(bh); 4043ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 4044c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, block, 4045c398eda0STheodore Ts'o "unable to read itable block"); 4046ac27a0ecSDave Kleikamp brelse(bh); 4047ac27a0ecSDave Kleikamp return -EIO; 4048ac27a0ecSDave Kleikamp } 4049ac27a0ecSDave Kleikamp } 4050ac27a0ecSDave Kleikamp has_buffer: 4051ac27a0ecSDave Kleikamp iloc->bh = bh; 4052ac27a0ecSDave Kleikamp return 0; 4053ac27a0ecSDave Kleikamp } 4054ac27a0ecSDave Kleikamp 4055617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 4056ac27a0ecSDave Kleikamp { 4057ac27a0ecSDave Kleikamp /* We have all inode data except xattrs in memory here. */ 4058617ba13bSMingming Cao return __ext4_get_inode_loc(inode, iloc, 405919f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_XATTR)); 4060ac27a0ecSDave Kleikamp } 4061ac27a0ecSDave Kleikamp 4062617ba13bSMingming Cao void ext4_set_inode_flags(struct inode *inode) 4063ac27a0ecSDave Kleikamp { 4064617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 4065ac27a0ecSDave Kleikamp 4066ac27a0ecSDave Kleikamp inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); 4067617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 4068ac27a0ecSDave Kleikamp inode->i_flags |= S_SYNC; 4069617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 4070ac27a0ecSDave Kleikamp inode->i_flags |= S_APPEND; 4071617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 4072ac27a0ecSDave Kleikamp inode->i_flags |= S_IMMUTABLE; 4073617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 4074ac27a0ecSDave Kleikamp inode->i_flags |= S_NOATIME; 4075617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 4076ac27a0ecSDave Kleikamp inode->i_flags |= S_DIRSYNC; 4077ac27a0ecSDave Kleikamp } 4078ac27a0ecSDave Kleikamp 4079ff9ddf7eSJan Kara /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */ 4080ff9ddf7eSJan Kara void ext4_get_inode_flags(struct ext4_inode_info *ei) 4081ff9ddf7eSJan Kara { 408284a8dce2SDmitry Monakhov unsigned int vfs_fl; 408384a8dce2SDmitry Monakhov unsigned long old_fl, new_fl; 4084ff9ddf7eSJan Kara 408584a8dce2SDmitry Monakhov do { 408684a8dce2SDmitry Monakhov vfs_fl = ei->vfs_inode.i_flags; 408784a8dce2SDmitry Monakhov old_fl = ei->i_flags; 408884a8dce2SDmitry Monakhov new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL| 408984a8dce2SDmitry Monakhov EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL| 409084a8dce2SDmitry Monakhov EXT4_DIRSYNC_FL); 409184a8dce2SDmitry Monakhov if (vfs_fl & S_SYNC) 409284a8dce2SDmitry Monakhov new_fl |= EXT4_SYNC_FL; 409384a8dce2SDmitry Monakhov if (vfs_fl & S_APPEND) 409484a8dce2SDmitry Monakhov new_fl |= EXT4_APPEND_FL; 409584a8dce2SDmitry Monakhov if (vfs_fl & S_IMMUTABLE) 409684a8dce2SDmitry Monakhov new_fl |= EXT4_IMMUTABLE_FL; 409784a8dce2SDmitry Monakhov if (vfs_fl & S_NOATIME) 409884a8dce2SDmitry Monakhov new_fl |= EXT4_NOATIME_FL; 409984a8dce2SDmitry Monakhov if (vfs_fl & S_DIRSYNC) 410084a8dce2SDmitry Monakhov new_fl |= EXT4_DIRSYNC_FL; 410184a8dce2SDmitry Monakhov } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl); 4102ff9ddf7eSJan Kara } 4103de9a55b8STheodore Ts'o 41040fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 41050fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 41060fc1b451SAneesh Kumar K.V { 41070fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 41088180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 41098180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 41100fc1b451SAneesh Kumar K.V 41110fc1b451SAneesh Kumar K.V if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 41120fc1b451SAneesh Kumar K.V EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) { 41130fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 41140fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 41150fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 411607a03824STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) { 41178180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 41188180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 41198180a562SAneesh Kumar K.V } else { 41200fc1b451SAneesh Kumar K.V return i_blocks; 41218180a562SAneesh Kumar K.V } 41220fc1b451SAneesh Kumar K.V } else { 41230fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 41240fc1b451SAneesh Kumar K.V } 41250fc1b451SAneesh Kumar K.V } 4126ff9ddf7eSJan Kara 4127152a7b0aSTao Ma static inline void ext4_iget_extra_inode(struct inode *inode, 4128152a7b0aSTao Ma struct ext4_inode *raw_inode, 4129152a7b0aSTao Ma struct ext4_inode_info *ei) 4130152a7b0aSTao Ma { 4131152a7b0aSTao Ma __le32 *magic = (void *)raw_inode + 4132152a7b0aSTao Ma EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize; 413367cf5b09STao Ma if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) { 4134152a7b0aSTao Ma ext4_set_inode_state(inode, EXT4_STATE_XATTR); 413567cf5b09STao Ma ext4_find_inline_data_nolock(inode); 4136f19d5870STao Ma } else 4137f19d5870STao Ma EXT4_I(inode)->i_inline_off = 0; 4138152a7b0aSTao Ma } 4139152a7b0aSTao Ma 41401d1fe1eeSDavid Howells struct inode *ext4_iget(struct super_block *sb, unsigned long ino) 4141ac27a0ecSDave Kleikamp { 4142617ba13bSMingming Cao struct ext4_iloc iloc; 4143617ba13bSMingming Cao struct ext4_inode *raw_inode; 41441d1fe1eeSDavid Howells struct ext4_inode_info *ei; 41451d1fe1eeSDavid Howells struct inode *inode; 4146b436b9beSJan Kara journal_t *journal = EXT4_SB(sb)->s_journal; 41471d1fe1eeSDavid Howells long ret; 4148ac27a0ecSDave Kleikamp int block; 414908cefc7aSEric W. Biederman uid_t i_uid; 415008cefc7aSEric W. Biederman gid_t i_gid; 4151ac27a0ecSDave Kleikamp 41521d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 41531d1fe1eeSDavid Howells if (!inode) 41541d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 41551d1fe1eeSDavid Howells if (!(inode->i_state & I_NEW)) 41561d1fe1eeSDavid Howells return inode; 41571d1fe1eeSDavid Howells 41581d1fe1eeSDavid Howells ei = EXT4_I(inode); 41597dc57615SPeter Huewe iloc.bh = NULL; 4160ac27a0ecSDave Kleikamp 41611d1fe1eeSDavid Howells ret = __ext4_get_inode_loc(inode, &iloc, 0); 41621d1fe1eeSDavid Howells if (ret < 0) 4163ac27a0ecSDave Kleikamp goto bad_inode; 4164617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 4165814525f4SDarrick J. Wong 4166814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4167814525f4SDarrick J. Wong ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 4168814525f4SDarrick J. Wong if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 4169814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)) { 4170814525f4SDarrick J. Wong EXT4_ERROR_INODE(inode, "bad extra_isize (%u != %u)", 4171814525f4SDarrick J. Wong EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize, 4172814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)); 4173814525f4SDarrick J. Wong ret = -EIO; 4174814525f4SDarrick J. Wong goto bad_inode; 4175814525f4SDarrick J. Wong } 4176814525f4SDarrick J. Wong } else 4177814525f4SDarrick J. Wong ei->i_extra_isize = 0; 4178814525f4SDarrick J. Wong 4179814525f4SDarrick J. Wong /* Precompute checksum seed for inode metadata */ 4180814525f4SDarrick J. Wong if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 4181814525f4SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) { 4182814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4183814525f4SDarrick J. Wong __u32 csum; 4184814525f4SDarrick J. Wong __le32 inum = cpu_to_le32(inode->i_ino); 4185814525f4SDarrick J. Wong __le32 gen = raw_inode->i_generation; 4186814525f4SDarrick J. Wong csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, 4187814525f4SDarrick J. Wong sizeof(inum)); 4188814525f4SDarrick J. Wong ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, 4189814525f4SDarrick J. Wong sizeof(gen)); 4190814525f4SDarrick J. Wong } 4191814525f4SDarrick J. Wong 4192814525f4SDarrick J. Wong if (!ext4_inode_csum_verify(inode, raw_inode, ei)) { 4193814525f4SDarrick J. Wong EXT4_ERROR_INODE(inode, "checksum invalid"); 4194814525f4SDarrick J. Wong ret = -EIO; 4195814525f4SDarrick J. Wong goto bad_inode; 4196814525f4SDarrick J. Wong } 4197814525f4SDarrick J. Wong 4198ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 419908cefc7aSEric W. Biederman i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 420008cefc7aSEric W. Biederman i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 4201ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 420208cefc7aSEric W. Biederman i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 420308cefc7aSEric W. Biederman i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 4204ac27a0ecSDave Kleikamp } 420508cefc7aSEric W. Biederman i_uid_write(inode, i_uid); 420608cefc7aSEric W. Biederman i_gid_write(inode, i_gid); 4207bfe86848SMiklos Szeredi set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); 4208ac27a0ecSDave Kleikamp 4209353eb83cSTheodore Ts'o ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 421067cf5b09STao Ma ei->i_inline_off = 0; 4211ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 4212ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 4213ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 4214ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 4215ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 4216ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 4217ac27a0ecSDave Kleikamp */ 4218ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 4219393d1d1dSDr. Tilmann Bubeck if ((inode->i_mode == 0 || 4220393d1d1dSDr. Tilmann Bubeck !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) && 4221393d1d1dSDr. Tilmann Bubeck ino != EXT4_BOOT_LOADER_INO) { 4222ac27a0ecSDave Kleikamp /* this inode is deleted */ 42231d1fe1eeSDavid Howells ret = -ESTALE; 4224ac27a0ecSDave Kleikamp goto bad_inode; 4225ac27a0ecSDave Kleikamp } 4226ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 4227ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 4228ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 4229393d1d1dSDr. Tilmann Bubeck * the process of deleting those. 4230393d1d1dSDr. Tilmann Bubeck * OR it is the EXT4_BOOT_LOADER_INO which is 4231393d1d1dSDr. Tilmann Bubeck * not initialized on a new filesystem. */ 4232ac27a0ecSDave Kleikamp } 4233ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 42340fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 42357973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 4236a9e81742STheodore Ts'o if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) 4237a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 4238a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 4239a48380f7SAneesh Kumar K.V inode->i_size = ext4_isize(raw_inode); 4240ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 4241a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 4242a9e7f447SDmitry Monakhov ei->i_reserved_quota = 0; 4243a9e7f447SDmitry Monakhov #endif 4244ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 4245ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 4246a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 4247ac27a0ecSDave Kleikamp /* 4248ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 4249ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 4250ac27a0ecSDave Kleikamp */ 4251617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 4252ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 4253ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 4254ac27a0ecSDave Kleikamp 4255b436b9beSJan Kara /* 4256b436b9beSJan Kara * Set transaction id's of transactions that have to be committed 4257b436b9beSJan Kara * to finish f[data]sync. We set them to currently running transaction 4258b436b9beSJan Kara * as we cannot be sure that the inode or some of its metadata isn't 4259b436b9beSJan Kara * part of the transaction - the inode could have been reclaimed and 4260b436b9beSJan Kara * now it is reread from disk. 4261b436b9beSJan Kara */ 4262b436b9beSJan Kara if (journal) { 4263b436b9beSJan Kara transaction_t *transaction; 4264b436b9beSJan Kara tid_t tid; 4265b436b9beSJan Kara 4266a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 4267b436b9beSJan Kara if (journal->j_running_transaction) 4268b436b9beSJan Kara transaction = journal->j_running_transaction; 4269b436b9beSJan Kara else 4270b436b9beSJan Kara transaction = journal->j_committing_transaction; 4271b436b9beSJan Kara if (transaction) 4272b436b9beSJan Kara tid = transaction->t_tid; 4273b436b9beSJan Kara else 4274b436b9beSJan Kara tid = journal->j_commit_sequence; 4275a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 4276b436b9beSJan Kara ei->i_sync_tid = tid; 4277b436b9beSJan Kara ei->i_datasync_tid = tid; 4278b436b9beSJan Kara } 4279b436b9beSJan Kara 42800040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4281ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 4282ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 4283617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 4284617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 4285ac27a0ecSDave Kleikamp } else { 4286152a7b0aSTao Ma ext4_iget_extra_inode(inode, raw_inode, ei); 4287ac27a0ecSDave Kleikamp } 4288814525f4SDarrick J. Wong } 4289ac27a0ecSDave Kleikamp 4290ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 4291ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 4292ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 4293ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 4294ef7f3835SKalpak Shah 429525ec56b5SJean Noel Cordenner inode->i_version = le32_to_cpu(raw_inode->i_disk_version); 429625ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 429725ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 429825ec56b5SJean Noel Cordenner inode->i_version |= 429925ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 430025ec56b5SJean Noel Cordenner } 430125ec56b5SJean Noel Cordenner 4302c4b5a614STheodore Ts'o ret = 0; 4303485c26ecSTheodore Ts'o if (ei->i_file_acl && 43041032988cSTheodore Ts'o !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) { 430524676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "bad extended attribute block %llu", 430624676da4STheodore Ts'o ei->i_file_acl); 4307485c26ecSTheodore Ts'o ret = -EIO; 4308485c26ecSTheodore Ts'o goto bad_inode; 4309f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 4310f19d5870STao Ma if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 4311f19d5870STao Ma if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 4312c4b5a614STheodore Ts'o (S_ISLNK(inode->i_mode) && 4313f19d5870STao Ma !ext4_inode_is_fast_symlink(inode)))) 43147a262f7cSAneesh Kumar K.V /* Validate extent which is part of inode */ 43157a262f7cSAneesh Kumar K.V ret = ext4_ext_check_inode(inode); 4316fe2c8191SThiemo Nagel } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 4317fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 4318fe2c8191SThiemo Nagel !ext4_inode_is_fast_symlink(inode))) { 4319fe2c8191SThiemo Nagel /* Validate block references which are part of inode */ 43201f7d1e77STheodore Ts'o ret = ext4_ind_check_inode(inode); 4321fe2c8191SThiemo Nagel } 4322f19d5870STao Ma } 4323567f3e9aSTheodore Ts'o if (ret) 43247a262f7cSAneesh Kumar K.V goto bad_inode; 43257a262f7cSAneesh Kumar K.V 4326ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 4327617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 4328617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 4329617ba13bSMingming Cao ext4_set_aops(inode); 4330ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 4331617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 4332617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 4333ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 4334e83c1397SDuane Griffin if (ext4_inode_is_fast_symlink(inode)) { 4335617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 4336e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 4337e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 4338e83c1397SDuane Griffin } else { 4339617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 4340617ba13bSMingming Cao ext4_set_aops(inode); 4341ac27a0ecSDave Kleikamp } 4342563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 4343563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 4344617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 4345ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 4346ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4347ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 4348ac27a0ecSDave Kleikamp else 4349ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4350ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 4351393d1d1dSDr. Tilmann Bubeck } else if (ino == EXT4_BOOT_LOADER_INO) { 4352393d1d1dSDr. Tilmann Bubeck make_bad_inode(inode); 4353563bdd61STheodore Ts'o } else { 4354563bdd61STheodore Ts'o ret = -EIO; 435524676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode); 4356563bdd61STheodore Ts'o goto bad_inode; 4357ac27a0ecSDave Kleikamp } 4358ac27a0ecSDave Kleikamp brelse(iloc.bh); 4359617ba13bSMingming Cao ext4_set_inode_flags(inode); 43601d1fe1eeSDavid Howells unlock_new_inode(inode); 43611d1fe1eeSDavid Howells return inode; 4362ac27a0ecSDave Kleikamp 4363ac27a0ecSDave Kleikamp bad_inode: 4364567f3e9aSTheodore Ts'o brelse(iloc.bh); 43651d1fe1eeSDavid Howells iget_failed(inode); 43661d1fe1eeSDavid Howells return ERR_PTR(ret); 4367ac27a0ecSDave Kleikamp } 4368ac27a0ecSDave Kleikamp 43690fc1b451SAneesh Kumar K.V static int ext4_inode_blocks_set(handle_t *handle, 43700fc1b451SAneesh Kumar K.V struct ext4_inode *raw_inode, 43710fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 43720fc1b451SAneesh Kumar K.V { 43730fc1b451SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 43740fc1b451SAneesh Kumar K.V u64 i_blocks = inode->i_blocks; 43750fc1b451SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 43760fc1b451SAneesh Kumar K.V 43770fc1b451SAneesh Kumar K.V if (i_blocks <= ~0U) { 43780fc1b451SAneesh Kumar K.V /* 43794907cb7bSAnatol Pomozov * i_blocks can be represented in a 32 bit variable 43800fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 43810fc1b451SAneesh Kumar K.V */ 43828180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 43830fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = 0; 438484a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 4385f287a1a5STheodore Ts'o return 0; 4386f287a1a5STheodore Ts'o } 4387f287a1a5STheodore Ts'o if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) 4388f287a1a5STheodore Ts'o return -EFBIG; 4389f287a1a5STheodore Ts'o 4390f287a1a5STheodore Ts'o if (i_blocks <= 0xffffffffffffULL) { 43910fc1b451SAneesh Kumar K.V /* 43920fc1b451SAneesh Kumar K.V * i_blocks can be represented in a 48 bit variable 43930fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 43940fc1b451SAneesh Kumar K.V */ 43958180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 43960fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 439784a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 43980fc1b451SAneesh Kumar K.V } else { 439984a8dce2SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE); 44008180a562SAneesh Kumar K.V /* i_block is stored in file system block size */ 44018180a562SAneesh Kumar K.V i_blocks = i_blocks >> (inode->i_blkbits - 9); 44028180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 44038180a562SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 44040fc1b451SAneesh Kumar K.V } 4405f287a1a5STheodore Ts'o return 0; 44060fc1b451SAneesh Kumar K.V } 44070fc1b451SAneesh Kumar K.V 4408ac27a0ecSDave Kleikamp /* 4409ac27a0ecSDave Kleikamp * Post the struct inode info into an on-disk inode location in the 4410ac27a0ecSDave Kleikamp * buffer-cache. This gobbles the caller's reference to the 4411ac27a0ecSDave Kleikamp * buffer_head in the inode location struct. 4412ac27a0ecSDave Kleikamp * 4413ac27a0ecSDave Kleikamp * The caller must have write access to iloc->bh. 4414ac27a0ecSDave Kleikamp */ 4415617ba13bSMingming Cao static int ext4_do_update_inode(handle_t *handle, 4416ac27a0ecSDave Kleikamp struct inode *inode, 4417830156c7SFrank Mayhar struct ext4_iloc *iloc) 4418ac27a0ecSDave Kleikamp { 4419617ba13bSMingming Cao struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 4420617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 4421ac27a0ecSDave Kleikamp struct buffer_head *bh = iloc->bh; 4422ac27a0ecSDave Kleikamp int err = 0, rc, block; 4423b71fc079SJan Kara int need_datasync = 0; 442408cefc7aSEric W. Biederman uid_t i_uid; 442508cefc7aSEric W. Biederman gid_t i_gid; 4426ac27a0ecSDave Kleikamp 4427ac27a0ecSDave Kleikamp /* For fields not not tracking in the in-memory inode, 4428ac27a0ecSDave Kleikamp * initialise them to zero for new inodes. */ 442919f5fb7aSTheodore Ts'o if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) 4430617ba13bSMingming Cao memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 4431ac27a0ecSDave Kleikamp 4432ff9ddf7eSJan Kara ext4_get_inode_flags(ei); 4433ac27a0ecSDave Kleikamp raw_inode->i_mode = cpu_to_le16(inode->i_mode); 443408cefc7aSEric W. Biederman i_uid = i_uid_read(inode); 443508cefc7aSEric W. Biederman i_gid = i_gid_read(inode); 4436ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 443708cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid)); 443808cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid)); 4439ac27a0ecSDave Kleikamp /* 4440ac27a0ecSDave Kleikamp * Fix up interoperability with old kernels. Otherwise, old inodes get 4441ac27a0ecSDave Kleikamp * re-used with the upper 16 bits of the uid/gid intact 4442ac27a0ecSDave Kleikamp */ 4443ac27a0ecSDave Kleikamp if (!ei->i_dtime) { 4444ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 444508cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_uid)); 4446ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 444708cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_gid)); 4448ac27a0ecSDave Kleikamp } else { 4449ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4450ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4451ac27a0ecSDave Kleikamp } 4452ac27a0ecSDave Kleikamp } else { 445308cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid)); 445408cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid)); 4455ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4456ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4457ac27a0ecSDave Kleikamp } 4458ac27a0ecSDave Kleikamp raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 4459ef7f3835SKalpak Shah 4460ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 4461ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 4462ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 4463ef7f3835SKalpak Shah EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 4464ef7f3835SKalpak Shah 44650fc1b451SAneesh Kumar K.V if (ext4_inode_blocks_set(handle, raw_inode, ei)) 44660fc1b451SAneesh Kumar K.V goto out_brelse; 4467ac27a0ecSDave Kleikamp raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 4468353eb83cSTheodore Ts'o raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF); 44699b8f1f01SMingming Cao if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 44709b8f1f01SMingming Cao cpu_to_le32(EXT4_OS_HURD)) 4471a1ddeb7eSBadari Pulavarty raw_inode->i_file_acl_high = 4472a1ddeb7eSBadari Pulavarty cpu_to_le16(ei->i_file_acl >> 32); 44737973c0c1SAneesh Kumar K.V raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 4474b71fc079SJan Kara if (ei->i_disksize != ext4_isize(raw_inode)) { 4475a48380f7SAneesh Kumar K.V ext4_isize_set(raw_inode, ei->i_disksize); 4476b71fc079SJan Kara need_datasync = 1; 4477b71fc079SJan Kara } 4478ac27a0ecSDave Kleikamp if (ei->i_disksize > 0x7fffffffULL) { 4479ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 4480617ba13bSMingming Cao if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, 4481617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE) || 4482617ba13bSMingming Cao EXT4_SB(sb)->s_es->s_rev_level == 4483617ba13bSMingming Cao cpu_to_le32(EXT4_GOOD_OLD_REV)) { 4484ac27a0ecSDave Kleikamp /* If this is the first large file 4485ac27a0ecSDave Kleikamp * created, add a flag to the superblock. 4486ac27a0ecSDave Kleikamp */ 4487617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, 4488617ba13bSMingming Cao EXT4_SB(sb)->s_sbh); 4489ac27a0ecSDave Kleikamp if (err) 4490ac27a0ecSDave Kleikamp goto out_brelse; 4491617ba13bSMingming Cao ext4_update_dynamic_rev(sb); 4492617ba13bSMingming Cao EXT4_SET_RO_COMPAT_FEATURE(sb, 4493617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE); 44940390131bSFrank Mayhar ext4_handle_sync(handle); 4495b50924c2SArtem Bityutskiy err = ext4_handle_dirty_super(handle, sb); 4496ac27a0ecSDave Kleikamp } 4497ac27a0ecSDave Kleikamp } 4498ac27a0ecSDave Kleikamp raw_inode->i_generation = cpu_to_le32(inode->i_generation); 4499ac27a0ecSDave Kleikamp if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 4500ac27a0ecSDave Kleikamp if (old_valid_dev(inode->i_rdev)) { 4501ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 4502ac27a0ecSDave Kleikamp cpu_to_le32(old_encode_dev(inode->i_rdev)); 4503ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 0; 4504ac27a0ecSDave Kleikamp } else { 4505ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 0; 4506ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 4507ac27a0ecSDave Kleikamp cpu_to_le32(new_encode_dev(inode->i_rdev)); 4508ac27a0ecSDave Kleikamp raw_inode->i_block[2] = 0; 4509ac27a0ecSDave Kleikamp } 4510f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 4511de9a55b8STheodore Ts'o for (block = 0; block < EXT4_N_BLOCKS; block++) 4512ac27a0ecSDave Kleikamp raw_inode->i_block[block] = ei->i_data[block]; 4513f19d5870STao Ma } 4514ac27a0ecSDave Kleikamp 451525ec56b5SJean Noel Cordenner raw_inode->i_disk_version = cpu_to_le32(inode->i_version); 451625ec56b5SJean Noel Cordenner if (ei->i_extra_isize) { 451725ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 451825ec56b5SJean Noel Cordenner raw_inode->i_version_hi = 451925ec56b5SJean Noel Cordenner cpu_to_le32(inode->i_version >> 32); 4520ac27a0ecSDave Kleikamp raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize); 452125ec56b5SJean Noel Cordenner } 452225ec56b5SJean Noel Cordenner 4523814525f4SDarrick J. Wong ext4_inode_csum_set(inode, raw_inode, ei); 4524814525f4SDarrick J. Wong 45250390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 452673b50c1cSCurt Wohlgemuth rc = ext4_handle_dirty_metadata(handle, NULL, bh); 4527ac27a0ecSDave Kleikamp if (!err) 4528ac27a0ecSDave Kleikamp err = rc; 452919f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_NEW); 4530ac27a0ecSDave Kleikamp 4531b71fc079SJan Kara ext4_update_inode_fsync_trans(handle, inode, need_datasync); 4532ac27a0ecSDave Kleikamp out_brelse: 4533ac27a0ecSDave Kleikamp brelse(bh); 4534617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4535ac27a0ecSDave Kleikamp return err; 4536ac27a0ecSDave Kleikamp } 4537ac27a0ecSDave Kleikamp 4538ac27a0ecSDave Kleikamp /* 4539617ba13bSMingming Cao * ext4_write_inode() 4540ac27a0ecSDave Kleikamp * 4541ac27a0ecSDave Kleikamp * We are called from a few places: 4542ac27a0ecSDave Kleikamp * 4543ac27a0ecSDave Kleikamp * - Within generic_file_write() for O_SYNC files. 4544ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 45454907cb7bSAnatol Pomozov * transaction to commit. 4546ac27a0ecSDave Kleikamp * 4547ac27a0ecSDave Kleikamp * - Within sys_sync(), kupdate and such. 4548ac27a0ecSDave Kleikamp * We wait on commit, if tol to. 4549ac27a0ecSDave Kleikamp * 4550ac27a0ecSDave Kleikamp * - Within prune_icache() (PF_MEMALLOC == true) 4551ac27a0ecSDave Kleikamp * Here we simply return. We can't afford to block kswapd on the 4552ac27a0ecSDave Kleikamp * journal commit. 4553ac27a0ecSDave Kleikamp * 4554ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 4555ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 4556617ba13bSMingming Cao * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for 4557ac27a0ecSDave Kleikamp * knfsd. 4558ac27a0ecSDave Kleikamp * 4559ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 4560ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 4561ac27a0ecSDave Kleikamp * which we are interested. 4562ac27a0ecSDave Kleikamp * 4563ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 4564ac27a0ecSDave Kleikamp * 4565ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 4566ac27a0ecSDave Kleikamp * stuff(); 4567ac27a0ecSDave Kleikamp * inode->i_size = expr; 4568ac27a0ecSDave Kleikamp * 4569ac27a0ecSDave Kleikamp * is in error because a kswapd-driven write_inode() could occur while 4570ac27a0ecSDave Kleikamp * `stuff()' is running, and the new i_size will be lost. Plus the inode 4571ac27a0ecSDave Kleikamp * will no longer be on the superblock's dirty inode list. 4572ac27a0ecSDave Kleikamp */ 4573a9185b41SChristoph Hellwig int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) 4574ac27a0ecSDave Kleikamp { 457591ac6f43SFrank Mayhar int err; 457691ac6f43SFrank Mayhar 4577ac27a0ecSDave Kleikamp if (current->flags & PF_MEMALLOC) 4578ac27a0ecSDave Kleikamp return 0; 4579ac27a0ecSDave Kleikamp 458091ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal) { 4581617ba13bSMingming Cao if (ext4_journal_current_handle()) { 4582b38bd33aSMingming Cao jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); 4583ac27a0ecSDave Kleikamp dump_stack(); 4584ac27a0ecSDave Kleikamp return -EIO; 4585ac27a0ecSDave Kleikamp } 4586ac27a0ecSDave Kleikamp 4587a9185b41SChristoph Hellwig if (wbc->sync_mode != WB_SYNC_ALL) 4588ac27a0ecSDave Kleikamp return 0; 4589ac27a0ecSDave Kleikamp 459091ac6f43SFrank Mayhar err = ext4_force_commit(inode->i_sb); 459191ac6f43SFrank Mayhar } else { 459291ac6f43SFrank Mayhar struct ext4_iloc iloc; 459391ac6f43SFrank Mayhar 45948b472d73SCurt Wohlgemuth err = __ext4_get_inode_loc(inode, &iloc, 0); 459591ac6f43SFrank Mayhar if (err) 459691ac6f43SFrank Mayhar return err; 4597a9185b41SChristoph Hellwig if (wbc->sync_mode == WB_SYNC_ALL) 4598830156c7SFrank Mayhar sync_dirty_buffer(iloc.bh); 4599830156c7SFrank Mayhar if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { 4600c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr, 4601c398eda0STheodore Ts'o "IO error syncing inode"); 4602830156c7SFrank Mayhar err = -EIO; 4603830156c7SFrank Mayhar } 4604fd2dd9fbSCurt Wohlgemuth brelse(iloc.bh); 460591ac6f43SFrank Mayhar } 460691ac6f43SFrank Mayhar return err; 4607ac27a0ecSDave Kleikamp } 4608ac27a0ecSDave Kleikamp 4609ac27a0ecSDave Kleikamp /* 461053e87268SJan Kara * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate 461153e87268SJan Kara * buffers that are attached to a page stradding i_size and are undergoing 461253e87268SJan Kara * commit. In that case we have to wait for commit to finish and try again. 461353e87268SJan Kara */ 461453e87268SJan Kara static void ext4_wait_for_tail_page_commit(struct inode *inode) 461553e87268SJan Kara { 461653e87268SJan Kara struct page *page; 461753e87268SJan Kara unsigned offset; 461853e87268SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 461953e87268SJan Kara tid_t commit_tid = 0; 462053e87268SJan Kara int ret; 462153e87268SJan Kara 462253e87268SJan Kara offset = inode->i_size & (PAGE_CACHE_SIZE - 1); 462353e87268SJan Kara /* 462453e87268SJan Kara * All buffers in the last page remain valid? Then there's nothing to 462553e87268SJan Kara * do. We do the check mainly to optimize the common PAGE_CACHE_SIZE == 462653e87268SJan Kara * blocksize case 462753e87268SJan Kara */ 462853e87268SJan Kara if (offset > PAGE_CACHE_SIZE - (1 << inode->i_blkbits)) 462953e87268SJan Kara return; 463053e87268SJan Kara while (1) { 463153e87268SJan Kara page = find_lock_page(inode->i_mapping, 463253e87268SJan Kara inode->i_size >> PAGE_CACHE_SHIFT); 463353e87268SJan Kara if (!page) 463453e87268SJan Kara return; 463553e87268SJan Kara ret = __ext4_journalled_invalidatepage(page, offset); 463653e87268SJan Kara unlock_page(page); 463753e87268SJan Kara page_cache_release(page); 463853e87268SJan Kara if (ret != -EBUSY) 463953e87268SJan Kara return; 464053e87268SJan Kara commit_tid = 0; 464153e87268SJan Kara read_lock(&journal->j_state_lock); 464253e87268SJan Kara if (journal->j_committing_transaction) 464353e87268SJan Kara commit_tid = journal->j_committing_transaction->t_tid; 464453e87268SJan Kara read_unlock(&journal->j_state_lock); 464553e87268SJan Kara if (commit_tid) 464653e87268SJan Kara jbd2_log_wait_commit(journal, commit_tid); 464753e87268SJan Kara } 464853e87268SJan Kara } 464953e87268SJan Kara 465053e87268SJan Kara /* 4651617ba13bSMingming Cao * ext4_setattr() 4652ac27a0ecSDave Kleikamp * 4653ac27a0ecSDave Kleikamp * Called from notify_change. 4654ac27a0ecSDave Kleikamp * 4655ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 4656ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 4657ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 4658ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 4659ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 4660ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 4661ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 4662ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 4663ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 4664ac27a0ecSDave Kleikamp * 4665678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 4666678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 4667678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 4668678aaf48SJan Kara * This way we are sure that all the data written in the previous 4669678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 4670678aaf48SJan Kara * writeback). 4671678aaf48SJan Kara * 4672678aaf48SJan Kara * Called with inode->i_mutex down. 4673ac27a0ecSDave Kleikamp */ 4674617ba13bSMingming Cao int ext4_setattr(struct dentry *dentry, struct iattr *attr) 4675ac27a0ecSDave Kleikamp { 4676ac27a0ecSDave Kleikamp struct inode *inode = dentry->d_inode; 4677ac27a0ecSDave Kleikamp int error, rc = 0; 46783d287de3SDmitry Monakhov int orphan = 0; 4679ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 4680ac27a0ecSDave Kleikamp 4681ac27a0ecSDave Kleikamp error = inode_change_ok(inode, attr); 4682ac27a0ecSDave Kleikamp if (error) 4683ac27a0ecSDave Kleikamp return error; 4684ac27a0ecSDave Kleikamp 468512755627SDmitry Monakhov if (is_quota_modification(inode, attr)) 4686871a2931SChristoph Hellwig dquot_initialize(inode); 468708cefc7aSEric W. Biederman if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) || 468808cefc7aSEric W. Biederman (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) { 4689ac27a0ecSDave Kleikamp handle_t *handle; 4690ac27a0ecSDave Kleikamp 4691ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 4692ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 46939924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 46949924a92aSTheodore Ts'o (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) + 4695194074acSDmitry Monakhov EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3); 4696ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 4697ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 4698ac27a0ecSDave Kleikamp goto err_out; 4699ac27a0ecSDave Kleikamp } 4700b43fa828SChristoph Hellwig error = dquot_transfer(inode, attr); 4701ac27a0ecSDave Kleikamp if (error) { 4702617ba13bSMingming Cao ext4_journal_stop(handle); 4703ac27a0ecSDave Kleikamp return error; 4704ac27a0ecSDave Kleikamp } 4705ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 4706ac27a0ecSDave Kleikamp * one transaction */ 4707ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_UID) 4708ac27a0ecSDave Kleikamp inode->i_uid = attr->ia_uid; 4709ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_GID) 4710ac27a0ecSDave Kleikamp inode->i_gid = attr->ia_gid; 4711617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 4712617ba13bSMingming Cao ext4_journal_stop(handle); 4713ac27a0ecSDave Kleikamp } 4714ac27a0ecSDave Kleikamp 4715e2b46574SEric Sandeen if (attr->ia_valid & ATTR_SIZE) { 4716562c72aaSChristoph Hellwig 471712e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 4718e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4719e2b46574SEric Sandeen 47200c095c7fSTheodore Ts'o if (attr->ia_size > sbi->s_bitmap_maxbytes) 47210c095c7fSTheodore Ts'o return -EFBIG; 4722e2b46574SEric Sandeen } 4723e2b46574SEric Sandeen } 4724e2b46574SEric Sandeen 4725ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode) && 4726c8d46e41SJiaying Zhang attr->ia_valid & ATTR_SIZE && 4727072bd7eaSTheodore Ts'o (attr->ia_size < inode->i_size)) { 4728ac27a0ecSDave Kleikamp handle_t *handle; 4729ac27a0ecSDave Kleikamp 47309924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 3); 4731ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 4732ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 4733ac27a0ecSDave Kleikamp goto err_out; 4734ac27a0ecSDave Kleikamp } 47353d287de3SDmitry Monakhov if (ext4_handle_valid(handle)) { 4736617ba13bSMingming Cao error = ext4_orphan_add(handle, inode); 47373d287de3SDmitry Monakhov orphan = 1; 47383d287de3SDmitry Monakhov } 4739617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 4740617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 4741ac27a0ecSDave Kleikamp if (!error) 4742ac27a0ecSDave Kleikamp error = rc; 4743617ba13bSMingming Cao ext4_journal_stop(handle); 4744678aaf48SJan Kara 4745678aaf48SJan Kara if (ext4_should_order_data(inode)) { 4746678aaf48SJan Kara error = ext4_begin_ordered_truncate(inode, 4747678aaf48SJan Kara attr->ia_size); 4748678aaf48SJan Kara if (error) { 4749678aaf48SJan Kara /* Do as much error cleanup as possible */ 47509924a92aSTheodore Ts'o handle = ext4_journal_start(inode, 47519924a92aSTheodore Ts'o EXT4_HT_INODE, 3); 4752678aaf48SJan Kara if (IS_ERR(handle)) { 4753678aaf48SJan Kara ext4_orphan_del(NULL, inode); 4754678aaf48SJan Kara goto err_out; 4755678aaf48SJan Kara } 4756678aaf48SJan Kara ext4_orphan_del(handle, inode); 47573d287de3SDmitry Monakhov orphan = 0; 4758678aaf48SJan Kara ext4_journal_stop(handle); 4759678aaf48SJan Kara goto err_out; 4760678aaf48SJan Kara } 4761678aaf48SJan Kara } 4762ac27a0ecSDave Kleikamp } 4763ac27a0ecSDave Kleikamp 4764072bd7eaSTheodore Ts'o if (attr->ia_valid & ATTR_SIZE) { 476553e87268SJan Kara if (attr->ia_size != inode->i_size) { 476653e87268SJan Kara loff_t oldsize = inode->i_size; 476753e87268SJan Kara 476853e87268SJan Kara i_size_write(inode, attr->ia_size); 476953e87268SJan Kara /* 477053e87268SJan Kara * Blocks are going to be removed from the inode. Wait 477153e87268SJan Kara * for dio in flight. Temporarily disable 477253e87268SJan Kara * dioread_nolock to prevent livelock. 477353e87268SJan Kara */ 47741b65007eSDmitry Monakhov if (orphan) { 477553e87268SJan Kara if (!ext4_should_journal_data(inode)) { 47761b65007eSDmitry Monakhov ext4_inode_block_unlocked_dio(inode); 47771c9114f9SDmitry Monakhov inode_dio_wait(inode); 47781b65007eSDmitry Monakhov ext4_inode_resume_unlocked_dio(inode); 477953e87268SJan Kara } else 478053e87268SJan Kara ext4_wait_for_tail_page_commit(inode); 47811b65007eSDmitry Monakhov } 478253e87268SJan Kara /* 478353e87268SJan Kara * Truncate pagecache after we've waited for commit 478453e87268SJan Kara * in data=journal mode to make pages freeable. 478553e87268SJan Kara */ 478653e87268SJan Kara truncate_pagecache(inode, oldsize, inode->i_size); 47871c9114f9SDmitry Monakhov } 4788072bd7eaSTheodore Ts'o ext4_truncate(inode); 4789072bd7eaSTheodore Ts'o } 4790ac27a0ecSDave Kleikamp 47911025774cSChristoph Hellwig if (!rc) { 47921025774cSChristoph Hellwig setattr_copy(inode, attr); 47931025774cSChristoph Hellwig mark_inode_dirty(inode); 47941025774cSChristoph Hellwig } 47951025774cSChristoph Hellwig 47961025774cSChristoph Hellwig /* 47971025774cSChristoph Hellwig * If the call to ext4_truncate failed to get a transaction handle at 47981025774cSChristoph Hellwig * all, we need to clean up the in-core orphan list manually. 47991025774cSChristoph Hellwig */ 48003d287de3SDmitry Monakhov if (orphan && inode->i_nlink) 4801617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 4802ac27a0ecSDave Kleikamp 4803ac27a0ecSDave Kleikamp if (!rc && (ia_valid & ATTR_MODE)) 4804617ba13bSMingming Cao rc = ext4_acl_chmod(inode); 4805ac27a0ecSDave Kleikamp 4806ac27a0ecSDave Kleikamp err_out: 4807617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 4808ac27a0ecSDave Kleikamp if (!error) 4809ac27a0ecSDave Kleikamp error = rc; 4810ac27a0ecSDave Kleikamp return error; 4811ac27a0ecSDave Kleikamp } 4812ac27a0ecSDave Kleikamp 48133e3398a0SMingming Cao int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry, 48143e3398a0SMingming Cao struct kstat *stat) 48153e3398a0SMingming Cao { 48163e3398a0SMingming Cao struct inode *inode; 48173e3398a0SMingming Cao unsigned long delalloc_blocks; 48183e3398a0SMingming Cao 48193e3398a0SMingming Cao inode = dentry->d_inode; 48203e3398a0SMingming Cao generic_fillattr(inode, stat); 48213e3398a0SMingming Cao 48223e3398a0SMingming Cao /* 48233e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 48243e3398a0SMingming Cao * otherwise in the case of system crash before the real block 48253e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 48263e3398a0SMingming Cao * on-disk file blocks. 48273e3398a0SMingming Cao * We always keep i_blocks updated together with real 48283e3398a0SMingming Cao * allocation. But to not confuse with user, stat 48293e3398a0SMingming Cao * will return the blocks that include the delayed allocation 48303e3398a0SMingming Cao * blocks for this file. 48313e3398a0SMingming Cao */ 483296607551STao Ma delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb), 483396607551STao Ma EXT4_I(inode)->i_reserved_data_blocks); 48343e3398a0SMingming Cao 48353e3398a0SMingming Cao stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9; 48363e3398a0SMingming Cao return 0; 48373e3398a0SMingming Cao } 4838ac27a0ecSDave Kleikamp 4839a02908f1SMingming Cao static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4840a02908f1SMingming Cao { 484112e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 48428bb2b247SAmir Goldstein return ext4_ind_trans_blocks(inode, nrblocks, chunk); 4843ac51d837STheodore Ts'o return ext4_ext_index_trans_blocks(inode, nrblocks, chunk); 4844a02908f1SMingming Cao } 4845ac51d837STheodore Ts'o 4846a02908f1SMingming Cao /* 4847a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 4848a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 4849a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 4850a02908f1SMingming Cao * 4851a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 48524907cb7bSAnatol Pomozov * different block groups too. If they are contiguous, with flexbg, 4853a02908f1SMingming Cao * they could still across block group boundary. 4854a02908f1SMingming Cao * 4855a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 4856a02908f1SMingming Cao */ 48571f109d5aSTheodore Ts'o static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4858a02908f1SMingming Cao { 48598df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 48608df9675fSTheodore Ts'o int gdpblocks; 4861a02908f1SMingming Cao int idxblocks; 4862a02908f1SMingming Cao int ret = 0; 4863a02908f1SMingming Cao 4864a02908f1SMingming Cao /* 4865a02908f1SMingming Cao * How many index blocks need to touch to modify nrblocks? 4866a02908f1SMingming Cao * The "Chunk" flag indicating whether the nrblocks is 4867a02908f1SMingming Cao * physically contiguous on disk 4868a02908f1SMingming Cao * 4869a02908f1SMingming Cao * For Direct IO and fallocate, they calls get_block to allocate 4870a02908f1SMingming Cao * one single extent at a time, so they could set the "Chunk" flag 4871a02908f1SMingming Cao */ 4872a02908f1SMingming Cao idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk); 4873a02908f1SMingming Cao 4874a02908f1SMingming Cao ret = idxblocks; 4875a02908f1SMingming Cao 4876a02908f1SMingming Cao /* 4877a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 4878a02908f1SMingming Cao * to account 4879a02908f1SMingming Cao */ 4880a02908f1SMingming Cao groups = idxblocks; 4881a02908f1SMingming Cao if (chunk) 4882a02908f1SMingming Cao groups += 1; 4883ac27a0ecSDave Kleikamp else 4884a02908f1SMingming Cao groups += nrblocks; 4885ac27a0ecSDave Kleikamp 4886a02908f1SMingming Cao gdpblocks = groups; 48878df9675fSTheodore Ts'o if (groups > ngroups) 48888df9675fSTheodore Ts'o groups = ngroups; 4889a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 4890a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 4891a02908f1SMingming Cao 4892a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 4893a02908f1SMingming Cao ret += groups + gdpblocks; 4894a02908f1SMingming Cao 4895a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 4896a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 4897ac27a0ecSDave Kleikamp 4898ac27a0ecSDave Kleikamp return ret; 4899ac27a0ecSDave Kleikamp } 4900ac27a0ecSDave Kleikamp 4901ac27a0ecSDave Kleikamp /* 490225985edcSLucas De Marchi * Calculate the total number of credits to reserve to fit 4903f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 4904f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 4905a02908f1SMingming Cao * 4906525f4ed8SMingming Cao * This could be called via ext4_write_begin() 4907a02908f1SMingming Cao * 4908525f4ed8SMingming Cao * We need to consider the worse case, when 4909a02908f1SMingming Cao * one new block per extent. 4910a02908f1SMingming Cao */ 4911a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 4912a02908f1SMingming Cao { 4913a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 4914a02908f1SMingming Cao int ret; 4915a02908f1SMingming Cao 4916a02908f1SMingming Cao ret = ext4_meta_trans_blocks(inode, bpp, 0); 4917a02908f1SMingming Cao 4918a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 4919a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 4920a02908f1SMingming Cao ret += bpp; 4921a02908f1SMingming Cao return ret; 4922a02908f1SMingming Cao } 4923f3bd1f3fSMingming Cao 4924f3bd1f3fSMingming Cao /* 4925f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 4926f3bd1f3fSMingming Cao * 4927f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 492879e83036SEric Sandeen * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks. 4929f3bd1f3fSMingming Cao * 4930f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 4931f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 4932f3bd1f3fSMingming Cao */ 4933f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 4934f3bd1f3fSMingming Cao { 4935f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 4936f3bd1f3fSMingming Cao } 4937f3bd1f3fSMingming Cao 4938a02908f1SMingming Cao /* 4939617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 4940ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 4941ac27a0ecSDave Kleikamp */ 4942617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 4943617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 4944ac27a0ecSDave Kleikamp { 4945ac27a0ecSDave Kleikamp int err = 0; 4946ac27a0ecSDave Kleikamp 4947c64db50eSTheodore Ts'o if (IS_I_VERSION(inode)) 494825ec56b5SJean Noel Cordenner inode_inc_iversion(inode); 494925ec56b5SJean Noel Cordenner 4950ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 4951ac27a0ecSDave Kleikamp get_bh(iloc->bh); 4952ac27a0ecSDave Kleikamp 4953dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 4954830156c7SFrank Mayhar err = ext4_do_update_inode(handle, inode, iloc); 4955ac27a0ecSDave Kleikamp put_bh(iloc->bh); 4956ac27a0ecSDave Kleikamp return err; 4957ac27a0ecSDave Kleikamp } 4958ac27a0ecSDave Kleikamp 4959ac27a0ecSDave Kleikamp /* 4960ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 4961ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 4962ac27a0ecSDave Kleikamp */ 4963ac27a0ecSDave Kleikamp 4964ac27a0ecSDave Kleikamp int 4965617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 4966617ba13bSMingming Cao struct ext4_iloc *iloc) 4967ac27a0ecSDave Kleikamp { 49680390131bSFrank Mayhar int err; 49690390131bSFrank Mayhar 4970617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 4971ac27a0ecSDave Kleikamp if (!err) { 4972ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 4973617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, iloc->bh); 4974ac27a0ecSDave Kleikamp if (err) { 4975ac27a0ecSDave Kleikamp brelse(iloc->bh); 4976ac27a0ecSDave Kleikamp iloc->bh = NULL; 4977ac27a0ecSDave Kleikamp } 4978ac27a0ecSDave Kleikamp } 4979617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4980ac27a0ecSDave Kleikamp return err; 4981ac27a0ecSDave Kleikamp } 4982ac27a0ecSDave Kleikamp 4983ac27a0ecSDave Kleikamp /* 49846dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 49856dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 49866dd4ee7cSKalpak Shah */ 49871d03ec98SAneesh Kumar K.V static int ext4_expand_extra_isize(struct inode *inode, 49881d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 49891d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 49901d03ec98SAneesh Kumar K.V handle_t *handle) 49916dd4ee7cSKalpak Shah { 49926dd4ee7cSKalpak Shah struct ext4_inode *raw_inode; 49936dd4ee7cSKalpak Shah struct ext4_xattr_ibody_header *header; 49946dd4ee7cSKalpak Shah 49956dd4ee7cSKalpak Shah if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) 49966dd4ee7cSKalpak Shah return 0; 49976dd4ee7cSKalpak Shah 49986dd4ee7cSKalpak Shah raw_inode = ext4_raw_inode(&iloc); 49996dd4ee7cSKalpak Shah 50006dd4ee7cSKalpak Shah header = IHDR(inode, raw_inode); 50016dd4ee7cSKalpak Shah 50026dd4ee7cSKalpak Shah /* No extended attributes present */ 500319f5fb7aSTheodore Ts'o if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) || 50046dd4ee7cSKalpak Shah header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 50056dd4ee7cSKalpak Shah memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0, 50066dd4ee7cSKalpak Shah new_extra_isize); 50076dd4ee7cSKalpak Shah EXT4_I(inode)->i_extra_isize = new_extra_isize; 50086dd4ee7cSKalpak Shah return 0; 50096dd4ee7cSKalpak Shah } 50106dd4ee7cSKalpak Shah 50116dd4ee7cSKalpak Shah /* try to expand with EAs present */ 50126dd4ee7cSKalpak Shah return ext4_expand_extra_isize_ea(inode, new_extra_isize, 50136dd4ee7cSKalpak Shah raw_inode, handle); 50146dd4ee7cSKalpak Shah } 50156dd4ee7cSKalpak Shah 50166dd4ee7cSKalpak Shah /* 5017ac27a0ecSDave Kleikamp * What we do here is to mark the in-core inode as clean with respect to inode 5018ac27a0ecSDave Kleikamp * dirtiness (it may still be data-dirty). 5019ac27a0ecSDave Kleikamp * This means that the in-core inode may be reaped by prune_icache 5020ac27a0ecSDave Kleikamp * without having to perform any I/O. This is a very good thing, 5021ac27a0ecSDave Kleikamp * because *any* task may call prune_icache - even ones which 5022ac27a0ecSDave Kleikamp * have a transaction open against a different journal. 5023ac27a0ecSDave Kleikamp * 5024ac27a0ecSDave Kleikamp * Is this cheating? Not really. Sure, we haven't written the 5025ac27a0ecSDave Kleikamp * inode out, but prune_icache isn't a user-visible syncing function. 5026ac27a0ecSDave Kleikamp * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 5027ac27a0ecSDave Kleikamp * we start and wait on commits. 5028ac27a0ecSDave Kleikamp */ 5029617ba13bSMingming Cao int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode) 5030ac27a0ecSDave Kleikamp { 5031617ba13bSMingming Cao struct ext4_iloc iloc; 50326dd4ee7cSKalpak Shah struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 50336dd4ee7cSKalpak Shah static unsigned int mnt_count; 50346dd4ee7cSKalpak Shah int err, ret; 5035ac27a0ecSDave Kleikamp 5036ac27a0ecSDave Kleikamp might_sleep(); 50377ff9c073STheodore Ts'o trace_ext4_mark_inode_dirty(inode, _RET_IP_); 5038617ba13bSMingming Cao err = ext4_reserve_inode_write(handle, inode, &iloc); 50390390131bSFrank Mayhar if (ext4_handle_valid(handle) && 50400390131bSFrank Mayhar EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize && 504119f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) { 50426dd4ee7cSKalpak Shah /* 50436dd4ee7cSKalpak Shah * We need extra buffer credits since we may write into EA block 50446dd4ee7cSKalpak Shah * with this same handle. If journal_extend fails, then it will 50456dd4ee7cSKalpak Shah * only result in a minor loss of functionality for that inode. 50466dd4ee7cSKalpak Shah * If this is felt to be critical, then e2fsck should be run to 50476dd4ee7cSKalpak Shah * force a large enough s_min_extra_isize. 50486dd4ee7cSKalpak Shah */ 50496dd4ee7cSKalpak Shah if ((jbd2_journal_extend(handle, 50506dd4ee7cSKalpak Shah EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) { 50516dd4ee7cSKalpak Shah ret = ext4_expand_extra_isize(inode, 50526dd4ee7cSKalpak Shah sbi->s_want_extra_isize, 50536dd4ee7cSKalpak Shah iloc, handle); 50546dd4ee7cSKalpak Shah if (ret) { 505519f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, 505619f5fb7aSTheodore Ts'o EXT4_STATE_NO_EXPAND); 5057c1bddad9SAneesh Kumar K.V if (mnt_count != 5058c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count)) { 505912062dddSEric Sandeen ext4_warning(inode->i_sb, 50606dd4ee7cSKalpak Shah "Unable to expand inode %lu. Delete" 50616dd4ee7cSKalpak Shah " some EAs or run e2fsck.", 50626dd4ee7cSKalpak Shah inode->i_ino); 5063c1bddad9SAneesh Kumar K.V mnt_count = 5064c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count); 50656dd4ee7cSKalpak Shah } 50666dd4ee7cSKalpak Shah } 50676dd4ee7cSKalpak Shah } 50686dd4ee7cSKalpak Shah } 5069ac27a0ecSDave Kleikamp if (!err) 5070617ba13bSMingming Cao err = ext4_mark_iloc_dirty(handle, inode, &iloc); 5071ac27a0ecSDave Kleikamp return err; 5072ac27a0ecSDave Kleikamp } 5073ac27a0ecSDave Kleikamp 5074ac27a0ecSDave Kleikamp /* 5075617ba13bSMingming Cao * ext4_dirty_inode() is called from __mark_inode_dirty() 5076ac27a0ecSDave Kleikamp * 5077ac27a0ecSDave Kleikamp * We're really interested in the case where a file is being extended. 5078ac27a0ecSDave Kleikamp * i_size has been changed by generic_commit_write() and we thus need 5079ac27a0ecSDave Kleikamp * to include the updated inode in the current transaction. 5080ac27a0ecSDave Kleikamp * 50815dd4056dSChristoph Hellwig * Also, dquot_alloc_block() will always dirty the inode when blocks 5082ac27a0ecSDave Kleikamp * are allocated to the file. 5083ac27a0ecSDave Kleikamp * 5084ac27a0ecSDave Kleikamp * If the inode is marked synchronous, we don't honour that here - doing 5085ac27a0ecSDave Kleikamp * so would cause a commit on atime updates, which we don't bother doing. 5086ac27a0ecSDave Kleikamp * We handle synchronous inodes at the highest possible level. 5087ac27a0ecSDave Kleikamp */ 5088aa385729SChristoph Hellwig void ext4_dirty_inode(struct inode *inode, int flags) 5089ac27a0ecSDave Kleikamp { 5090ac27a0ecSDave Kleikamp handle_t *handle; 5091ac27a0ecSDave Kleikamp 50929924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 2); 5093ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5094ac27a0ecSDave Kleikamp goto out; 5095f3dc272fSCurt Wohlgemuth 5096617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 5097f3dc272fSCurt Wohlgemuth 5098617ba13bSMingming Cao ext4_journal_stop(handle); 5099ac27a0ecSDave Kleikamp out: 5100ac27a0ecSDave Kleikamp return; 5101ac27a0ecSDave Kleikamp } 5102ac27a0ecSDave Kleikamp 5103ac27a0ecSDave Kleikamp #if 0 5104ac27a0ecSDave Kleikamp /* 5105ac27a0ecSDave Kleikamp * Bind an inode's backing buffer_head into this transaction, to prevent 5106ac27a0ecSDave Kleikamp * it from being flushed to disk early. Unlike 5107617ba13bSMingming Cao * ext4_reserve_inode_write, this leaves behind no bh reference and 5108ac27a0ecSDave Kleikamp * returns no iloc structure, so the caller needs to repeat the iloc 5109ac27a0ecSDave Kleikamp * lookup to mark the inode dirty later. 5110ac27a0ecSDave Kleikamp */ 5111617ba13bSMingming Cao static int ext4_pin_inode(handle_t *handle, struct inode *inode) 5112ac27a0ecSDave Kleikamp { 5113617ba13bSMingming Cao struct ext4_iloc iloc; 5114ac27a0ecSDave Kleikamp 5115ac27a0ecSDave Kleikamp int err = 0; 5116ac27a0ecSDave Kleikamp if (handle) { 5117617ba13bSMingming Cao err = ext4_get_inode_loc(inode, &iloc); 5118ac27a0ecSDave Kleikamp if (!err) { 5119ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc.bh, "get_write_access"); 5120dab291afSMingming Cao err = jbd2_journal_get_write_access(handle, iloc.bh); 5121ac27a0ecSDave Kleikamp if (!err) 51220390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, 512373b50c1cSCurt Wohlgemuth NULL, 5124ac27a0ecSDave Kleikamp iloc.bh); 5125ac27a0ecSDave Kleikamp brelse(iloc.bh); 5126ac27a0ecSDave Kleikamp } 5127ac27a0ecSDave Kleikamp } 5128617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5129ac27a0ecSDave Kleikamp return err; 5130ac27a0ecSDave Kleikamp } 5131ac27a0ecSDave Kleikamp #endif 5132ac27a0ecSDave Kleikamp 5133617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val) 5134ac27a0ecSDave Kleikamp { 5135ac27a0ecSDave Kleikamp journal_t *journal; 5136ac27a0ecSDave Kleikamp handle_t *handle; 5137ac27a0ecSDave Kleikamp int err; 5138ac27a0ecSDave Kleikamp 5139ac27a0ecSDave Kleikamp /* 5140ac27a0ecSDave Kleikamp * We have to be very careful here: changing a data block's 5141ac27a0ecSDave Kleikamp * journaling status dynamically is dangerous. If we write a 5142ac27a0ecSDave Kleikamp * data block to the journal, change the status and then delete 5143ac27a0ecSDave Kleikamp * that block, we risk forgetting to revoke the old log record 5144ac27a0ecSDave Kleikamp * from the journal and so a subsequent replay can corrupt data. 5145ac27a0ecSDave Kleikamp * So, first we make sure that the journal is empty and that 5146ac27a0ecSDave Kleikamp * nobody is changing anything. 5147ac27a0ecSDave Kleikamp */ 5148ac27a0ecSDave Kleikamp 5149617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 51500390131bSFrank Mayhar if (!journal) 51510390131bSFrank Mayhar return 0; 5152d699594dSDave Hansen if (is_journal_aborted(journal)) 5153ac27a0ecSDave Kleikamp return -EROFS; 51542aff57b0SYongqiang Yang /* We have to allocate physical blocks for delalloc blocks 51552aff57b0SYongqiang Yang * before flushing journal. otherwise delalloc blocks can not 51562aff57b0SYongqiang Yang * be allocated any more. even more truncate on delalloc blocks 51572aff57b0SYongqiang Yang * could trigger BUG by flushing delalloc blocks in journal. 51582aff57b0SYongqiang Yang * There is no delalloc block in non-journal data mode. 51592aff57b0SYongqiang Yang */ 51602aff57b0SYongqiang Yang if (val && test_opt(inode->i_sb, DELALLOC)) { 51612aff57b0SYongqiang Yang err = ext4_alloc_da_blocks(inode); 51622aff57b0SYongqiang Yang if (err < 0) 51632aff57b0SYongqiang Yang return err; 51642aff57b0SYongqiang Yang } 5165ac27a0ecSDave Kleikamp 516617335dccSDmitry Monakhov /* Wait for all existing dio workers */ 516717335dccSDmitry Monakhov ext4_inode_block_unlocked_dio(inode); 516817335dccSDmitry Monakhov inode_dio_wait(inode); 516917335dccSDmitry Monakhov 5170dab291afSMingming Cao jbd2_journal_lock_updates(journal); 5171ac27a0ecSDave Kleikamp 5172ac27a0ecSDave Kleikamp /* 5173ac27a0ecSDave Kleikamp * OK, there are no updates running now, and all cached data is 5174ac27a0ecSDave Kleikamp * synced to disk. We are now in a completely consistent state 5175ac27a0ecSDave Kleikamp * which doesn't have anything in the journal, and we know that 5176ac27a0ecSDave Kleikamp * no filesystem updates are running, so it is safe to modify 5177ac27a0ecSDave Kleikamp * the inode's in-core data-journaling state flag now. 5178ac27a0ecSDave Kleikamp */ 5179ac27a0ecSDave Kleikamp 5180ac27a0ecSDave Kleikamp if (val) 518112e9b892SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 51825872ddaaSYongqiang Yang else { 51835872ddaaSYongqiang Yang jbd2_journal_flush(journal); 518412e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 51855872ddaaSYongqiang Yang } 5186617ba13bSMingming Cao ext4_set_aops(inode); 5187ac27a0ecSDave Kleikamp 5188dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 518917335dccSDmitry Monakhov ext4_inode_resume_unlocked_dio(inode); 5190ac27a0ecSDave Kleikamp 5191ac27a0ecSDave Kleikamp /* Finally we can mark the inode as dirty. */ 5192ac27a0ecSDave Kleikamp 51939924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 5194ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5195ac27a0ecSDave Kleikamp return PTR_ERR(handle); 5196ac27a0ecSDave Kleikamp 5197617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 51980390131bSFrank Mayhar ext4_handle_sync(handle); 5199617ba13bSMingming Cao ext4_journal_stop(handle); 5200617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5201ac27a0ecSDave Kleikamp 5202ac27a0ecSDave Kleikamp return err; 5203ac27a0ecSDave Kleikamp } 52042e9ee850SAneesh Kumar K.V 52052e9ee850SAneesh Kumar K.V static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) 52062e9ee850SAneesh Kumar K.V { 52072e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 52082e9ee850SAneesh Kumar K.V } 52092e9ee850SAneesh Kumar K.V 5210c2ec175cSNick Piggin int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) 52112e9ee850SAneesh Kumar K.V { 5212c2ec175cSNick Piggin struct page *page = vmf->page; 52132e9ee850SAneesh Kumar K.V loff_t size; 52142e9ee850SAneesh Kumar K.V unsigned long len; 52159ea7df53SJan Kara int ret; 52162e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 5217496ad9aaSAl Viro struct inode *inode = file_inode(file); 52182e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 52199ea7df53SJan Kara handle_t *handle; 52209ea7df53SJan Kara get_block_t *get_block; 52219ea7df53SJan Kara int retries = 0; 52222e9ee850SAneesh Kumar K.V 52238e8ad8a5SJan Kara sb_start_pagefault(inode->i_sb); 5224041bbb6dSTheodore Ts'o file_update_time(vma->vm_file); 52259ea7df53SJan Kara /* Delalloc case is easy... */ 52269ea7df53SJan Kara if (test_opt(inode->i_sb, DELALLOC) && 52279ea7df53SJan Kara !ext4_should_journal_data(inode) && 52289ea7df53SJan Kara !ext4_nonda_switch(inode->i_sb)) { 52299ea7df53SJan Kara do { 52309ea7df53SJan Kara ret = __block_page_mkwrite(vma, vmf, 52319ea7df53SJan Kara ext4_da_get_block_prep); 52329ea7df53SJan Kara } while (ret == -ENOSPC && 52339ea7df53SJan Kara ext4_should_retry_alloc(inode->i_sb, &retries)); 52349ea7df53SJan Kara goto out_ret; 52352e9ee850SAneesh Kumar K.V } 52360e499890SDarrick J. Wong 52370e499890SDarrick J. Wong lock_page(page); 52389ea7df53SJan Kara size = i_size_read(inode); 52399ea7df53SJan Kara /* Page got truncated from under us? */ 52409ea7df53SJan Kara if (page->mapping != mapping || page_offset(page) > size) { 52419ea7df53SJan Kara unlock_page(page); 52429ea7df53SJan Kara ret = VM_FAULT_NOPAGE; 52439ea7df53SJan Kara goto out; 52440e499890SDarrick J. Wong } 52452e9ee850SAneesh Kumar K.V 52462e9ee850SAneesh Kumar K.V if (page->index == size >> PAGE_CACHE_SHIFT) 52472e9ee850SAneesh Kumar K.V len = size & ~PAGE_CACHE_MASK; 52482e9ee850SAneesh Kumar K.V else 52492e9ee850SAneesh Kumar K.V len = PAGE_CACHE_SIZE; 5250a827eaffSAneesh Kumar K.V /* 52519ea7df53SJan Kara * Return if we have all the buffers mapped. This avoids the need to do 52529ea7df53SJan Kara * journal_start/journal_stop which can block and take a long time 5253a827eaffSAneesh Kumar K.V */ 52542e9ee850SAneesh Kumar K.V if (page_has_buffers(page)) { 5255f19d5870STao Ma if (!ext4_walk_page_buffers(NULL, page_buffers(page), 5256f19d5870STao Ma 0, len, NULL, 5257a827eaffSAneesh Kumar K.V ext4_bh_unmapped)) { 52589ea7df53SJan Kara /* Wait so that we don't change page under IO */ 52591d1d1a76SDarrick J. Wong wait_for_stable_page(page); 52609ea7df53SJan Kara ret = VM_FAULT_LOCKED; 52619ea7df53SJan Kara goto out; 52622e9ee850SAneesh Kumar K.V } 5263a827eaffSAneesh Kumar K.V } 5264a827eaffSAneesh Kumar K.V unlock_page(page); 52659ea7df53SJan Kara /* OK, we need to fill the hole... */ 52669ea7df53SJan Kara if (ext4_should_dioread_nolock(inode)) 52679ea7df53SJan Kara get_block = ext4_get_block_write; 52689ea7df53SJan Kara else 52699ea7df53SJan Kara get_block = ext4_get_block; 52709ea7df53SJan Kara retry_alloc: 52719924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 52729924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 52739ea7df53SJan Kara if (IS_ERR(handle)) { 5274c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 52759ea7df53SJan Kara goto out; 52769ea7df53SJan Kara } 52779ea7df53SJan Kara ret = __block_page_mkwrite(vma, vmf, get_block); 52789ea7df53SJan Kara if (!ret && ext4_should_journal_data(inode)) { 5279f19d5870STao Ma if (ext4_walk_page_buffers(handle, page_buffers(page), 0, 52809ea7df53SJan Kara PAGE_CACHE_SIZE, NULL, do_journal_get_write_access)) { 52819ea7df53SJan Kara unlock_page(page); 52829ea7df53SJan Kara ret = VM_FAULT_SIGBUS; 5283fcbb5515SYongqiang Yang ext4_journal_stop(handle); 52849ea7df53SJan Kara goto out; 52859ea7df53SJan Kara } 52869ea7df53SJan Kara ext4_set_inode_state(inode, EXT4_STATE_JDATA); 52879ea7df53SJan Kara } 52889ea7df53SJan Kara ext4_journal_stop(handle); 52899ea7df53SJan Kara if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 52909ea7df53SJan Kara goto retry_alloc; 52919ea7df53SJan Kara out_ret: 52929ea7df53SJan Kara ret = block_page_mkwrite_return(ret); 52939ea7df53SJan Kara out: 52948e8ad8a5SJan Kara sb_end_pagefault(inode->i_sb); 52952e9ee850SAneesh Kumar K.V return ret; 52962e9ee850SAneesh Kumar K.V } 5297