1ac27a0ecSDave Kleikamp /* 2617ba13bSMingming Cao * linux/fs/ext4/inode.c 3ac27a0ecSDave Kleikamp * 4ac27a0ecSDave Kleikamp * Copyright (C) 1992, 1993, 1994, 1995 5ac27a0ecSDave Kleikamp * Remy Card (card@masi.ibp.fr) 6ac27a0ecSDave Kleikamp * Laboratoire MASI - Institut Blaise Pascal 7ac27a0ecSDave Kleikamp * Universite Pierre et Marie Curie (Paris VI) 8ac27a0ecSDave Kleikamp * 9ac27a0ecSDave Kleikamp * from 10ac27a0ecSDave Kleikamp * 11ac27a0ecSDave Kleikamp * linux/fs/minix/inode.c 12ac27a0ecSDave Kleikamp * 13ac27a0ecSDave Kleikamp * Copyright (C) 1991, 1992 Linus Torvalds 14ac27a0ecSDave Kleikamp * 15ac27a0ecSDave Kleikamp * 64-bit file support on 64-bit platforms by Jakub Jelinek 16ac27a0ecSDave Kleikamp * (jj@sunsite.ms.mff.cuni.cz) 17ac27a0ecSDave Kleikamp * 18617ba13bSMingming Cao * Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000 19ac27a0ecSDave Kleikamp */ 20ac27a0ecSDave Kleikamp 21ac27a0ecSDave Kleikamp #include <linux/fs.h> 22ac27a0ecSDave Kleikamp #include <linux/time.h> 23dab291afSMingming Cao #include <linux/jbd2.h> 24ac27a0ecSDave Kleikamp #include <linux/highuid.h> 25ac27a0ecSDave Kleikamp #include <linux/pagemap.h> 26ac27a0ecSDave Kleikamp #include <linux/quotaops.h> 27ac27a0ecSDave Kleikamp #include <linux/string.h> 28ac27a0ecSDave Kleikamp #include <linux/buffer_head.h> 29ac27a0ecSDave Kleikamp #include <linux/writeback.h> 3064769240SAlex Tomas #include <linux/pagevec.h> 31ac27a0ecSDave Kleikamp #include <linux/mpage.h> 32e83c1397SDuane Griffin #include <linux/namei.h> 33ac27a0ecSDave Kleikamp #include <linux/uio.h> 34ac27a0ecSDave Kleikamp #include <linux/bio.h> 354c0425ffSMingming Cao #include <linux/workqueue.h> 36744692dcSJiaying Zhang #include <linux/kernel.h> 376db26ffcSAndrew Morton #include <linux/printk.h> 385a0e3ad6STejun Heo #include <linux/slab.h> 39a8901d34STheodore Ts'o #include <linux/ratelimit.h> 409bffad1eSTheodore Ts'o 413dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 42ac27a0ecSDave Kleikamp #include "xattr.h" 43ac27a0ecSDave Kleikamp #include "acl.h" 449f125d64STheodore Ts'o #include "truncate.h" 45ac27a0ecSDave Kleikamp 469bffad1eSTheodore Ts'o #include <trace/events/ext4.h> 479bffad1eSTheodore Ts'o 48a1d6cc56SAneesh Kumar K.V #define MPAGE_DA_EXTENT_TAIL 0x01 49a1d6cc56SAneesh Kumar K.V 50814525f4SDarrick J. Wong static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw, 51814525f4SDarrick J. Wong struct ext4_inode_info *ei) 52814525f4SDarrick J. Wong { 53814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 54814525f4SDarrick J. Wong __u16 csum_lo; 55814525f4SDarrick J. Wong __u16 csum_hi = 0; 56814525f4SDarrick J. Wong __u32 csum; 57814525f4SDarrick J. Wong 58814525f4SDarrick J. Wong csum_lo = raw->i_checksum_lo; 59814525f4SDarrick J. Wong raw->i_checksum_lo = 0; 60814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 61814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) { 62814525f4SDarrick J. Wong csum_hi = raw->i_checksum_hi; 63814525f4SDarrick J. Wong raw->i_checksum_hi = 0; 64814525f4SDarrick J. Wong } 65814525f4SDarrick J. Wong 66814525f4SDarrick J. Wong csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw, 67814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)); 68814525f4SDarrick J. Wong 69814525f4SDarrick J. Wong raw->i_checksum_lo = csum_lo; 70814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 71814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 72814525f4SDarrick J. Wong raw->i_checksum_hi = csum_hi; 73814525f4SDarrick J. Wong 74814525f4SDarrick J. Wong return csum; 75814525f4SDarrick J. Wong } 76814525f4SDarrick J. Wong 77814525f4SDarrick J. Wong static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw, 78814525f4SDarrick J. Wong struct ext4_inode_info *ei) 79814525f4SDarrick J. Wong { 80814525f4SDarrick J. Wong __u32 provided, calculated; 81814525f4SDarrick J. Wong 82814525f4SDarrick J. Wong if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 83814525f4SDarrick J. Wong cpu_to_le32(EXT4_OS_LINUX) || 84814525f4SDarrick J. Wong !EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, 85814525f4SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) 86814525f4SDarrick J. Wong return 1; 87814525f4SDarrick J. Wong 88814525f4SDarrick J. Wong provided = le16_to_cpu(raw->i_checksum_lo); 89814525f4SDarrick J. Wong calculated = ext4_inode_csum(inode, raw, ei); 90814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 91814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 92814525f4SDarrick J. Wong provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16; 93814525f4SDarrick J. Wong else 94814525f4SDarrick J. Wong calculated &= 0xFFFF; 95814525f4SDarrick J. Wong 96814525f4SDarrick J. Wong return provided == calculated; 97814525f4SDarrick J. Wong } 98814525f4SDarrick J. Wong 99814525f4SDarrick J. Wong static void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw, 100814525f4SDarrick J. Wong struct ext4_inode_info *ei) 101814525f4SDarrick J. Wong { 102814525f4SDarrick J. Wong __u32 csum; 103814525f4SDarrick J. Wong 104814525f4SDarrick J. Wong if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 105814525f4SDarrick J. Wong cpu_to_le32(EXT4_OS_LINUX) || 106814525f4SDarrick J. Wong !EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, 107814525f4SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) 108814525f4SDarrick J. Wong return; 109814525f4SDarrick J. Wong 110814525f4SDarrick J. Wong csum = ext4_inode_csum(inode, raw, ei); 111814525f4SDarrick J. Wong raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF); 112814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 113814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 114814525f4SDarrick J. Wong raw->i_checksum_hi = cpu_to_le16(csum >> 16); 115814525f4SDarrick J. Wong } 116814525f4SDarrick J. Wong 117678aaf48SJan Kara static inline int ext4_begin_ordered_truncate(struct inode *inode, 118678aaf48SJan Kara loff_t new_size) 119678aaf48SJan Kara { 1207ff9c073STheodore Ts'o trace_ext4_begin_ordered_truncate(inode, new_size); 1218aefcd55STheodore Ts'o /* 1228aefcd55STheodore Ts'o * If jinode is zero, then we never opened the file for 1238aefcd55STheodore Ts'o * writing, so there's no need to call 1248aefcd55STheodore Ts'o * jbd2_journal_begin_ordered_truncate() since there's no 1258aefcd55STheodore Ts'o * outstanding writes we need to flush. 1268aefcd55STheodore Ts'o */ 1278aefcd55STheodore Ts'o if (!EXT4_I(inode)->jinode) 1288aefcd55STheodore Ts'o return 0; 1298aefcd55STheodore Ts'o return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode), 1308aefcd55STheodore Ts'o EXT4_I(inode)->jinode, 131678aaf48SJan Kara new_size); 132678aaf48SJan Kara } 133678aaf48SJan Kara 13464769240SAlex Tomas static void ext4_invalidatepage(struct page *page, unsigned long offset); 135cb20d518STheodore Ts'o static int __ext4_journalled_writepage(struct page *page, unsigned int len); 136cb20d518STheodore Ts'o static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh); 1375f163cc7SEric Sandeen static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle, 1385f163cc7SEric Sandeen struct inode *inode, struct page *page, loff_t from, 1395f163cc7SEric Sandeen loff_t length, int flags); 14064769240SAlex Tomas 141ac27a0ecSDave Kleikamp /* 142ac27a0ecSDave Kleikamp * Test whether an inode is a fast symlink. 143ac27a0ecSDave Kleikamp */ 144617ba13bSMingming Cao static int ext4_inode_is_fast_symlink(struct inode *inode) 145ac27a0ecSDave Kleikamp { 146617ba13bSMingming Cao int ea_blocks = EXT4_I(inode)->i_file_acl ? 147ac27a0ecSDave Kleikamp (inode->i_sb->s_blocksize >> 9) : 0; 148ac27a0ecSDave Kleikamp 149ac27a0ecSDave Kleikamp return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0); 150ac27a0ecSDave Kleikamp } 151ac27a0ecSDave Kleikamp 152ac27a0ecSDave Kleikamp /* 153ac27a0ecSDave Kleikamp * Restart the transaction associated with *handle. This does a commit, 154ac27a0ecSDave Kleikamp * so before we call here everything must be consistently dirtied against 155ac27a0ecSDave Kleikamp * this transaction. 156ac27a0ecSDave Kleikamp */ 157487caeefSJan Kara int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode, 158487caeefSJan Kara int nblocks) 159ac27a0ecSDave Kleikamp { 160487caeefSJan Kara int ret; 161487caeefSJan Kara 162487caeefSJan Kara /* 163e35fd660STheodore Ts'o * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this 164487caeefSJan Kara * moment, get_block can be called only for blocks inside i_size since 165487caeefSJan Kara * page cache has been already dropped and writes are blocked by 166487caeefSJan Kara * i_mutex. So we can safely drop the i_data_sem here. 167487caeefSJan Kara */ 1680390131bSFrank Mayhar BUG_ON(EXT4_JOURNAL(inode) == NULL); 169ac27a0ecSDave Kleikamp jbd_debug(2, "restarting handle %p\n", handle); 170487caeefSJan Kara up_write(&EXT4_I(inode)->i_data_sem); 1718e8eaabeSAmir Goldstein ret = ext4_journal_restart(handle, nblocks); 172487caeefSJan Kara down_write(&EXT4_I(inode)->i_data_sem); 173fa5d1113SAneesh Kumar K.V ext4_discard_preallocations(inode); 174487caeefSJan Kara 175487caeefSJan Kara return ret; 176ac27a0ecSDave Kleikamp } 177ac27a0ecSDave Kleikamp 178ac27a0ecSDave Kleikamp /* 179ac27a0ecSDave Kleikamp * Called at the last iput() if i_nlink is zero. 180ac27a0ecSDave Kleikamp */ 1810930fcc1SAl Viro void ext4_evict_inode(struct inode *inode) 182ac27a0ecSDave Kleikamp { 183ac27a0ecSDave Kleikamp handle_t *handle; 184bc965ab3STheodore Ts'o int err; 185ac27a0ecSDave Kleikamp 1867ff9c073STheodore Ts'o trace_ext4_evict_inode(inode); 1872581fdc8SJiaying Zhang 1882581fdc8SJiaying Zhang ext4_ioend_wait(inode); 1892581fdc8SJiaying Zhang 1900930fcc1SAl Viro if (inode->i_nlink) { 1912d859db3SJan Kara /* 1922d859db3SJan Kara * When journalling data dirty buffers are tracked only in the 1932d859db3SJan Kara * journal. So although mm thinks everything is clean and 1942d859db3SJan Kara * ready for reaping the inode might still have some pages to 1952d859db3SJan Kara * write in the running transaction or waiting to be 1962d859db3SJan Kara * checkpointed. Thus calling jbd2_journal_invalidatepage() 1972d859db3SJan Kara * (via truncate_inode_pages()) to discard these buffers can 1982d859db3SJan Kara * cause data loss. Also even if we did not discard these 1992d859db3SJan Kara * buffers, we would have no way to find them after the inode 2002d859db3SJan Kara * is reaped and thus user could see stale data if he tries to 2012d859db3SJan Kara * read them before the transaction is checkpointed. So be 2022d859db3SJan Kara * careful and force everything to disk here... We use 2032d859db3SJan Kara * ei->i_datasync_tid to store the newest transaction 2042d859db3SJan Kara * containing inode's data. 2052d859db3SJan Kara * 2062d859db3SJan Kara * Note that directories do not have this problem because they 2072d859db3SJan Kara * don't use page cache. 2082d859db3SJan Kara */ 2092d859db3SJan Kara if (ext4_should_journal_data(inode) && 2102d859db3SJan Kara (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode))) { 2112d859db3SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 2122d859db3SJan Kara tid_t commit_tid = EXT4_I(inode)->i_datasync_tid; 2132d859db3SJan Kara 2142d859db3SJan Kara jbd2_log_start_commit(journal, commit_tid); 2152d859db3SJan Kara jbd2_log_wait_commit(journal, commit_tid); 2162d859db3SJan Kara filemap_write_and_wait(&inode->i_data); 2172d859db3SJan Kara } 2180930fcc1SAl Viro truncate_inode_pages(&inode->i_data, 0); 2190930fcc1SAl Viro goto no_delete; 2200930fcc1SAl Viro } 2210930fcc1SAl Viro 222907f4554SChristoph Hellwig if (!is_bad_inode(inode)) 223871a2931SChristoph Hellwig dquot_initialize(inode); 224907f4554SChristoph Hellwig 225678aaf48SJan Kara if (ext4_should_order_data(inode)) 226678aaf48SJan Kara ext4_begin_ordered_truncate(inode, 0); 227ac27a0ecSDave Kleikamp truncate_inode_pages(&inode->i_data, 0); 228ac27a0ecSDave Kleikamp 229ac27a0ecSDave Kleikamp if (is_bad_inode(inode)) 230ac27a0ecSDave Kleikamp goto no_delete; 231ac27a0ecSDave Kleikamp 2328e8ad8a5SJan Kara /* 2338e8ad8a5SJan Kara * Protect us against freezing - iput() caller didn't have to have any 2348e8ad8a5SJan Kara * protection against it 2358e8ad8a5SJan Kara */ 2368e8ad8a5SJan Kara sb_start_intwrite(inode->i_sb); 2379924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, 2389924a92aSTheodore Ts'o ext4_blocks_for_truncate(inode)+3); 239ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 240bc965ab3STheodore Ts'o ext4_std_error(inode->i_sb, PTR_ERR(handle)); 241ac27a0ecSDave Kleikamp /* 242ac27a0ecSDave Kleikamp * If we're going to skip the normal cleanup, we still need to 243ac27a0ecSDave Kleikamp * make sure that the in-core orphan linked list is properly 244ac27a0ecSDave Kleikamp * cleaned up. 245ac27a0ecSDave Kleikamp */ 246617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 2478e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 248ac27a0ecSDave Kleikamp goto no_delete; 249ac27a0ecSDave Kleikamp } 250ac27a0ecSDave Kleikamp 251ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 2520390131bSFrank Mayhar ext4_handle_sync(handle); 253ac27a0ecSDave Kleikamp inode->i_size = 0; 254bc965ab3STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 255bc965ab3STheodore Ts'o if (err) { 25612062dddSEric Sandeen ext4_warning(inode->i_sb, 257bc965ab3STheodore Ts'o "couldn't mark inode dirty (err %d)", err); 258bc965ab3STheodore Ts'o goto stop_handle; 259bc965ab3STheodore Ts'o } 260ac27a0ecSDave Kleikamp if (inode->i_blocks) 261617ba13bSMingming Cao ext4_truncate(inode); 262bc965ab3STheodore Ts'o 263bc965ab3STheodore Ts'o /* 264bc965ab3STheodore Ts'o * ext4_ext_truncate() doesn't reserve any slop when it 265bc965ab3STheodore Ts'o * restarts journal transactions; therefore there may not be 266bc965ab3STheodore Ts'o * enough credits left in the handle to remove the inode from 267bc965ab3STheodore Ts'o * the orphan list and set the dtime field. 268bc965ab3STheodore Ts'o */ 2690390131bSFrank Mayhar if (!ext4_handle_has_enough_credits(handle, 3)) { 270bc965ab3STheodore Ts'o err = ext4_journal_extend(handle, 3); 271bc965ab3STheodore Ts'o if (err > 0) 272bc965ab3STheodore Ts'o err = ext4_journal_restart(handle, 3); 273bc965ab3STheodore Ts'o if (err != 0) { 27412062dddSEric Sandeen ext4_warning(inode->i_sb, 275bc965ab3STheodore Ts'o "couldn't extend journal (err %d)", err); 276bc965ab3STheodore Ts'o stop_handle: 277bc965ab3STheodore Ts'o ext4_journal_stop(handle); 27845388219STheodore Ts'o ext4_orphan_del(NULL, inode); 2798e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 280bc965ab3STheodore Ts'o goto no_delete; 281bc965ab3STheodore Ts'o } 282bc965ab3STheodore Ts'o } 283bc965ab3STheodore Ts'o 284ac27a0ecSDave Kleikamp /* 285617ba13bSMingming Cao * Kill off the orphan record which ext4_truncate created. 286ac27a0ecSDave Kleikamp * AKPM: I think this can be inside the above `if'. 287617ba13bSMingming Cao * Note that ext4_orphan_del() has to be able to cope with the 288ac27a0ecSDave Kleikamp * deletion of a non-existent orphan - this is because we don't 289617ba13bSMingming Cao * know if ext4_truncate() actually created an orphan record. 290ac27a0ecSDave Kleikamp * (Well, we could do this if we need to, but heck - it works) 291ac27a0ecSDave Kleikamp */ 292617ba13bSMingming Cao ext4_orphan_del(handle, inode); 293617ba13bSMingming Cao EXT4_I(inode)->i_dtime = get_seconds(); 294ac27a0ecSDave Kleikamp 295ac27a0ecSDave Kleikamp /* 296ac27a0ecSDave Kleikamp * One subtle ordering requirement: if anything has gone wrong 297ac27a0ecSDave Kleikamp * (transaction abort, IO errors, whatever), then we can still 298ac27a0ecSDave Kleikamp * do these next steps (the fs will already have been marked as 299ac27a0ecSDave Kleikamp * having errors), but we can't free the inode if the mark_dirty 300ac27a0ecSDave Kleikamp * fails. 301ac27a0ecSDave Kleikamp */ 302617ba13bSMingming Cao if (ext4_mark_inode_dirty(handle, inode)) 303ac27a0ecSDave Kleikamp /* If that failed, just do the required in-core inode clear. */ 3040930fcc1SAl Viro ext4_clear_inode(inode); 305ac27a0ecSDave Kleikamp else 306617ba13bSMingming Cao ext4_free_inode(handle, inode); 307617ba13bSMingming Cao ext4_journal_stop(handle); 3088e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 309ac27a0ecSDave Kleikamp return; 310ac27a0ecSDave Kleikamp no_delete: 3110930fcc1SAl Viro ext4_clear_inode(inode); /* We must guarantee clearing of inode... */ 312ac27a0ecSDave Kleikamp } 313ac27a0ecSDave Kleikamp 314a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 315a9e7f447SDmitry Monakhov qsize_t *ext4_get_reserved_space(struct inode *inode) 31660e58e0fSMingming Cao { 317a9e7f447SDmitry Monakhov return &EXT4_I(inode)->i_reserved_quota; 31860e58e0fSMingming Cao } 319a9e7f447SDmitry Monakhov #endif 3209d0be502STheodore Ts'o 32112219aeaSAneesh Kumar K.V /* 32212219aeaSAneesh Kumar K.V * Calculate the number of metadata blocks need to reserve 3239d0be502STheodore Ts'o * to allocate a block located at @lblock 32412219aeaSAneesh Kumar K.V */ 32501f49d0bSTheodore Ts'o static int ext4_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock) 32612219aeaSAneesh Kumar K.V { 32712e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3289d0be502STheodore Ts'o return ext4_ext_calc_metadata_amount(inode, lblock); 32912219aeaSAneesh Kumar K.V 3308bb2b247SAmir Goldstein return ext4_ind_calc_metadata_amount(inode, lblock); 33112219aeaSAneesh Kumar K.V } 33212219aeaSAneesh Kumar K.V 3330637c6f4STheodore Ts'o /* 3340637c6f4STheodore Ts'o * Called with i_data_sem down, which is important since we can call 3350637c6f4STheodore Ts'o * ext4_discard_preallocations() from here. 3360637c6f4STheodore Ts'o */ 3375f634d06SAneesh Kumar K.V void ext4_da_update_reserve_space(struct inode *inode, 3385f634d06SAneesh Kumar K.V int used, int quota_claim) 33912219aeaSAneesh Kumar K.V { 34012219aeaSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3410637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 34212219aeaSAneesh Kumar K.V 3430637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 344d8990240SAditya Kali trace_ext4_da_update_reserve_space(inode, used, quota_claim); 3450637c6f4STheodore Ts'o if (unlikely(used > ei->i_reserved_data_blocks)) { 3468de5c325STheodore Ts'o ext4_warning(inode->i_sb, "%s: ino %lu, used %d " 3471084f252STheodore Ts'o "with only %d reserved data blocks", 3480637c6f4STheodore Ts'o __func__, inode->i_ino, used, 3490637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 3500637c6f4STheodore Ts'o WARN_ON(1); 3510637c6f4STheodore Ts'o used = ei->i_reserved_data_blocks; 3526bc6e63fSAneesh Kumar K.V } 35312219aeaSAneesh Kumar K.V 35497795d2aSBrian Foster if (unlikely(ei->i_allocated_meta_blocks > ei->i_reserved_meta_blocks)) { 35501a523ebSTheodore Ts'o ext4_warning(inode->i_sb, "ino %lu, allocated %d " 35601a523ebSTheodore Ts'o "with only %d reserved metadata blocks " 35701a523ebSTheodore Ts'o "(releasing %d blocks with reserved %d data blocks)", 35897795d2aSBrian Foster inode->i_ino, ei->i_allocated_meta_blocks, 35901a523ebSTheodore Ts'o ei->i_reserved_meta_blocks, used, 36001a523ebSTheodore Ts'o ei->i_reserved_data_blocks); 36197795d2aSBrian Foster WARN_ON(1); 36297795d2aSBrian Foster ei->i_allocated_meta_blocks = ei->i_reserved_meta_blocks; 36397795d2aSBrian Foster } 36497795d2aSBrian Foster 3650637c6f4STheodore Ts'o /* Update per-inode reservations */ 3660637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= used; 3670637c6f4STheodore Ts'o ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks; 36857042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 36972b8ab9dSEric Sandeen used + ei->i_allocated_meta_blocks); 3700637c6f4STheodore Ts'o ei->i_allocated_meta_blocks = 0; 3710637c6f4STheodore Ts'o 3720637c6f4STheodore Ts'o if (ei->i_reserved_data_blocks == 0) { 3730637c6f4STheodore Ts'o /* 3740637c6f4STheodore Ts'o * We can release all of the reserved metadata blocks 3750637c6f4STheodore Ts'o * only when we have written all of the delayed 3760637c6f4STheodore Ts'o * allocation blocks. 3770637c6f4STheodore Ts'o */ 37857042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 37972b8ab9dSEric Sandeen ei->i_reserved_meta_blocks); 380ee5f4d9cSTheodore Ts'o ei->i_reserved_meta_blocks = 0; 3819d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 3820637c6f4STheodore Ts'o } 38312219aeaSAneesh Kumar K.V spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 38460e58e0fSMingming Cao 38572b8ab9dSEric Sandeen /* Update quota subsystem for data blocks */ 38672b8ab9dSEric Sandeen if (quota_claim) 3877b415bf6SAditya Kali dquot_claim_block(inode, EXT4_C2B(sbi, used)); 38872b8ab9dSEric Sandeen else { 3895f634d06SAneesh Kumar K.V /* 3905f634d06SAneesh Kumar K.V * We did fallocate with an offset that is already delayed 3915f634d06SAneesh Kumar K.V * allocated. So on delayed allocated writeback we should 39272b8ab9dSEric Sandeen * not re-claim the quota for fallocated blocks. 3935f634d06SAneesh Kumar K.V */ 3947b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, used)); 3955f634d06SAneesh Kumar K.V } 396d6014301SAneesh Kumar K.V 397d6014301SAneesh Kumar K.V /* 398d6014301SAneesh Kumar K.V * If we have done all the pending block allocations and if 399d6014301SAneesh Kumar K.V * there aren't any writers on the inode, we can discard the 400d6014301SAneesh Kumar K.V * inode's preallocations. 401d6014301SAneesh Kumar K.V */ 4020637c6f4STheodore Ts'o if ((ei->i_reserved_data_blocks == 0) && 4030637c6f4STheodore Ts'o (atomic_read(&inode->i_writecount) == 0)) 404d6014301SAneesh Kumar K.V ext4_discard_preallocations(inode); 40512219aeaSAneesh Kumar K.V } 40612219aeaSAneesh Kumar K.V 407e29136f8STheodore Ts'o static int __check_block_validity(struct inode *inode, const char *func, 408c398eda0STheodore Ts'o unsigned int line, 40924676da4STheodore Ts'o struct ext4_map_blocks *map) 4106fd058f7STheodore Ts'o { 41124676da4STheodore Ts'o if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk, 41224676da4STheodore Ts'o map->m_len)) { 413c398eda0STheodore Ts'o ext4_error_inode(inode, func, line, map->m_pblk, 414c398eda0STheodore Ts'o "lblock %lu mapped to illegal pblock " 41524676da4STheodore Ts'o "(length %d)", (unsigned long) map->m_lblk, 416c398eda0STheodore Ts'o map->m_len); 4176fd058f7STheodore Ts'o return -EIO; 4186fd058f7STheodore Ts'o } 4196fd058f7STheodore Ts'o return 0; 4206fd058f7STheodore Ts'o } 4216fd058f7STheodore Ts'o 422e29136f8STheodore Ts'o #define check_block_validity(inode, map) \ 423c398eda0STheodore Ts'o __check_block_validity((inode), __func__, __LINE__, (map)) 424e29136f8STheodore Ts'o 425f5ab0d1fSMingming Cao /* 4261f94533dSTheodore Ts'o * Return the number of contiguous dirty pages in a given inode 4271f94533dSTheodore Ts'o * starting at page frame idx. 42855138e0bSTheodore Ts'o */ 42955138e0bSTheodore Ts'o static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx, 43055138e0bSTheodore Ts'o unsigned int max_pages) 43155138e0bSTheodore Ts'o { 43255138e0bSTheodore Ts'o struct address_space *mapping = inode->i_mapping; 43355138e0bSTheodore Ts'o pgoff_t index; 43455138e0bSTheodore Ts'o struct pagevec pvec; 43555138e0bSTheodore Ts'o pgoff_t num = 0; 43655138e0bSTheodore Ts'o int i, nr_pages, done = 0; 43755138e0bSTheodore Ts'o 43855138e0bSTheodore Ts'o if (max_pages == 0) 43955138e0bSTheodore Ts'o return 0; 44055138e0bSTheodore Ts'o pagevec_init(&pvec, 0); 44155138e0bSTheodore Ts'o while (!done) { 44255138e0bSTheodore Ts'o index = idx; 44355138e0bSTheodore Ts'o nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 44455138e0bSTheodore Ts'o PAGECACHE_TAG_DIRTY, 44555138e0bSTheodore Ts'o (pgoff_t)PAGEVEC_SIZE); 44655138e0bSTheodore Ts'o if (nr_pages == 0) 44755138e0bSTheodore Ts'o break; 44855138e0bSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 44955138e0bSTheodore Ts'o struct page *page = pvec.pages[i]; 45055138e0bSTheodore Ts'o struct buffer_head *bh, *head; 45155138e0bSTheodore Ts'o 45255138e0bSTheodore Ts'o lock_page(page); 45355138e0bSTheodore Ts'o if (unlikely(page->mapping != mapping) || 45455138e0bSTheodore Ts'o !PageDirty(page) || 45555138e0bSTheodore Ts'o PageWriteback(page) || 45655138e0bSTheodore Ts'o page->index != idx) { 45755138e0bSTheodore Ts'o done = 1; 45855138e0bSTheodore Ts'o unlock_page(page); 45955138e0bSTheodore Ts'o break; 46055138e0bSTheodore Ts'o } 4611f94533dSTheodore Ts'o if (page_has_buffers(page)) { 4621f94533dSTheodore Ts'o bh = head = page_buffers(page); 46355138e0bSTheodore Ts'o do { 46455138e0bSTheodore Ts'o if (!buffer_delay(bh) && 4651f94533dSTheodore Ts'o !buffer_unwritten(bh)) 46655138e0bSTheodore Ts'o done = 1; 4671f94533dSTheodore Ts'o bh = bh->b_this_page; 4681f94533dSTheodore Ts'o } while (!done && (bh != head)); 46955138e0bSTheodore Ts'o } 47055138e0bSTheodore Ts'o unlock_page(page); 47155138e0bSTheodore Ts'o if (done) 47255138e0bSTheodore Ts'o break; 47355138e0bSTheodore Ts'o idx++; 47455138e0bSTheodore Ts'o num++; 475659c6009SEric Sandeen if (num >= max_pages) { 476659c6009SEric Sandeen done = 1; 47755138e0bSTheodore Ts'o break; 47855138e0bSTheodore Ts'o } 479659c6009SEric Sandeen } 48055138e0bSTheodore Ts'o pagevec_release(&pvec); 48155138e0bSTheodore Ts'o } 48255138e0bSTheodore Ts'o return num; 48355138e0bSTheodore Ts'o } 48455138e0bSTheodore Ts'o 48555138e0bSTheodore Ts'o /* 486e35fd660STheodore Ts'o * The ext4_map_blocks() function tries to look up the requested blocks, 4872b2d6d01STheodore Ts'o * and returns if the blocks are already mapped. 488f5ab0d1fSMingming Cao * 489f5ab0d1fSMingming Cao * Otherwise it takes the write lock of the i_data_sem and allocate blocks 490f5ab0d1fSMingming Cao * and store the allocated blocks in the result buffer head and mark it 491f5ab0d1fSMingming Cao * mapped. 492f5ab0d1fSMingming Cao * 493e35fd660STheodore Ts'o * If file type is extents based, it will call ext4_ext_map_blocks(), 494e35fd660STheodore Ts'o * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping 495f5ab0d1fSMingming Cao * based files 496f5ab0d1fSMingming Cao * 497f5ab0d1fSMingming Cao * On success, it returns the number of blocks being mapped or allocate. 498f5ab0d1fSMingming Cao * if create==0 and the blocks are pre-allocated and uninitialized block, 499f5ab0d1fSMingming Cao * the result buffer head is unmapped. If the create ==1, it will make sure 500f5ab0d1fSMingming Cao * the buffer head is mapped. 501f5ab0d1fSMingming Cao * 502f5ab0d1fSMingming Cao * It returns 0 if plain look up failed (blocks have not been allocated), in 503df3ab170STao Ma * that case, buffer head is unmapped 504f5ab0d1fSMingming Cao * 505f5ab0d1fSMingming Cao * It returns the error in case of allocation failure. 506f5ab0d1fSMingming Cao */ 507e35fd660STheodore Ts'o int ext4_map_blocks(handle_t *handle, struct inode *inode, 508e35fd660STheodore Ts'o struct ext4_map_blocks *map, int flags) 5090e855ac8SAneesh Kumar K.V { 5100e855ac8SAneesh Kumar K.V int retval; 511f5ab0d1fSMingming Cao 512e35fd660STheodore Ts'o map->m_flags = 0; 513e35fd660STheodore Ts'o ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u," 514e35fd660STheodore Ts'o "logical block %lu\n", inode->i_ino, flags, map->m_len, 515e35fd660STheodore Ts'o (unsigned long) map->m_lblk); 5164df3d265SAneesh Kumar K.V /* 517b920c755STheodore Ts'o * Try to see if we can get the block without requesting a new 518b920c755STheodore Ts'o * file system block. 5194df3d265SAneesh Kumar K.V */ 520729f52c6SZheng Liu if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 5210e855ac8SAneesh Kumar K.V down_read((&EXT4_I(inode)->i_data_sem)); 52212e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 523a4e5d88bSDmitry Monakhov retval = ext4_ext_map_blocks(handle, inode, map, flags & 524a4e5d88bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 5254df3d265SAneesh Kumar K.V } else { 526a4e5d88bSDmitry Monakhov retval = ext4_ind_map_blocks(handle, inode, map, flags & 527a4e5d88bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 5280e855ac8SAneesh Kumar K.V } 529729f52c6SZheng Liu if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 5304df3d265SAneesh Kumar K.V up_read((&EXT4_I(inode)->i_data_sem)); 531f5ab0d1fSMingming Cao 532e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 53351865fdaSZheng Liu int ret; 53451865fdaSZheng Liu if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) { 53551865fdaSZheng Liu /* delayed alloc may be allocated by fallocate and 53651865fdaSZheng Liu * coverted to initialized by directIO. 53751865fdaSZheng Liu * we need to handle delayed extent here. 53851865fdaSZheng Liu */ 53951865fdaSZheng Liu down_write((&EXT4_I(inode)->i_data_sem)); 54051865fdaSZheng Liu goto delayed_mapped; 54151865fdaSZheng Liu } 54251865fdaSZheng Liu ret = check_block_validity(inode, map); 5436fd058f7STheodore Ts'o if (ret != 0) 5446fd058f7STheodore Ts'o return ret; 5456fd058f7STheodore Ts'o } 5466fd058f7STheodore Ts'o 547f5ab0d1fSMingming Cao /* If it is only a block(s) look up */ 548c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 5494df3d265SAneesh Kumar K.V return retval; 5504df3d265SAneesh Kumar K.V 5514df3d265SAneesh Kumar K.V /* 552f5ab0d1fSMingming Cao * Returns if the blocks have already allocated 553f5ab0d1fSMingming Cao * 554f5ab0d1fSMingming Cao * Note that if blocks have been preallocated 555df3ab170STao Ma * ext4_ext_get_block() returns the create = 0 556f5ab0d1fSMingming Cao * with buffer head unmapped. 557f5ab0d1fSMingming Cao */ 558e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) 559f5ab0d1fSMingming Cao return retval; 560f5ab0d1fSMingming Cao 561f5ab0d1fSMingming Cao /* 5622a8964d6SAneesh Kumar K.V * When we call get_blocks without the create flag, the 5632a8964d6SAneesh Kumar K.V * BH_Unwritten flag could have gotten set if the blocks 5642a8964d6SAneesh Kumar K.V * requested were part of a uninitialized extent. We need to 5652a8964d6SAneesh Kumar K.V * clear this flag now that we are committed to convert all or 5662a8964d6SAneesh Kumar K.V * part of the uninitialized extent to be an initialized 5672a8964d6SAneesh Kumar K.V * extent. This is because we need to avoid the combination 5682a8964d6SAneesh Kumar K.V * of BH_Unwritten and BH_Mapped flags being simultaneously 5692a8964d6SAneesh Kumar K.V * set on the buffer_head. 5702a8964d6SAneesh Kumar K.V */ 571e35fd660STheodore Ts'o map->m_flags &= ~EXT4_MAP_UNWRITTEN; 5722a8964d6SAneesh Kumar K.V 5732a8964d6SAneesh Kumar K.V /* 574f5ab0d1fSMingming Cao * New blocks allocate and/or writing to uninitialized extent 575f5ab0d1fSMingming Cao * will possibly result in updating i_data, so we take 576f5ab0d1fSMingming Cao * the write lock of i_data_sem, and call get_blocks() 577f5ab0d1fSMingming Cao * with create == 1 flag. 5784df3d265SAneesh Kumar K.V */ 5794df3d265SAneesh Kumar K.V down_write((&EXT4_I(inode)->i_data_sem)); 580d2a17637SMingming Cao 581d2a17637SMingming Cao /* 582d2a17637SMingming Cao * if the caller is from delayed allocation writeout path 583d2a17637SMingming Cao * we have already reserved fs blocks for allocation 584d2a17637SMingming Cao * let the underlying get_block() function know to 585d2a17637SMingming Cao * avoid double accounting 586d2a17637SMingming Cao */ 587c2177057STheodore Ts'o if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 588f2321097STheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED); 5894df3d265SAneesh Kumar K.V /* 5904df3d265SAneesh Kumar K.V * We need to check for EXT4 here because migrate 5914df3d265SAneesh Kumar K.V * could have changed the inode type in between 5924df3d265SAneesh Kumar K.V */ 59312e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 594e35fd660STheodore Ts'o retval = ext4_ext_map_blocks(handle, inode, map, flags); 5950e855ac8SAneesh Kumar K.V } else { 596e35fd660STheodore Ts'o retval = ext4_ind_map_blocks(handle, inode, map, flags); 597267e4db9SAneesh Kumar K.V 598e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_NEW) { 599267e4db9SAneesh Kumar K.V /* 600267e4db9SAneesh Kumar K.V * We allocated new blocks which will result in 601267e4db9SAneesh Kumar K.V * i_data's format changing. Force the migrate 602267e4db9SAneesh Kumar K.V * to fail by clearing migrate flags 603267e4db9SAneesh Kumar K.V */ 60419f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE); 605267e4db9SAneesh Kumar K.V } 6062ac3b6e0STheodore Ts'o 607d2a17637SMingming Cao /* 6082ac3b6e0STheodore Ts'o * Update reserved blocks/metadata blocks after successful 6095f634d06SAneesh Kumar K.V * block allocation which had been deferred till now. We don't 6105f634d06SAneesh Kumar K.V * support fallocate for non extent files. So we can update 6115f634d06SAneesh Kumar K.V * reserve space here. 612d2a17637SMingming Cao */ 6135f634d06SAneesh Kumar K.V if ((retval > 0) && 6141296cc85SAneesh Kumar K.V (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)) 6155f634d06SAneesh Kumar K.V ext4_da_update_reserve_space(inode, retval, 1); 6165f634d06SAneesh Kumar K.V } 6175356f261SAditya Kali if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) { 618f2321097STheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED); 619d2a17637SMingming Cao 62051865fdaSZheng Liu if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 62151865fdaSZheng Liu int ret; 62251865fdaSZheng Liu delayed_mapped: 62351865fdaSZheng Liu /* delayed allocation blocks has been allocated */ 62451865fdaSZheng Liu ret = ext4_es_remove_extent(inode, map->m_lblk, 62551865fdaSZheng Liu map->m_len); 62651865fdaSZheng Liu if (ret < 0) 62751865fdaSZheng Liu retval = ret; 62851865fdaSZheng Liu } 6295356f261SAditya Kali } 6305356f261SAditya Kali 6310e855ac8SAneesh Kumar K.V up_write((&EXT4_I(inode)->i_data_sem)); 632e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 633e29136f8STheodore Ts'o int ret = check_block_validity(inode, map); 6346fd058f7STheodore Ts'o if (ret != 0) 6356fd058f7STheodore Ts'o return ret; 6366fd058f7STheodore Ts'o } 6370e855ac8SAneesh Kumar K.V return retval; 6380e855ac8SAneesh Kumar K.V } 6390e855ac8SAneesh Kumar K.V 640f3bd1f3fSMingming Cao /* Maximum number of blocks we map for direct IO at once. */ 641f3bd1f3fSMingming Cao #define DIO_MAX_BLOCKS 4096 642f3bd1f3fSMingming Cao 6432ed88685STheodore Ts'o static int _ext4_get_block(struct inode *inode, sector_t iblock, 6442ed88685STheodore Ts'o struct buffer_head *bh, int flags) 645ac27a0ecSDave Kleikamp { 6463e4fdaf8SDmitriy Monakhov handle_t *handle = ext4_journal_current_handle(); 6472ed88685STheodore Ts'o struct ext4_map_blocks map; 6487fb5409dSJan Kara int ret = 0, started = 0; 649f3bd1f3fSMingming Cao int dio_credits; 650ac27a0ecSDave Kleikamp 65146c7f254STao Ma if (ext4_has_inline_data(inode)) 65246c7f254STao Ma return -ERANGE; 65346c7f254STao Ma 6542ed88685STheodore Ts'o map.m_lblk = iblock; 6552ed88685STheodore Ts'o map.m_len = bh->b_size >> inode->i_blkbits; 6562ed88685STheodore Ts'o 6578b0f165fSAnatol Pomozov if (flags && !(flags & EXT4_GET_BLOCKS_NO_LOCK) && !handle) { 6587fb5409dSJan Kara /* Direct IO write... */ 6592ed88685STheodore Ts'o if (map.m_len > DIO_MAX_BLOCKS) 6602ed88685STheodore Ts'o map.m_len = DIO_MAX_BLOCKS; 6612ed88685STheodore Ts'o dio_credits = ext4_chunk_trans_blocks(inode, map.m_len); 6629924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, 6639924a92aSTheodore Ts'o dio_credits); 6647fb5409dSJan Kara if (IS_ERR(handle)) { 665ac27a0ecSDave Kleikamp ret = PTR_ERR(handle); 6662ed88685STheodore Ts'o return ret; 6677fb5409dSJan Kara } 6687fb5409dSJan Kara started = 1; 669ac27a0ecSDave Kleikamp } 670ac27a0ecSDave Kleikamp 6712ed88685STheodore Ts'o ret = ext4_map_blocks(handle, inode, &map, flags); 672ac27a0ecSDave Kleikamp if (ret > 0) { 6732ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 6742ed88685STheodore Ts'o bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; 6752ed88685STheodore Ts'o bh->b_size = inode->i_sb->s_blocksize * map.m_len; 676ac27a0ecSDave Kleikamp ret = 0; 677ac27a0ecSDave Kleikamp } 6787fb5409dSJan Kara if (started) 6797fb5409dSJan Kara ext4_journal_stop(handle); 680ac27a0ecSDave Kleikamp return ret; 681ac27a0ecSDave Kleikamp } 682ac27a0ecSDave Kleikamp 6832ed88685STheodore Ts'o int ext4_get_block(struct inode *inode, sector_t iblock, 6842ed88685STheodore Ts'o struct buffer_head *bh, int create) 6852ed88685STheodore Ts'o { 6862ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh, 6872ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 6882ed88685STheodore Ts'o } 6892ed88685STheodore Ts'o 690ac27a0ecSDave Kleikamp /* 691ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 692ac27a0ecSDave Kleikamp */ 693617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 694725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *errp) 695ac27a0ecSDave Kleikamp { 6962ed88685STheodore Ts'o struct ext4_map_blocks map; 6972ed88685STheodore Ts'o struct buffer_head *bh; 698ac27a0ecSDave Kleikamp int fatal = 0, err; 699ac27a0ecSDave Kleikamp 700ac27a0ecSDave Kleikamp J_ASSERT(handle != NULL || create == 0); 701ac27a0ecSDave Kleikamp 7022ed88685STheodore Ts'o map.m_lblk = block; 7032ed88685STheodore Ts'o map.m_len = 1; 7042ed88685STheodore Ts'o err = ext4_map_blocks(handle, inode, &map, 7052ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 7062ed88685STheodore Ts'o 70790b0a973SCarlos Maiolino /* ensure we send some value back into *errp */ 70890b0a973SCarlos Maiolino *errp = 0; 70990b0a973SCarlos Maiolino 7100f70b406STheodore Ts'o if (create && err == 0) 7110f70b406STheodore Ts'o err = -ENOSPC; /* should never happen */ 7122ed88685STheodore Ts'o if (err < 0) 713ac27a0ecSDave Kleikamp *errp = err; 7142ed88685STheodore Ts'o if (err <= 0) 7152ed88685STheodore Ts'o return NULL; 7162ed88685STheodore Ts'o 7172ed88685STheodore Ts'o bh = sb_getblk(inode->i_sb, map.m_pblk); 718aebf0243SWang Shilong if (unlikely(!bh)) { 719860d21e2STheodore Ts'o *errp = -ENOMEM; 7202ed88685STheodore Ts'o return NULL; 721ac27a0ecSDave Kleikamp } 7222ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 723ac27a0ecSDave Kleikamp J_ASSERT(create != 0); 724ac39849dSAneesh Kumar K.V J_ASSERT(handle != NULL); 725ac27a0ecSDave Kleikamp 726ac27a0ecSDave Kleikamp /* 727ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 728ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 729ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 730617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 731ac27a0ecSDave Kleikamp * problem. 732ac27a0ecSDave Kleikamp */ 733ac27a0ecSDave Kleikamp lock_buffer(bh); 734ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 735617ba13bSMingming Cao fatal = ext4_journal_get_create_access(handle, bh); 736ac27a0ecSDave Kleikamp if (!fatal && !buffer_uptodate(bh)) { 737ac27a0ecSDave Kleikamp memset(bh->b_data, 0, inode->i_sb->s_blocksize); 738ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 739ac27a0ecSDave Kleikamp } 740ac27a0ecSDave Kleikamp unlock_buffer(bh); 7410390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 7420390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 743ac27a0ecSDave Kleikamp if (!fatal) 744ac27a0ecSDave Kleikamp fatal = err; 745ac27a0ecSDave Kleikamp } else { 746ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "not a new buffer"); 747ac27a0ecSDave Kleikamp } 748ac27a0ecSDave Kleikamp if (fatal) { 749ac27a0ecSDave Kleikamp *errp = fatal; 750ac27a0ecSDave Kleikamp brelse(bh); 751ac27a0ecSDave Kleikamp bh = NULL; 752ac27a0ecSDave Kleikamp } 753ac27a0ecSDave Kleikamp return bh; 754ac27a0ecSDave Kleikamp } 755ac27a0ecSDave Kleikamp 756617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 757725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *err) 758ac27a0ecSDave Kleikamp { 759ac27a0ecSDave Kleikamp struct buffer_head *bh; 760ac27a0ecSDave Kleikamp 761617ba13bSMingming Cao bh = ext4_getblk(handle, inode, block, create, err); 762ac27a0ecSDave Kleikamp if (!bh) 763ac27a0ecSDave Kleikamp return bh; 764ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 765ac27a0ecSDave Kleikamp return bh; 76665299a3bSChristoph Hellwig ll_rw_block(READ | REQ_META | REQ_PRIO, 1, &bh); 767ac27a0ecSDave Kleikamp wait_on_buffer(bh); 768ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 769ac27a0ecSDave Kleikamp return bh; 770ac27a0ecSDave Kleikamp put_bh(bh); 771ac27a0ecSDave Kleikamp *err = -EIO; 772ac27a0ecSDave Kleikamp return NULL; 773ac27a0ecSDave Kleikamp } 774ac27a0ecSDave Kleikamp 775f19d5870STao Ma int ext4_walk_page_buffers(handle_t *handle, 776ac27a0ecSDave Kleikamp struct buffer_head *head, 777ac27a0ecSDave Kleikamp unsigned from, 778ac27a0ecSDave Kleikamp unsigned to, 779ac27a0ecSDave Kleikamp int *partial, 780ac27a0ecSDave Kleikamp int (*fn)(handle_t *handle, 781ac27a0ecSDave Kleikamp struct buffer_head *bh)) 782ac27a0ecSDave Kleikamp { 783ac27a0ecSDave Kleikamp struct buffer_head *bh; 784ac27a0ecSDave Kleikamp unsigned block_start, block_end; 785ac27a0ecSDave Kleikamp unsigned blocksize = head->b_size; 786ac27a0ecSDave Kleikamp int err, ret = 0; 787ac27a0ecSDave Kleikamp struct buffer_head *next; 788ac27a0ecSDave Kleikamp 789ac27a0ecSDave Kleikamp for (bh = head, block_start = 0; 790ac27a0ecSDave Kleikamp ret == 0 && (bh != head || !block_start); 791de9a55b8STheodore Ts'o block_start = block_end, bh = next) { 792ac27a0ecSDave Kleikamp next = bh->b_this_page; 793ac27a0ecSDave Kleikamp block_end = block_start + blocksize; 794ac27a0ecSDave Kleikamp if (block_end <= from || block_start >= to) { 795ac27a0ecSDave Kleikamp if (partial && !buffer_uptodate(bh)) 796ac27a0ecSDave Kleikamp *partial = 1; 797ac27a0ecSDave Kleikamp continue; 798ac27a0ecSDave Kleikamp } 799ac27a0ecSDave Kleikamp err = (*fn)(handle, bh); 800ac27a0ecSDave Kleikamp if (!ret) 801ac27a0ecSDave Kleikamp ret = err; 802ac27a0ecSDave Kleikamp } 803ac27a0ecSDave Kleikamp return ret; 804ac27a0ecSDave Kleikamp } 805ac27a0ecSDave Kleikamp 806ac27a0ecSDave Kleikamp /* 807ac27a0ecSDave Kleikamp * To preserve ordering, it is essential that the hole instantiation and 808ac27a0ecSDave Kleikamp * the data write be encapsulated in a single transaction. We cannot 809617ba13bSMingming Cao * close off a transaction and start a new one between the ext4_get_block() 810dab291afSMingming Cao * and the commit_write(). So doing the jbd2_journal_start at the start of 811ac27a0ecSDave Kleikamp * prepare_write() is the right place. 812ac27a0ecSDave Kleikamp * 81336ade451SJan Kara * Also, this function can nest inside ext4_writepage(). In that case, we 81436ade451SJan Kara * *know* that ext4_writepage() has generated enough buffer credits to do the 81536ade451SJan Kara * whole page. So we won't block on the journal in that case, which is good, 81636ade451SJan Kara * because the caller may be PF_MEMALLOC. 817ac27a0ecSDave Kleikamp * 818617ba13bSMingming Cao * By accident, ext4 can be reentered when a transaction is open via 819ac27a0ecSDave Kleikamp * quota file writes. If we were to commit the transaction while thus 820ac27a0ecSDave Kleikamp * reentered, there can be a deadlock - we would be holding a quota 821ac27a0ecSDave Kleikamp * lock, and the commit would never complete if another thread had a 822ac27a0ecSDave Kleikamp * transaction open and was blocking on the quota lock - a ranking 823ac27a0ecSDave Kleikamp * violation. 824ac27a0ecSDave Kleikamp * 825dab291afSMingming Cao * So what we do is to rely on the fact that jbd2_journal_stop/journal_start 826ac27a0ecSDave Kleikamp * will _not_ run commit under these circumstances because handle->h_ref 827ac27a0ecSDave Kleikamp * is elevated. We'll still have enough credits for the tiny quotafile 828ac27a0ecSDave Kleikamp * write. 829ac27a0ecSDave Kleikamp */ 830f19d5870STao Ma int do_journal_get_write_access(handle_t *handle, 831ac27a0ecSDave Kleikamp struct buffer_head *bh) 832ac27a0ecSDave Kleikamp { 83356d35a4cSJan Kara int dirty = buffer_dirty(bh); 83456d35a4cSJan Kara int ret; 83556d35a4cSJan Kara 836ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 837ac27a0ecSDave Kleikamp return 0; 83856d35a4cSJan Kara /* 839ebdec241SChristoph Hellwig * __block_write_begin() could have dirtied some buffers. Clean 84056d35a4cSJan Kara * the dirty bit as jbd2_journal_get_write_access() could complain 84156d35a4cSJan Kara * otherwise about fs integrity issues. Setting of the dirty bit 842ebdec241SChristoph Hellwig * by __block_write_begin() isn't a real problem here as we clear 84356d35a4cSJan Kara * the bit before releasing a page lock and thus writeback cannot 84456d35a4cSJan Kara * ever write the buffer. 84556d35a4cSJan Kara */ 84656d35a4cSJan Kara if (dirty) 84756d35a4cSJan Kara clear_buffer_dirty(bh); 84856d35a4cSJan Kara ret = ext4_journal_get_write_access(handle, bh); 84956d35a4cSJan Kara if (!ret && dirty) 85056d35a4cSJan Kara ret = ext4_handle_dirty_metadata(handle, NULL, bh); 85156d35a4cSJan Kara return ret; 852ac27a0ecSDave Kleikamp } 853ac27a0ecSDave Kleikamp 8548b0f165fSAnatol Pomozov static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock, 8558b0f165fSAnatol Pomozov struct buffer_head *bh_result, int create); 856bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 857bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned flags, 858bfc1af65SNick Piggin struct page **pagep, void **fsdata) 859ac27a0ecSDave Kleikamp { 860bfc1af65SNick Piggin struct inode *inode = mapping->host; 8611938a150SAneesh Kumar K.V int ret, needed_blocks; 862ac27a0ecSDave Kleikamp handle_t *handle; 863ac27a0ecSDave Kleikamp int retries = 0; 864bfc1af65SNick Piggin struct page *page; 865bfc1af65SNick Piggin pgoff_t index; 866bfc1af65SNick Piggin unsigned from, to; 867bfc1af65SNick Piggin 8689bffad1eSTheodore Ts'o trace_ext4_write_begin(inode, pos, len, flags); 8691938a150SAneesh Kumar K.V /* 8701938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 8711938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 8721938a150SAneesh Kumar K.V */ 8731938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 874bfc1af65SNick Piggin index = pos >> PAGE_CACHE_SHIFT; 875bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 876bfc1af65SNick Piggin to = from + len; 877ac27a0ecSDave Kleikamp 878f19d5870STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 879f19d5870STao Ma ret = ext4_try_to_write_inline_data(mapping, inode, pos, len, 880f19d5870STao Ma flags, pagep); 881f19d5870STao Ma if (ret < 0) 88247564bfbSTheodore Ts'o return ret; 88347564bfbSTheodore Ts'o if (ret == 1) 88447564bfbSTheodore Ts'o return 0; 885f19d5870STao Ma } 886f19d5870STao Ma 88747564bfbSTheodore Ts'o /* 88847564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 88947564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 89047564bfbSTheodore Ts'o * is being written back. So grab it first before we start 89147564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 89247564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 89347564bfbSTheodore Ts'o */ 89447564bfbSTheodore Ts'o retry_grab: 89547564bfbSTheodore Ts'o page = grab_cache_page_write_begin(mapping, index, flags); 89647564bfbSTheodore Ts'o if (!page) 89747564bfbSTheodore Ts'o return -ENOMEM; 89847564bfbSTheodore Ts'o unlock_page(page); 89947564bfbSTheodore Ts'o 90047564bfbSTheodore Ts'o retry_journal: 9019924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks); 9027479d2b9SAndrew Morton if (IS_ERR(handle)) { 90347564bfbSTheodore Ts'o page_cache_release(page); 90447564bfbSTheodore Ts'o return PTR_ERR(handle); 9057479d2b9SAndrew Morton } 906ac27a0ecSDave Kleikamp 90747564bfbSTheodore Ts'o lock_page(page); 90847564bfbSTheodore Ts'o if (page->mapping != mapping) { 90947564bfbSTheodore Ts'o /* The page got truncated from under us */ 91047564bfbSTheodore Ts'o unlock_page(page); 91147564bfbSTheodore Ts'o page_cache_release(page); 912cf108bcaSJan Kara ext4_journal_stop(handle); 91347564bfbSTheodore Ts'o goto retry_grab; 914cf108bcaSJan Kara } 91547564bfbSTheodore Ts'o wait_on_page_writeback(page); 916cf108bcaSJan Kara 917744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 9186e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block_write); 919744692dcSJiaying Zhang else 9206e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block); 921bfc1af65SNick Piggin 922bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 923f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_buffers(page), 924f19d5870STao Ma from, to, NULL, 925f19d5870STao Ma do_journal_get_write_access); 926b46be050SAndrey Savochkin } 927bfc1af65SNick Piggin 928bfc1af65SNick Piggin if (ret) { 929bfc1af65SNick Piggin unlock_page(page); 930ae4d5372SAneesh Kumar K.V /* 9316e1db88dSChristoph Hellwig * __block_write_begin may have instantiated a few blocks 932ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 933ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 9341938a150SAneesh Kumar K.V * 9351938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 9361938a150SAneesh Kumar K.V * truncate finishes 937ae4d5372SAneesh Kumar K.V */ 938ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 9391938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 9401938a150SAneesh Kumar K.V 9411938a150SAneesh Kumar K.V ext4_journal_stop(handle); 9421938a150SAneesh Kumar K.V if (pos + len > inode->i_size) { 943b9a4207dSJan Kara ext4_truncate_failed_write(inode); 9441938a150SAneesh Kumar K.V /* 945ffacfa7aSJan Kara * If truncate failed early the inode might 9461938a150SAneesh Kumar K.V * still be on the orphan list; we need to 9471938a150SAneesh Kumar K.V * make sure the inode is removed from the 9481938a150SAneesh Kumar K.V * orphan list in that case. 9491938a150SAneesh Kumar K.V */ 9501938a150SAneesh Kumar K.V if (inode->i_nlink) 9511938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 9521938a150SAneesh Kumar K.V } 953bfc1af65SNick Piggin 95447564bfbSTheodore Ts'o if (ret == -ENOSPC && 95547564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 95647564bfbSTheodore Ts'o goto retry_journal; 95747564bfbSTheodore Ts'o page_cache_release(page); 95847564bfbSTheodore Ts'o return ret; 95947564bfbSTheodore Ts'o } 96047564bfbSTheodore Ts'o *pagep = page; 961ac27a0ecSDave Kleikamp return ret; 962ac27a0ecSDave Kleikamp } 963ac27a0ecSDave Kleikamp 964bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 965bfc1af65SNick Piggin static int write_end_fn(handle_t *handle, struct buffer_head *bh) 966ac27a0ecSDave Kleikamp { 967ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 968ac27a0ecSDave Kleikamp return 0; 969ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 9700390131bSFrank Mayhar return ext4_handle_dirty_metadata(handle, NULL, bh); 971ac27a0ecSDave Kleikamp } 972ac27a0ecSDave Kleikamp 973f8514083SAneesh Kumar K.V static int ext4_generic_write_end(struct file *file, 974f8514083SAneesh Kumar K.V struct address_space *mapping, 975f8514083SAneesh Kumar K.V loff_t pos, unsigned len, unsigned copied, 976f8514083SAneesh Kumar K.V struct page *page, void *fsdata) 977f8514083SAneesh Kumar K.V { 978f8514083SAneesh Kumar K.V int i_size_changed = 0; 979f8514083SAneesh Kumar K.V struct inode *inode = mapping->host; 980f8514083SAneesh Kumar K.V handle_t *handle = ext4_journal_current_handle(); 981f8514083SAneesh Kumar K.V 982f19d5870STao Ma if (ext4_has_inline_data(inode)) 983f19d5870STao Ma copied = ext4_write_inline_data_end(inode, pos, len, 984f19d5870STao Ma copied, page); 985f19d5870STao Ma else 986f19d5870STao Ma copied = block_write_end(file, mapping, pos, 987f19d5870STao Ma len, copied, page, fsdata); 988f8514083SAneesh Kumar K.V 989f8514083SAneesh Kumar K.V /* 990f8514083SAneesh Kumar K.V * No need to use i_size_read() here, the i_size 991f8514083SAneesh Kumar K.V * cannot change under us because we hold i_mutex. 992f8514083SAneesh Kumar K.V * 993f8514083SAneesh Kumar K.V * But it's important to update i_size while still holding page lock: 994f8514083SAneesh Kumar K.V * page writeout could otherwise come in and zero beyond i_size. 995f8514083SAneesh Kumar K.V */ 996f8514083SAneesh Kumar K.V if (pos + copied > inode->i_size) { 997f8514083SAneesh Kumar K.V i_size_write(inode, pos + copied); 998f8514083SAneesh Kumar K.V i_size_changed = 1; 999f8514083SAneesh Kumar K.V } 1000f8514083SAneesh Kumar K.V 1001f8514083SAneesh Kumar K.V if (pos + copied > EXT4_I(inode)->i_disksize) { 1002f8514083SAneesh Kumar K.V /* We need to mark inode dirty even if 1003f8514083SAneesh Kumar K.V * new_i_size is less that inode->i_size 1004f8514083SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 1005f8514083SAneesh Kumar K.V */ 1006f8514083SAneesh Kumar K.V ext4_update_i_disksize(inode, (pos + copied)); 1007f8514083SAneesh Kumar K.V i_size_changed = 1; 1008f8514083SAneesh Kumar K.V } 1009f8514083SAneesh Kumar K.V unlock_page(page); 1010f8514083SAneesh Kumar K.V page_cache_release(page); 1011f8514083SAneesh Kumar K.V 1012f8514083SAneesh Kumar K.V /* 1013f8514083SAneesh Kumar K.V * Don't mark the inode dirty under page lock. First, it unnecessarily 1014f8514083SAneesh Kumar K.V * makes the holding time of page lock longer. Second, it forces lock 1015f8514083SAneesh Kumar K.V * ordering of page lock and transaction start for journaling 1016f8514083SAneesh Kumar K.V * filesystems. 1017f8514083SAneesh Kumar K.V */ 1018f8514083SAneesh Kumar K.V if (i_size_changed) 1019f8514083SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 1020f8514083SAneesh Kumar K.V 1021f8514083SAneesh Kumar K.V return copied; 1022f8514083SAneesh Kumar K.V } 1023f8514083SAneesh Kumar K.V 1024ac27a0ecSDave Kleikamp /* 1025ac27a0ecSDave Kleikamp * We need to pick up the new inode size which generic_commit_write gave us 1026ac27a0ecSDave Kleikamp * `file' can be NULL - eg, when called from page_symlink(). 1027ac27a0ecSDave Kleikamp * 1028617ba13bSMingming Cao * ext4 never places buffers on inode->i_mapping->private_list. metadata 1029ac27a0ecSDave Kleikamp * buffers are managed internally. 1030ac27a0ecSDave Kleikamp */ 1031bfc1af65SNick Piggin static int ext4_ordered_write_end(struct file *file, 1032bfc1af65SNick Piggin struct address_space *mapping, 1033bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1034bfc1af65SNick Piggin struct page *page, void *fsdata) 1035ac27a0ecSDave Kleikamp { 1036617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1037cf108bcaSJan Kara struct inode *inode = mapping->host; 1038ac27a0ecSDave Kleikamp int ret = 0, ret2; 1039ac27a0ecSDave Kleikamp 10409bffad1eSTheodore Ts'o trace_ext4_ordered_write_end(inode, pos, len, copied); 1041678aaf48SJan Kara ret = ext4_jbd2_file_inode(handle, inode); 1042ac27a0ecSDave Kleikamp 1043ac27a0ecSDave Kleikamp if (ret == 0) { 1044f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 1045bfc1af65SNick Piggin page, fsdata); 1046f8a87d89SRoel Kluin copied = ret2; 1047ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1048f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1049f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1050f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1051f8514083SAneesh Kumar K.V */ 1052f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1053f8a87d89SRoel Kluin if (ret2 < 0) 1054f8a87d89SRoel Kluin ret = ret2; 105509e0834fSAkira Fujita } else { 105609e0834fSAkira Fujita unlock_page(page); 105709e0834fSAkira Fujita page_cache_release(page); 1058ac27a0ecSDave Kleikamp } 105909e0834fSAkira Fujita 1060617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1061ac27a0ecSDave Kleikamp if (!ret) 1062ac27a0ecSDave Kleikamp ret = ret2; 1063bfc1af65SNick Piggin 1064f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1065b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1066f8514083SAneesh Kumar K.V /* 1067ffacfa7aSJan Kara * If truncate failed early the inode might still be 1068f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1069f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1070f8514083SAneesh Kumar K.V */ 1071f8514083SAneesh Kumar K.V if (inode->i_nlink) 1072f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1073f8514083SAneesh Kumar K.V } 1074f8514083SAneesh Kumar K.V 1075f8514083SAneesh Kumar K.V 1076bfc1af65SNick Piggin return ret ? ret : copied; 1077ac27a0ecSDave Kleikamp } 1078ac27a0ecSDave Kleikamp 1079bfc1af65SNick Piggin static int ext4_writeback_write_end(struct file *file, 1080bfc1af65SNick Piggin struct address_space *mapping, 1081bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1082bfc1af65SNick Piggin struct page *page, void *fsdata) 1083ac27a0ecSDave Kleikamp { 1084617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1085cf108bcaSJan Kara struct inode *inode = mapping->host; 1086ac27a0ecSDave Kleikamp int ret = 0, ret2; 1087ac27a0ecSDave Kleikamp 10889bffad1eSTheodore Ts'o trace_ext4_writeback_write_end(inode, pos, len, copied); 1089f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 1090bfc1af65SNick Piggin page, fsdata); 1091f8a87d89SRoel Kluin copied = ret2; 1092ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1093f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1094f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1095f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1096f8514083SAneesh Kumar K.V */ 1097f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1098f8514083SAneesh Kumar K.V 1099f8a87d89SRoel Kluin if (ret2 < 0) 1100f8a87d89SRoel Kluin ret = ret2; 1101ac27a0ecSDave Kleikamp 1102617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1103ac27a0ecSDave Kleikamp if (!ret) 1104ac27a0ecSDave Kleikamp ret = ret2; 1105bfc1af65SNick Piggin 1106f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1107b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1108f8514083SAneesh Kumar K.V /* 1109ffacfa7aSJan Kara * If truncate failed early the inode might still be 1110f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1111f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1112f8514083SAneesh Kumar K.V */ 1113f8514083SAneesh Kumar K.V if (inode->i_nlink) 1114f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1115f8514083SAneesh Kumar K.V } 1116f8514083SAneesh Kumar K.V 1117bfc1af65SNick Piggin return ret ? ret : copied; 1118ac27a0ecSDave Kleikamp } 1119ac27a0ecSDave Kleikamp 1120bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 1121bfc1af65SNick Piggin struct address_space *mapping, 1122bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1123bfc1af65SNick Piggin struct page *page, void *fsdata) 1124ac27a0ecSDave Kleikamp { 1125617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1126bfc1af65SNick Piggin struct inode *inode = mapping->host; 1127ac27a0ecSDave Kleikamp int ret = 0, ret2; 1128ac27a0ecSDave Kleikamp int partial = 0; 1129bfc1af65SNick Piggin unsigned from, to; 1130cf17fea6SAneesh Kumar K.V loff_t new_i_size; 1131ac27a0ecSDave Kleikamp 11329bffad1eSTheodore Ts'o trace_ext4_journalled_write_end(inode, pos, len, copied); 1133bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 1134bfc1af65SNick Piggin to = from + len; 1135bfc1af65SNick Piggin 1136441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1137441c8508SCurt Wohlgemuth 11383fdcfb66STao Ma if (ext4_has_inline_data(inode)) 11393fdcfb66STao Ma copied = ext4_write_inline_data_end(inode, pos, len, 11403fdcfb66STao Ma copied, page); 11413fdcfb66STao Ma else { 1142bfc1af65SNick Piggin if (copied < len) { 1143bfc1af65SNick Piggin if (!PageUptodate(page)) 1144bfc1af65SNick Piggin copied = 0; 1145bfc1af65SNick Piggin page_zero_new_buffers(page, from+copied, to); 1146bfc1af65SNick Piggin } 1147ac27a0ecSDave Kleikamp 1148f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_buffers(page), from, 1149bfc1af65SNick Piggin to, &partial, write_end_fn); 1150ac27a0ecSDave Kleikamp if (!partial) 1151ac27a0ecSDave Kleikamp SetPageUptodate(page); 11523fdcfb66STao Ma } 1153cf17fea6SAneesh Kumar K.V new_i_size = pos + copied; 1154cf17fea6SAneesh Kumar K.V if (new_i_size > inode->i_size) 1155bfc1af65SNick Piggin i_size_write(inode, pos+copied); 115619f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 11572d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 1158cf17fea6SAneesh Kumar K.V if (new_i_size > EXT4_I(inode)->i_disksize) { 1159cf17fea6SAneesh Kumar K.V ext4_update_i_disksize(inode, new_i_size); 1160617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1161ac27a0ecSDave Kleikamp if (!ret) 1162ac27a0ecSDave Kleikamp ret = ret2; 1163ac27a0ecSDave Kleikamp } 1164bfc1af65SNick Piggin 1165cf108bcaSJan Kara unlock_page(page); 1166f8514083SAneesh Kumar K.V page_cache_release(page); 1167ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1168f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1169f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1170f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1171f8514083SAneesh Kumar K.V */ 1172f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1173f8514083SAneesh Kumar K.V 1174617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1175ac27a0ecSDave Kleikamp if (!ret) 1176ac27a0ecSDave Kleikamp ret = ret2; 1177f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1178b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1179f8514083SAneesh Kumar K.V /* 1180ffacfa7aSJan Kara * If truncate failed early the inode might still be 1181f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1182f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1183f8514083SAneesh Kumar K.V */ 1184f8514083SAneesh Kumar K.V if (inode->i_nlink) 1185f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1186f8514083SAneesh Kumar K.V } 1187bfc1af65SNick Piggin 1188bfc1af65SNick Piggin return ret ? ret : copied; 1189ac27a0ecSDave Kleikamp } 1190d2a17637SMingming Cao 11919d0be502STheodore Ts'o /* 11927b415bf6SAditya Kali * Reserve a single cluster located at lblock 11939d0be502STheodore Ts'o */ 119401f49d0bSTheodore Ts'o static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock) 1195d2a17637SMingming Cao { 1196030ba6bcSAneesh Kumar K.V int retries = 0; 1197d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 11980637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 11997b415bf6SAditya Kali unsigned int md_needed; 12005dd4056dSChristoph Hellwig int ret; 120103179fe9STheodore Ts'o ext4_lblk_t save_last_lblock; 120203179fe9STheodore Ts'o int save_len; 1203d2a17637SMingming Cao 120460e58e0fSMingming Cao /* 120572b8ab9dSEric Sandeen * We will charge metadata quota at writeout time; this saves 120672b8ab9dSEric Sandeen * us from metadata over-estimation, though we may go over by 120772b8ab9dSEric Sandeen * a small amount in the end. Here we just reserve for data. 120860e58e0fSMingming Cao */ 12097b415bf6SAditya Kali ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1)); 12105dd4056dSChristoph Hellwig if (ret) 12115dd4056dSChristoph Hellwig return ret; 121203179fe9STheodore Ts'o 121303179fe9STheodore Ts'o /* 121403179fe9STheodore Ts'o * recalculate the amount of metadata blocks to reserve 121503179fe9STheodore Ts'o * in order to allocate nrblocks 121603179fe9STheodore Ts'o * worse case is one extent per block 121703179fe9STheodore Ts'o */ 121803179fe9STheodore Ts'o repeat: 121903179fe9STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 122003179fe9STheodore Ts'o /* 122103179fe9STheodore Ts'o * ext4_calc_metadata_amount() has side effects, which we have 122203179fe9STheodore Ts'o * to be prepared undo if we fail to claim space. 122303179fe9STheodore Ts'o */ 122403179fe9STheodore Ts'o save_len = ei->i_da_metadata_calc_len; 122503179fe9STheodore Ts'o save_last_lblock = ei->i_da_metadata_calc_last_lblock; 122603179fe9STheodore Ts'o md_needed = EXT4_NUM_B2C(sbi, 122703179fe9STheodore Ts'o ext4_calc_metadata_amount(inode, lblock)); 122803179fe9STheodore Ts'o trace_ext4_da_reserve_space(inode, md_needed); 122903179fe9STheodore Ts'o 123072b8ab9dSEric Sandeen /* 123172b8ab9dSEric Sandeen * We do still charge estimated metadata to the sb though; 123272b8ab9dSEric Sandeen * we cannot afford to run out of free blocks. 123372b8ab9dSEric Sandeen */ 1234e7d5f315STheodore Ts'o if (ext4_claim_free_clusters(sbi, md_needed + 1, 0)) { 123503179fe9STheodore Ts'o ei->i_da_metadata_calc_len = save_len; 123603179fe9STheodore Ts'o ei->i_da_metadata_calc_last_lblock = save_last_lblock; 123703179fe9STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 1238030ba6bcSAneesh Kumar K.V if (ext4_should_retry_alloc(inode->i_sb, &retries)) { 1239030ba6bcSAneesh Kumar K.V yield(); 1240030ba6bcSAneesh Kumar K.V goto repeat; 1241030ba6bcSAneesh Kumar K.V } 124203179fe9STheodore Ts'o dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1)); 1243d2a17637SMingming Cao return -ENOSPC; 1244d2a17637SMingming Cao } 12459d0be502STheodore Ts'o ei->i_reserved_data_blocks++; 12460637c6f4STheodore Ts'o ei->i_reserved_meta_blocks += md_needed; 12470637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 124839bc680aSDmitry Monakhov 1249d2a17637SMingming Cao return 0; /* success */ 1250d2a17637SMingming Cao } 1251d2a17637SMingming Cao 125212219aeaSAneesh Kumar K.V static void ext4_da_release_space(struct inode *inode, int to_free) 1253d2a17637SMingming Cao { 1254d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 12550637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 1256d2a17637SMingming Cao 1257cd213226SMingming Cao if (!to_free) 1258cd213226SMingming Cao return; /* Nothing to release, exit */ 1259cd213226SMingming Cao 1260d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1261cd213226SMingming Cao 12625a58ec87SLi Zefan trace_ext4_da_release_space(inode, to_free); 12630637c6f4STheodore Ts'o if (unlikely(to_free > ei->i_reserved_data_blocks)) { 1264cd213226SMingming Cao /* 12650637c6f4STheodore Ts'o * if there aren't enough reserved blocks, then the 12660637c6f4STheodore Ts'o * counter is messed up somewhere. Since this 12670637c6f4STheodore Ts'o * function is called from invalidate page, it's 12680637c6f4STheodore Ts'o * harmless to return without any action. 1269cd213226SMingming Cao */ 12708de5c325STheodore Ts'o ext4_warning(inode->i_sb, "ext4_da_release_space: " 12710637c6f4STheodore Ts'o "ino %lu, to_free %d with only %d reserved " 12721084f252STheodore Ts'o "data blocks", inode->i_ino, to_free, 12730637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 12740637c6f4STheodore Ts'o WARN_ON(1); 12750637c6f4STheodore Ts'o to_free = ei->i_reserved_data_blocks; 12760637c6f4STheodore Ts'o } 12770637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= to_free; 12780637c6f4STheodore Ts'o 12790637c6f4STheodore Ts'o if (ei->i_reserved_data_blocks == 0) { 12800637c6f4STheodore Ts'o /* 12810637c6f4STheodore Ts'o * We can release all of the reserved metadata blocks 12820637c6f4STheodore Ts'o * only when we have written all of the delayed 12830637c6f4STheodore Ts'o * allocation blocks. 12847b415bf6SAditya Kali * Note that in case of bigalloc, i_reserved_meta_blocks, 12857b415bf6SAditya Kali * i_reserved_data_blocks, etc. refer to number of clusters. 12860637c6f4STheodore Ts'o */ 128757042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 128872b8ab9dSEric Sandeen ei->i_reserved_meta_blocks); 1289ee5f4d9cSTheodore Ts'o ei->i_reserved_meta_blocks = 0; 12909d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 1291cd213226SMingming Cao } 1292cd213226SMingming Cao 129372b8ab9dSEric Sandeen /* update fs dirty data blocks counter */ 129457042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free); 1295d2a17637SMingming Cao 1296d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 129760e58e0fSMingming Cao 12987b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free)); 1299d2a17637SMingming Cao } 1300d2a17637SMingming Cao 1301d2a17637SMingming Cao static void ext4_da_page_release_reservation(struct page *page, 1302d2a17637SMingming Cao unsigned long offset) 1303d2a17637SMingming Cao { 1304d2a17637SMingming Cao int to_release = 0; 1305d2a17637SMingming Cao struct buffer_head *head, *bh; 1306d2a17637SMingming Cao unsigned int curr_off = 0; 13077b415bf6SAditya Kali struct inode *inode = page->mapping->host; 13087b415bf6SAditya Kali struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 13097b415bf6SAditya Kali int num_clusters; 131051865fdaSZheng Liu ext4_fsblk_t lblk; 1311d2a17637SMingming Cao 1312d2a17637SMingming Cao head = page_buffers(page); 1313d2a17637SMingming Cao bh = head; 1314d2a17637SMingming Cao do { 1315d2a17637SMingming Cao unsigned int next_off = curr_off + bh->b_size; 1316d2a17637SMingming Cao 1317d2a17637SMingming Cao if ((offset <= curr_off) && (buffer_delay(bh))) { 1318d2a17637SMingming Cao to_release++; 1319d2a17637SMingming Cao clear_buffer_delay(bh); 1320d2a17637SMingming Cao } 1321d2a17637SMingming Cao curr_off = next_off; 1322d2a17637SMingming Cao } while ((bh = bh->b_this_page) != head); 13237b415bf6SAditya Kali 132451865fdaSZheng Liu if (to_release) { 132551865fdaSZheng Liu lblk = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits); 132651865fdaSZheng Liu ext4_es_remove_extent(inode, lblk, to_release); 132751865fdaSZheng Liu } 132851865fdaSZheng Liu 13297b415bf6SAditya Kali /* If we have released all the blocks belonging to a cluster, then we 13307b415bf6SAditya Kali * need to release the reserved space for that cluster. */ 13317b415bf6SAditya Kali num_clusters = EXT4_NUM_B2C(sbi, to_release); 13327b415bf6SAditya Kali while (num_clusters > 0) { 13337b415bf6SAditya Kali lblk = (page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits)) + 13347b415bf6SAditya Kali ((num_clusters - 1) << sbi->s_cluster_bits); 13357b415bf6SAditya Kali if (sbi->s_cluster_ratio == 1 || 13367d1b1fbcSZheng Liu !ext4_find_delalloc_cluster(inode, lblk)) 13377b415bf6SAditya Kali ext4_da_release_space(inode, 1); 13387b415bf6SAditya Kali 13397b415bf6SAditya Kali num_clusters--; 13407b415bf6SAditya Kali } 1341d2a17637SMingming Cao } 1342ac27a0ecSDave Kleikamp 1343ac27a0ecSDave Kleikamp /* 134464769240SAlex Tomas * Delayed allocation stuff 134564769240SAlex Tomas */ 134664769240SAlex Tomas 134764769240SAlex Tomas /* 134864769240SAlex Tomas * mpage_da_submit_io - walks through extent of pages and try to write 1349a1d6cc56SAneesh Kumar K.V * them with writepage() call back 135064769240SAlex Tomas * 135164769240SAlex Tomas * @mpd->inode: inode 135264769240SAlex Tomas * @mpd->first_page: first page of the extent 135364769240SAlex Tomas * @mpd->next_page: page after the last page of the extent 135464769240SAlex Tomas * 135564769240SAlex Tomas * By the time mpage_da_submit_io() is called we expect all blocks 135664769240SAlex Tomas * to be allocated. this may be wrong if allocation failed. 135764769240SAlex Tomas * 135864769240SAlex Tomas * As pages are already locked by write_cache_pages(), we can't use it 135964769240SAlex Tomas */ 13601de3e3dfSTheodore Ts'o static int mpage_da_submit_io(struct mpage_da_data *mpd, 13611de3e3dfSTheodore Ts'o struct ext4_map_blocks *map) 136264769240SAlex Tomas { 1363791b7f08SAneesh Kumar K.V struct pagevec pvec; 1364791b7f08SAneesh Kumar K.V unsigned long index, end; 1365791b7f08SAneesh Kumar K.V int ret = 0, err, nr_pages, i; 1366791b7f08SAneesh Kumar K.V struct inode *inode = mpd->inode; 1367791b7f08SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 1368cb20d518STheodore Ts'o loff_t size = i_size_read(inode); 13693ecdb3a1STheodore Ts'o unsigned int len, block_start; 13703ecdb3a1STheodore Ts'o struct buffer_head *bh, *page_bufs = NULL; 13711de3e3dfSTheodore Ts'o sector_t pblock = 0, cur_logical = 0; 1372bd2d0210STheodore Ts'o struct ext4_io_submit io_submit; 137364769240SAlex Tomas 137464769240SAlex Tomas BUG_ON(mpd->next_page <= mpd->first_page); 1375bd2d0210STheodore Ts'o memset(&io_submit, 0, sizeof(io_submit)); 1376791b7f08SAneesh Kumar K.V /* 1377791b7f08SAneesh Kumar K.V * We need to start from the first_page to the next_page - 1 1378791b7f08SAneesh Kumar K.V * to make sure we also write the mapped dirty buffer_heads. 13798dc207c0STheodore Ts'o * If we look at mpd->b_blocknr we would only be looking 1380791b7f08SAneesh Kumar K.V * at the currently mapped buffer_heads. 1381791b7f08SAneesh Kumar K.V */ 138264769240SAlex Tomas index = mpd->first_page; 138364769240SAlex Tomas end = mpd->next_page - 1; 138464769240SAlex Tomas 1385791b7f08SAneesh Kumar K.V pagevec_init(&pvec, 0); 138664769240SAlex Tomas while (index <= end) { 1387791b7f08SAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 138864769240SAlex Tomas if (nr_pages == 0) 138964769240SAlex Tomas break; 139064769240SAlex Tomas for (i = 0; i < nr_pages; i++) { 1391f8bec370SJan Kara int skip_page = 0; 139264769240SAlex Tomas struct page *page = pvec.pages[i]; 139364769240SAlex Tomas 1394791b7f08SAneesh Kumar K.V index = page->index; 1395791b7f08SAneesh Kumar K.V if (index > end) 1396791b7f08SAneesh Kumar K.V break; 1397cb20d518STheodore Ts'o 1398cb20d518STheodore Ts'o if (index == size >> PAGE_CACHE_SHIFT) 1399cb20d518STheodore Ts'o len = size & ~PAGE_CACHE_MASK; 1400cb20d518STheodore Ts'o else 1401cb20d518STheodore Ts'o len = PAGE_CACHE_SIZE; 14021de3e3dfSTheodore Ts'o if (map) { 14031de3e3dfSTheodore Ts'o cur_logical = index << (PAGE_CACHE_SHIFT - 14041de3e3dfSTheodore Ts'o inode->i_blkbits); 14051de3e3dfSTheodore Ts'o pblock = map->m_pblk + (cur_logical - 14061de3e3dfSTheodore Ts'o map->m_lblk); 14071de3e3dfSTheodore Ts'o } 1408791b7f08SAneesh Kumar K.V index++; 1409791b7f08SAneesh Kumar K.V 1410791b7f08SAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1411791b7f08SAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1412791b7f08SAneesh Kumar K.V 14133ecdb3a1STheodore Ts'o bh = page_bufs = page_buffers(page); 14143ecdb3a1STheodore Ts'o block_start = 0; 14153ecdb3a1STheodore Ts'o do { 14161de3e3dfSTheodore Ts'o if (map && (cur_logical >= map->m_lblk) && 14171de3e3dfSTheodore Ts'o (cur_logical <= (map->m_lblk + 14181de3e3dfSTheodore Ts'o (map->m_len - 1)))) { 14191de3e3dfSTheodore Ts'o if (buffer_delay(bh)) { 14201de3e3dfSTheodore Ts'o clear_buffer_delay(bh); 14211de3e3dfSTheodore Ts'o bh->b_blocknr = pblock; 14221de3e3dfSTheodore Ts'o } 14231de3e3dfSTheodore Ts'o if (buffer_unwritten(bh) || 14241de3e3dfSTheodore Ts'o buffer_mapped(bh)) 14251de3e3dfSTheodore Ts'o BUG_ON(bh->b_blocknr != pblock); 14261de3e3dfSTheodore Ts'o if (map->m_flags & EXT4_MAP_UNINIT) 14271de3e3dfSTheodore Ts'o set_buffer_uninit(bh); 14281de3e3dfSTheodore Ts'o clear_buffer_unwritten(bh); 14291de3e3dfSTheodore Ts'o } 14301de3e3dfSTheodore Ts'o 143113a79a47SYongqiang Yang /* 143213a79a47SYongqiang Yang * skip page if block allocation undone and 143313a79a47SYongqiang Yang * block is dirty 143413a79a47SYongqiang Yang */ 143513a79a47SYongqiang Yang if (ext4_bh_delay_or_unwritten(NULL, bh)) 143697498956STheodore Ts'o skip_page = 1; 14373ecdb3a1STheodore Ts'o bh = bh->b_this_page; 14383ecdb3a1STheodore Ts'o block_start += bh->b_size; 14391de3e3dfSTheodore Ts'o cur_logical++; 14401de3e3dfSTheodore Ts'o pblock++; 14411de3e3dfSTheodore Ts'o } while (bh != page_bufs); 14421de3e3dfSTheodore Ts'o 1443f8bec370SJan Kara if (skip_page) { 1444f8bec370SJan Kara unlock_page(page); 1445f8bec370SJan Kara continue; 1446f8bec370SJan Kara } 1447cb20d518STheodore Ts'o 144897498956STheodore Ts'o clear_page_dirty_for_io(page); 1449fe089c77SJan Kara err = ext4_bio_write_page(&io_submit, page, len, 1450fe089c77SJan Kara mpd->wbc); 1451cb20d518STheodore Ts'o if (!err) 1452a1d6cc56SAneesh Kumar K.V mpd->pages_written++; 145364769240SAlex Tomas /* 145464769240SAlex Tomas * In error case, we have to continue because 145564769240SAlex Tomas * remaining pages are still locked 145664769240SAlex Tomas */ 145764769240SAlex Tomas if (ret == 0) 145864769240SAlex Tomas ret = err; 145964769240SAlex Tomas } 146064769240SAlex Tomas pagevec_release(&pvec); 146164769240SAlex Tomas } 1462bd2d0210STheodore Ts'o ext4_io_submit(&io_submit); 146364769240SAlex Tomas return ret; 146464769240SAlex Tomas } 146564769240SAlex Tomas 1466c7f5938aSCurt Wohlgemuth static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd) 1467c4a0c46eSAneesh Kumar K.V { 1468c4a0c46eSAneesh Kumar K.V int nr_pages, i; 1469c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 1470c4a0c46eSAneesh Kumar K.V struct pagevec pvec; 1471c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 1472c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 147351865fdaSZheng Liu ext4_lblk_t start, last; 1474c4a0c46eSAneesh Kumar K.V 1475c7f5938aSCurt Wohlgemuth index = mpd->first_page; 1476c7f5938aSCurt Wohlgemuth end = mpd->next_page - 1; 147751865fdaSZheng Liu 147851865fdaSZheng Liu start = index << (PAGE_CACHE_SHIFT - inode->i_blkbits); 147951865fdaSZheng Liu last = end << (PAGE_CACHE_SHIFT - inode->i_blkbits); 148051865fdaSZheng Liu ext4_es_remove_extent(inode, start, last - start + 1); 148151865fdaSZheng Liu 148266bea92cSEric Sandeen pagevec_init(&pvec, 0); 1483c4a0c46eSAneesh Kumar K.V while (index <= end) { 1484c4a0c46eSAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 1485c4a0c46eSAneesh Kumar K.V if (nr_pages == 0) 1486c4a0c46eSAneesh Kumar K.V break; 1487c4a0c46eSAneesh Kumar K.V for (i = 0; i < nr_pages; i++) { 1488c4a0c46eSAneesh Kumar K.V struct page *page = pvec.pages[i]; 14899b1d0998SJan Kara if (page->index > end) 1490c4a0c46eSAneesh Kumar K.V break; 1491c4a0c46eSAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1492c4a0c46eSAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1493c4a0c46eSAneesh Kumar K.V block_invalidatepage(page, 0); 1494c4a0c46eSAneesh Kumar K.V ClearPageUptodate(page); 1495c4a0c46eSAneesh Kumar K.V unlock_page(page); 1496c4a0c46eSAneesh Kumar K.V } 14979b1d0998SJan Kara index = pvec.pages[nr_pages - 1]->index + 1; 14989b1d0998SJan Kara pagevec_release(&pvec); 1499c4a0c46eSAneesh Kumar K.V } 1500c4a0c46eSAneesh Kumar K.V return; 1501c4a0c46eSAneesh Kumar K.V } 1502c4a0c46eSAneesh Kumar K.V 1503df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 1504df22291fSAneesh Kumar K.V { 1505df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 150692b97816STheodore Ts'o struct super_block *sb = inode->i_sb; 150792b97816STheodore Ts'o 150892b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld", 15095dee5437STheodore Ts'o EXT4_C2B(EXT4_SB(inode->i_sb), 15105dee5437STheodore Ts'o ext4_count_free_clusters(inode->i_sb))); 151192b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Free/Dirty block details"); 151292b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "free_blocks=%lld", 151357042651STheodore Ts'o (long long) EXT4_C2B(EXT4_SB(inode->i_sb), 151457042651STheodore Ts'o percpu_counter_sum(&sbi->s_freeclusters_counter))); 151592b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld", 15167b415bf6SAditya Kali (long long) EXT4_C2B(EXT4_SB(inode->i_sb), 15177b415bf6SAditya Kali percpu_counter_sum(&sbi->s_dirtyclusters_counter))); 151892b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Block reservation details"); 151992b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u", 1520df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_data_blocks); 152192b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_meta_blocks=%u", 1522df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_meta_blocks); 1523df22291fSAneesh Kumar K.V return; 1524df22291fSAneesh Kumar K.V } 1525df22291fSAneesh Kumar K.V 1526b920c755STheodore Ts'o /* 15275a87b7a5STheodore Ts'o * mpage_da_map_and_submit - go through given space, map them 15285a87b7a5STheodore Ts'o * if necessary, and then submit them for I/O 152964769240SAlex Tomas * 15308dc207c0STheodore Ts'o * @mpd - bh describing space 153164769240SAlex Tomas * 153264769240SAlex Tomas * The function skips space we know is already mapped to disk blocks. 153364769240SAlex Tomas * 153464769240SAlex Tomas */ 15355a87b7a5STheodore Ts'o static void mpage_da_map_and_submit(struct mpage_da_data *mpd) 153664769240SAlex Tomas { 15372ac3b6e0STheodore Ts'o int err, blks, get_blocks_flags; 15381de3e3dfSTheodore Ts'o struct ext4_map_blocks map, *mapp = NULL; 15392fa3cdfbSTheodore Ts'o sector_t next = mpd->b_blocknr; 15402fa3cdfbSTheodore Ts'o unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits; 15412fa3cdfbSTheodore Ts'o loff_t disksize = EXT4_I(mpd->inode)->i_disksize; 15422fa3cdfbSTheodore Ts'o handle_t *handle = NULL; 154364769240SAlex Tomas 154464769240SAlex Tomas /* 15455a87b7a5STheodore Ts'o * If the blocks are mapped already, or we couldn't accumulate 15465a87b7a5STheodore Ts'o * any blocks, then proceed immediately to the submission stage. 154764769240SAlex Tomas */ 15485a87b7a5STheodore Ts'o if ((mpd->b_size == 0) || 15495a87b7a5STheodore Ts'o ((mpd->b_state & (1 << BH_Mapped)) && 155029fa89d0SAneesh Kumar K.V !(mpd->b_state & (1 << BH_Delay)) && 15515a87b7a5STheodore Ts'o !(mpd->b_state & (1 << BH_Unwritten)))) 15525a87b7a5STheodore Ts'o goto submit_io; 15532fa3cdfbSTheodore Ts'o 15542fa3cdfbSTheodore Ts'o handle = ext4_journal_current_handle(); 15552fa3cdfbSTheodore Ts'o BUG_ON(!handle); 15562fa3cdfbSTheodore Ts'o 155779ffab34SAneesh Kumar K.V /* 155879e83036SEric Sandeen * Call ext4_map_blocks() to allocate any delayed allocation 15592ac3b6e0STheodore Ts'o * blocks, or to convert an uninitialized extent to be 15602ac3b6e0STheodore Ts'o * initialized (in the case where we have written into 15612ac3b6e0STheodore Ts'o * one or more preallocated blocks). 15622ac3b6e0STheodore Ts'o * 15632ac3b6e0STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to 15642ac3b6e0STheodore Ts'o * indicate that we are on the delayed allocation path. This 15652ac3b6e0STheodore Ts'o * affects functions in many different parts of the allocation 15662ac3b6e0STheodore Ts'o * call path. This flag exists primarily because we don't 156779e83036SEric Sandeen * want to change *many* call functions, so ext4_map_blocks() 1568f2321097STheodore Ts'o * will set the EXT4_STATE_DELALLOC_RESERVED flag once the 15692ac3b6e0STheodore Ts'o * inode's allocation semaphore is taken. 15702ac3b6e0STheodore Ts'o * 15712ac3b6e0STheodore Ts'o * If the blocks in questions were delalloc blocks, set 15722ac3b6e0STheodore Ts'o * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting 15732ac3b6e0STheodore Ts'o * variables are updated after the blocks have been allocated. 157479ffab34SAneesh Kumar K.V */ 15752ed88685STheodore Ts'o map.m_lblk = next; 15762ed88685STheodore Ts'o map.m_len = max_blocks; 15771296cc85SAneesh Kumar K.V get_blocks_flags = EXT4_GET_BLOCKS_CREATE; 1578744692dcSJiaying Zhang if (ext4_should_dioread_nolock(mpd->inode)) 1579744692dcSJiaying Zhang get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; 15802ac3b6e0STheodore Ts'o if (mpd->b_state & (1 << BH_Delay)) 15811296cc85SAneesh Kumar K.V get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; 15821296cc85SAneesh Kumar K.V 15832ed88685STheodore Ts'o blks = ext4_map_blocks(handle, mpd->inode, &map, get_blocks_flags); 15842fa3cdfbSTheodore Ts'o if (blks < 0) { 1585e3570639SEric Sandeen struct super_block *sb = mpd->inode->i_sb; 1586e3570639SEric Sandeen 15872fa3cdfbSTheodore Ts'o err = blks; 1588ed5bde0bSTheodore Ts'o /* 15895a87b7a5STheodore Ts'o * If get block returns EAGAIN or ENOSPC and there 159097498956STheodore Ts'o * appears to be free blocks we will just let 159197498956STheodore Ts'o * mpage_da_submit_io() unlock all of the pages. 1592c4a0c46eSAneesh Kumar K.V */ 1593c4a0c46eSAneesh Kumar K.V if (err == -EAGAIN) 15945a87b7a5STheodore Ts'o goto submit_io; 1595df22291fSAneesh Kumar K.V 15965dee5437STheodore Ts'o if (err == -ENOSPC && ext4_count_free_clusters(sb)) { 1597df22291fSAneesh Kumar K.V mpd->retval = err; 15985a87b7a5STheodore Ts'o goto submit_io; 1599df22291fSAneesh Kumar K.V } 1600df22291fSAneesh Kumar K.V 1601c4a0c46eSAneesh Kumar K.V /* 1602ed5bde0bSTheodore Ts'o * get block failure will cause us to loop in 1603ed5bde0bSTheodore Ts'o * writepages, because a_ops->writepage won't be able 1604ed5bde0bSTheodore Ts'o * to make progress. The page will be redirtied by 1605ed5bde0bSTheodore Ts'o * writepage and writepages will again try to write 1606ed5bde0bSTheodore Ts'o * the same. 1607c4a0c46eSAneesh Kumar K.V */ 1608e3570639SEric Sandeen if (!(EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) { 1609e3570639SEric Sandeen ext4_msg(sb, KERN_CRIT, 1610e3570639SEric Sandeen "delayed block allocation failed for inode %lu " 1611e3570639SEric Sandeen "at logical offset %llu with max blocks %zd " 1612e3570639SEric Sandeen "with error %d", mpd->inode->i_ino, 1613c4a0c46eSAneesh Kumar K.V (unsigned long long) next, 16148dc207c0STheodore Ts'o mpd->b_size >> mpd->inode->i_blkbits, err); 1615e3570639SEric Sandeen ext4_msg(sb, KERN_CRIT, 161601a523ebSTheodore Ts'o "This should not happen!! Data will be lost"); 1617e3570639SEric Sandeen if (err == -ENOSPC) 1618df22291fSAneesh Kumar K.V ext4_print_free_blocks(mpd->inode); 1619030ba6bcSAneesh Kumar K.V } 16202fa3cdfbSTheodore Ts'o /* invalidate all the pages */ 1621c7f5938aSCurt Wohlgemuth ext4_da_block_invalidatepages(mpd); 1622e0fd9b90SCurt Wohlgemuth 1623e0fd9b90SCurt Wohlgemuth /* Mark this page range as having been completed */ 1624e0fd9b90SCurt Wohlgemuth mpd->io_done = 1; 16255a87b7a5STheodore Ts'o return; 1626c4a0c46eSAneesh Kumar K.V } 16272fa3cdfbSTheodore Ts'o BUG_ON(blks == 0); 16282fa3cdfbSTheodore Ts'o 16291de3e3dfSTheodore Ts'o mapp = ↦ 16302ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 16312ed88685STheodore Ts'o struct block_device *bdev = mpd->inode->i_sb->s_bdev; 16322ed88685STheodore Ts'o int i; 163364769240SAlex Tomas 16342ed88685STheodore Ts'o for (i = 0; i < map.m_len; i++) 16352ed88685STheodore Ts'o unmap_underlying_metadata(bdev, map.m_pblk + i); 16362fa3cdfbSTheodore Ts'o } 16372fa3cdfbSTheodore Ts'o 16382fa3cdfbSTheodore Ts'o /* 163903f5d8bcSJan Kara * Update on-disk size along with block allocation. 16402fa3cdfbSTheodore Ts'o */ 16412fa3cdfbSTheodore Ts'o disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits; 16422fa3cdfbSTheodore Ts'o if (disksize > i_size_read(mpd->inode)) 16432fa3cdfbSTheodore Ts'o disksize = i_size_read(mpd->inode); 16442fa3cdfbSTheodore Ts'o if (disksize > EXT4_I(mpd->inode)->i_disksize) { 16452fa3cdfbSTheodore Ts'o ext4_update_i_disksize(mpd->inode, disksize); 16465a87b7a5STheodore Ts'o err = ext4_mark_inode_dirty(handle, mpd->inode); 16475a87b7a5STheodore Ts'o if (err) 16485a87b7a5STheodore Ts'o ext4_error(mpd->inode->i_sb, 16495a87b7a5STheodore Ts'o "Failed to mark inode %lu dirty", 16505a87b7a5STheodore Ts'o mpd->inode->i_ino); 16512fa3cdfbSTheodore Ts'o } 16522fa3cdfbSTheodore Ts'o 16535a87b7a5STheodore Ts'o submit_io: 16541de3e3dfSTheodore Ts'o mpage_da_submit_io(mpd, mapp); 16555a87b7a5STheodore Ts'o mpd->io_done = 1; 165664769240SAlex Tomas } 165764769240SAlex Tomas 1658bf068ee2SAneesh Kumar K.V #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \ 1659bf068ee2SAneesh Kumar K.V (1 << BH_Delay) | (1 << BH_Unwritten)) 166064769240SAlex Tomas 166164769240SAlex Tomas /* 166264769240SAlex Tomas * mpage_add_bh_to_extent - try to add one more block to extent of blocks 166364769240SAlex Tomas * 166464769240SAlex Tomas * @mpd->lbh - extent of blocks 166564769240SAlex Tomas * @logical - logical number of the block in the file 1666b6a8e62fSJan Kara * @b_state - b_state of the buffer head added 166764769240SAlex Tomas * 166864769240SAlex Tomas * the function is used to collect contig. blocks in same state 166964769240SAlex Tomas */ 1670b6a8e62fSJan Kara static void mpage_add_bh_to_extent(struct mpage_da_data *mpd, sector_t logical, 16718dc207c0STheodore Ts'o unsigned long b_state) 167264769240SAlex Tomas { 167364769240SAlex Tomas sector_t next; 1674b6a8e62fSJan Kara int blkbits = mpd->inode->i_blkbits; 1675b6a8e62fSJan Kara int nrblocks = mpd->b_size >> blkbits; 167664769240SAlex Tomas 1677c445e3e0SEric Sandeen /* 1678c445e3e0SEric Sandeen * XXX Don't go larger than mballoc is willing to allocate 1679c445e3e0SEric Sandeen * This is a stopgap solution. We eventually need to fold 1680c445e3e0SEric Sandeen * mpage_da_submit_io() into this function and then call 168179e83036SEric Sandeen * ext4_map_blocks() multiple times in a loop 1682c445e3e0SEric Sandeen */ 1683b6a8e62fSJan Kara if (nrblocks >= (8*1024*1024 >> blkbits)) 1684c445e3e0SEric Sandeen goto flush_it; 1685c445e3e0SEric Sandeen 1686525f4ed8SMingming Cao /* check if the reserved journal credits might overflow */ 1687b6a8e62fSJan Kara if (!ext4_test_inode_flag(mpd->inode, EXT4_INODE_EXTENTS)) { 1688525f4ed8SMingming Cao if (nrblocks >= EXT4_MAX_TRANS_DATA) { 1689525f4ed8SMingming Cao /* 1690525f4ed8SMingming Cao * With non-extent format we are limited by the journal 1691525f4ed8SMingming Cao * credit available. Total credit needed to insert 1692525f4ed8SMingming Cao * nrblocks contiguous blocks is dependent on the 1693525f4ed8SMingming Cao * nrblocks. So limit nrblocks. 1694525f4ed8SMingming Cao */ 1695525f4ed8SMingming Cao goto flush_it; 1696525f4ed8SMingming Cao } 1697525f4ed8SMingming Cao } 169864769240SAlex Tomas /* 169964769240SAlex Tomas * First block in the extent 170064769240SAlex Tomas */ 17018dc207c0STheodore Ts'o if (mpd->b_size == 0) { 17028dc207c0STheodore Ts'o mpd->b_blocknr = logical; 1703b6a8e62fSJan Kara mpd->b_size = 1 << blkbits; 17048dc207c0STheodore Ts'o mpd->b_state = b_state & BH_FLAGS; 170564769240SAlex Tomas return; 170664769240SAlex Tomas } 170764769240SAlex Tomas 17088dc207c0STheodore Ts'o next = mpd->b_blocknr + nrblocks; 170964769240SAlex Tomas /* 171064769240SAlex Tomas * Can we merge the block to our big extent? 171164769240SAlex Tomas */ 17128dc207c0STheodore Ts'o if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) { 1713b6a8e62fSJan Kara mpd->b_size += 1 << blkbits; 171464769240SAlex Tomas return; 171564769240SAlex Tomas } 171664769240SAlex Tomas 1717525f4ed8SMingming Cao flush_it: 171864769240SAlex Tomas /* 171964769240SAlex Tomas * We couldn't merge the block to our extent, so we 172064769240SAlex Tomas * need to flush current extent and start new one 172164769240SAlex Tomas */ 17225a87b7a5STheodore Ts'o mpage_da_map_and_submit(mpd); 1723a1d6cc56SAneesh Kumar K.V return; 172464769240SAlex Tomas } 172564769240SAlex Tomas 1726c364b22cSAneesh Kumar K.V static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh) 172729fa89d0SAneesh Kumar K.V { 1728c364b22cSAneesh Kumar K.V return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 172929fa89d0SAneesh Kumar K.V } 173029fa89d0SAneesh Kumar K.V 173164769240SAlex Tomas /* 17325356f261SAditya Kali * This function is grabs code from the very beginning of 17335356f261SAditya Kali * ext4_map_blocks, but assumes that the caller is from delayed write 17345356f261SAditya Kali * time. This function looks up the requested blocks and sets the 17355356f261SAditya Kali * buffer delay bit under the protection of i_data_sem. 17365356f261SAditya Kali */ 17375356f261SAditya Kali static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, 17385356f261SAditya Kali struct ext4_map_blocks *map, 17395356f261SAditya Kali struct buffer_head *bh) 17405356f261SAditya Kali { 17415356f261SAditya Kali int retval; 17425356f261SAditya Kali sector_t invalid_block = ~((sector_t) 0xffff); 17435356f261SAditya Kali 17445356f261SAditya Kali if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 17455356f261SAditya Kali invalid_block = ~0; 17465356f261SAditya Kali 17475356f261SAditya Kali map->m_flags = 0; 17485356f261SAditya Kali ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u," 17495356f261SAditya Kali "logical block %lu\n", inode->i_ino, map->m_len, 17505356f261SAditya Kali (unsigned long) map->m_lblk); 17515356f261SAditya Kali /* 17525356f261SAditya Kali * Try to see if we can get the block without requesting a new 17535356f261SAditya Kali * file system block. 17545356f261SAditya Kali */ 17555356f261SAditya Kali down_read((&EXT4_I(inode)->i_data_sem)); 17569c3569b5STao Ma if (ext4_has_inline_data(inode)) { 17579c3569b5STao Ma /* 17589c3569b5STao Ma * We will soon create blocks for this page, and let 17599c3569b5STao Ma * us pretend as if the blocks aren't allocated yet. 17609c3569b5STao Ma * In case of clusters, we have to handle the work 17619c3569b5STao Ma * of mapping from cluster so that the reserved space 17629c3569b5STao Ma * is calculated properly. 17639c3569b5STao Ma */ 17649c3569b5STao Ma if ((EXT4_SB(inode->i_sb)->s_cluster_ratio > 1) && 17659c3569b5STao Ma ext4_find_delalloc_cluster(inode, map->m_lblk)) 17669c3569b5STao Ma map->m_flags |= EXT4_MAP_FROM_CLUSTER; 17679c3569b5STao Ma retval = 0; 17689c3569b5STao Ma } else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 17695356f261SAditya Kali retval = ext4_ext_map_blocks(NULL, inode, map, 0); 17705356f261SAditya Kali else 17715356f261SAditya Kali retval = ext4_ind_map_blocks(NULL, inode, map, 0); 17725356f261SAditya Kali 17735356f261SAditya Kali if (retval == 0) { 17745356f261SAditya Kali /* 17755356f261SAditya Kali * XXX: __block_prepare_write() unmaps passed block, 17765356f261SAditya Kali * is it OK? 17775356f261SAditya Kali */ 17785356f261SAditya Kali /* If the block was allocated from previously allocated cluster, 17795356f261SAditya Kali * then we dont need to reserve it again. */ 17805356f261SAditya Kali if (!(map->m_flags & EXT4_MAP_FROM_CLUSTER)) { 17815356f261SAditya Kali retval = ext4_da_reserve_space(inode, iblock); 17825356f261SAditya Kali if (retval) 17835356f261SAditya Kali /* not enough space to reserve */ 17845356f261SAditya Kali goto out_unlock; 17855356f261SAditya Kali } 17865356f261SAditya Kali 1787*fdc0212eSZheng Liu retval = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 1788*fdc0212eSZheng Liu ~0, EXTENT_STATUS_DELAYED); 178951865fdaSZheng Liu if (retval) 179051865fdaSZheng Liu goto out_unlock; 179151865fdaSZheng Liu 17925356f261SAditya Kali /* Clear EXT4_MAP_FROM_CLUSTER flag since its purpose is served 17935356f261SAditya Kali * and it should not appear on the bh->b_state. 17945356f261SAditya Kali */ 17955356f261SAditya Kali map->m_flags &= ~EXT4_MAP_FROM_CLUSTER; 17965356f261SAditya Kali 17975356f261SAditya Kali map_bh(bh, inode->i_sb, invalid_block); 17985356f261SAditya Kali set_buffer_new(bh); 17995356f261SAditya Kali set_buffer_delay(bh); 18005356f261SAditya Kali } 18015356f261SAditya Kali 18025356f261SAditya Kali out_unlock: 18035356f261SAditya Kali up_read((&EXT4_I(inode)->i_data_sem)); 18045356f261SAditya Kali 18055356f261SAditya Kali return retval; 18065356f261SAditya Kali } 18075356f261SAditya Kali 18085356f261SAditya Kali /* 1809b920c755STheodore Ts'o * This is a special get_blocks_t callback which is used by 1810b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 1811b920c755STheodore Ts'o * reserve space for a single block. 181229fa89d0SAneesh Kumar K.V * 181329fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 181429fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 181529fa89d0SAneesh Kumar K.V * 181629fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 181729fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 181829fa89d0SAneesh Kumar K.V * initialized properly. 181964769240SAlex Tomas */ 18209c3569b5STao Ma int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 18212ed88685STheodore Ts'o struct buffer_head *bh, int create) 182264769240SAlex Tomas { 18232ed88685STheodore Ts'o struct ext4_map_blocks map; 182464769240SAlex Tomas int ret = 0; 182564769240SAlex Tomas 182664769240SAlex Tomas BUG_ON(create == 0); 18272ed88685STheodore Ts'o BUG_ON(bh->b_size != inode->i_sb->s_blocksize); 18282ed88685STheodore Ts'o 18292ed88685STheodore Ts'o map.m_lblk = iblock; 18302ed88685STheodore Ts'o map.m_len = 1; 183164769240SAlex Tomas 183264769240SAlex Tomas /* 183364769240SAlex Tomas * first, we need to know whether the block is allocated already 183464769240SAlex Tomas * preallocated blocks are unmapped but should treated 183564769240SAlex Tomas * the same as allocated blocks. 183664769240SAlex Tomas */ 18375356f261SAditya Kali ret = ext4_da_map_blocks(inode, iblock, &map, bh); 18385356f261SAditya Kali if (ret <= 0) 18392ed88685STheodore Ts'o return ret; 184064769240SAlex Tomas 18412ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 18422ed88685STheodore Ts'o bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; 18432ed88685STheodore Ts'o 18442ed88685STheodore Ts'o if (buffer_unwritten(bh)) { 18452ed88685STheodore Ts'o /* A delayed write to unwritten bh should be marked 18462ed88685STheodore Ts'o * new and mapped. Mapped ensures that we don't do 18472ed88685STheodore Ts'o * get_block multiple times when we write to the same 18482ed88685STheodore Ts'o * offset and new ensures that we do proper zero out 18492ed88685STheodore Ts'o * for partial write. 18502ed88685STheodore Ts'o */ 18512ed88685STheodore Ts'o set_buffer_new(bh); 1852c8205636STheodore Ts'o set_buffer_mapped(bh); 18532ed88685STheodore Ts'o } 18542ed88685STheodore Ts'o return 0; 185564769240SAlex Tomas } 185661628a3fSMingming Cao 185762e086beSAneesh Kumar K.V static int bget_one(handle_t *handle, struct buffer_head *bh) 185862e086beSAneesh Kumar K.V { 185962e086beSAneesh Kumar K.V get_bh(bh); 186062e086beSAneesh Kumar K.V return 0; 186162e086beSAneesh Kumar K.V } 186262e086beSAneesh Kumar K.V 186362e086beSAneesh Kumar K.V static int bput_one(handle_t *handle, struct buffer_head *bh) 186462e086beSAneesh Kumar K.V { 186562e086beSAneesh Kumar K.V put_bh(bh); 186662e086beSAneesh Kumar K.V return 0; 186762e086beSAneesh Kumar K.V } 186862e086beSAneesh Kumar K.V 186962e086beSAneesh Kumar K.V static int __ext4_journalled_writepage(struct page *page, 187062e086beSAneesh Kumar K.V unsigned int len) 187162e086beSAneesh Kumar K.V { 187262e086beSAneesh Kumar K.V struct address_space *mapping = page->mapping; 187362e086beSAneesh Kumar K.V struct inode *inode = mapping->host; 18743fdcfb66STao Ma struct buffer_head *page_bufs = NULL; 187562e086beSAneesh Kumar K.V handle_t *handle = NULL; 18763fdcfb66STao Ma int ret = 0, err = 0; 18773fdcfb66STao Ma int inline_data = ext4_has_inline_data(inode); 18783fdcfb66STao Ma struct buffer_head *inode_bh = NULL; 187962e086beSAneesh Kumar K.V 1880cb20d518STheodore Ts'o ClearPageChecked(page); 18813fdcfb66STao Ma 18823fdcfb66STao Ma if (inline_data) { 18833fdcfb66STao Ma BUG_ON(page->index != 0); 18843fdcfb66STao Ma BUG_ON(len > ext4_get_max_inline_size(inode)); 18853fdcfb66STao Ma inode_bh = ext4_journalled_write_inline_data(inode, len, page); 18863fdcfb66STao Ma if (inode_bh == NULL) 18873fdcfb66STao Ma goto out; 18883fdcfb66STao Ma } else { 188962e086beSAneesh Kumar K.V page_bufs = page_buffers(page); 18903fdcfb66STao Ma if (!page_bufs) { 18913fdcfb66STao Ma BUG(); 18923fdcfb66STao Ma goto out; 18933fdcfb66STao Ma } 18943fdcfb66STao Ma ext4_walk_page_buffers(handle, page_bufs, 0, len, 18953fdcfb66STao Ma NULL, bget_one); 18963fdcfb66STao Ma } 189762e086beSAneesh Kumar K.V /* As soon as we unlock the page, it can go away, but we have 189862e086beSAneesh Kumar K.V * references to buffers so we are safe */ 189962e086beSAneesh Kumar K.V unlock_page(page); 190062e086beSAneesh Kumar K.V 19019924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 19029924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 190362e086beSAneesh Kumar K.V if (IS_ERR(handle)) { 190462e086beSAneesh Kumar K.V ret = PTR_ERR(handle); 190562e086beSAneesh Kumar K.V goto out; 190662e086beSAneesh Kumar K.V } 190762e086beSAneesh Kumar K.V 1908441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1909441c8508SCurt Wohlgemuth 19103fdcfb66STao Ma if (inline_data) { 19113fdcfb66STao Ma ret = ext4_journal_get_write_access(handle, inode_bh); 19123fdcfb66STao Ma 19133fdcfb66STao Ma err = ext4_handle_dirty_metadata(handle, inode, inode_bh); 19143fdcfb66STao Ma 19153fdcfb66STao Ma } else { 1916f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 191762e086beSAneesh Kumar K.V do_journal_get_write_access); 191862e086beSAneesh Kumar K.V 1919f19d5870STao Ma err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 192062e086beSAneesh Kumar K.V write_end_fn); 19213fdcfb66STao Ma } 192262e086beSAneesh Kumar K.V if (ret == 0) 192362e086beSAneesh Kumar K.V ret = err; 19242d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 192562e086beSAneesh Kumar K.V err = ext4_journal_stop(handle); 192662e086beSAneesh Kumar K.V if (!ret) 192762e086beSAneesh Kumar K.V ret = err; 192862e086beSAneesh Kumar K.V 19293fdcfb66STao Ma if (!ext4_has_inline_data(inode)) 19303fdcfb66STao Ma ext4_walk_page_buffers(handle, page_bufs, 0, len, 19313fdcfb66STao Ma NULL, bput_one); 193219f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 193362e086beSAneesh Kumar K.V out: 19343fdcfb66STao Ma brelse(inode_bh); 193562e086beSAneesh Kumar K.V return ret; 193662e086beSAneesh Kumar K.V } 193762e086beSAneesh Kumar K.V 193861628a3fSMingming Cao /* 193943ce1d23SAneesh Kumar K.V * Note that we don't need to start a transaction unless we're journaling data 194043ce1d23SAneesh Kumar K.V * because we should have holes filled from ext4_page_mkwrite(). We even don't 194143ce1d23SAneesh Kumar K.V * need to file the inode to the transaction's list in ordered mode because if 194243ce1d23SAneesh Kumar K.V * we are writing back data added by write(), the inode is already there and if 194343ce1d23SAneesh Kumar K.V * we are writing back data modified via mmap(), no one guarantees in which 194443ce1d23SAneesh Kumar K.V * transaction the data will hit the disk. In case we are journaling data, we 194543ce1d23SAneesh Kumar K.V * cannot start transaction directly because transaction start ranks above page 194643ce1d23SAneesh Kumar K.V * lock so we have to do some magic. 194743ce1d23SAneesh Kumar K.V * 1948b920c755STheodore Ts'o * This function can get called via... 1949b920c755STheodore Ts'o * - ext4_da_writepages after taking page lock (have journal handle) 1950b920c755STheodore Ts'o * - journal_submit_inode_data_buffers (no journal handle) 1951f6463b0dSArtem Bityutskiy * - shrink_page_list via the kswapd/direct reclaim (no journal handle) 1952b920c755STheodore Ts'o * - grab_page_cache when doing write_begin (have journal handle) 195343ce1d23SAneesh Kumar K.V * 195443ce1d23SAneesh Kumar K.V * We don't do any block allocation in this function. If we have page with 195543ce1d23SAneesh Kumar K.V * multiple blocks we need to write those buffer_heads that are mapped. This 195643ce1d23SAneesh Kumar K.V * is important for mmaped based write. So if we do with blocksize 1K 195743ce1d23SAneesh Kumar K.V * truncate(f, 1024); 195843ce1d23SAneesh Kumar K.V * a = mmap(f, 0, 4096); 195943ce1d23SAneesh Kumar K.V * a[0] = 'a'; 196043ce1d23SAneesh Kumar K.V * truncate(f, 4096); 196143ce1d23SAneesh Kumar K.V * we have in the page first buffer_head mapped via page_mkwrite call back 196290802ed9SPaul Bolle * but other buffer_heads would be unmapped but dirty (dirty done via the 196343ce1d23SAneesh Kumar K.V * do_wp_page). So writepage should write the first block. If we modify 196443ce1d23SAneesh Kumar K.V * the mmap area beyond 1024 we will again get a page_fault and the 196543ce1d23SAneesh Kumar K.V * page_mkwrite callback will do the block allocation and mark the 196643ce1d23SAneesh Kumar K.V * buffer_heads mapped. 196743ce1d23SAneesh Kumar K.V * 196843ce1d23SAneesh Kumar K.V * We redirty the page if we have any buffer_heads that is either delay or 196943ce1d23SAneesh Kumar K.V * unwritten in the page. 197043ce1d23SAneesh Kumar K.V * 197143ce1d23SAneesh Kumar K.V * We can get recursively called as show below. 197243ce1d23SAneesh Kumar K.V * 197343ce1d23SAneesh Kumar K.V * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> 197443ce1d23SAneesh Kumar K.V * ext4_writepage() 197543ce1d23SAneesh Kumar K.V * 197643ce1d23SAneesh Kumar K.V * But since we don't do any block allocation we should not deadlock. 197743ce1d23SAneesh Kumar K.V * Page also have the dirty flag cleared so we don't get recurive page_lock. 197861628a3fSMingming Cao */ 197943ce1d23SAneesh Kumar K.V static int ext4_writepage(struct page *page, 198064769240SAlex Tomas struct writeback_control *wbc) 198164769240SAlex Tomas { 1982f8bec370SJan Kara int ret = 0; 198361628a3fSMingming Cao loff_t size; 1984498e5f24STheodore Ts'o unsigned int len; 1985744692dcSJiaying Zhang struct buffer_head *page_bufs = NULL; 198661628a3fSMingming Cao struct inode *inode = page->mapping->host; 198736ade451SJan Kara struct ext4_io_submit io_submit; 198864769240SAlex Tomas 1989a9c667f8SLukas Czerner trace_ext4_writepage(page); 199061628a3fSMingming Cao size = i_size_read(inode); 199161628a3fSMingming Cao if (page->index == size >> PAGE_CACHE_SHIFT) 199261628a3fSMingming Cao len = size & ~PAGE_CACHE_MASK; 199361628a3fSMingming Cao else 199461628a3fSMingming Cao len = PAGE_CACHE_SIZE; 199561628a3fSMingming Cao 1996f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 1997fe386132SJan Kara /* 1998fe386132SJan Kara * We cannot do block allocation or other extent handling in this 1999fe386132SJan Kara * function. If there are buffers needing that, we have to redirty 2000fe386132SJan Kara * the page. But we may reach here when we do a journal commit via 2001fe386132SJan Kara * journal_submit_inode_data_buffers() and in that case we must write 2002fe386132SJan Kara * allocated buffers to achieve data=ordered mode guarantees. 2003fe386132SJan Kara */ 2004f19d5870STao Ma if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2005c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 2006f8bec370SJan Kara redirty_page_for_writepage(wbc, page); 2007fe386132SJan Kara if (current->flags & PF_MEMALLOC) { 2008fe386132SJan Kara /* 2009fe386132SJan Kara * For memory cleaning there's no point in writing only 2010fe386132SJan Kara * some buffers. So just bail out. Warn if we came here 2011fe386132SJan Kara * from direct reclaim. 2012fe386132SJan Kara */ 2013fe386132SJan Kara WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) 2014fe386132SJan Kara == PF_MEMALLOC); 2015f8bec370SJan Kara unlock_page(page); 2016f8bec370SJan Kara return 0; 2017f0e6c985SAneesh Kumar K.V } 2018fe386132SJan Kara } 201964769240SAlex Tomas 2020cb20d518STheodore Ts'o if (PageChecked(page) && ext4_should_journal_data(inode)) 202143ce1d23SAneesh Kumar K.V /* 202243ce1d23SAneesh Kumar K.V * It's mmapped pagecache. Add buffers and journal it. There 202343ce1d23SAneesh Kumar K.V * doesn't seem much point in redirtying the page here. 202443ce1d23SAneesh Kumar K.V */ 20253f0ca309SWu Fengguang return __ext4_journalled_writepage(page, len); 202643ce1d23SAneesh Kumar K.V 202736ade451SJan Kara memset(&io_submit, 0, sizeof(io_submit)); 202836ade451SJan Kara ret = ext4_bio_write_page(&io_submit, page, len, wbc); 202936ade451SJan Kara ext4_io_submit(&io_submit); 203064769240SAlex Tomas return ret; 203164769240SAlex Tomas } 203264769240SAlex Tomas 203361628a3fSMingming Cao /* 2034525f4ed8SMingming Cao * This is called via ext4_da_writepages() to 203525985edcSLucas De Marchi * calculate the total number of credits to reserve to fit 2036525f4ed8SMingming Cao * a single extent allocation into a single transaction, 2037525f4ed8SMingming Cao * ext4_da_writpeages() will loop calling this before 2038525f4ed8SMingming Cao * the block allocation. 203961628a3fSMingming Cao */ 2040525f4ed8SMingming Cao 2041525f4ed8SMingming Cao static int ext4_da_writepages_trans_blocks(struct inode *inode) 2042525f4ed8SMingming Cao { 2043525f4ed8SMingming Cao int max_blocks = EXT4_I(inode)->i_reserved_data_blocks; 2044525f4ed8SMingming Cao 2045525f4ed8SMingming Cao /* 2046525f4ed8SMingming Cao * With non-extent format the journal credit needed to 2047525f4ed8SMingming Cao * insert nrblocks contiguous block is dependent on 2048525f4ed8SMingming Cao * number of contiguous block. So we will limit 2049525f4ed8SMingming Cao * number of contiguous block to a sane value 2050525f4ed8SMingming Cao */ 205112e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) && 2052525f4ed8SMingming Cao (max_blocks > EXT4_MAX_TRANS_DATA)) 2053525f4ed8SMingming Cao max_blocks = EXT4_MAX_TRANS_DATA; 2054525f4ed8SMingming Cao 2055525f4ed8SMingming Cao return ext4_chunk_trans_blocks(inode, max_blocks); 2056525f4ed8SMingming Cao } 205761628a3fSMingming Cao 20588e48dcfbSTheodore Ts'o /* 20598e48dcfbSTheodore Ts'o * write_cache_pages_da - walk the list of dirty pages of the given 20608eb9e5ceSTheodore Ts'o * address space and accumulate pages that need writing, and call 2061168fc022STheodore Ts'o * mpage_da_map_and_submit to map a single contiguous memory region 2062168fc022STheodore Ts'o * and then write them. 20638e48dcfbSTheodore Ts'o */ 20649c3569b5STao Ma static int write_cache_pages_da(handle_t *handle, 20659c3569b5STao Ma struct address_space *mapping, 20668e48dcfbSTheodore Ts'o struct writeback_control *wbc, 206772f84e65SEric Sandeen struct mpage_da_data *mpd, 206872f84e65SEric Sandeen pgoff_t *done_index) 20698e48dcfbSTheodore Ts'o { 20708eb9e5ceSTheodore Ts'o struct buffer_head *bh, *head; 2071168fc022STheodore Ts'o struct inode *inode = mapping->host; 20728e48dcfbSTheodore Ts'o struct pagevec pvec; 20734f01b02cSTheodore Ts'o unsigned int nr_pages; 20744f01b02cSTheodore Ts'o sector_t logical; 20754f01b02cSTheodore Ts'o pgoff_t index, end; 20768e48dcfbSTheodore Ts'o long nr_to_write = wbc->nr_to_write; 20774f01b02cSTheodore Ts'o int i, tag, ret = 0; 20788e48dcfbSTheodore Ts'o 2079168fc022STheodore Ts'o memset(mpd, 0, sizeof(struct mpage_da_data)); 2080168fc022STheodore Ts'o mpd->wbc = wbc; 2081168fc022STheodore Ts'o mpd->inode = inode; 20828e48dcfbSTheodore Ts'o pagevec_init(&pvec, 0); 20838e48dcfbSTheodore Ts'o index = wbc->range_start >> PAGE_CACHE_SHIFT; 20848e48dcfbSTheodore Ts'o end = wbc->range_end >> PAGE_CACHE_SHIFT; 20858e48dcfbSTheodore Ts'o 20866e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 20875b41d924SEric Sandeen tag = PAGECACHE_TAG_TOWRITE; 20885b41d924SEric Sandeen else 20895b41d924SEric Sandeen tag = PAGECACHE_TAG_DIRTY; 20905b41d924SEric Sandeen 209172f84e65SEric Sandeen *done_index = index; 20924f01b02cSTheodore Ts'o while (index <= end) { 20935b41d924SEric Sandeen nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, 20948e48dcfbSTheodore Ts'o min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); 20958e48dcfbSTheodore Ts'o if (nr_pages == 0) 20964f01b02cSTheodore Ts'o return 0; 20978e48dcfbSTheodore Ts'o 20988e48dcfbSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 20998e48dcfbSTheodore Ts'o struct page *page = pvec.pages[i]; 21008e48dcfbSTheodore Ts'o 21018e48dcfbSTheodore Ts'o /* 21028e48dcfbSTheodore Ts'o * At this point, the page may be truncated or 21038e48dcfbSTheodore Ts'o * invalidated (changing page->mapping to NULL), or 21048e48dcfbSTheodore Ts'o * even swizzled back from swapper_space to tmpfs file 21058e48dcfbSTheodore Ts'o * mapping. However, page->index will not change 21068e48dcfbSTheodore Ts'o * because we have a reference on the page. 21078e48dcfbSTheodore Ts'o */ 21084f01b02cSTheodore Ts'o if (page->index > end) 21094f01b02cSTheodore Ts'o goto out; 21108e48dcfbSTheodore Ts'o 211172f84e65SEric Sandeen *done_index = page->index + 1; 211272f84e65SEric Sandeen 211378aaced3STheodore Ts'o /* 211478aaced3STheodore Ts'o * If we can't merge this page, and we have 211578aaced3STheodore Ts'o * accumulated an contiguous region, write it 211678aaced3STheodore Ts'o */ 211778aaced3STheodore Ts'o if ((mpd->next_page != page->index) && 211878aaced3STheodore Ts'o (mpd->next_page != mpd->first_page)) { 211978aaced3STheodore Ts'o mpage_da_map_and_submit(mpd); 212078aaced3STheodore Ts'o goto ret_extent_tail; 212178aaced3STheodore Ts'o } 212278aaced3STheodore Ts'o 21238e48dcfbSTheodore Ts'o lock_page(page); 21248e48dcfbSTheodore Ts'o 21258e48dcfbSTheodore Ts'o /* 21264f01b02cSTheodore Ts'o * If the page is no longer dirty, or its 21274f01b02cSTheodore Ts'o * mapping no longer corresponds to inode we 21284f01b02cSTheodore Ts'o * are writing (which means it has been 21294f01b02cSTheodore Ts'o * truncated or invalidated), or the page is 21304f01b02cSTheodore Ts'o * already under writeback and we are not 21314f01b02cSTheodore Ts'o * doing a data integrity writeback, skip the page 21328e48dcfbSTheodore Ts'o */ 21334f01b02cSTheodore Ts'o if (!PageDirty(page) || 21344f01b02cSTheodore Ts'o (PageWriteback(page) && 21354f01b02cSTheodore Ts'o (wbc->sync_mode == WB_SYNC_NONE)) || 21364f01b02cSTheodore Ts'o unlikely(page->mapping != mapping)) { 21378e48dcfbSTheodore Ts'o unlock_page(page); 21388e48dcfbSTheodore Ts'o continue; 21398e48dcfbSTheodore Ts'o } 21408e48dcfbSTheodore Ts'o 21418e48dcfbSTheodore Ts'o wait_on_page_writeback(page); 21428e48dcfbSTheodore Ts'o BUG_ON(PageWriteback(page)); 21438e48dcfbSTheodore Ts'o 21449c3569b5STao Ma /* 21459c3569b5STao Ma * If we have inline data and arrive here, it means that 21469c3569b5STao Ma * we will soon create the block for the 1st page, so 21479c3569b5STao Ma * we'd better clear the inline data here. 21489c3569b5STao Ma */ 21499c3569b5STao Ma if (ext4_has_inline_data(inode)) { 21509c3569b5STao Ma BUG_ON(ext4_test_inode_state(inode, 21519c3569b5STao Ma EXT4_STATE_MAY_INLINE_DATA)); 21529c3569b5STao Ma ext4_destroy_inline_data(handle, inode); 21539c3569b5STao Ma } 21549c3569b5STao Ma 2155168fc022STheodore Ts'o if (mpd->next_page != page->index) 21568eb9e5ceSTheodore Ts'o mpd->first_page = page->index; 21578eb9e5ceSTheodore Ts'o mpd->next_page = page->index + 1; 21588eb9e5ceSTheodore Ts'o logical = (sector_t) page->index << 21598eb9e5ceSTheodore Ts'o (PAGE_CACHE_SHIFT - inode->i_blkbits); 21608eb9e5ceSTheodore Ts'o 2161f8bec370SJan Kara /* Add all dirty buffers to mpd */ 21628eb9e5ceSTheodore Ts'o head = page_buffers(page); 21638eb9e5ceSTheodore Ts'o bh = head; 21648eb9e5ceSTheodore Ts'o do { 21658eb9e5ceSTheodore Ts'o BUG_ON(buffer_locked(bh)); 21668eb9e5ceSTheodore Ts'o /* 2167f8bec370SJan Kara * We need to try to allocate unmapped blocks 2168f8bec370SJan Kara * in the same page. Otherwise we won't make 2169f8bec370SJan Kara * progress with the page in ext4_writepage 21708eb9e5ceSTheodore Ts'o */ 21718eb9e5ceSTheodore Ts'o if (ext4_bh_delay_or_unwritten(NULL, bh)) { 21728eb9e5ceSTheodore Ts'o mpage_add_bh_to_extent(mpd, logical, 21738eb9e5ceSTheodore Ts'o bh->b_state); 21744f01b02cSTheodore Ts'o if (mpd->io_done) 21754f01b02cSTheodore Ts'o goto ret_extent_tail; 2176f8bec370SJan Kara } else if (buffer_dirty(bh) && 2177f8bec370SJan Kara buffer_mapped(bh)) { 21788eb9e5ceSTheodore Ts'o /* 2179f8bec370SJan Kara * mapped dirty buffer. We need to 2180f8bec370SJan Kara * update the b_state because we look 2181f8bec370SJan Kara * at b_state in mpage_da_map_blocks. 2182f8bec370SJan Kara * We don't update b_size because if we 2183f8bec370SJan Kara * find an unmapped buffer_head later 2184f8bec370SJan Kara * we need to use the b_state flag of 2185f8bec370SJan Kara * that buffer_head. 21868eb9e5ceSTheodore Ts'o */ 21878eb9e5ceSTheodore Ts'o if (mpd->b_size == 0) 2188f8bec370SJan Kara mpd->b_state = 2189f8bec370SJan Kara bh->b_state & BH_FLAGS; 21908e48dcfbSTheodore Ts'o } 21918eb9e5ceSTheodore Ts'o logical++; 21928eb9e5ceSTheodore Ts'o } while ((bh = bh->b_this_page) != head); 21938e48dcfbSTheodore Ts'o 21948e48dcfbSTheodore Ts'o if (nr_to_write > 0) { 21958e48dcfbSTheodore Ts'o nr_to_write--; 21968e48dcfbSTheodore Ts'o if (nr_to_write == 0 && 21974f01b02cSTheodore Ts'o wbc->sync_mode == WB_SYNC_NONE) 21988e48dcfbSTheodore Ts'o /* 21998e48dcfbSTheodore Ts'o * We stop writing back only if we are 22008e48dcfbSTheodore Ts'o * not doing integrity sync. In case of 22018e48dcfbSTheodore Ts'o * integrity sync we have to keep going 22028e48dcfbSTheodore Ts'o * because someone may be concurrently 22038e48dcfbSTheodore Ts'o * dirtying pages, and we might have 22048e48dcfbSTheodore Ts'o * synced a lot of newly appeared dirty 22058e48dcfbSTheodore Ts'o * pages, but have not synced all of the 22068e48dcfbSTheodore Ts'o * old dirty pages. 22078e48dcfbSTheodore Ts'o */ 22084f01b02cSTheodore Ts'o goto out; 22098e48dcfbSTheodore Ts'o } 22108e48dcfbSTheodore Ts'o } 22118e48dcfbSTheodore Ts'o pagevec_release(&pvec); 22128e48dcfbSTheodore Ts'o cond_resched(); 22138e48dcfbSTheodore Ts'o } 22144f01b02cSTheodore Ts'o return 0; 22154f01b02cSTheodore Ts'o ret_extent_tail: 22164f01b02cSTheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 22178eb9e5ceSTheodore Ts'o out: 22188eb9e5ceSTheodore Ts'o pagevec_release(&pvec); 22198eb9e5ceSTheodore Ts'o cond_resched(); 22208e48dcfbSTheodore Ts'o return ret; 22218e48dcfbSTheodore Ts'o } 22228e48dcfbSTheodore Ts'o 22238e48dcfbSTheodore Ts'o 222464769240SAlex Tomas static int ext4_da_writepages(struct address_space *mapping, 222564769240SAlex Tomas struct writeback_control *wbc) 222664769240SAlex Tomas { 222722208dedSAneesh Kumar K.V pgoff_t index; 222822208dedSAneesh Kumar K.V int range_whole = 0; 222961628a3fSMingming Cao handle_t *handle = NULL; 2230df22291fSAneesh Kumar K.V struct mpage_da_data mpd; 22315e745b04SAneesh Kumar K.V struct inode *inode = mapping->host; 2232498e5f24STheodore Ts'o int pages_written = 0; 223355138e0bSTheodore Ts'o unsigned int max_pages; 22342acf2c26SAneesh Kumar K.V int range_cyclic, cycled = 1, io_done = 0; 223555138e0bSTheodore Ts'o int needed_blocks, ret = 0; 223655138e0bSTheodore Ts'o long desired_nr_to_write, nr_to_writebump = 0; 2237de89de6eSTheodore Ts'o loff_t range_start = wbc->range_start; 22385e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 223972f84e65SEric Sandeen pgoff_t done_index = 0; 22405b41d924SEric Sandeen pgoff_t end; 22411bce63d1SShaohua Li struct blk_plug plug; 224261628a3fSMingming Cao 22439bffad1eSTheodore Ts'o trace_ext4_da_writepages(inode, wbc); 2244ba80b101STheodore Ts'o 224561628a3fSMingming Cao /* 224661628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 224761628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 224861628a3fSMingming Cao * because that could violate lock ordering on umount 224961628a3fSMingming Cao */ 2250a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 225161628a3fSMingming Cao return 0; 22522a21e37eSTheodore Ts'o 22532a21e37eSTheodore Ts'o /* 22542a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 22552a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 22562a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 22574ab2f15bSTheodore Ts'o * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because 22582a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 22592a21e37eSTheodore Ts'o * read-only, and in that case, ext4_da_writepages should 22602a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 22612a21e37eSTheodore Ts'o * the stack trace. 22622a21e37eSTheodore Ts'o */ 22634ab2f15bSTheodore Ts'o if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) 22642a21e37eSTheodore Ts'o return -EROFS; 22652a21e37eSTheodore Ts'o 226622208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 226722208dedSAneesh Kumar K.V range_whole = 1; 226861628a3fSMingming Cao 22692acf2c26SAneesh Kumar K.V range_cyclic = wbc->range_cyclic; 22702acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 227122208dedSAneesh Kumar K.V index = mapping->writeback_index; 22722acf2c26SAneesh Kumar K.V if (index) 22732acf2c26SAneesh Kumar K.V cycled = 0; 22742acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 22752acf2c26SAneesh Kumar K.V wbc->range_end = LLONG_MAX; 22762acf2c26SAneesh Kumar K.V wbc->range_cyclic = 0; 22775b41d924SEric Sandeen end = -1; 22785b41d924SEric Sandeen } else { 227922208dedSAneesh Kumar K.V index = wbc->range_start >> PAGE_CACHE_SHIFT; 22805b41d924SEric Sandeen end = wbc->range_end >> PAGE_CACHE_SHIFT; 22815b41d924SEric Sandeen } 2282a1d6cc56SAneesh Kumar K.V 228355138e0bSTheodore Ts'o /* 228455138e0bSTheodore Ts'o * This works around two forms of stupidity. The first is in 228555138e0bSTheodore Ts'o * the writeback code, which caps the maximum number of pages 228655138e0bSTheodore Ts'o * written to be 1024 pages. This is wrong on multiple 228755138e0bSTheodore Ts'o * levels; different architectues have a different page size, 228855138e0bSTheodore Ts'o * which changes the maximum amount of data which gets 228955138e0bSTheodore Ts'o * written. Secondly, 4 megabytes is way too small. XFS 229055138e0bSTheodore Ts'o * forces this value to be 16 megabytes by multiplying 229155138e0bSTheodore Ts'o * nr_to_write parameter by four, and then relies on its 229255138e0bSTheodore Ts'o * allocator to allocate larger extents to make them 229355138e0bSTheodore Ts'o * contiguous. Unfortunately this brings us to the second 229455138e0bSTheodore Ts'o * stupidity, which is that ext4's mballoc code only allocates 229555138e0bSTheodore Ts'o * at most 2048 blocks. So we force contiguous writes up to 229655138e0bSTheodore Ts'o * the number of dirty blocks in the inode, or 229755138e0bSTheodore Ts'o * sbi->max_writeback_mb_bump whichever is smaller. 229855138e0bSTheodore Ts'o */ 229955138e0bSTheodore Ts'o max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT); 2300b443e733SEric Sandeen if (!range_cyclic && range_whole) { 2301b443e733SEric Sandeen if (wbc->nr_to_write == LONG_MAX) 2302b443e733SEric Sandeen desired_nr_to_write = wbc->nr_to_write; 230355138e0bSTheodore Ts'o else 2304b443e733SEric Sandeen desired_nr_to_write = wbc->nr_to_write * 8; 2305b443e733SEric Sandeen } else 230655138e0bSTheodore Ts'o desired_nr_to_write = ext4_num_dirty_pages(inode, index, 230755138e0bSTheodore Ts'o max_pages); 230855138e0bSTheodore Ts'o if (desired_nr_to_write > max_pages) 230955138e0bSTheodore Ts'o desired_nr_to_write = max_pages; 231055138e0bSTheodore Ts'o 231155138e0bSTheodore Ts'o if (wbc->nr_to_write < desired_nr_to_write) { 231255138e0bSTheodore Ts'o nr_to_writebump = desired_nr_to_write - wbc->nr_to_write; 231355138e0bSTheodore Ts'o wbc->nr_to_write = desired_nr_to_write; 231455138e0bSTheodore Ts'o } 231555138e0bSTheodore Ts'o 23162acf2c26SAneesh Kumar K.V retry: 23176e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 23185b41d924SEric Sandeen tag_pages_for_writeback(mapping, index, end); 23195b41d924SEric Sandeen 23201bce63d1SShaohua Li blk_start_plug(&plug); 232122208dedSAneesh Kumar K.V while (!ret && wbc->nr_to_write > 0) { 2322a1d6cc56SAneesh Kumar K.V 2323a1d6cc56SAneesh Kumar K.V /* 2324a1d6cc56SAneesh Kumar K.V * we insert one extent at a time. So we need 2325a1d6cc56SAneesh Kumar K.V * credit needed for single extent allocation. 2326a1d6cc56SAneesh Kumar K.V * journalled mode is currently not supported 2327a1d6cc56SAneesh Kumar K.V * by delalloc 2328a1d6cc56SAneesh Kumar K.V */ 2329a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 2330525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 2331a1d6cc56SAneesh Kumar K.V 233261628a3fSMingming Cao /* start a new transaction*/ 23339924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 23349924a92aSTheodore Ts'o needed_blocks); 233561628a3fSMingming Cao if (IS_ERR(handle)) { 233661628a3fSMingming Cao ret = PTR_ERR(handle); 23371693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2338fbe845ddSCurt Wohlgemuth "%ld pages, ino %lu; err %d", __func__, 2339a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 23403c1fcb2cSNamjae Jeon blk_finish_plug(&plug); 234161628a3fSMingming Cao goto out_writepages; 234261628a3fSMingming Cao } 2343f63e6005STheodore Ts'o 2344f63e6005STheodore Ts'o /* 23458eb9e5ceSTheodore Ts'o * Now call write_cache_pages_da() to find the next 2346f63e6005STheodore Ts'o * contiguous region of logical blocks that need 23478eb9e5ceSTheodore Ts'o * blocks to be allocated by ext4 and submit them. 2348f63e6005STheodore Ts'o */ 23499c3569b5STao Ma ret = write_cache_pages_da(handle, mapping, 23509c3569b5STao Ma wbc, &mpd, &done_index); 2351f63e6005STheodore Ts'o /* 2352af901ca1SAndré Goddard Rosa * If we have a contiguous extent of pages and we 2353f63e6005STheodore Ts'o * haven't done the I/O yet, map the blocks and submit 2354f63e6005STheodore Ts'o * them for I/O. 2355f63e6005STheodore Ts'o */ 2356f63e6005STheodore Ts'o if (!mpd.io_done && mpd.next_page != mpd.first_page) { 23575a87b7a5STheodore Ts'o mpage_da_map_and_submit(&mpd); 2358f63e6005STheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 2359f63e6005STheodore Ts'o } 2360b3a3ca8cSTheodore Ts'o trace_ext4_da_write_pages(inode, &mpd); 2361f63e6005STheodore Ts'o wbc->nr_to_write -= mpd.pages_written; 2362df22291fSAneesh Kumar K.V 236361628a3fSMingming Cao ext4_journal_stop(handle); 2364df22291fSAneesh Kumar K.V 23658f64b32eSEric Sandeen if ((mpd.retval == -ENOSPC) && sbi->s_journal) { 236622208dedSAneesh Kumar K.V /* commit the transaction which would 236722208dedSAneesh Kumar K.V * free blocks released in the transaction 236822208dedSAneesh Kumar K.V * and try again 236922208dedSAneesh Kumar K.V */ 2370df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 237122208dedSAneesh Kumar K.V ret = 0; 237222208dedSAneesh Kumar K.V } else if (ret == MPAGE_DA_EXTENT_TAIL) { 2373a1d6cc56SAneesh Kumar K.V /* 23748de49e67SKazuya Mio * Got one extent now try with rest of the pages. 23758de49e67SKazuya Mio * If mpd.retval is set -EIO, journal is aborted. 23768de49e67SKazuya Mio * So we don't need to write any more. 2377a1d6cc56SAneesh Kumar K.V */ 237822208dedSAneesh Kumar K.V pages_written += mpd.pages_written; 23798de49e67SKazuya Mio ret = mpd.retval; 23802acf2c26SAneesh Kumar K.V io_done = 1; 238122208dedSAneesh Kumar K.V } else if (wbc->nr_to_write) 238261628a3fSMingming Cao /* 238361628a3fSMingming Cao * There is no more writeout needed 238461628a3fSMingming Cao * or we requested for a noblocking writeout 238561628a3fSMingming Cao * and we found the device congested 238661628a3fSMingming Cao */ 238761628a3fSMingming Cao break; 238861628a3fSMingming Cao } 23891bce63d1SShaohua Li blk_finish_plug(&plug); 23902acf2c26SAneesh Kumar K.V if (!io_done && !cycled) { 23912acf2c26SAneesh Kumar K.V cycled = 1; 23922acf2c26SAneesh Kumar K.V index = 0; 23932acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 23942acf2c26SAneesh Kumar K.V wbc->range_end = mapping->writeback_index - 1; 23952acf2c26SAneesh Kumar K.V goto retry; 23962acf2c26SAneesh Kumar K.V } 239761628a3fSMingming Cao 239822208dedSAneesh Kumar K.V /* Update index */ 23992acf2c26SAneesh Kumar K.V wbc->range_cyclic = range_cyclic; 240022208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 240122208dedSAneesh Kumar K.V /* 240222208dedSAneesh Kumar K.V * set the writeback_index so that range_cyclic 240322208dedSAneesh Kumar K.V * mode will write it back later 240422208dedSAneesh Kumar K.V */ 240572f84e65SEric Sandeen mapping->writeback_index = done_index; 2406a1d6cc56SAneesh Kumar K.V 240761628a3fSMingming Cao out_writepages: 240822208dedSAneesh Kumar K.V wbc->nr_to_write -= nr_to_writebump; 2409de89de6eSTheodore Ts'o wbc->range_start = range_start; 24109bffad1eSTheodore Ts'o trace_ext4_da_writepages_result(inode, wbc, ret, pages_written); 241161628a3fSMingming Cao return ret; 241264769240SAlex Tomas } 241364769240SAlex Tomas 241479f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 241579f0be8dSAneesh Kumar K.V { 241679f0be8dSAneesh Kumar K.V s64 free_blocks, dirty_blocks; 241779f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 241879f0be8dSAneesh Kumar K.V 241979f0be8dSAneesh Kumar K.V /* 242079f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 242179f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 2422179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 242379f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 242479f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 242579f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 242679f0be8dSAneesh Kumar K.V */ 242757042651STheodore Ts'o free_blocks = EXT4_C2B(sbi, 242857042651STheodore Ts'o percpu_counter_read_positive(&sbi->s_freeclusters_counter)); 242957042651STheodore Ts'o dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyclusters_counter); 243000d4e736STheodore Ts'o /* 243100d4e736STheodore Ts'o * Start pushing delalloc when 1/2 of free blocks are dirty. 243200d4e736STheodore Ts'o */ 243300d4e736STheodore Ts'o if (dirty_blocks && (free_blocks < 2 * dirty_blocks) && 243400d4e736STheodore Ts'o !writeback_in_progress(sb->s_bdi) && 243500d4e736STheodore Ts'o down_read_trylock(&sb->s_umount)) { 243600d4e736STheodore Ts'o writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE); 243700d4e736STheodore Ts'o up_read(&sb->s_umount); 243800d4e736STheodore Ts'o } 243900d4e736STheodore Ts'o 244079f0be8dSAneesh Kumar K.V if (2 * free_blocks < 3 * dirty_blocks || 2441df55c99dSTheodore Ts'o free_blocks < (dirty_blocks + EXT4_FREECLUSTERS_WATERMARK)) { 244279f0be8dSAneesh Kumar K.V /* 2443c8afb446SEric Sandeen * free block count is less than 150% of dirty blocks 2444c8afb446SEric Sandeen * or free blocks is less than watermark 244579f0be8dSAneesh Kumar K.V */ 244679f0be8dSAneesh Kumar K.V return 1; 244779f0be8dSAneesh Kumar K.V } 244879f0be8dSAneesh Kumar K.V return 0; 244979f0be8dSAneesh Kumar K.V } 245079f0be8dSAneesh Kumar K.V 245164769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 245264769240SAlex Tomas loff_t pos, unsigned len, unsigned flags, 245364769240SAlex Tomas struct page **pagep, void **fsdata) 245464769240SAlex Tomas { 245572b8ab9dSEric Sandeen int ret, retries = 0; 245664769240SAlex Tomas struct page *page; 245764769240SAlex Tomas pgoff_t index; 245864769240SAlex Tomas struct inode *inode = mapping->host; 245964769240SAlex Tomas handle_t *handle; 246064769240SAlex Tomas 246164769240SAlex Tomas index = pos >> PAGE_CACHE_SHIFT; 246279f0be8dSAneesh Kumar K.V 246379f0be8dSAneesh Kumar K.V if (ext4_nonda_switch(inode->i_sb)) { 246479f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 246579f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 246679f0be8dSAneesh Kumar K.V len, flags, pagep, fsdata); 246779f0be8dSAneesh Kumar K.V } 246879f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 24699bffad1eSTheodore Ts'o trace_ext4_da_write_begin(inode, pos, len, flags); 24709c3569b5STao Ma 24719c3569b5STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 24729c3569b5STao Ma ret = ext4_da_write_inline_data_begin(mapping, inode, 24739c3569b5STao Ma pos, len, flags, 24749c3569b5STao Ma pagep, fsdata); 24759c3569b5STao Ma if (ret < 0) 247647564bfbSTheodore Ts'o return ret; 247747564bfbSTheodore Ts'o if (ret == 1) 247847564bfbSTheodore Ts'o return 0; 24799c3569b5STao Ma } 24809c3569b5STao Ma 248147564bfbSTheodore Ts'o /* 248247564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 248347564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 248447564bfbSTheodore Ts'o * is being written back. So grab it first before we start 248547564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 248647564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 248747564bfbSTheodore Ts'o */ 248847564bfbSTheodore Ts'o retry_grab: 248947564bfbSTheodore Ts'o page = grab_cache_page_write_begin(mapping, index, flags); 249047564bfbSTheodore Ts'o if (!page) 249147564bfbSTheodore Ts'o return -ENOMEM; 249247564bfbSTheodore Ts'o unlock_page(page); 249347564bfbSTheodore Ts'o 249464769240SAlex Tomas /* 249564769240SAlex Tomas * With delayed allocation, we don't log the i_disksize update 249664769240SAlex Tomas * if there is delayed block allocation. But we still need 249764769240SAlex Tomas * to journalling the i_disksize update if writes to the end 249864769240SAlex Tomas * of file which has an already mapped buffer. 249964769240SAlex Tomas */ 250047564bfbSTheodore Ts'o retry_journal: 25019924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 1); 250264769240SAlex Tomas if (IS_ERR(handle)) { 250347564bfbSTheodore Ts'o page_cache_release(page); 250447564bfbSTheodore Ts'o return PTR_ERR(handle); 250564769240SAlex Tomas } 250664769240SAlex Tomas 250747564bfbSTheodore Ts'o lock_page(page); 250847564bfbSTheodore Ts'o if (page->mapping != mapping) { 250947564bfbSTheodore Ts'o /* The page got truncated from under us */ 251047564bfbSTheodore Ts'o unlock_page(page); 251147564bfbSTheodore Ts'o page_cache_release(page); 2512d5a0d4f7SEric Sandeen ext4_journal_stop(handle); 251347564bfbSTheodore Ts'o goto retry_grab; 2514d5a0d4f7SEric Sandeen } 251547564bfbSTheodore Ts'o /* In case writeback began while the page was unlocked */ 251647564bfbSTheodore Ts'o wait_on_page_writeback(page); 251764769240SAlex Tomas 25186e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep); 251964769240SAlex Tomas if (ret < 0) { 252064769240SAlex Tomas unlock_page(page); 252164769240SAlex Tomas ext4_journal_stop(handle); 2522ae4d5372SAneesh Kumar K.V /* 2523ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 2524ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 2525ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 2526ae4d5372SAneesh Kumar K.V */ 2527ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 2528b9a4207dSJan Kara ext4_truncate_failed_write(inode); 252947564bfbSTheodore Ts'o 253047564bfbSTheodore Ts'o if (ret == -ENOSPC && 253147564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 253247564bfbSTheodore Ts'o goto retry_journal; 253347564bfbSTheodore Ts'o 253447564bfbSTheodore Ts'o page_cache_release(page); 253547564bfbSTheodore Ts'o return ret; 253664769240SAlex Tomas } 253764769240SAlex Tomas 253847564bfbSTheodore Ts'o *pagep = page; 253964769240SAlex Tomas return ret; 254064769240SAlex Tomas } 254164769240SAlex Tomas 2542632eaeabSMingming Cao /* 2543632eaeabSMingming Cao * Check if we should update i_disksize 2544632eaeabSMingming Cao * when write to the end of file but not require block allocation 2545632eaeabSMingming Cao */ 2546632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page, 2547632eaeabSMingming Cao unsigned long offset) 2548632eaeabSMingming Cao { 2549632eaeabSMingming Cao struct buffer_head *bh; 2550632eaeabSMingming Cao struct inode *inode = page->mapping->host; 2551632eaeabSMingming Cao unsigned int idx; 2552632eaeabSMingming Cao int i; 2553632eaeabSMingming Cao 2554632eaeabSMingming Cao bh = page_buffers(page); 2555632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 2556632eaeabSMingming Cao 2557632eaeabSMingming Cao for (i = 0; i < idx; i++) 2558632eaeabSMingming Cao bh = bh->b_this_page; 2559632eaeabSMingming Cao 256029fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 2561632eaeabSMingming Cao return 0; 2562632eaeabSMingming Cao return 1; 2563632eaeabSMingming Cao } 2564632eaeabSMingming Cao 256564769240SAlex Tomas static int ext4_da_write_end(struct file *file, 256664769240SAlex Tomas struct address_space *mapping, 256764769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 256864769240SAlex Tomas struct page *page, void *fsdata) 256964769240SAlex Tomas { 257064769240SAlex Tomas struct inode *inode = mapping->host; 257164769240SAlex Tomas int ret = 0, ret2; 257264769240SAlex Tomas handle_t *handle = ext4_journal_current_handle(); 257364769240SAlex Tomas loff_t new_i_size; 2574632eaeabSMingming Cao unsigned long start, end; 257579f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 257679f0be8dSAneesh Kumar K.V 257779f0be8dSAneesh Kumar K.V if (write_mode == FALL_BACK_TO_NONDELALLOC) { 25783d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 25793d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 258079f0be8dSAneesh Kumar K.V return ext4_ordered_write_end(file, mapping, pos, 258179f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 25823d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 258379f0be8dSAneesh Kumar K.V return ext4_writeback_write_end(file, mapping, pos, 258479f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 25853d2b1582SLukas Czerner default: 258679f0be8dSAneesh Kumar K.V BUG(); 258779f0be8dSAneesh Kumar K.V } 258879f0be8dSAneesh Kumar K.V } 2589632eaeabSMingming Cao 25909bffad1eSTheodore Ts'o trace_ext4_da_write_end(inode, pos, len, copied); 2591632eaeabSMingming Cao start = pos & (PAGE_CACHE_SIZE - 1); 2592632eaeabSMingming Cao end = start + copied - 1; 259364769240SAlex Tomas 259464769240SAlex Tomas /* 259564769240SAlex Tomas * generic_write_end() will run mark_inode_dirty() if i_size 259664769240SAlex Tomas * changes. So let's piggyback the i_disksize mark_inode_dirty 259764769240SAlex Tomas * into that. 259864769240SAlex Tomas */ 259964769240SAlex Tomas new_i_size = pos + copied; 2600ea51d132SAndrea Arcangeli if (copied && new_i_size > EXT4_I(inode)->i_disksize) { 26019c3569b5STao Ma if (ext4_has_inline_data(inode) || 26029c3569b5STao Ma ext4_da_should_update_i_disksize(page, end)) { 2603632eaeabSMingming Cao down_write(&EXT4_I(inode)->i_data_sem); 2604f3b59291STheodore Ts'o if (new_i_size > EXT4_I(inode)->i_disksize) 260564769240SAlex Tomas EXT4_I(inode)->i_disksize = new_i_size; 2606632eaeabSMingming Cao up_write(&EXT4_I(inode)->i_data_sem); 2607cf17fea6SAneesh Kumar K.V /* We need to mark inode dirty even if 2608cf17fea6SAneesh Kumar K.V * new_i_size is less that inode->i_size 2609cf17fea6SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 2610cf17fea6SAneesh Kumar K.V */ 2611cf17fea6SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 2612632eaeabSMingming Cao } 2613632eaeabSMingming Cao } 26149c3569b5STao Ma 26159c3569b5STao Ma if (write_mode != CONVERT_INLINE_DATA && 26169c3569b5STao Ma ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) && 26179c3569b5STao Ma ext4_has_inline_data(inode)) 26189c3569b5STao Ma ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied, 26199c3569b5STao Ma page); 26209c3569b5STao Ma else 262164769240SAlex Tomas ret2 = generic_write_end(file, mapping, pos, len, copied, 262264769240SAlex Tomas page, fsdata); 26239c3569b5STao Ma 262464769240SAlex Tomas copied = ret2; 262564769240SAlex Tomas if (ret2 < 0) 262664769240SAlex Tomas ret = ret2; 262764769240SAlex Tomas ret2 = ext4_journal_stop(handle); 262864769240SAlex Tomas if (!ret) 262964769240SAlex Tomas ret = ret2; 263064769240SAlex Tomas 263164769240SAlex Tomas return ret ? ret : copied; 263264769240SAlex Tomas } 263364769240SAlex Tomas 263464769240SAlex Tomas static void ext4_da_invalidatepage(struct page *page, unsigned long offset) 263564769240SAlex Tomas { 263664769240SAlex Tomas /* 263764769240SAlex Tomas * Drop reserved blocks 263864769240SAlex Tomas */ 263964769240SAlex Tomas BUG_ON(!PageLocked(page)); 264064769240SAlex Tomas if (!page_has_buffers(page)) 264164769240SAlex Tomas goto out; 264264769240SAlex Tomas 2643d2a17637SMingming Cao ext4_da_page_release_reservation(page, offset); 264464769240SAlex Tomas 264564769240SAlex Tomas out: 264664769240SAlex Tomas ext4_invalidatepage(page, offset); 264764769240SAlex Tomas 264864769240SAlex Tomas return; 264964769240SAlex Tomas } 265064769240SAlex Tomas 2651ccd2506bSTheodore Ts'o /* 2652ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 2653ccd2506bSTheodore Ts'o */ 2654ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 2655ccd2506bSTheodore Ts'o { 2656fb40ba0dSTheodore Ts'o trace_ext4_alloc_da_blocks(inode); 2657fb40ba0dSTheodore Ts'o 2658ccd2506bSTheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks && 2659ccd2506bSTheodore Ts'o !EXT4_I(inode)->i_reserved_meta_blocks) 2660ccd2506bSTheodore Ts'o return 0; 2661ccd2506bSTheodore Ts'o 2662ccd2506bSTheodore Ts'o /* 2663ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 2664ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 2665ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 2666ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 2667ccd2506bSTheodore Ts'o * would require replicating code paths in: 2668ccd2506bSTheodore Ts'o * 2669ccd2506bSTheodore Ts'o * ext4_da_writepages() -> 2670ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 2671ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 2672ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 2673ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 2674ccd2506bSTheodore Ts'o * 2675ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 2676ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 2677ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 2678ccd2506bSTheodore Ts'o * doing I/O at all. 2679ccd2506bSTheodore Ts'o * 2680ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 2681380cf090SWu Fengguang * the pages by calling redirty_page_for_writepage() but that 2682ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 2683ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 268425985edcSLucas De Marchi * simplifying them because we wouldn't actually intend to 2685ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 2686ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 2687ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 2688ccd2506bSTheodore Ts'o * 2689ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 2690ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 2691ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 2692ccd2506bSTheodore Ts'o */ 2693ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 2694ccd2506bSTheodore Ts'o } 269564769240SAlex Tomas 269664769240SAlex Tomas /* 2697ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 2698ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 2699ac27a0ecSDave Kleikamp * 2700ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 2701617ba13bSMingming Cao * journal. If somebody makes a swapfile on an ext4 data-journaling 2702ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 2703ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 2704ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 2705ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 2706ac27a0ecSDave Kleikamp * 2707ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 2708ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 2709ac27a0ecSDave Kleikamp */ 2710617ba13bSMingming Cao static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 2711ac27a0ecSDave Kleikamp { 2712ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 2713ac27a0ecSDave Kleikamp journal_t *journal; 2714ac27a0ecSDave Kleikamp int err; 2715ac27a0ecSDave Kleikamp 271646c7f254STao Ma /* 271746c7f254STao Ma * We can get here for an inline file via the FIBMAP ioctl 271846c7f254STao Ma */ 271946c7f254STao Ma if (ext4_has_inline_data(inode)) 272046c7f254STao Ma return 0; 272146c7f254STao Ma 272264769240SAlex Tomas if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 272364769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) { 272464769240SAlex Tomas /* 272564769240SAlex Tomas * With delalloc we want to sync the file 272664769240SAlex Tomas * so that we can make sure we allocate 272764769240SAlex Tomas * blocks for file 272864769240SAlex Tomas */ 272964769240SAlex Tomas filemap_write_and_wait(mapping); 273064769240SAlex Tomas } 273164769240SAlex Tomas 273219f5fb7aSTheodore Ts'o if (EXT4_JOURNAL(inode) && 273319f5fb7aSTheodore Ts'o ext4_test_inode_state(inode, EXT4_STATE_JDATA)) { 2734ac27a0ecSDave Kleikamp /* 2735ac27a0ecSDave Kleikamp * This is a REALLY heavyweight approach, but the use of 2736ac27a0ecSDave Kleikamp * bmap on dirty files is expected to be extremely rare: 2737ac27a0ecSDave Kleikamp * only if we run lilo or swapon on a freshly made file 2738ac27a0ecSDave Kleikamp * do we expect this to happen. 2739ac27a0ecSDave Kleikamp * 2740ac27a0ecSDave Kleikamp * (bmap requires CAP_SYS_RAWIO so this does not 2741ac27a0ecSDave Kleikamp * represent an unprivileged user DOS attack --- we'd be 2742ac27a0ecSDave Kleikamp * in trouble if mortal users could trigger this path at 2743ac27a0ecSDave Kleikamp * will.) 2744ac27a0ecSDave Kleikamp * 2745617ba13bSMingming Cao * NB. EXT4_STATE_JDATA is not set on files other than 2746ac27a0ecSDave Kleikamp * regular files. If somebody wants to bmap a directory 2747ac27a0ecSDave Kleikamp * or symlink and gets confused because the buffer 2748ac27a0ecSDave Kleikamp * hasn't yet been flushed to disk, they deserve 2749ac27a0ecSDave Kleikamp * everything they get. 2750ac27a0ecSDave Kleikamp */ 2751ac27a0ecSDave Kleikamp 275219f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_JDATA); 2753617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 2754dab291afSMingming Cao jbd2_journal_lock_updates(journal); 2755dab291afSMingming Cao err = jbd2_journal_flush(journal); 2756dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 2757ac27a0ecSDave Kleikamp 2758ac27a0ecSDave Kleikamp if (err) 2759ac27a0ecSDave Kleikamp return 0; 2760ac27a0ecSDave Kleikamp } 2761ac27a0ecSDave Kleikamp 2762617ba13bSMingming Cao return generic_block_bmap(mapping, block, ext4_get_block); 2763ac27a0ecSDave Kleikamp } 2764ac27a0ecSDave Kleikamp 2765617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page) 2766ac27a0ecSDave Kleikamp { 276746c7f254STao Ma int ret = -EAGAIN; 276846c7f254STao Ma struct inode *inode = page->mapping->host; 276946c7f254STao Ma 27700562e0baSJiaying Zhang trace_ext4_readpage(page); 277146c7f254STao Ma 277246c7f254STao Ma if (ext4_has_inline_data(inode)) 277346c7f254STao Ma ret = ext4_readpage_inline(inode, page); 277446c7f254STao Ma 277546c7f254STao Ma if (ret == -EAGAIN) 2776617ba13bSMingming Cao return mpage_readpage(page, ext4_get_block); 277746c7f254STao Ma 277846c7f254STao Ma return ret; 2779ac27a0ecSDave Kleikamp } 2780ac27a0ecSDave Kleikamp 2781ac27a0ecSDave Kleikamp static int 2782617ba13bSMingming Cao ext4_readpages(struct file *file, struct address_space *mapping, 2783ac27a0ecSDave Kleikamp struct list_head *pages, unsigned nr_pages) 2784ac27a0ecSDave Kleikamp { 278546c7f254STao Ma struct inode *inode = mapping->host; 278646c7f254STao Ma 278746c7f254STao Ma /* If the file has inline data, no need to do readpages. */ 278846c7f254STao Ma if (ext4_has_inline_data(inode)) 278946c7f254STao Ma return 0; 279046c7f254STao Ma 2791617ba13bSMingming Cao return mpage_readpages(mapping, pages, nr_pages, ext4_get_block); 2792ac27a0ecSDave Kleikamp } 2793ac27a0ecSDave Kleikamp 2794617ba13bSMingming Cao static void ext4_invalidatepage(struct page *page, unsigned long offset) 2795ac27a0ecSDave Kleikamp { 27960562e0baSJiaying Zhang trace_ext4_invalidatepage(page, offset); 27970562e0baSJiaying Zhang 27984520fb3cSJan Kara /* No journalling happens on data buffers when this function is used */ 27994520fb3cSJan Kara WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page))); 28004520fb3cSJan Kara 28014520fb3cSJan Kara block_invalidatepage(page, offset); 28024520fb3cSJan Kara } 28034520fb3cSJan Kara 280453e87268SJan Kara static int __ext4_journalled_invalidatepage(struct page *page, 28054520fb3cSJan Kara unsigned long offset) 28064520fb3cSJan Kara { 28074520fb3cSJan Kara journal_t *journal = EXT4_JOURNAL(page->mapping->host); 28084520fb3cSJan Kara 28094520fb3cSJan Kara trace_ext4_journalled_invalidatepage(page, offset); 28104520fb3cSJan Kara 2811744692dcSJiaying Zhang /* 2812ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 2813ac27a0ecSDave Kleikamp */ 2814ac27a0ecSDave Kleikamp if (offset == 0) 2815ac27a0ecSDave Kleikamp ClearPageChecked(page); 2816ac27a0ecSDave Kleikamp 281753e87268SJan Kara return jbd2_journal_invalidatepage(journal, page, offset); 281853e87268SJan Kara } 281953e87268SJan Kara 282053e87268SJan Kara /* Wrapper for aops... */ 282153e87268SJan Kara static void ext4_journalled_invalidatepage(struct page *page, 282253e87268SJan Kara unsigned long offset) 282353e87268SJan Kara { 282453e87268SJan Kara WARN_ON(__ext4_journalled_invalidatepage(page, offset) < 0); 2825ac27a0ecSDave Kleikamp } 2826ac27a0ecSDave Kleikamp 2827617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait) 2828ac27a0ecSDave Kleikamp { 2829617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 2830ac27a0ecSDave Kleikamp 28310562e0baSJiaying Zhang trace_ext4_releasepage(page); 28320562e0baSJiaying Zhang 2833ac27a0ecSDave Kleikamp WARN_ON(PageChecked(page)); 2834ac27a0ecSDave Kleikamp if (!page_has_buffers(page)) 2835ac27a0ecSDave Kleikamp return 0; 28360390131bSFrank Mayhar if (journal) 2837dab291afSMingming Cao return jbd2_journal_try_to_free_buffers(journal, page, wait); 28380390131bSFrank Mayhar else 28390390131bSFrank Mayhar return try_to_free_buffers(page); 2840ac27a0ecSDave Kleikamp } 2841ac27a0ecSDave Kleikamp 2842ac27a0ecSDave Kleikamp /* 28432ed88685STheodore Ts'o * ext4_get_block used when preparing for a DIO write or buffer write. 28442ed88685STheodore Ts'o * We allocate an uinitialized extent if blocks haven't been allocated. 28452ed88685STheodore Ts'o * The extent will be converted to initialized after the IO is complete. 28462ed88685STheodore Ts'o */ 2847f19d5870STao Ma int ext4_get_block_write(struct inode *inode, sector_t iblock, 28484c0425ffSMingming Cao struct buffer_head *bh_result, int create) 28494c0425ffSMingming Cao { 2850c7064ef1SJiaying Zhang ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n", 28518d5d02e6SMingming Cao inode->i_ino, create); 28522ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh_result, 28532ed88685STheodore Ts'o EXT4_GET_BLOCKS_IO_CREATE_EXT); 28544c0425ffSMingming Cao } 28554c0425ffSMingming Cao 2856729f52c6SZheng Liu static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock, 28578b0f165fSAnatol Pomozov struct buffer_head *bh_result, int create) 2858729f52c6SZheng Liu { 28598b0f165fSAnatol Pomozov ext4_debug("ext4_get_block_write_nolock: inode %lu, create flag %d\n", 28608b0f165fSAnatol Pomozov inode->i_ino, create); 28618b0f165fSAnatol Pomozov return _ext4_get_block(inode, iblock, bh_result, 28628b0f165fSAnatol Pomozov EXT4_GET_BLOCKS_NO_LOCK); 2863729f52c6SZheng Liu } 2864729f52c6SZheng Liu 28654c0425ffSMingming Cao static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset, 2866552ef802SChristoph Hellwig ssize_t size, void *private, int ret, 2867552ef802SChristoph Hellwig bool is_async) 28684c0425ffSMingming Cao { 286972c5052dSChristoph Hellwig struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode; 28704c0425ffSMingming Cao ext4_io_end_t *io_end = iocb->private; 28714c0425ffSMingming Cao 28724b70df18SMingming /* if not async direct IO or dio with 0 bytes write, just return */ 28734b70df18SMingming if (!io_end || !size) 2874552ef802SChristoph Hellwig goto out; 28754b70df18SMingming 28768d5d02e6SMingming Cao ext_debug("ext4_end_io_dio(): io_end 0x%p " 2877ace36ad4SJoe Perches "for inode %lu, iocb 0x%p, offset %llu, size %zd\n", 28788d5d02e6SMingming Cao iocb->private, io_end->inode->i_ino, iocb, offset, 28798d5d02e6SMingming Cao size); 28808d5d02e6SMingming Cao 2881b5a7e970STheodore Ts'o iocb->private = NULL; 2882b5a7e970STheodore Ts'o 28838d5d02e6SMingming Cao /* if not aio dio with unwritten extents, just free io and return */ 2884bd2d0210STheodore Ts'o if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) { 28858d5d02e6SMingming Cao ext4_free_io_end(io_end); 28865b3ff237Sjiayingz@google.com (Jiaying Zhang) out: 2887091e26dfSJan Kara inode_dio_done(inode); 28885b3ff237Sjiayingz@google.com (Jiaying Zhang) if (is_async) 28895b3ff237Sjiayingz@google.com (Jiaying Zhang) aio_complete(iocb, ret, 0); 28905b3ff237Sjiayingz@google.com (Jiaying Zhang) return; 28918d5d02e6SMingming Cao } 28928d5d02e6SMingming Cao 28934c0425ffSMingming Cao io_end->offset = offset; 28944c0425ffSMingming Cao io_end->size = size; 28955b3ff237Sjiayingz@google.com (Jiaying Zhang) if (is_async) { 28965b3ff237Sjiayingz@google.com (Jiaying Zhang) io_end->iocb = iocb; 28975b3ff237Sjiayingz@google.com (Jiaying Zhang) io_end->result = ret; 28985b3ff237Sjiayingz@google.com (Jiaying Zhang) } 28994c0425ffSMingming Cao 290028a535f9SDmitry Monakhov ext4_add_complete_io(io_end); 29014c0425ffSMingming Cao } 2902c7064ef1SJiaying Zhang 29034c0425ffSMingming Cao /* 29044c0425ffSMingming Cao * For ext4 extent files, ext4 will do direct-io write to holes, 29054c0425ffSMingming Cao * preallocated extents, and those write extend the file, no need to 29064c0425ffSMingming Cao * fall back to buffered IO. 29074c0425ffSMingming Cao * 2908b595076aSUwe Kleine-König * For holes, we fallocate those blocks, mark them as uninitialized 290969c499d1STheodore Ts'o * If those blocks were preallocated, we mark sure they are split, but 2910b595076aSUwe Kleine-König * still keep the range to write as uninitialized. 29114c0425ffSMingming Cao * 291269c499d1STheodore Ts'o * The unwritten extents will be converted to written when DIO is completed. 29138d5d02e6SMingming Cao * For async direct IO, since the IO may still pending when return, we 291425985edcSLucas De Marchi * set up an end_io call back function, which will do the conversion 29158d5d02e6SMingming Cao * when async direct IO completed. 29164c0425ffSMingming Cao * 29174c0425ffSMingming Cao * If the O_DIRECT write will extend the file then add this inode to the 29184c0425ffSMingming Cao * orphan list. So recovery will truncate it back to the original size 29194c0425ffSMingming Cao * if the machine crashes during the write. 29204c0425ffSMingming Cao * 29214c0425ffSMingming Cao */ 29224c0425ffSMingming Cao static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb, 29234c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 29244c0425ffSMingming Cao unsigned long nr_segs) 29254c0425ffSMingming Cao { 29264c0425ffSMingming Cao struct file *file = iocb->ki_filp; 29274c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 29284c0425ffSMingming Cao ssize_t ret; 29294c0425ffSMingming Cao size_t count = iov_length(iov, nr_segs); 2930729f52c6SZheng Liu int overwrite = 0; 29318b0f165fSAnatol Pomozov get_block_t *get_block_func = NULL; 29328b0f165fSAnatol Pomozov int dio_flags = 0; 293369c499d1STheodore Ts'o loff_t final_size = offset + count; 293469c499d1STheodore Ts'o 293569c499d1STheodore Ts'o /* Use the old path for reads and writes beyond i_size. */ 293669c499d1STheodore Ts'o if (rw != WRITE || final_size > inode->i_size) 293769c499d1STheodore Ts'o return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 2938729f52c6SZheng Liu 29394bd809dbSZheng Liu BUG_ON(iocb->private == NULL); 29404bd809dbSZheng Liu 29414bd809dbSZheng Liu /* If we do a overwrite dio, i_mutex locking can be released */ 29424bd809dbSZheng Liu overwrite = *((int *)iocb->private); 29434bd809dbSZheng Liu 29444bd809dbSZheng Liu if (overwrite) { 29451f555cfaSDmitry Monakhov atomic_inc(&inode->i_dio_count); 29464bd809dbSZheng Liu down_read(&EXT4_I(inode)->i_data_sem); 29474bd809dbSZheng Liu mutex_unlock(&inode->i_mutex); 29484bd809dbSZheng Liu } 29494bd809dbSZheng Liu 29504c0425ffSMingming Cao /* 29518d5d02e6SMingming Cao * We could direct write to holes and fallocate. 29528d5d02e6SMingming Cao * 295369c499d1STheodore Ts'o * Allocated blocks to fill the hole are marked as 295469c499d1STheodore Ts'o * uninitialized to prevent parallel buffered read to expose 295569c499d1STheodore Ts'o * the stale data before DIO complete the data IO. 29568d5d02e6SMingming Cao * 295769c499d1STheodore Ts'o * As to previously fallocated extents, ext4 get_block will 295869c499d1STheodore Ts'o * just simply mark the buffer mapped but still keep the 295969c499d1STheodore Ts'o * extents uninitialized. 29604c0425ffSMingming Cao * 296169c499d1STheodore Ts'o * For non AIO case, we will convert those unwritten extents 29628d5d02e6SMingming Cao * to written after return back from blockdev_direct_IO. 29634c0425ffSMingming Cao * 296469c499d1STheodore Ts'o * For async DIO, the conversion needs to be deferred when the 296569c499d1STheodore Ts'o * IO is completed. The ext4 end_io callback function will be 296669c499d1STheodore Ts'o * called to take care of the conversion work. Here for async 296769c499d1STheodore Ts'o * case, we allocate an io_end structure to hook to the iocb. 29684c0425ffSMingming Cao */ 29698d5d02e6SMingming Cao iocb->private = NULL; 2970f45ee3a1SDmitry Monakhov ext4_inode_aio_set(inode, NULL); 29718d5d02e6SMingming Cao if (!is_sync_kiocb(iocb)) { 297269c499d1STheodore Ts'o ext4_io_end_t *io_end = ext4_init_io_end(inode, GFP_NOFS); 29734bd809dbSZheng Liu if (!io_end) { 29744bd809dbSZheng Liu ret = -ENOMEM; 29754bd809dbSZheng Liu goto retake_lock; 29764bd809dbSZheng Liu } 2977266991b1SJeff Moyer io_end->flag |= EXT4_IO_END_DIRECT; 2978266991b1SJeff Moyer iocb->private = io_end; 29798d5d02e6SMingming Cao /* 298069c499d1STheodore Ts'o * we save the io structure for current async direct 298169c499d1STheodore Ts'o * IO, so that later ext4_map_blocks() could flag the 298269c499d1STheodore Ts'o * io structure whether there is a unwritten extents 298369c499d1STheodore Ts'o * needs to be converted when IO is completed. 29848d5d02e6SMingming Cao */ 2985f45ee3a1SDmitry Monakhov ext4_inode_aio_set(inode, io_end); 29868d5d02e6SMingming Cao } 29878d5d02e6SMingming Cao 29888b0f165fSAnatol Pomozov if (overwrite) { 29898b0f165fSAnatol Pomozov get_block_func = ext4_get_block_write_nolock; 29908b0f165fSAnatol Pomozov } else { 29918b0f165fSAnatol Pomozov get_block_func = ext4_get_block_write; 29928b0f165fSAnatol Pomozov dio_flags = DIO_LOCKING; 29938b0f165fSAnatol Pomozov } 2994729f52c6SZheng Liu ret = __blockdev_direct_IO(rw, iocb, inode, 2995729f52c6SZheng Liu inode->i_sb->s_bdev, iov, 2996729f52c6SZheng Liu offset, nr_segs, 29978b0f165fSAnatol Pomozov get_block_func, 2998729f52c6SZheng Liu ext4_end_io_dio, 2999729f52c6SZheng Liu NULL, 30008b0f165fSAnatol Pomozov dio_flags); 30018b0f165fSAnatol Pomozov 30028d5d02e6SMingming Cao if (iocb->private) 3003f45ee3a1SDmitry Monakhov ext4_inode_aio_set(inode, NULL); 30048d5d02e6SMingming Cao /* 300569c499d1STheodore Ts'o * The io_end structure takes a reference to the inode, that 300669c499d1STheodore Ts'o * structure needs to be destroyed and the reference to the 300769c499d1STheodore Ts'o * inode need to be dropped, when IO is complete, even with 0 300869c499d1STheodore Ts'o * byte write, or failed. 30098d5d02e6SMingming Cao * 301069c499d1STheodore Ts'o * In the successful AIO DIO case, the io_end structure will 301169c499d1STheodore Ts'o * be destroyed and the reference to the inode will be dropped 30128d5d02e6SMingming Cao * after the end_io call back function is called. 30138d5d02e6SMingming Cao * 301469c499d1STheodore Ts'o * In the case there is 0 byte write, or error case, since VFS 301569c499d1STheodore Ts'o * direct IO won't invoke the end_io call back function, we 301669c499d1STheodore Ts'o * need to free the end_io structure here. 30178d5d02e6SMingming Cao */ 30188d5d02e6SMingming Cao if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) { 30198d5d02e6SMingming Cao ext4_free_io_end(iocb->private); 30208d5d02e6SMingming Cao iocb->private = NULL; 3021729f52c6SZheng Liu } else if (ret > 0 && !overwrite && ext4_test_inode_state(inode, 30225f524950SMingming EXT4_STATE_DIO_UNWRITTEN)) { 3023109f5565SMingming int err; 30248d5d02e6SMingming Cao /* 30258d5d02e6SMingming Cao * for non AIO case, since the IO is already 302625985edcSLucas De Marchi * completed, we could do the conversion right here 30278d5d02e6SMingming Cao */ 3028109f5565SMingming err = ext4_convert_unwritten_extents(inode, 30298d5d02e6SMingming Cao offset, ret); 3030109f5565SMingming if (err < 0) 3031109f5565SMingming ret = err; 303219f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN); 3033109f5565SMingming } 30344bd809dbSZheng Liu 30354bd809dbSZheng Liu retake_lock: 30364bd809dbSZheng Liu /* take i_mutex locking again if we do a ovewrite dio */ 30374bd809dbSZheng Liu if (overwrite) { 30381f555cfaSDmitry Monakhov inode_dio_done(inode); 30394bd809dbSZheng Liu up_read(&EXT4_I(inode)->i_data_sem); 30404bd809dbSZheng Liu mutex_lock(&inode->i_mutex); 30414bd809dbSZheng Liu } 30424bd809dbSZheng Liu 30434c0425ffSMingming Cao return ret; 30444c0425ffSMingming Cao } 30458d5d02e6SMingming Cao 30464c0425ffSMingming Cao static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb, 30474c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 30484c0425ffSMingming Cao unsigned long nr_segs) 30494c0425ffSMingming Cao { 30504c0425ffSMingming Cao struct file *file = iocb->ki_filp; 30514c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 30520562e0baSJiaying Zhang ssize_t ret; 30534c0425ffSMingming Cao 305484ebd795STheodore Ts'o /* 305584ebd795STheodore Ts'o * If we are doing data journalling we don't support O_DIRECT 305684ebd795STheodore Ts'o */ 305784ebd795STheodore Ts'o if (ext4_should_journal_data(inode)) 305884ebd795STheodore Ts'o return 0; 305984ebd795STheodore Ts'o 306046c7f254STao Ma /* Let buffer I/O handle the inline data case. */ 306146c7f254STao Ma if (ext4_has_inline_data(inode)) 306246c7f254STao Ma return 0; 306346c7f254STao Ma 30640562e0baSJiaying Zhang trace_ext4_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw); 306512e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 30660562e0baSJiaying Zhang ret = ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs); 30670562e0baSJiaying Zhang else 30680562e0baSJiaying Zhang ret = ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 30690562e0baSJiaying Zhang trace_ext4_direct_IO_exit(inode, offset, 30700562e0baSJiaying Zhang iov_length(iov, nr_segs), rw, ret); 30710562e0baSJiaying Zhang return ret; 30724c0425ffSMingming Cao } 30734c0425ffSMingming Cao 3074ac27a0ecSDave Kleikamp /* 3075617ba13bSMingming Cao * Pages can be marked dirty completely asynchronously from ext4's journalling 3076ac27a0ecSDave Kleikamp * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do 3077ac27a0ecSDave Kleikamp * much here because ->set_page_dirty is called under VFS locks. The page is 3078ac27a0ecSDave Kleikamp * not necessarily locked. 3079ac27a0ecSDave Kleikamp * 3080ac27a0ecSDave Kleikamp * We cannot just dirty the page and leave attached buffers clean, because the 3081ac27a0ecSDave Kleikamp * buffers' dirty state is "definitive". We cannot just set the buffers dirty 3082ac27a0ecSDave Kleikamp * or jbddirty because all the journalling code will explode. 3083ac27a0ecSDave Kleikamp * 3084ac27a0ecSDave Kleikamp * So what we do is to mark the page "pending dirty" and next time writepage 3085ac27a0ecSDave Kleikamp * is called, propagate that into the buffers appropriately. 3086ac27a0ecSDave Kleikamp */ 3087617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page) 3088ac27a0ecSDave Kleikamp { 3089ac27a0ecSDave Kleikamp SetPageChecked(page); 3090ac27a0ecSDave Kleikamp return __set_page_dirty_nobuffers(page); 3091ac27a0ecSDave Kleikamp } 3092ac27a0ecSDave Kleikamp 3093617ba13bSMingming Cao static const struct address_space_operations ext4_ordered_aops = { 3094617ba13bSMingming Cao .readpage = ext4_readpage, 3095617ba13bSMingming Cao .readpages = ext4_readpages, 309643ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3097bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3098bfc1af65SNick Piggin .write_end = ext4_ordered_write_end, 3099617ba13bSMingming Cao .bmap = ext4_bmap, 3100617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3101617ba13bSMingming Cao .releasepage = ext4_releasepage, 3102617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 3103ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 31048ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3105aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3106ac27a0ecSDave Kleikamp }; 3107ac27a0ecSDave Kleikamp 3108617ba13bSMingming Cao static const struct address_space_operations ext4_writeback_aops = { 3109617ba13bSMingming Cao .readpage = ext4_readpage, 3110617ba13bSMingming Cao .readpages = ext4_readpages, 311143ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3112bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3113bfc1af65SNick Piggin .write_end = ext4_writeback_write_end, 3114617ba13bSMingming Cao .bmap = ext4_bmap, 3115617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3116617ba13bSMingming Cao .releasepage = ext4_releasepage, 3117617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 3118ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 31198ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3120aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3121ac27a0ecSDave Kleikamp }; 3122ac27a0ecSDave Kleikamp 3123617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 3124617ba13bSMingming Cao .readpage = ext4_readpage, 3125617ba13bSMingming Cao .readpages = ext4_readpages, 312643ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3127bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3128bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 3129617ba13bSMingming Cao .set_page_dirty = ext4_journalled_set_page_dirty, 3130617ba13bSMingming Cao .bmap = ext4_bmap, 31314520fb3cSJan Kara .invalidatepage = ext4_journalled_invalidatepage, 3132617ba13bSMingming Cao .releasepage = ext4_releasepage, 313384ebd795STheodore Ts'o .direct_IO = ext4_direct_IO, 31348ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3135aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3136ac27a0ecSDave Kleikamp }; 3137ac27a0ecSDave Kleikamp 313864769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 313964769240SAlex Tomas .readpage = ext4_readpage, 314064769240SAlex Tomas .readpages = ext4_readpages, 314143ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 314264769240SAlex Tomas .writepages = ext4_da_writepages, 314364769240SAlex Tomas .write_begin = ext4_da_write_begin, 314464769240SAlex Tomas .write_end = ext4_da_write_end, 314564769240SAlex Tomas .bmap = ext4_bmap, 314664769240SAlex Tomas .invalidatepage = ext4_da_invalidatepage, 314764769240SAlex Tomas .releasepage = ext4_releasepage, 314864769240SAlex Tomas .direct_IO = ext4_direct_IO, 314964769240SAlex Tomas .migratepage = buffer_migrate_page, 31508ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3151aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 315264769240SAlex Tomas }; 315364769240SAlex Tomas 3154617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 3155ac27a0ecSDave Kleikamp { 31563d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 31573d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 31583d2b1582SLukas Czerner if (test_opt(inode->i_sb, DELALLOC)) 3159cd1aac32SAneesh Kumar K.V inode->i_mapping->a_ops = &ext4_da_aops; 3160ac27a0ecSDave Kleikamp else 31613d2b1582SLukas Czerner inode->i_mapping->a_ops = &ext4_ordered_aops; 31623d2b1582SLukas Czerner break; 31633d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 31643d2b1582SLukas Czerner if (test_opt(inode->i_sb, DELALLOC)) 31653d2b1582SLukas Czerner inode->i_mapping->a_ops = &ext4_da_aops; 31663d2b1582SLukas Czerner else 31673d2b1582SLukas Czerner inode->i_mapping->a_ops = &ext4_writeback_aops; 31683d2b1582SLukas Czerner break; 31693d2b1582SLukas Czerner case EXT4_INODE_JOURNAL_DATA_MODE: 3170617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 31713d2b1582SLukas Czerner break; 31723d2b1582SLukas Czerner default: 31733d2b1582SLukas Czerner BUG(); 31743d2b1582SLukas Czerner } 3175ac27a0ecSDave Kleikamp } 3176ac27a0ecSDave Kleikamp 31774e96b2dbSAllison Henderson 31784e96b2dbSAllison Henderson /* 31794e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers() 31804e96b2dbSAllison Henderson * Wrapper function for ext4_discard_partial_page_buffers_no_lock. 31814e96b2dbSAllison Henderson * This function finds and locks the page containing the offset 31824e96b2dbSAllison Henderson * "from" and passes it to ext4_discard_partial_page_buffers_no_lock. 31834e96b2dbSAllison Henderson * Calling functions that already have the page locked should call 31844e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers_no_lock directly. 31854e96b2dbSAllison Henderson */ 31864e96b2dbSAllison Henderson int ext4_discard_partial_page_buffers(handle_t *handle, 31874e96b2dbSAllison Henderson struct address_space *mapping, loff_t from, 31884e96b2dbSAllison Henderson loff_t length, int flags) 31894e96b2dbSAllison Henderson { 31904e96b2dbSAllison Henderson struct inode *inode = mapping->host; 31914e96b2dbSAllison Henderson struct page *page; 31924e96b2dbSAllison Henderson int err = 0; 31934e96b2dbSAllison Henderson 31944e96b2dbSAllison Henderson page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT, 31954e96b2dbSAllison Henderson mapping_gfp_mask(mapping) & ~__GFP_FS); 31964e96b2dbSAllison Henderson if (!page) 31975129d05fSYongqiang Yang return -ENOMEM; 31984e96b2dbSAllison Henderson 31994e96b2dbSAllison Henderson err = ext4_discard_partial_page_buffers_no_lock(handle, inode, page, 32004e96b2dbSAllison Henderson from, length, flags); 32014e96b2dbSAllison Henderson 32024e96b2dbSAllison Henderson unlock_page(page); 32034e96b2dbSAllison Henderson page_cache_release(page); 32044e96b2dbSAllison Henderson return err; 32054e96b2dbSAllison Henderson } 32064e96b2dbSAllison Henderson 32074e96b2dbSAllison Henderson /* 32084e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers_no_lock() 32094e96b2dbSAllison Henderson * Zeros a page range of length 'length' starting from offset 'from'. 32104e96b2dbSAllison Henderson * Buffer heads that correspond to the block aligned regions of the 32114e96b2dbSAllison Henderson * zeroed range will be unmapped. Unblock aligned regions 32124e96b2dbSAllison Henderson * will have the corresponding buffer head mapped if needed so that 32134e96b2dbSAllison Henderson * that region of the page can be updated with the partial zero out. 32144e96b2dbSAllison Henderson * 32154e96b2dbSAllison Henderson * This function assumes that the page has already been locked. The 32164e96b2dbSAllison Henderson * The range to be discarded must be contained with in the given page. 32174e96b2dbSAllison Henderson * If the specified range exceeds the end of the page it will be shortened 32184e96b2dbSAllison Henderson * to the end of the page that corresponds to 'from'. This function is 32194e96b2dbSAllison Henderson * appropriate for updating a page and it buffer heads to be unmapped and 32204e96b2dbSAllison Henderson * zeroed for blocks that have been either released, or are going to be 32214e96b2dbSAllison Henderson * released. 32224e96b2dbSAllison Henderson * 32234e96b2dbSAllison Henderson * handle: The journal handle 32244e96b2dbSAllison Henderson * inode: The files inode 32254e96b2dbSAllison Henderson * page: A locked page that contains the offset "from" 32264907cb7bSAnatol Pomozov * from: The starting byte offset (from the beginning of the file) 32274e96b2dbSAllison Henderson * to begin discarding 32284e96b2dbSAllison Henderson * len: The length of bytes to discard 32294e96b2dbSAllison Henderson * flags: Optional flags that may be used: 32304e96b2dbSAllison Henderson * 32314e96b2dbSAllison Henderson * EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED 32324e96b2dbSAllison Henderson * Only zero the regions of the page whose buffer heads 32334e96b2dbSAllison Henderson * have already been unmapped. This flag is appropriate 32344907cb7bSAnatol Pomozov * for updating the contents of a page whose blocks may 32354e96b2dbSAllison Henderson * have already been released, and we only want to zero 32364e96b2dbSAllison Henderson * out the regions that correspond to those released blocks. 32374e96b2dbSAllison Henderson * 32384907cb7bSAnatol Pomozov * Returns zero on success or negative on failure. 32394e96b2dbSAllison Henderson */ 32405f163cc7SEric Sandeen static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle, 32414e96b2dbSAllison Henderson struct inode *inode, struct page *page, loff_t from, 32424e96b2dbSAllison Henderson loff_t length, int flags) 32434e96b2dbSAllison Henderson { 32444e96b2dbSAllison Henderson ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; 32454e96b2dbSAllison Henderson unsigned int offset = from & (PAGE_CACHE_SIZE-1); 32464e96b2dbSAllison Henderson unsigned int blocksize, max, pos; 32474e96b2dbSAllison Henderson ext4_lblk_t iblock; 32484e96b2dbSAllison Henderson struct buffer_head *bh; 32494e96b2dbSAllison Henderson int err = 0; 32504e96b2dbSAllison Henderson 32514e96b2dbSAllison Henderson blocksize = inode->i_sb->s_blocksize; 32524e96b2dbSAllison Henderson max = PAGE_CACHE_SIZE - offset; 32534e96b2dbSAllison Henderson 32544e96b2dbSAllison Henderson if (index != page->index) 32554e96b2dbSAllison Henderson return -EINVAL; 32564e96b2dbSAllison Henderson 32574e96b2dbSAllison Henderson /* 32584e96b2dbSAllison Henderson * correct length if it does not fall between 32594e96b2dbSAllison Henderson * 'from' and the end of the page 32604e96b2dbSAllison Henderson */ 32614e96b2dbSAllison Henderson if (length > max || length < 0) 32624e96b2dbSAllison Henderson length = max; 32634e96b2dbSAllison Henderson 32644e96b2dbSAllison Henderson iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); 32654e96b2dbSAllison Henderson 3266093e6e36SYongqiang Yang if (!page_has_buffers(page)) 32674e96b2dbSAllison Henderson create_empty_buffers(page, blocksize, 0); 32684e96b2dbSAllison Henderson 32694e96b2dbSAllison Henderson /* Find the buffer that contains "offset" */ 32704e96b2dbSAllison Henderson bh = page_buffers(page); 32714e96b2dbSAllison Henderson pos = blocksize; 32724e96b2dbSAllison Henderson while (offset >= pos) { 32734e96b2dbSAllison Henderson bh = bh->b_this_page; 32744e96b2dbSAllison Henderson iblock++; 32754e96b2dbSAllison Henderson pos += blocksize; 32764e96b2dbSAllison Henderson } 32774e96b2dbSAllison Henderson 32784e96b2dbSAllison Henderson pos = offset; 32794e96b2dbSAllison Henderson while (pos < offset + length) { 3280e260daf2SYongqiang Yang unsigned int end_of_block, range_to_discard; 3281e260daf2SYongqiang Yang 32824e96b2dbSAllison Henderson err = 0; 32834e96b2dbSAllison Henderson 32844e96b2dbSAllison Henderson /* The length of space left to zero and unmap */ 32854e96b2dbSAllison Henderson range_to_discard = offset + length - pos; 32864e96b2dbSAllison Henderson 32874e96b2dbSAllison Henderson /* The length of space until the end of the block */ 32884e96b2dbSAllison Henderson end_of_block = blocksize - (pos & (blocksize-1)); 32894e96b2dbSAllison Henderson 32904e96b2dbSAllison Henderson /* 32914e96b2dbSAllison Henderson * Do not unmap or zero past end of block 32924e96b2dbSAllison Henderson * for this buffer head 32934e96b2dbSAllison Henderson */ 32944e96b2dbSAllison Henderson if (range_to_discard > end_of_block) 32954e96b2dbSAllison Henderson range_to_discard = end_of_block; 32964e96b2dbSAllison Henderson 32974e96b2dbSAllison Henderson 32984e96b2dbSAllison Henderson /* 32994e96b2dbSAllison Henderson * Skip this buffer head if we are only zeroing unampped 33004e96b2dbSAllison Henderson * regions of the page 33014e96b2dbSAllison Henderson */ 33024e96b2dbSAllison Henderson if (flags & EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED && 33034e96b2dbSAllison Henderson buffer_mapped(bh)) 33044e96b2dbSAllison Henderson goto next; 33054e96b2dbSAllison Henderson 33064e96b2dbSAllison Henderson /* If the range is block aligned, unmap */ 33074e96b2dbSAllison Henderson if (range_to_discard == blocksize) { 33084e96b2dbSAllison Henderson clear_buffer_dirty(bh); 33094e96b2dbSAllison Henderson bh->b_bdev = NULL; 33104e96b2dbSAllison Henderson clear_buffer_mapped(bh); 33114e96b2dbSAllison Henderson clear_buffer_req(bh); 33124e96b2dbSAllison Henderson clear_buffer_new(bh); 33134e96b2dbSAllison Henderson clear_buffer_delay(bh); 33144e96b2dbSAllison Henderson clear_buffer_unwritten(bh); 33154e96b2dbSAllison Henderson clear_buffer_uptodate(bh); 33164e96b2dbSAllison Henderson zero_user(page, pos, range_to_discard); 33174e96b2dbSAllison Henderson BUFFER_TRACE(bh, "Buffer discarded"); 33184e96b2dbSAllison Henderson goto next; 33194e96b2dbSAllison Henderson } 33204e96b2dbSAllison Henderson 33214e96b2dbSAllison Henderson /* 33224e96b2dbSAllison Henderson * If this block is not completely contained in the range 33234e96b2dbSAllison Henderson * to be discarded, then it is not going to be released. Because 33244e96b2dbSAllison Henderson * we need to keep this block, we need to make sure this part 33254e96b2dbSAllison Henderson * of the page is uptodate before we modify it by writeing 33264e96b2dbSAllison Henderson * partial zeros on it. 33274e96b2dbSAllison Henderson */ 33284e96b2dbSAllison Henderson if (!buffer_mapped(bh)) { 33294e96b2dbSAllison Henderson /* 33304e96b2dbSAllison Henderson * Buffer head must be mapped before we can read 33314e96b2dbSAllison Henderson * from the block 33324e96b2dbSAllison Henderson */ 33334e96b2dbSAllison Henderson BUFFER_TRACE(bh, "unmapped"); 33344e96b2dbSAllison Henderson ext4_get_block(inode, iblock, bh, 0); 33354e96b2dbSAllison Henderson /* unmapped? It's a hole - nothing to do */ 33364e96b2dbSAllison Henderson if (!buffer_mapped(bh)) { 33374e96b2dbSAllison Henderson BUFFER_TRACE(bh, "still unmapped"); 33384e96b2dbSAllison Henderson goto next; 33394e96b2dbSAllison Henderson } 33404e96b2dbSAllison Henderson } 33414e96b2dbSAllison Henderson 33424e96b2dbSAllison Henderson /* Ok, it's mapped. Make sure it's up-to-date */ 33434e96b2dbSAllison Henderson if (PageUptodate(page)) 33444e96b2dbSAllison Henderson set_buffer_uptodate(bh); 33454e96b2dbSAllison Henderson 33464e96b2dbSAllison Henderson if (!buffer_uptodate(bh)) { 33474e96b2dbSAllison Henderson err = -EIO; 33484e96b2dbSAllison Henderson ll_rw_block(READ, 1, &bh); 33494e96b2dbSAllison Henderson wait_on_buffer(bh); 33504e96b2dbSAllison Henderson /* Uhhuh. Read error. Complain and punt.*/ 33514e96b2dbSAllison Henderson if (!buffer_uptodate(bh)) 33524e96b2dbSAllison Henderson goto next; 33534e96b2dbSAllison Henderson } 33544e96b2dbSAllison Henderson 33554e96b2dbSAllison Henderson if (ext4_should_journal_data(inode)) { 33564e96b2dbSAllison Henderson BUFFER_TRACE(bh, "get write access"); 33574e96b2dbSAllison Henderson err = ext4_journal_get_write_access(handle, bh); 33584e96b2dbSAllison Henderson if (err) 33594e96b2dbSAllison Henderson goto next; 33604e96b2dbSAllison Henderson } 33614e96b2dbSAllison Henderson 33624e96b2dbSAllison Henderson zero_user(page, pos, range_to_discard); 33634e96b2dbSAllison Henderson 33644e96b2dbSAllison Henderson err = 0; 33654e96b2dbSAllison Henderson if (ext4_should_journal_data(inode)) { 33664e96b2dbSAllison Henderson err = ext4_handle_dirty_metadata(handle, inode, bh); 3367decbd919STheodore Ts'o } else 33684e96b2dbSAllison Henderson mark_buffer_dirty(bh); 33694e96b2dbSAllison Henderson 33704e96b2dbSAllison Henderson BUFFER_TRACE(bh, "Partial buffer zeroed"); 33714e96b2dbSAllison Henderson next: 33724e96b2dbSAllison Henderson bh = bh->b_this_page; 33734e96b2dbSAllison Henderson iblock++; 33744e96b2dbSAllison Henderson pos += range_to_discard; 33754e96b2dbSAllison Henderson } 33764e96b2dbSAllison Henderson 33774e96b2dbSAllison Henderson return err; 33784e96b2dbSAllison Henderson } 33794e96b2dbSAllison Henderson 338091ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 338191ef4cafSDuane Griffin { 338291ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 338391ef4cafSDuane Griffin return 1; 338491ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 338591ef4cafSDuane Griffin return 1; 338691ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 338791ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 338891ef4cafSDuane Griffin return 0; 338991ef4cafSDuane Griffin } 339091ef4cafSDuane Griffin 3391ac27a0ecSDave Kleikamp /* 3392a4bb6b64SAllison Henderson * ext4_punch_hole: punches a hole in a file by releaseing the blocks 3393a4bb6b64SAllison Henderson * associated with the given offset and length 3394a4bb6b64SAllison Henderson * 3395a4bb6b64SAllison Henderson * @inode: File inode 3396a4bb6b64SAllison Henderson * @offset: The offset where the hole will begin 3397a4bb6b64SAllison Henderson * @len: The length of the hole 3398a4bb6b64SAllison Henderson * 33994907cb7bSAnatol Pomozov * Returns: 0 on success or negative on failure 3400a4bb6b64SAllison Henderson */ 3401a4bb6b64SAllison Henderson 3402a4bb6b64SAllison Henderson int ext4_punch_hole(struct file *file, loff_t offset, loff_t length) 3403a4bb6b64SAllison Henderson { 3404a4bb6b64SAllison Henderson struct inode *inode = file->f_path.dentry->d_inode; 3405a4bb6b64SAllison Henderson if (!S_ISREG(inode->i_mode)) 340673355192SAllison Henderson return -EOPNOTSUPP; 3407a4bb6b64SAllison Henderson 34088bad6fc8SZheng Liu if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 34098bad6fc8SZheng Liu return ext4_ind_punch_hole(file, offset, length); 3410a4bb6b64SAllison Henderson 3411bab08ab9STheodore Ts'o if (EXT4_SB(inode->i_sb)->s_cluster_ratio > 1) { 3412bab08ab9STheodore Ts'o /* TODO: Add support for bigalloc file systems */ 341373355192SAllison Henderson return -EOPNOTSUPP; 3414bab08ab9STheodore Ts'o } 3415bab08ab9STheodore Ts'o 3416aaddea81SZheng Liu trace_ext4_punch_hole(inode, offset, length); 3417aaddea81SZheng Liu 3418a4bb6b64SAllison Henderson return ext4_ext_punch_hole(file, offset, length); 3419a4bb6b64SAllison Henderson } 3420a4bb6b64SAllison Henderson 3421a4bb6b64SAllison Henderson /* 3422617ba13bSMingming Cao * ext4_truncate() 3423ac27a0ecSDave Kleikamp * 3424617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 3425617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 3426ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 3427ac27a0ecSDave Kleikamp * 342842b2aa86SJustin P. Mattock * As we work through the truncate and commit bits of it to the journal there 3429ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 3430ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 3431ac27a0ecSDave Kleikamp * 3432ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 3433ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 3434ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 3435ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 3436ac27a0ecSDave Kleikamp * left-to-right works OK too). 3437ac27a0ecSDave Kleikamp * 3438ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 3439ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 3440ac27a0ecSDave Kleikamp * 3441ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 3442617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 3443ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 3444617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 3445617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 3446ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 3447617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 3448ac27a0ecSDave Kleikamp */ 3449617ba13bSMingming Cao void ext4_truncate(struct inode *inode) 3450ac27a0ecSDave Kleikamp { 34510562e0baSJiaying Zhang trace_ext4_truncate_enter(inode); 34520562e0baSJiaying Zhang 345391ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 3454ac27a0ecSDave Kleikamp return; 3455ac27a0ecSDave Kleikamp 345612e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS); 3457c8d46e41SJiaying Zhang 34585534fb5bSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 345919f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE); 34607d8f9f7dSTheodore Ts'o 3461aef1c851STao Ma if (ext4_has_inline_data(inode)) { 3462aef1c851STao Ma int has_inline = 1; 3463aef1c851STao Ma 3464aef1c851STao Ma ext4_inline_data_truncate(inode, &has_inline); 3465aef1c851STao Ma if (has_inline) 3466aef1c851STao Ma return; 3467aef1c851STao Ma } 3468aef1c851STao Ma 3469ff9893dcSAmir Goldstein if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3470cf108bcaSJan Kara ext4_ext_truncate(inode); 3471ff9893dcSAmir Goldstein else 3472ff9893dcSAmir Goldstein ext4_ind_truncate(inode); 3473a86c6181SAlex Tomas 34740562e0baSJiaying Zhang trace_ext4_truncate_exit(inode); 3475ac27a0ecSDave Kleikamp } 3476ac27a0ecSDave Kleikamp 3477ac27a0ecSDave Kleikamp /* 3478617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 3479ac27a0ecSDave Kleikamp * underlying buffer_head on success. If 'in_mem' is true, we have all 3480ac27a0ecSDave Kleikamp * data in memory that is needed to recreate the on-disk version of this 3481ac27a0ecSDave Kleikamp * inode. 3482ac27a0ecSDave Kleikamp */ 3483617ba13bSMingming Cao static int __ext4_get_inode_loc(struct inode *inode, 3484617ba13bSMingming Cao struct ext4_iloc *iloc, int in_mem) 3485ac27a0ecSDave Kleikamp { 3486240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 3487ac27a0ecSDave Kleikamp struct buffer_head *bh; 3488240799cdSTheodore Ts'o struct super_block *sb = inode->i_sb; 3489240799cdSTheodore Ts'o ext4_fsblk_t block; 3490240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 3491ac27a0ecSDave Kleikamp 34923a06d778SAneesh Kumar K.V iloc->bh = NULL; 3493240799cdSTheodore Ts'o if (!ext4_valid_inum(sb, inode->i_ino)) 3494ac27a0ecSDave Kleikamp return -EIO; 3495ac27a0ecSDave Kleikamp 3496240799cdSTheodore Ts'o iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb); 3497240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 3498240799cdSTheodore Ts'o if (!gdp) 3499240799cdSTheodore Ts'o return -EIO; 3500240799cdSTheodore Ts'o 3501240799cdSTheodore Ts'o /* 3502240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 3503240799cdSTheodore Ts'o */ 350400d09882STao Ma inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 3505240799cdSTheodore Ts'o inode_offset = ((inode->i_ino - 1) % 3506240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 3507240799cdSTheodore Ts'o block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); 3508240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 3509240799cdSTheodore Ts'o 3510240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 3511aebf0243SWang Shilong if (unlikely(!bh)) 3512860d21e2STheodore Ts'o return -ENOMEM; 3513ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 3514ac27a0ecSDave Kleikamp lock_buffer(bh); 35159c83a923SHidehiro Kawai 35169c83a923SHidehiro Kawai /* 35179c83a923SHidehiro Kawai * If the buffer has the write error flag, we have failed 35189c83a923SHidehiro Kawai * to write out another inode in the same block. In this 35199c83a923SHidehiro Kawai * case, we don't have to read the block because we may 35209c83a923SHidehiro Kawai * read the old inode data successfully. 35219c83a923SHidehiro Kawai */ 35229c83a923SHidehiro Kawai if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) 35239c83a923SHidehiro Kawai set_buffer_uptodate(bh); 35249c83a923SHidehiro Kawai 3525ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) { 3526ac27a0ecSDave Kleikamp /* someone brought it uptodate while we waited */ 3527ac27a0ecSDave Kleikamp unlock_buffer(bh); 3528ac27a0ecSDave Kleikamp goto has_buffer; 3529ac27a0ecSDave Kleikamp } 3530ac27a0ecSDave Kleikamp 3531ac27a0ecSDave Kleikamp /* 3532ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 3533ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 3534ac27a0ecSDave Kleikamp * block. 3535ac27a0ecSDave Kleikamp */ 3536ac27a0ecSDave Kleikamp if (in_mem) { 3537ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 3538240799cdSTheodore Ts'o int i, start; 3539ac27a0ecSDave Kleikamp 3540240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 3541ac27a0ecSDave Kleikamp 3542ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 3543240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 3544aebf0243SWang Shilong if (unlikely(!bitmap_bh)) 3545ac27a0ecSDave Kleikamp goto make_io; 3546ac27a0ecSDave Kleikamp 3547ac27a0ecSDave Kleikamp /* 3548ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 3549ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 3550ac27a0ecSDave Kleikamp * of one, so skip it. 3551ac27a0ecSDave Kleikamp */ 3552ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 3553ac27a0ecSDave Kleikamp brelse(bitmap_bh); 3554ac27a0ecSDave Kleikamp goto make_io; 3555ac27a0ecSDave Kleikamp } 3556240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 3557ac27a0ecSDave Kleikamp if (i == inode_offset) 3558ac27a0ecSDave Kleikamp continue; 3559617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 3560ac27a0ecSDave Kleikamp break; 3561ac27a0ecSDave Kleikamp } 3562ac27a0ecSDave Kleikamp brelse(bitmap_bh); 3563240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 3564ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 3565ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 3566ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 3567ac27a0ecSDave Kleikamp unlock_buffer(bh); 3568ac27a0ecSDave Kleikamp goto has_buffer; 3569ac27a0ecSDave Kleikamp } 3570ac27a0ecSDave Kleikamp } 3571ac27a0ecSDave Kleikamp 3572ac27a0ecSDave Kleikamp make_io: 3573ac27a0ecSDave Kleikamp /* 3574240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 3575240799cdSTheodore Ts'o * blocks from the inode table. 3576240799cdSTheodore Ts'o */ 3577240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 3578240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 3579240799cdSTheodore Ts'o unsigned num; 3580240799cdSTheodore Ts'o 3581240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 3582b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 3583240799cdSTheodore Ts'o b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1); 3584240799cdSTheodore Ts'o if (table > b) 3585240799cdSTheodore Ts'o b = table; 3586240799cdSTheodore Ts'o end = b + EXT4_SB(sb)->s_inode_readahead_blks; 3587240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 3588feb0ab32SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) 3589560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 3590240799cdSTheodore Ts'o table += num / inodes_per_block; 3591240799cdSTheodore Ts'o if (end > table) 3592240799cdSTheodore Ts'o end = table; 3593240799cdSTheodore Ts'o while (b <= end) 3594240799cdSTheodore Ts'o sb_breadahead(sb, b++); 3595240799cdSTheodore Ts'o } 3596240799cdSTheodore Ts'o 3597240799cdSTheodore Ts'o /* 3598ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 3599ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 3600ac27a0ecSDave Kleikamp * Read the block from disk. 3601ac27a0ecSDave Kleikamp */ 36020562e0baSJiaying Zhang trace_ext4_load_inode(inode); 3603ac27a0ecSDave Kleikamp get_bh(bh); 3604ac27a0ecSDave Kleikamp bh->b_end_io = end_buffer_read_sync; 360565299a3bSChristoph Hellwig submit_bh(READ | REQ_META | REQ_PRIO, bh); 3606ac27a0ecSDave Kleikamp wait_on_buffer(bh); 3607ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 3608c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, block, 3609c398eda0STheodore Ts'o "unable to read itable block"); 3610ac27a0ecSDave Kleikamp brelse(bh); 3611ac27a0ecSDave Kleikamp return -EIO; 3612ac27a0ecSDave Kleikamp } 3613ac27a0ecSDave Kleikamp } 3614ac27a0ecSDave Kleikamp has_buffer: 3615ac27a0ecSDave Kleikamp iloc->bh = bh; 3616ac27a0ecSDave Kleikamp return 0; 3617ac27a0ecSDave Kleikamp } 3618ac27a0ecSDave Kleikamp 3619617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 3620ac27a0ecSDave Kleikamp { 3621ac27a0ecSDave Kleikamp /* We have all inode data except xattrs in memory here. */ 3622617ba13bSMingming Cao return __ext4_get_inode_loc(inode, iloc, 362319f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_XATTR)); 3624ac27a0ecSDave Kleikamp } 3625ac27a0ecSDave Kleikamp 3626617ba13bSMingming Cao void ext4_set_inode_flags(struct inode *inode) 3627ac27a0ecSDave Kleikamp { 3628617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 3629ac27a0ecSDave Kleikamp 3630ac27a0ecSDave Kleikamp inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); 3631617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 3632ac27a0ecSDave Kleikamp inode->i_flags |= S_SYNC; 3633617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 3634ac27a0ecSDave Kleikamp inode->i_flags |= S_APPEND; 3635617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 3636ac27a0ecSDave Kleikamp inode->i_flags |= S_IMMUTABLE; 3637617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 3638ac27a0ecSDave Kleikamp inode->i_flags |= S_NOATIME; 3639617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 3640ac27a0ecSDave Kleikamp inode->i_flags |= S_DIRSYNC; 3641ac27a0ecSDave Kleikamp } 3642ac27a0ecSDave Kleikamp 3643ff9ddf7eSJan Kara /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */ 3644ff9ddf7eSJan Kara void ext4_get_inode_flags(struct ext4_inode_info *ei) 3645ff9ddf7eSJan Kara { 364684a8dce2SDmitry Monakhov unsigned int vfs_fl; 364784a8dce2SDmitry Monakhov unsigned long old_fl, new_fl; 3648ff9ddf7eSJan Kara 364984a8dce2SDmitry Monakhov do { 365084a8dce2SDmitry Monakhov vfs_fl = ei->vfs_inode.i_flags; 365184a8dce2SDmitry Monakhov old_fl = ei->i_flags; 365284a8dce2SDmitry Monakhov new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL| 365384a8dce2SDmitry Monakhov EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL| 365484a8dce2SDmitry Monakhov EXT4_DIRSYNC_FL); 365584a8dce2SDmitry Monakhov if (vfs_fl & S_SYNC) 365684a8dce2SDmitry Monakhov new_fl |= EXT4_SYNC_FL; 365784a8dce2SDmitry Monakhov if (vfs_fl & S_APPEND) 365884a8dce2SDmitry Monakhov new_fl |= EXT4_APPEND_FL; 365984a8dce2SDmitry Monakhov if (vfs_fl & S_IMMUTABLE) 366084a8dce2SDmitry Monakhov new_fl |= EXT4_IMMUTABLE_FL; 366184a8dce2SDmitry Monakhov if (vfs_fl & S_NOATIME) 366284a8dce2SDmitry Monakhov new_fl |= EXT4_NOATIME_FL; 366384a8dce2SDmitry Monakhov if (vfs_fl & S_DIRSYNC) 366484a8dce2SDmitry Monakhov new_fl |= EXT4_DIRSYNC_FL; 366584a8dce2SDmitry Monakhov } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl); 3666ff9ddf7eSJan Kara } 3667de9a55b8STheodore Ts'o 36680fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 36690fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 36700fc1b451SAneesh Kumar K.V { 36710fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 36728180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 36738180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 36740fc1b451SAneesh Kumar K.V 36750fc1b451SAneesh Kumar K.V if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 36760fc1b451SAneesh Kumar K.V EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) { 36770fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 36780fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 36790fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 368007a03824STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) { 36818180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 36828180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 36838180a562SAneesh Kumar K.V } else { 36840fc1b451SAneesh Kumar K.V return i_blocks; 36858180a562SAneesh Kumar K.V } 36860fc1b451SAneesh Kumar K.V } else { 36870fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 36880fc1b451SAneesh Kumar K.V } 36890fc1b451SAneesh Kumar K.V } 3690ff9ddf7eSJan Kara 3691152a7b0aSTao Ma static inline void ext4_iget_extra_inode(struct inode *inode, 3692152a7b0aSTao Ma struct ext4_inode *raw_inode, 3693152a7b0aSTao Ma struct ext4_inode_info *ei) 3694152a7b0aSTao Ma { 3695152a7b0aSTao Ma __le32 *magic = (void *)raw_inode + 3696152a7b0aSTao Ma EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize; 369767cf5b09STao Ma if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) { 3698152a7b0aSTao Ma ext4_set_inode_state(inode, EXT4_STATE_XATTR); 369967cf5b09STao Ma ext4_find_inline_data_nolock(inode); 3700f19d5870STao Ma } else 3701f19d5870STao Ma EXT4_I(inode)->i_inline_off = 0; 3702152a7b0aSTao Ma } 3703152a7b0aSTao Ma 37041d1fe1eeSDavid Howells struct inode *ext4_iget(struct super_block *sb, unsigned long ino) 3705ac27a0ecSDave Kleikamp { 3706617ba13bSMingming Cao struct ext4_iloc iloc; 3707617ba13bSMingming Cao struct ext4_inode *raw_inode; 37081d1fe1eeSDavid Howells struct ext4_inode_info *ei; 37091d1fe1eeSDavid Howells struct inode *inode; 3710b436b9beSJan Kara journal_t *journal = EXT4_SB(sb)->s_journal; 37111d1fe1eeSDavid Howells long ret; 3712ac27a0ecSDave Kleikamp int block; 371308cefc7aSEric W. Biederman uid_t i_uid; 371408cefc7aSEric W. Biederman gid_t i_gid; 3715ac27a0ecSDave Kleikamp 37161d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 37171d1fe1eeSDavid Howells if (!inode) 37181d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 37191d1fe1eeSDavid Howells if (!(inode->i_state & I_NEW)) 37201d1fe1eeSDavid Howells return inode; 37211d1fe1eeSDavid Howells 37221d1fe1eeSDavid Howells ei = EXT4_I(inode); 37237dc57615SPeter Huewe iloc.bh = NULL; 3724ac27a0ecSDave Kleikamp 37251d1fe1eeSDavid Howells ret = __ext4_get_inode_loc(inode, &iloc, 0); 37261d1fe1eeSDavid Howells if (ret < 0) 3727ac27a0ecSDave Kleikamp goto bad_inode; 3728617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 3729814525f4SDarrick J. Wong 3730814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 3731814525f4SDarrick J. Wong ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 3732814525f4SDarrick J. Wong if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 3733814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)) { 3734814525f4SDarrick J. Wong EXT4_ERROR_INODE(inode, "bad extra_isize (%u != %u)", 3735814525f4SDarrick J. Wong EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize, 3736814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)); 3737814525f4SDarrick J. Wong ret = -EIO; 3738814525f4SDarrick J. Wong goto bad_inode; 3739814525f4SDarrick J. Wong } 3740814525f4SDarrick J. Wong } else 3741814525f4SDarrick J. Wong ei->i_extra_isize = 0; 3742814525f4SDarrick J. Wong 3743814525f4SDarrick J. Wong /* Precompute checksum seed for inode metadata */ 3744814525f4SDarrick J. Wong if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 3745814525f4SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) { 3746814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3747814525f4SDarrick J. Wong __u32 csum; 3748814525f4SDarrick J. Wong __le32 inum = cpu_to_le32(inode->i_ino); 3749814525f4SDarrick J. Wong __le32 gen = raw_inode->i_generation; 3750814525f4SDarrick J. Wong csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, 3751814525f4SDarrick J. Wong sizeof(inum)); 3752814525f4SDarrick J. Wong ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, 3753814525f4SDarrick J. Wong sizeof(gen)); 3754814525f4SDarrick J. Wong } 3755814525f4SDarrick J. Wong 3756814525f4SDarrick J. Wong if (!ext4_inode_csum_verify(inode, raw_inode, ei)) { 3757814525f4SDarrick J. Wong EXT4_ERROR_INODE(inode, "checksum invalid"); 3758814525f4SDarrick J. Wong ret = -EIO; 3759814525f4SDarrick J. Wong goto bad_inode; 3760814525f4SDarrick J. Wong } 3761814525f4SDarrick J. Wong 3762ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 376308cefc7aSEric W. Biederman i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 376408cefc7aSEric W. Biederman i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 3765ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 376608cefc7aSEric W. Biederman i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 376708cefc7aSEric W. Biederman i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 3768ac27a0ecSDave Kleikamp } 376908cefc7aSEric W. Biederman i_uid_write(inode, i_uid); 377008cefc7aSEric W. Biederman i_gid_write(inode, i_gid); 3771bfe86848SMiklos Szeredi set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); 3772ac27a0ecSDave Kleikamp 3773353eb83cSTheodore Ts'o ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 377467cf5b09STao Ma ei->i_inline_off = 0; 3775ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 3776ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 3777ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 3778ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 3779ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 3780ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 3781ac27a0ecSDave Kleikamp */ 3782ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 3783ac27a0ecSDave Kleikamp if (inode->i_mode == 0 || 3784617ba13bSMingming Cao !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) { 3785ac27a0ecSDave Kleikamp /* this inode is deleted */ 37861d1fe1eeSDavid Howells ret = -ESTALE; 3787ac27a0ecSDave Kleikamp goto bad_inode; 3788ac27a0ecSDave Kleikamp } 3789ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 3790ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 3791ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 3792ac27a0ecSDave Kleikamp * the process of deleting those. */ 3793ac27a0ecSDave Kleikamp } 3794ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 37950fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 37967973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 3797a9e81742STheodore Ts'o if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) 3798a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 3799a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 3800a48380f7SAneesh Kumar K.V inode->i_size = ext4_isize(raw_inode); 3801ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 3802a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 3803a9e7f447SDmitry Monakhov ei->i_reserved_quota = 0; 3804a9e7f447SDmitry Monakhov #endif 3805ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 3806ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 3807a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 3808ac27a0ecSDave Kleikamp /* 3809ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 3810ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 3811ac27a0ecSDave Kleikamp */ 3812617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 3813ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 3814ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 3815ac27a0ecSDave Kleikamp 3816b436b9beSJan Kara /* 3817b436b9beSJan Kara * Set transaction id's of transactions that have to be committed 3818b436b9beSJan Kara * to finish f[data]sync. We set them to currently running transaction 3819b436b9beSJan Kara * as we cannot be sure that the inode or some of its metadata isn't 3820b436b9beSJan Kara * part of the transaction - the inode could have been reclaimed and 3821b436b9beSJan Kara * now it is reread from disk. 3822b436b9beSJan Kara */ 3823b436b9beSJan Kara if (journal) { 3824b436b9beSJan Kara transaction_t *transaction; 3825b436b9beSJan Kara tid_t tid; 3826b436b9beSJan Kara 3827a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 3828b436b9beSJan Kara if (journal->j_running_transaction) 3829b436b9beSJan Kara transaction = journal->j_running_transaction; 3830b436b9beSJan Kara else 3831b436b9beSJan Kara transaction = journal->j_committing_transaction; 3832b436b9beSJan Kara if (transaction) 3833b436b9beSJan Kara tid = transaction->t_tid; 3834b436b9beSJan Kara else 3835b436b9beSJan Kara tid = journal->j_commit_sequence; 3836a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 3837b436b9beSJan Kara ei->i_sync_tid = tid; 3838b436b9beSJan Kara ei->i_datasync_tid = tid; 3839b436b9beSJan Kara } 3840b436b9beSJan Kara 38410040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 3842ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 3843ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 3844617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 3845617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 3846ac27a0ecSDave Kleikamp } else { 3847152a7b0aSTao Ma ext4_iget_extra_inode(inode, raw_inode, ei); 3848ac27a0ecSDave Kleikamp } 3849814525f4SDarrick J. Wong } 3850ac27a0ecSDave Kleikamp 3851ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 3852ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 3853ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 3854ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 3855ef7f3835SKalpak Shah 385625ec56b5SJean Noel Cordenner inode->i_version = le32_to_cpu(raw_inode->i_disk_version); 385725ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 385825ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 385925ec56b5SJean Noel Cordenner inode->i_version |= 386025ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 386125ec56b5SJean Noel Cordenner } 386225ec56b5SJean Noel Cordenner 3863c4b5a614STheodore Ts'o ret = 0; 3864485c26ecSTheodore Ts'o if (ei->i_file_acl && 38651032988cSTheodore Ts'o !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) { 386624676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "bad extended attribute block %llu", 386724676da4STheodore Ts'o ei->i_file_acl); 3868485c26ecSTheodore Ts'o ret = -EIO; 3869485c26ecSTheodore Ts'o goto bad_inode; 3870f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 3871f19d5870STao Ma if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 3872f19d5870STao Ma if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 3873c4b5a614STheodore Ts'o (S_ISLNK(inode->i_mode) && 3874f19d5870STao Ma !ext4_inode_is_fast_symlink(inode)))) 38757a262f7cSAneesh Kumar K.V /* Validate extent which is part of inode */ 38767a262f7cSAneesh Kumar K.V ret = ext4_ext_check_inode(inode); 3877fe2c8191SThiemo Nagel } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 3878fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 3879fe2c8191SThiemo Nagel !ext4_inode_is_fast_symlink(inode))) { 3880fe2c8191SThiemo Nagel /* Validate block references which are part of inode */ 38811f7d1e77STheodore Ts'o ret = ext4_ind_check_inode(inode); 3882fe2c8191SThiemo Nagel } 3883f19d5870STao Ma } 3884567f3e9aSTheodore Ts'o if (ret) 38857a262f7cSAneesh Kumar K.V goto bad_inode; 38867a262f7cSAneesh Kumar K.V 3887ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 3888617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 3889617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 3890617ba13bSMingming Cao ext4_set_aops(inode); 3891ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 3892617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 3893617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 3894ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 3895e83c1397SDuane Griffin if (ext4_inode_is_fast_symlink(inode)) { 3896617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 3897e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 3898e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 3899e83c1397SDuane Griffin } else { 3900617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 3901617ba13bSMingming Cao ext4_set_aops(inode); 3902ac27a0ecSDave Kleikamp } 3903563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 3904563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 3905617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 3906ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 3907ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 3908ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 3909ac27a0ecSDave Kleikamp else 3910ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 3911ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 3912563bdd61STheodore Ts'o } else { 3913563bdd61STheodore Ts'o ret = -EIO; 391424676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode); 3915563bdd61STheodore Ts'o goto bad_inode; 3916ac27a0ecSDave Kleikamp } 3917ac27a0ecSDave Kleikamp brelse(iloc.bh); 3918617ba13bSMingming Cao ext4_set_inode_flags(inode); 39191d1fe1eeSDavid Howells unlock_new_inode(inode); 39201d1fe1eeSDavid Howells return inode; 3921ac27a0ecSDave Kleikamp 3922ac27a0ecSDave Kleikamp bad_inode: 3923567f3e9aSTheodore Ts'o brelse(iloc.bh); 39241d1fe1eeSDavid Howells iget_failed(inode); 39251d1fe1eeSDavid Howells return ERR_PTR(ret); 3926ac27a0ecSDave Kleikamp } 3927ac27a0ecSDave Kleikamp 39280fc1b451SAneesh Kumar K.V static int ext4_inode_blocks_set(handle_t *handle, 39290fc1b451SAneesh Kumar K.V struct ext4_inode *raw_inode, 39300fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 39310fc1b451SAneesh Kumar K.V { 39320fc1b451SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 39330fc1b451SAneesh Kumar K.V u64 i_blocks = inode->i_blocks; 39340fc1b451SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 39350fc1b451SAneesh Kumar K.V 39360fc1b451SAneesh Kumar K.V if (i_blocks <= ~0U) { 39370fc1b451SAneesh Kumar K.V /* 39384907cb7bSAnatol Pomozov * i_blocks can be represented in a 32 bit variable 39390fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 39400fc1b451SAneesh Kumar K.V */ 39418180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 39420fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = 0; 394384a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 3944f287a1a5STheodore Ts'o return 0; 3945f287a1a5STheodore Ts'o } 3946f287a1a5STheodore Ts'o if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) 3947f287a1a5STheodore Ts'o return -EFBIG; 3948f287a1a5STheodore Ts'o 3949f287a1a5STheodore Ts'o if (i_blocks <= 0xffffffffffffULL) { 39500fc1b451SAneesh Kumar K.V /* 39510fc1b451SAneesh Kumar K.V * i_blocks can be represented in a 48 bit variable 39520fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 39530fc1b451SAneesh Kumar K.V */ 39548180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 39550fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 395684a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 39570fc1b451SAneesh Kumar K.V } else { 395884a8dce2SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE); 39598180a562SAneesh Kumar K.V /* i_block is stored in file system block size */ 39608180a562SAneesh Kumar K.V i_blocks = i_blocks >> (inode->i_blkbits - 9); 39618180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 39628180a562SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 39630fc1b451SAneesh Kumar K.V } 3964f287a1a5STheodore Ts'o return 0; 39650fc1b451SAneesh Kumar K.V } 39660fc1b451SAneesh Kumar K.V 3967ac27a0ecSDave Kleikamp /* 3968ac27a0ecSDave Kleikamp * Post the struct inode info into an on-disk inode location in the 3969ac27a0ecSDave Kleikamp * buffer-cache. This gobbles the caller's reference to the 3970ac27a0ecSDave Kleikamp * buffer_head in the inode location struct. 3971ac27a0ecSDave Kleikamp * 3972ac27a0ecSDave Kleikamp * The caller must have write access to iloc->bh. 3973ac27a0ecSDave Kleikamp */ 3974617ba13bSMingming Cao static int ext4_do_update_inode(handle_t *handle, 3975ac27a0ecSDave Kleikamp struct inode *inode, 3976830156c7SFrank Mayhar struct ext4_iloc *iloc) 3977ac27a0ecSDave Kleikamp { 3978617ba13bSMingming Cao struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 3979617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 3980ac27a0ecSDave Kleikamp struct buffer_head *bh = iloc->bh; 3981ac27a0ecSDave Kleikamp int err = 0, rc, block; 3982b71fc079SJan Kara int need_datasync = 0; 398308cefc7aSEric W. Biederman uid_t i_uid; 398408cefc7aSEric W. Biederman gid_t i_gid; 3985ac27a0ecSDave Kleikamp 3986ac27a0ecSDave Kleikamp /* For fields not not tracking in the in-memory inode, 3987ac27a0ecSDave Kleikamp * initialise them to zero for new inodes. */ 398819f5fb7aSTheodore Ts'o if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) 3989617ba13bSMingming Cao memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 3990ac27a0ecSDave Kleikamp 3991ff9ddf7eSJan Kara ext4_get_inode_flags(ei); 3992ac27a0ecSDave Kleikamp raw_inode->i_mode = cpu_to_le16(inode->i_mode); 399308cefc7aSEric W. Biederman i_uid = i_uid_read(inode); 399408cefc7aSEric W. Biederman i_gid = i_gid_read(inode); 3995ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 399608cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid)); 399708cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid)); 3998ac27a0ecSDave Kleikamp /* 3999ac27a0ecSDave Kleikamp * Fix up interoperability with old kernels. Otherwise, old inodes get 4000ac27a0ecSDave Kleikamp * re-used with the upper 16 bits of the uid/gid intact 4001ac27a0ecSDave Kleikamp */ 4002ac27a0ecSDave Kleikamp if (!ei->i_dtime) { 4003ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 400408cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_uid)); 4005ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 400608cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_gid)); 4007ac27a0ecSDave Kleikamp } else { 4008ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4009ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4010ac27a0ecSDave Kleikamp } 4011ac27a0ecSDave Kleikamp } else { 401208cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid)); 401308cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid)); 4014ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4015ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4016ac27a0ecSDave Kleikamp } 4017ac27a0ecSDave Kleikamp raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 4018ef7f3835SKalpak Shah 4019ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 4020ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 4021ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 4022ef7f3835SKalpak Shah EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 4023ef7f3835SKalpak Shah 40240fc1b451SAneesh Kumar K.V if (ext4_inode_blocks_set(handle, raw_inode, ei)) 40250fc1b451SAneesh Kumar K.V goto out_brelse; 4026ac27a0ecSDave Kleikamp raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 4027353eb83cSTheodore Ts'o raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF); 40289b8f1f01SMingming Cao if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 40299b8f1f01SMingming Cao cpu_to_le32(EXT4_OS_HURD)) 4030a1ddeb7eSBadari Pulavarty raw_inode->i_file_acl_high = 4031a1ddeb7eSBadari Pulavarty cpu_to_le16(ei->i_file_acl >> 32); 40327973c0c1SAneesh Kumar K.V raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 4033b71fc079SJan Kara if (ei->i_disksize != ext4_isize(raw_inode)) { 4034a48380f7SAneesh Kumar K.V ext4_isize_set(raw_inode, ei->i_disksize); 4035b71fc079SJan Kara need_datasync = 1; 4036b71fc079SJan Kara } 4037ac27a0ecSDave Kleikamp if (ei->i_disksize > 0x7fffffffULL) { 4038ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 4039617ba13bSMingming Cao if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, 4040617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE) || 4041617ba13bSMingming Cao EXT4_SB(sb)->s_es->s_rev_level == 4042617ba13bSMingming Cao cpu_to_le32(EXT4_GOOD_OLD_REV)) { 4043ac27a0ecSDave Kleikamp /* If this is the first large file 4044ac27a0ecSDave Kleikamp * created, add a flag to the superblock. 4045ac27a0ecSDave Kleikamp */ 4046617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, 4047617ba13bSMingming Cao EXT4_SB(sb)->s_sbh); 4048ac27a0ecSDave Kleikamp if (err) 4049ac27a0ecSDave Kleikamp goto out_brelse; 4050617ba13bSMingming Cao ext4_update_dynamic_rev(sb); 4051617ba13bSMingming Cao EXT4_SET_RO_COMPAT_FEATURE(sb, 4052617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE); 40530390131bSFrank Mayhar ext4_handle_sync(handle); 4054b50924c2SArtem Bityutskiy err = ext4_handle_dirty_super(handle, sb); 4055ac27a0ecSDave Kleikamp } 4056ac27a0ecSDave Kleikamp } 4057ac27a0ecSDave Kleikamp raw_inode->i_generation = cpu_to_le32(inode->i_generation); 4058ac27a0ecSDave Kleikamp if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 4059ac27a0ecSDave Kleikamp if (old_valid_dev(inode->i_rdev)) { 4060ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 4061ac27a0ecSDave Kleikamp cpu_to_le32(old_encode_dev(inode->i_rdev)); 4062ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 0; 4063ac27a0ecSDave Kleikamp } else { 4064ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 0; 4065ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 4066ac27a0ecSDave Kleikamp cpu_to_le32(new_encode_dev(inode->i_rdev)); 4067ac27a0ecSDave Kleikamp raw_inode->i_block[2] = 0; 4068ac27a0ecSDave Kleikamp } 4069f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 4070de9a55b8STheodore Ts'o for (block = 0; block < EXT4_N_BLOCKS; block++) 4071ac27a0ecSDave Kleikamp raw_inode->i_block[block] = ei->i_data[block]; 4072f19d5870STao Ma } 4073ac27a0ecSDave Kleikamp 407425ec56b5SJean Noel Cordenner raw_inode->i_disk_version = cpu_to_le32(inode->i_version); 407525ec56b5SJean Noel Cordenner if (ei->i_extra_isize) { 407625ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 407725ec56b5SJean Noel Cordenner raw_inode->i_version_hi = 407825ec56b5SJean Noel Cordenner cpu_to_le32(inode->i_version >> 32); 4079ac27a0ecSDave Kleikamp raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize); 408025ec56b5SJean Noel Cordenner } 408125ec56b5SJean Noel Cordenner 4082814525f4SDarrick J. Wong ext4_inode_csum_set(inode, raw_inode, ei); 4083814525f4SDarrick J. Wong 40840390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 408573b50c1cSCurt Wohlgemuth rc = ext4_handle_dirty_metadata(handle, NULL, bh); 4086ac27a0ecSDave Kleikamp if (!err) 4087ac27a0ecSDave Kleikamp err = rc; 408819f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_NEW); 4089ac27a0ecSDave Kleikamp 4090b71fc079SJan Kara ext4_update_inode_fsync_trans(handle, inode, need_datasync); 4091ac27a0ecSDave Kleikamp out_brelse: 4092ac27a0ecSDave Kleikamp brelse(bh); 4093617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4094ac27a0ecSDave Kleikamp return err; 4095ac27a0ecSDave Kleikamp } 4096ac27a0ecSDave Kleikamp 4097ac27a0ecSDave Kleikamp /* 4098617ba13bSMingming Cao * ext4_write_inode() 4099ac27a0ecSDave Kleikamp * 4100ac27a0ecSDave Kleikamp * We are called from a few places: 4101ac27a0ecSDave Kleikamp * 4102ac27a0ecSDave Kleikamp * - Within generic_file_write() for O_SYNC files. 4103ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 41044907cb7bSAnatol Pomozov * transaction to commit. 4105ac27a0ecSDave Kleikamp * 4106ac27a0ecSDave Kleikamp * - Within sys_sync(), kupdate and such. 4107ac27a0ecSDave Kleikamp * We wait on commit, if tol to. 4108ac27a0ecSDave Kleikamp * 4109ac27a0ecSDave Kleikamp * - Within prune_icache() (PF_MEMALLOC == true) 4110ac27a0ecSDave Kleikamp * Here we simply return. We can't afford to block kswapd on the 4111ac27a0ecSDave Kleikamp * journal commit. 4112ac27a0ecSDave Kleikamp * 4113ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 4114ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 4115617ba13bSMingming Cao * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for 4116ac27a0ecSDave Kleikamp * knfsd. 4117ac27a0ecSDave Kleikamp * 4118ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 4119ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 4120ac27a0ecSDave Kleikamp * which we are interested. 4121ac27a0ecSDave Kleikamp * 4122ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 4123ac27a0ecSDave Kleikamp * 4124ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 4125ac27a0ecSDave Kleikamp * stuff(); 4126ac27a0ecSDave Kleikamp * inode->i_size = expr; 4127ac27a0ecSDave Kleikamp * 4128ac27a0ecSDave Kleikamp * is in error because a kswapd-driven write_inode() could occur while 4129ac27a0ecSDave Kleikamp * `stuff()' is running, and the new i_size will be lost. Plus the inode 4130ac27a0ecSDave Kleikamp * will no longer be on the superblock's dirty inode list. 4131ac27a0ecSDave Kleikamp */ 4132a9185b41SChristoph Hellwig int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) 4133ac27a0ecSDave Kleikamp { 413491ac6f43SFrank Mayhar int err; 413591ac6f43SFrank Mayhar 4136ac27a0ecSDave Kleikamp if (current->flags & PF_MEMALLOC) 4137ac27a0ecSDave Kleikamp return 0; 4138ac27a0ecSDave Kleikamp 413991ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal) { 4140617ba13bSMingming Cao if (ext4_journal_current_handle()) { 4141b38bd33aSMingming Cao jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); 4142ac27a0ecSDave Kleikamp dump_stack(); 4143ac27a0ecSDave Kleikamp return -EIO; 4144ac27a0ecSDave Kleikamp } 4145ac27a0ecSDave Kleikamp 4146a9185b41SChristoph Hellwig if (wbc->sync_mode != WB_SYNC_ALL) 4147ac27a0ecSDave Kleikamp return 0; 4148ac27a0ecSDave Kleikamp 414991ac6f43SFrank Mayhar err = ext4_force_commit(inode->i_sb); 415091ac6f43SFrank Mayhar } else { 415191ac6f43SFrank Mayhar struct ext4_iloc iloc; 415291ac6f43SFrank Mayhar 41538b472d73SCurt Wohlgemuth err = __ext4_get_inode_loc(inode, &iloc, 0); 415491ac6f43SFrank Mayhar if (err) 415591ac6f43SFrank Mayhar return err; 4156a9185b41SChristoph Hellwig if (wbc->sync_mode == WB_SYNC_ALL) 4157830156c7SFrank Mayhar sync_dirty_buffer(iloc.bh); 4158830156c7SFrank Mayhar if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { 4159c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr, 4160c398eda0STheodore Ts'o "IO error syncing inode"); 4161830156c7SFrank Mayhar err = -EIO; 4162830156c7SFrank Mayhar } 4163fd2dd9fbSCurt Wohlgemuth brelse(iloc.bh); 416491ac6f43SFrank Mayhar } 416591ac6f43SFrank Mayhar return err; 4166ac27a0ecSDave Kleikamp } 4167ac27a0ecSDave Kleikamp 4168ac27a0ecSDave Kleikamp /* 416953e87268SJan Kara * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate 417053e87268SJan Kara * buffers that are attached to a page stradding i_size and are undergoing 417153e87268SJan Kara * commit. In that case we have to wait for commit to finish and try again. 417253e87268SJan Kara */ 417353e87268SJan Kara static void ext4_wait_for_tail_page_commit(struct inode *inode) 417453e87268SJan Kara { 417553e87268SJan Kara struct page *page; 417653e87268SJan Kara unsigned offset; 417753e87268SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 417853e87268SJan Kara tid_t commit_tid = 0; 417953e87268SJan Kara int ret; 418053e87268SJan Kara 418153e87268SJan Kara offset = inode->i_size & (PAGE_CACHE_SIZE - 1); 418253e87268SJan Kara /* 418353e87268SJan Kara * All buffers in the last page remain valid? Then there's nothing to 418453e87268SJan Kara * do. We do the check mainly to optimize the common PAGE_CACHE_SIZE == 418553e87268SJan Kara * blocksize case 418653e87268SJan Kara */ 418753e87268SJan Kara if (offset > PAGE_CACHE_SIZE - (1 << inode->i_blkbits)) 418853e87268SJan Kara return; 418953e87268SJan Kara while (1) { 419053e87268SJan Kara page = find_lock_page(inode->i_mapping, 419153e87268SJan Kara inode->i_size >> PAGE_CACHE_SHIFT); 419253e87268SJan Kara if (!page) 419353e87268SJan Kara return; 419453e87268SJan Kara ret = __ext4_journalled_invalidatepage(page, offset); 419553e87268SJan Kara unlock_page(page); 419653e87268SJan Kara page_cache_release(page); 419753e87268SJan Kara if (ret != -EBUSY) 419853e87268SJan Kara return; 419953e87268SJan Kara commit_tid = 0; 420053e87268SJan Kara read_lock(&journal->j_state_lock); 420153e87268SJan Kara if (journal->j_committing_transaction) 420253e87268SJan Kara commit_tid = journal->j_committing_transaction->t_tid; 420353e87268SJan Kara read_unlock(&journal->j_state_lock); 420453e87268SJan Kara if (commit_tid) 420553e87268SJan Kara jbd2_log_wait_commit(journal, commit_tid); 420653e87268SJan Kara } 420753e87268SJan Kara } 420853e87268SJan Kara 420953e87268SJan Kara /* 4210617ba13bSMingming Cao * ext4_setattr() 4211ac27a0ecSDave Kleikamp * 4212ac27a0ecSDave Kleikamp * Called from notify_change. 4213ac27a0ecSDave Kleikamp * 4214ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 4215ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 4216ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 4217ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 4218ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 4219ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 4220ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 4221ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 4222ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 4223ac27a0ecSDave Kleikamp * 4224678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 4225678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 4226678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 4227678aaf48SJan Kara * This way we are sure that all the data written in the previous 4228678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 4229678aaf48SJan Kara * writeback). 4230678aaf48SJan Kara * 4231678aaf48SJan Kara * Called with inode->i_mutex down. 4232ac27a0ecSDave Kleikamp */ 4233617ba13bSMingming Cao int ext4_setattr(struct dentry *dentry, struct iattr *attr) 4234ac27a0ecSDave Kleikamp { 4235ac27a0ecSDave Kleikamp struct inode *inode = dentry->d_inode; 4236ac27a0ecSDave Kleikamp int error, rc = 0; 42373d287de3SDmitry Monakhov int orphan = 0; 4238ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 4239ac27a0ecSDave Kleikamp 4240ac27a0ecSDave Kleikamp error = inode_change_ok(inode, attr); 4241ac27a0ecSDave Kleikamp if (error) 4242ac27a0ecSDave Kleikamp return error; 4243ac27a0ecSDave Kleikamp 424412755627SDmitry Monakhov if (is_quota_modification(inode, attr)) 4245871a2931SChristoph Hellwig dquot_initialize(inode); 424608cefc7aSEric W. Biederman if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) || 424708cefc7aSEric W. Biederman (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) { 4248ac27a0ecSDave Kleikamp handle_t *handle; 4249ac27a0ecSDave Kleikamp 4250ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 4251ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 42529924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 42539924a92aSTheodore Ts'o (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) + 4254194074acSDmitry Monakhov EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3); 4255ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 4256ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 4257ac27a0ecSDave Kleikamp goto err_out; 4258ac27a0ecSDave Kleikamp } 4259b43fa828SChristoph Hellwig error = dquot_transfer(inode, attr); 4260ac27a0ecSDave Kleikamp if (error) { 4261617ba13bSMingming Cao ext4_journal_stop(handle); 4262ac27a0ecSDave Kleikamp return error; 4263ac27a0ecSDave Kleikamp } 4264ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 4265ac27a0ecSDave Kleikamp * one transaction */ 4266ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_UID) 4267ac27a0ecSDave Kleikamp inode->i_uid = attr->ia_uid; 4268ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_GID) 4269ac27a0ecSDave Kleikamp inode->i_gid = attr->ia_gid; 4270617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 4271617ba13bSMingming Cao ext4_journal_stop(handle); 4272ac27a0ecSDave Kleikamp } 4273ac27a0ecSDave Kleikamp 4274e2b46574SEric Sandeen if (attr->ia_valid & ATTR_SIZE) { 4275562c72aaSChristoph Hellwig 427612e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 4277e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4278e2b46574SEric Sandeen 42790c095c7fSTheodore Ts'o if (attr->ia_size > sbi->s_bitmap_maxbytes) 42800c095c7fSTheodore Ts'o return -EFBIG; 4281e2b46574SEric Sandeen } 4282e2b46574SEric Sandeen } 4283e2b46574SEric Sandeen 4284ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode) && 4285c8d46e41SJiaying Zhang attr->ia_valid & ATTR_SIZE && 4286072bd7eaSTheodore Ts'o (attr->ia_size < inode->i_size)) { 4287ac27a0ecSDave Kleikamp handle_t *handle; 4288ac27a0ecSDave Kleikamp 42899924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 3); 4290ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 4291ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 4292ac27a0ecSDave Kleikamp goto err_out; 4293ac27a0ecSDave Kleikamp } 42943d287de3SDmitry Monakhov if (ext4_handle_valid(handle)) { 4295617ba13bSMingming Cao error = ext4_orphan_add(handle, inode); 42963d287de3SDmitry Monakhov orphan = 1; 42973d287de3SDmitry Monakhov } 4298617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 4299617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 4300ac27a0ecSDave Kleikamp if (!error) 4301ac27a0ecSDave Kleikamp error = rc; 4302617ba13bSMingming Cao ext4_journal_stop(handle); 4303678aaf48SJan Kara 4304678aaf48SJan Kara if (ext4_should_order_data(inode)) { 4305678aaf48SJan Kara error = ext4_begin_ordered_truncate(inode, 4306678aaf48SJan Kara attr->ia_size); 4307678aaf48SJan Kara if (error) { 4308678aaf48SJan Kara /* Do as much error cleanup as possible */ 43099924a92aSTheodore Ts'o handle = ext4_journal_start(inode, 43109924a92aSTheodore Ts'o EXT4_HT_INODE, 3); 4311678aaf48SJan Kara if (IS_ERR(handle)) { 4312678aaf48SJan Kara ext4_orphan_del(NULL, inode); 4313678aaf48SJan Kara goto err_out; 4314678aaf48SJan Kara } 4315678aaf48SJan Kara ext4_orphan_del(handle, inode); 43163d287de3SDmitry Monakhov orphan = 0; 4317678aaf48SJan Kara ext4_journal_stop(handle); 4318678aaf48SJan Kara goto err_out; 4319678aaf48SJan Kara } 4320678aaf48SJan Kara } 4321ac27a0ecSDave Kleikamp } 4322ac27a0ecSDave Kleikamp 4323072bd7eaSTheodore Ts'o if (attr->ia_valid & ATTR_SIZE) { 432453e87268SJan Kara if (attr->ia_size != inode->i_size) { 432553e87268SJan Kara loff_t oldsize = inode->i_size; 432653e87268SJan Kara 432753e87268SJan Kara i_size_write(inode, attr->ia_size); 432853e87268SJan Kara /* 432953e87268SJan Kara * Blocks are going to be removed from the inode. Wait 433053e87268SJan Kara * for dio in flight. Temporarily disable 433153e87268SJan Kara * dioread_nolock to prevent livelock. 433253e87268SJan Kara */ 43331b65007eSDmitry Monakhov if (orphan) { 433453e87268SJan Kara if (!ext4_should_journal_data(inode)) { 43351b65007eSDmitry Monakhov ext4_inode_block_unlocked_dio(inode); 43361c9114f9SDmitry Monakhov inode_dio_wait(inode); 43371b65007eSDmitry Monakhov ext4_inode_resume_unlocked_dio(inode); 433853e87268SJan Kara } else 433953e87268SJan Kara ext4_wait_for_tail_page_commit(inode); 43401b65007eSDmitry Monakhov } 434153e87268SJan Kara /* 434253e87268SJan Kara * Truncate pagecache after we've waited for commit 434353e87268SJan Kara * in data=journal mode to make pages freeable. 434453e87268SJan Kara */ 434553e87268SJan Kara truncate_pagecache(inode, oldsize, inode->i_size); 43461c9114f9SDmitry Monakhov } 4347072bd7eaSTheodore Ts'o ext4_truncate(inode); 4348072bd7eaSTheodore Ts'o } 4349ac27a0ecSDave Kleikamp 43501025774cSChristoph Hellwig if (!rc) { 43511025774cSChristoph Hellwig setattr_copy(inode, attr); 43521025774cSChristoph Hellwig mark_inode_dirty(inode); 43531025774cSChristoph Hellwig } 43541025774cSChristoph Hellwig 43551025774cSChristoph Hellwig /* 43561025774cSChristoph Hellwig * If the call to ext4_truncate failed to get a transaction handle at 43571025774cSChristoph Hellwig * all, we need to clean up the in-core orphan list manually. 43581025774cSChristoph Hellwig */ 43593d287de3SDmitry Monakhov if (orphan && inode->i_nlink) 4360617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 4361ac27a0ecSDave Kleikamp 4362ac27a0ecSDave Kleikamp if (!rc && (ia_valid & ATTR_MODE)) 4363617ba13bSMingming Cao rc = ext4_acl_chmod(inode); 4364ac27a0ecSDave Kleikamp 4365ac27a0ecSDave Kleikamp err_out: 4366617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 4367ac27a0ecSDave Kleikamp if (!error) 4368ac27a0ecSDave Kleikamp error = rc; 4369ac27a0ecSDave Kleikamp return error; 4370ac27a0ecSDave Kleikamp } 4371ac27a0ecSDave Kleikamp 43723e3398a0SMingming Cao int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry, 43733e3398a0SMingming Cao struct kstat *stat) 43743e3398a0SMingming Cao { 43753e3398a0SMingming Cao struct inode *inode; 43763e3398a0SMingming Cao unsigned long delalloc_blocks; 43773e3398a0SMingming Cao 43783e3398a0SMingming Cao inode = dentry->d_inode; 43793e3398a0SMingming Cao generic_fillattr(inode, stat); 43803e3398a0SMingming Cao 43813e3398a0SMingming Cao /* 43823e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 43833e3398a0SMingming Cao * otherwise in the case of system crash before the real block 43843e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 43853e3398a0SMingming Cao * on-disk file blocks. 43863e3398a0SMingming Cao * We always keep i_blocks updated together with real 43873e3398a0SMingming Cao * allocation. But to not confuse with user, stat 43883e3398a0SMingming Cao * will return the blocks that include the delayed allocation 43893e3398a0SMingming Cao * blocks for this file. 43903e3398a0SMingming Cao */ 439196607551STao Ma delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb), 439296607551STao Ma EXT4_I(inode)->i_reserved_data_blocks); 43933e3398a0SMingming Cao 43943e3398a0SMingming Cao stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9; 43953e3398a0SMingming Cao return 0; 43963e3398a0SMingming Cao } 4397ac27a0ecSDave Kleikamp 4398a02908f1SMingming Cao static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4399a02908f1SMingming Cao { 440012e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 44018bb2b247SAmir Goldstein return ext4_ind_trans_blocks(inode, nrblocks, chunk); 4402ac51d837STheodore Ts'o return ext4_ext_index_trans_blocks(inode, nrblocks, chunk); 4403a02908f1SMingming Cao } 4404ac51d837STheodore Ts'o 4405a02908f1SMingming Cao /* 4406a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 4407a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 4408a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 4409a02908f1SMingming Cao * 4410a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 44114907cb7bSAnatol Pomozov * different block groups too. If they are contiguous, with flexbg, 4412a02908f1SMingming Cao * they could still across block group boundary. 4413a02908f1SMingming Cao * 4414a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 4415a02908f1SMingming Cao */ 44161f109d5aSTheodore Ts'o static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4417a02908f1SMingming Cao { 44188df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 44198df9675fSTheodore Ts'o int gdpblocks; 4420a02908f1SMingming Cao int idxblocks; 4421a02908f1SMingming Cao int ret = 0; 4422a02908f1SMingming Cao 4423a02908f1SMingming Cao /* 4424a02908f1SMingming Cao * How many index blocks need to touch to modify nrblocks? 4425a02908f1SMingming Cao * The "Chunk" flag indicating whether the nrblocks is 4426a02908f1SMingming Cao * physically contiguous on disk 4427a02908f1SMingming Cao * 4428a02908f1SMingming Cao * For Direct IO and fallocate, they calls get_block to allocate 4429a02908f1SMingming Cao * one single extent at a time, so they could set the "Chunk" flag 4430a02908f1SMingming Cao */ 4431a02908f1SMingming Cao idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk); 4432a02908f1SMingming Cao 4433a02908f1SMingming Cao ret = idxblocks; 4434a02908f1SMingming Cao 4435a02908f1SMingming Cao /* 4436a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 4437a02908f1SMingming Cao * to account 4438a02908f1SMingming Cao */ 4439a02908f1SMingming Cao groups = idxblocks; 4440a02908f1SMingming Cao if (chunk) 4441a02908f1SMingming Cao groups += 1; 4442ac27a0ecSDave Kleikamp else 4443a02908f1SMingming Cao groups += nrblocks; 4444ac27a0ecSDave Kleikamp 4445a02908f1SMingming Cao gdpblocks = groups; 44468df9675fSTheodore Ts'o if (groups > ngroups) 44478df9675fSTheodore Ts'o groups = ngroups; 4448a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 4449a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 4450a02908f1SMingming Cao 4451a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 4452a02908f1SMingming Cao ret += groups + gdpblocks; 4453a02908f1SMingming Cao 4454a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 4455a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 4456ac27a0ecSDave Kleikamp 4457ac27a0ecSDave Kleikamp return ret; 4458ac27a0ecSDave Kleikamp } 4459ac27a0ecSDave Kleikamp 4460ac27a0ecSDave Kleikamp /* 446125985edcSLucas De Marchi * Calculate the total number of credits to reserve to fit 4462f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 4463f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 4464a02908f1SMingming Cao * 4465525f4ed8SMingming Cao * This could be called via ext4_write_begin() 4466a02908f1SMingming Cao * 4467525f4ed8SMingming Cao * We need to consider the worse case, when 4468a02908f1SMingming Cao * one new block per extent. 4469a02908f1SMingming Cao */ 4470a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 4471a02908f1SMingming Cao { 4472a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 4473a02908f1SMingming Cao int ret; 4474a02908f1SMingming Cao 4475a02908f1SMingming Cao ret = ext4_meta_trans_blocks(inode, bpp, 0); 4476a02908f1SMingming Cao 4477a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 4478a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 4479a02908f1SMingming Cao ret += bpp; 4480a02908f1SMingming Cao return ret; 4481a02908f1SMingming Cao } 4482f3bd1f3fSMingming Cao 4483f3bd1f3fSMingming Cao /* 4484f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 4485f3bd1f3fSMingming Cao * 4486f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 448779e83036SEric Sandeen * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks. 4488f3bd1f3fSMingming Cao * 4489f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 4490f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 4491f3bd1f3fSMingming Cao */ 4492f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 4493f3bd1f3fSMingming Cao { 4494f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 4495f3bd1f3fSMingming Cao } 4496f3bd1f3fSMingming Cao 4497a02908f1SMingming Cao /* 4498617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 4499ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 4500ac27a0ecSDave Kleikamp */ 4501617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 4502617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 4503ac27a0ecSDave Kleikamp { 4504ac27a0ecSDave Kleikamp int err = 0; 4505ac27a0ecSDave Kleikamp 4506c64db50eSTheodore Ts'o if (IS_I_VERSION(inode)) 450725ec56b5SJean Noel Cordenner inode_inc_iversion(inode); 450825ec56b5SJean Noel Cordenner 4509ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 4510ac27a0ecSDave Kleikamp get_bh(iloc->bh); 4511ac27a0ecSDave Kleikamp 4512dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 4513830156c7SFrank Mayhar err = ext4_do_update_inode(handle, inode, iloc); 4514ac27a0ecSDave Kleikamp put_bh(iloc->bh); 4515ac27a0ecSDave Kleikamp return err; 4516ac27a0ecSDave Kleikamp } 4517ac27a0ecSDave Kleikamp 4518ac27a0ecSDave Kleikamp /* 4519ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 4520ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 4521ac27a0ecSDave Kleikamp */ 4522ac27a0ecSDave Kleikamp 4523ac27a0ecSDave Kleikamp int 4524617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 4525617ba13bSMingming Cao struct ext4_iloc *iloc) 4526ac27a0ecSDave Kleikamp { 45270390131bSFrank Mayhar int err; 45280390131bSFrank Mayhar 4529617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 4530ac27a0ecSDave Kleikamp if (!err) { 4531ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 4532617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, iloc->bh); 4533ac27a0ecSDave Kleikamp if (err) { 4534ac27a0ecSDave Kleikamp brelse(iloc->bh); 4535ac27a0ecSDave Kleikamp iloc->bh = NULL; 4536ac27a0ecSDave Kleikamp } 4537ac27a0ecSDave Kleikamp } 4538617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4539ac27a0ecSDave Kleikamp return err; 4540ac27a0ecSDave Kleikamp } 4541ac27a0ecSDave Kleikamp 4542ac27a0ecSDave Kleikamp /* 45436dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 45446dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 45456dd4ee7cSKalpak Shah */ 45461d03ec98SAneesh Kumar K.V static int ext4_expand_extra_isize(struct inode *inode, 45471d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 45481d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 45491d03ec98SAneesh Kumar K.V handle_t *handle) 45506dd4ee7cSKalpak Shah { 45516dd4ee7cSKalpak Shah struct ext4_inode *raw_inode; 45526dd4ee7cSKalpak Shah struct ext4_xattr_ibody_header *header; 45536dd4ee7cSKalpak Shah 45546dd4ee7cSKalpak Shah if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) 45556dd4ee7cSKalpak Shah return 0; 45566dd4ee7cSKalpak Shah 45576dd4ee7cSKalpak Shah raw_inode = ext4_raw_inode(&iloc); 45586dd4ee7cSKalpak Shah 45596dd4ee7cSKalpak Shah header = IHDR(inode, raw_inode); 45606dd4ee7cSKalpak Shah 45616dd4ee7cSKalpak Shah /* No extended attributes present */ 456219f5fb7aSTheodore Ts'o if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) || 45636dd4ee7cSKalpak Shah header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 45646dd4ee7cSKalpak Shah memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0, 45656dd4ee7cSKalpak Shah new_extra_isize); 45666dd4ee7cSKalpak Shah EXT4_I(inode)->i_extra_isize = new_extra_isize; 45676dd4ee7cSKalpak Shah return 0; 45686dd4ee7cSKalpak Shah } 45696dd4ee7cSKalpak Shah 45706dd4ee7cSKalpak Shah /* try to expand with EAs present */ 45716dd4ee7cSKalpak Shah return ext4_expand_extra_isize_ea(inode, new_extra_isize, 45726dd4ee7cSKalpak Shah raw_inode, handle); 45736dd4ee7cSKalpak Shah } 45746dd4ee7cSKalpak Shah 45756dd4ee7cSKalpak Shah /* 4576ac27a0ecSDave Kleikamp * What we do here is to mark the in-core inode as clean with respect to inode 4577ac27a0ecSDave Kleikamp * dirtiness (it may still be data-dirty). 4578ac27a0ecSDave Kleikamp * This means that the in-core inode may be reaped by prune_icache 4579ac27a0ecSDave Kleikamp * without having to perform any I/O. This is a very good thing, 4580ac27a0ecSDave Kleikamp * because *any* task may call prune_icache - even ones which 4581ac27a0ecSDave Kleikamp * have a transaction open against a different journal. 4582ac27a0ecSDave Kleikamp * 4583ac27a0ecSDave Kleikamp * Is this cheating? Not really. Sure, we haven't written the 4584ac27a0ecSDave Kleikamp * inode out, but prune_icache isn't a user-visible syncing function. 4585ac27a0ecSDave Kleikamp * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 4586ac27a0ecSDave Kleikamp * we start and wait on commits. 4587ac27a0ecSDave Kleikamp */ 4588617ba13bSMingming Cao int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode) 4589ac27a0ecSDave Kleikamp { 4590617ba13bSMingming Cao struct ext4_iloc iloc; 45916dd4ee7cSKalpak Shah struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 45926dd4ee7cSKalpak Shah static unsigned int mnt_count; 45936dd4ee7cSKalpak Shah int err, ret; 4594ac27a0ecSDave Kleikamp 4595ac27a0ecSDave Kleikamp might_sleep(); 45967ff9c073STheodore Ts'o trace_ext4_mark_inode_dirty(inode, _RET_IP_); 4597617ba13bSMingming Cao err = ext4_reserve_inode_write(handle, inode, &iloc); 45980390131bSFrank Mayhar if (ext4_handle_valid(handle) && 45990390131bSFrank Mayhar EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize && 460019f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) { 46016dd4ee7cSKalpak Shah /* 46026dd4ee7cSKalpak Shah * We need extra buffer credits since we may write into EA block 46036dd4ee7cSKalpak Shah * with this same handle. If journal_extend fails, then it will 46046dd4ee7cSKalpak Shah * only result in a minor loss of functionality for that inode. 46056dd4ee7cSKalpak Shah * If this is felt to be critical, then e2fsck should be run to 46066dd4ee7cSKalpak Shah * force a large enough s_min_extra_isize. 46076dd4ee7cSKalpak Shah */ 46086dd4ee7cSKalpak Shah if ((jbd2_journal_extend(handle, 46096dd4ee7cSKalpak Shah EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) { 46106dd4ee7cSKalpak Shah ret = ext4_expand_extra_isize(inode, 46116dd4ee7cSKalpak Shah sbi->s_want_extra_isize, 46126dd4ee7cSKalpak Shah iloc, handle); 46136dd4ee7cSKalpak Shah if (ret) { 461419f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, 461519f5fb7aSTheodore Ts'o EXT4_STATE_NO_EXPAND); 4616c1bddad9SAneesh Kumar K.V if (mnt_count != 4617c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count)) { 461812062dddSEric Sandeen ext4_warning(inode->i_sb, 46196dd4ee7cSKalpak Shah "Unable to expand inode %lu. Delete" 46206dd4ee7cSKalpak Shah " some EAs or run e2fsck.", 46216dd4ee7cSKalpak Shah inode->i_ino); 4622c1bddad9SAneesh Kumar K.V mnt_count = 4623c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count); 46246dd4ee7cSKalpak Shah } 46256dd4ee7cSKalpak Shah } 46266dd4ee7cSKalpak Shah } 46276dd4ee7cSKalpak Shah } 4628ac27a0ecSDave Kleikamp if (!err) 4629617ba13bSMingming Cao err = ext4_mark_iloc_dirty(handle, inode, &iloc); 4630ac27a0ecSDave Kleikamp return err; 4631ac27a0ecSDave Kleikamp } 4632ac27a0ecSDave Kleikamp 4633ac27a0ecSDave Kleikamp /* 4634617ba13bSMingming Cao * ext4_dirty_inode() is called from __mark_inode_dirty() 4635ac27a0ecSDave Kleikamp * 4636ac27a0ecSDave Kleikamp * We're really interested in the case where a file is being extended. 4637ac27a0ecSDave Kleikamp * i_size has been changed by generic_commit_write() and we thus need 4638ac27a0ecSDave Kleikamp * to include the updated inode in the current transaction. 4639ac27a0ecSDave Kleikamp * 46405dd4056dSChristoph Hellwig * Also, dquot_alloc_block() will always dirty the inode when blocks 4641ac27a0ecSDave Kleikamp * are allocated to the file. 4642ac27a0ecSDave Kleikamp * 4643ac27a0ecSDave Kleikamp * If the inode is marked synchronous, we don't honour that here - doing 4644ac27a0ecSDave Kleikamp * so would cause a commit on atime updates, which we don't bother doing. 4645ac27a0ecSDave Kleikamp * We handle synchronous inodes at the highest possible level. 4646ac27a0ecSDave Kleikamp */ 4647aa385729SChristoph Hellwig void ext4_dirty_inode(struct inode *inode, int flags) 4648ac27a0ecSDave Kleikamp { 4649ac27a0ecSDave Kleikamp handle_t *handle; 4650ac27a0ecSDave Kleikamp 46519924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 2); 4652ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 4653ac27a0ecSDave Kleikamp goto out; 4654f3dc272fSCurt Wohlgemuth 4655617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 4656f3dc272fSCurt Wohlgemuth 4657617ba13bSMingming Cao ext4_journal_stop(handle); 4658ac27a0ecSDave Kleikamp out: 4659ac27a0ecSDave Kleikamp return; 4660ac27a0ecSDave Kleikamp } 4661ac27a0ecSDave Kleikamp 4662ac27a0ecSDave Kleikamp #if 0 4663ac27a0ecSDave Kleikamp /* 4664ac27a0ecSDave Kleikamp * Bind an inode's backing buffer_head into this transaction, to prevent 4665ac27a0ecSDave Kleikamp * it from being flushed to disk early. Unlike 4666617ba13bSMingming Cao * ext4_reserve_inode_write, this leaves behind no bh reference and 4667ac27a0ecSDave Kleikamp * returns no iloc structure, so the caller needs to repeat the iloc 4668ac27a0ecSDave Kleikamp * lookup to mark the inode dirty later. 4669ac27a0ecSDave Kleikamp */ 4670617ba13bSMingming Cao static int ext4_pin_inode(handle_t *handle, struct inode *inode) 4671ac27a0ecSDave Kleikamp { 4672617ba13bSMingming Cao struct ext4_iloc iloc; 4673ac27a0ecSDave Kleikamp 4674ac27a0ecSDave Kleikamp int err = 0; 4675ac27a0ecSDave Kleikamp if (handle) { 4676617ba13bSMingming Cao err = ext4_get_inode_loc(inode, &iloc); 4677ac27a0ecSDave Kleikamp if (!err) { 4678ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc.bh, "get_write_access"); 4679dab291afSMingming Cao err = jbd2_journal_get_write_access(handle, iloc.bh); 4680ac27a0ecSDave Kleikamp if (!err) 46810390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, 468273b50c1cSCurt Wohlgemuth NULL, 4683ac27a0ecSDave Kleikamp iloc.bh); 4684ac27a0ecSDave Kleikamp brelse(iloc.bh); 4685ac27a0ecSDave Kleikamp } 4686ac27a0ecSDave Kleikamp } 4687617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4688ac27a0ecSDave Kleikamp return err; 4689ac27a0ecSDave Kleikamp } 4690ac27a0ecSDave Kleikamp #endif 4691ac27a0ecSDave Kleikamp 4692617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val) 4693ac27a0ecSDave Kleikamp { 4694ac27a0ecSDave Kleikamp journal_t *journal; 4695ac27a0ecSDave Kleikamp handle_t *handle; 4696ac27a0ecSDave Kleikamp int err; 4697ac27a0ecSDave Kleikamp 4698ac27a0ecSDave Kleikamp /* 4699ac27a0ecSDave Kleikamp * We have to be very careful here: changing a data block's 4700ac27a0ecSDave Kleikamp * journaling status dynamically is dangerous. If we write a 4701ac27a0ecSDave Kleikamp * data block to the journal, change the status and then delete 4702ac27a0ecSDave Kleikamp * that block, we risk forgetting to revoke the old log record 4703ac27a0ecSDave Kleikamp * from the journal and so a subsequent replay can corrupt data. 4704ac27a0ecSDave Kleikamp * So, first we make sure that the journal is empty and that 4705ac27a0ecSDave Kleikamp * nobody is changing anything. 4706ac27a0ecSDave Kleikamp */ 4707ac27a0ecSDave Kleikamp 4708617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 47090390131bSFrank Mayhar if (!journal) 47100390131bSFrank Mayhar return 0; 4711d699594dSDave Hansen if (is_journal_aborted(journal)) 4712ac27a0ecSDave Kleikamp return -EROFS; 47132aff57b0SYongqiang Yang /* We have to allocate physical blocks for delalloc blocks 47142aff57b0SYongqiang Yang * before flushing journal. otherwise delalloc blocks can not 47152aff57b0SYongqiang Yang * be allocated any more. even more truncate on delalloc blocks 47162aff57b0SYongqiang Yang * could trigger BUG by flushing delalloc blocks in journal. 47172aff57b0SYongqiang Yang * There is no delalloc block in non-journal data mode. 47182aff57b0SYongqiang Yang */ 47192aff57b0SYongqiang Yang if (val && test_opt(inode->i_sb, DELALLOC)) { 47202aff57b0SYongqiang Yang err = ext4_alloc_da_blocks(inode); 47212aff57b0SYongqiang Yang if (err < 0) 47222aff57b0SYongqiang Yang return err; 47232aff57b0SYongqiang Yang } 4724ac27a0ecSDave Kleikamp 472517335dccSDmitry Monakhov /* Wait for all existing dio workers */ 472617335dccSDmitry Monakhov ext4_inode_block_unlocked_dio(inode); 472717335dccSDmitry Monakhov inode_dio_wait(inode); 472817335dccSDmitry Monakhov 4729dab291afSMingming Cao jbd2_journal_lock_updates(journal); 4730ac27a0ecSDave Kleikamp 4731ac27a0ecSDave Kleikamp /* 4732ac27a0ecSDave Kleikamp * OK, there are no updates running now, and all cached data is 4733ac27a0ecSDave Kleikamp * synced to disk. We are now in a completely consistent state 4734ac27a0ecSDave Kleikamp * which doesn't have anything in the journal, and we know that 4735ac27a0ecSDave Kleikamp * no filesystem updates are running, so it is safe to modify 4736ac27a0ecSDave Kleikamp * the inode's in-core data-journaling state flag now. 4737ac27a0ecSDave Kleikamp */ 4738ac27a0ecSDave Kleikamp 4739ac27a0ecSDave Kleikamp if (val) 474012e9b892SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 47415872ddaaSYongqiang Yang else { 47425872ddaaSYongqiang Yang jbd2_journal_flush(journal); 474312e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 47445872ddaaSYongqiang Yang } 4745617ba13bSMingming Cao ext4_set_aops(inode); 4746ac27a0ecSDave Kleikamp 4747dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 474817335dccSDmitry Monakhov ext4_inode_resume_unlocked_dio(inode); 4749ac27a0ecSDave Kleikamp 4750ac27a0ecSDave Kleikamp /* Finally we can mark the inode as dirty. */ 4751ac27a0ecSDave Kleikamp 47529924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 4753ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 4754ac27a0ecSDave Kleikamp return PTR_ERR(handle); 4755ac27a0ecSDave Kleikamp 4756617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 47570390131bSFrank Mayhar ext4_handle_sync(handle); 4758617ba13bSMingming Cao ext4_journal_stop(handle); 4759617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4760ac27a0ecSDave Kleikamp 4761ac27a0ecSDave Kleikamp return err; 4762ac27a0ecSDave Kleikamp } 47632e9ee850SAneesh Kumar K.V 47642e9ee850SAneesh Kumar K.V static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) 47652e9ee850SAneesh Kumar K.V { 47662e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 47672e9ee850SAneesh Kumar K.V } 47682e9ee850SAneesh Kumar K.V 4769c2ec175cSNick Piggin int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) 47702e9ee850SAneesh Kumar K.V { 4771c2ec175cSNick Piggin struct page *page = vmf->page; 47722e9ee850SAneesh Kumar K.V loff_t size; 47732e9ee850SAneesh Kumar K.V unsigned long len; 47749ea7df53SJan Kara int ret; 47752e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 47762e9ee850SAneesh Kumar K.V struct inode *inode = file->f_path.dentry->d_inode; 47772e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 47789ea7df53SJan Kara handle_t *handle; 47799ea7df53SJan Kara get_block_t *get_block; 47809ea7df53SJan Kara int retries = 0; 47812e9ee850SAneesh Kumar K.V 47828e8ad8a5SJan Kara sb_start_pagefault(inode->i_sb); 4783041bbb6dSTheodore Ts'o file_update_time(vma->vm_file); 47849ea7df53SJan Kara /* Delalloc case is easy... */ 47859ea7df53SJan Kara if (test_opt(inode->i_sb, DELALLOC) && 47869ea7df53SJan Kara !ext4_should_journal_data(inode) && 47879ea7df53SJan Kara !ext4_nonda_switch(inode->i_sb)) { 47889ea7df53SJan Kara do { 47899ea7df53SJan Kara ret = __block_page_mkwrite(vma, vmf, 47909ea7df53SJan Kara ext4_da_get_block_prep); 47919ea7df53SJan Kara } while (ret == -ENOSPC && 47929ea7df53SJan Kara ext4_should_retry_alloc(inode->i_sb, &retries)); 47939ea7df53SJan Kara goto out_ret; 47942e9ee850SAneesh Kumar K.V } 47950e499890SDarrick J. Wong 47960e499890SDarrick J. Wong lock_page(page); 47979ea7df53SJan Kara size = i_size_read(inode); 47989ea7df53SJan Kara /* Page got truncated from under us? */ 47999ea7df53SJan Kara if (page->mapping != mapping || page_offset(page) > size) { 48009ea7df53SJan Kara unlock_page(page); 48019ea7df53SJan Kara ret = VM_FAULT_NOPAGE; 48029ea7df53SJan Kara goto out; 48030e499890SDarrick J. Wong } 48042e9ee850SAneesh Kumar K.V 48052e9ee850SAneesh Kumar K.V if (page->index == size >> PAGE_CACHE_SHIFT) 48062e9ee850SAneesh Kumar K.V len = size & ~PAGE_CACHE_MASK; 48072e9ee850SAneesh Kumar K.V else 48082e9ee850SAneesh Kumar K.V len = PAGE_CACHE_SIZE; 4809a827eaffSAneesh Kumar K.V /* 48109ea7df53SJan Kara * Return if we have all the buffers mapped. This avoids the need to do 48119ea7df53SJan Kara * journal_start/journal_stop which can block and take a long time 4812a827eaffSAneesh Kumar K.V */ 48132e9ee850SAneesh Kumar K.V if (page_has_buffers(page)) { 4814f19d5870STao Ma if (!ext4_walk_page_buffers(NULL, page_buffers(page), 4815f19d5870STao Ma 0, len, NULL, 4816a827eaffSAneesh Kumar K.V ext4_bh_unmapped)) { 48179ea7df53SJan Kara /* Wait so that we don't change page under IO */ 48189ea7df53SJan Kara wait_on_page_writeback(page); 48199ea7df53SJan Kara ret = VM_FAULT_LOCKED; 48209ea7df53SJan Kara goto out; 48212e9ee850SAneesh Kumar K.V } 4822a827eaffSAneesh Kumar K.V } 4823a827eaffSAneesh Kumar K.V unlock_page(page); 48249ea7df53SJan Kara /* OK, we need to fill the hole... */ 48259ea7df53SJan Kara if (ext4_should_dioread_nolock(inode)) 48269ea7df53SJan Kara get_block = ext4_get_block_write; 48279ea7df53SJan Kara else 48289ea7df53SJan Kara get_block = ext4_get_block; 48299ea7df53SJan Kara retry_alloc: 48309924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 48319924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 48329ea7df53SJan Kara if (IS_ERR(handle)) { 4833c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 48349ea7df53SJan Kara goto out; 48359ea7df53SJan Kara } 48369ea7df53SJan Kara ret = __block_page_mkwrite(vma, vmf, get_block); 48379ea7df53SJan Kara if (!ret && ext4_should_journal_data(inode)) { 4838f19d5870STao Ma if (ext4_walk_page_buffers(handle, page_buffers(page), 0, 48399ea7df53SJan Kara PAGE_CACHE_SIZE, NULL, do_journal_get_write_access)) { 48409ea7df53SJan Kara unlock_page(page); 48419ea7df53SJan Kara ret = VM_FAULT_SIGBUS; 4842fcbb5515SYongqiang Yang ext4_journal_stop(handle); 48439ea7df53SJan Kara goto out; 48449ea7df53SJan Kara } 48459ea7df53SJan Kara ext4_set_inode_state(inode, EXT4_STATE_JDATA); 48469ea7df53SJan Kara } 48479ea7df53SJan Kara ext4_journal_stop(handle); 48489ea7df53SJan Kara if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 48499ea7df53SJan Kara goto retry_alloc; 48509ea7df53SJan Kara out_ret: 48519ea7df53SJan Kara ret = block_page_mkwrite_return(ret); 48529ea7df53SJan Kara out: 48538e8ad8a5SJan Kara sb_end_pagefault(inode->i_sb); 48542e9ee850SAneesh Kumar K.V return ret; 48552e9ee850SAneesh Kumar K.V } 4856