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 } 529*f7fec032SZheng Liu if (retval > 0) { 530*f7fec032SZheng Liu int ret; 531*f7fec032SZheng Liu unsigned long long status; 532*f7fec032SZheng Liu 533*f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 534*f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 535*f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 536*f7fec032SZheng Liu ext4_find_delalloc_range(inode, map->m_lblk, 537*f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 538*f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 539*f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, 540*f7fec032SZheng Liu map->m_len, map->m_pblk, status); 541*f7fec032SZheng Liu if (ret < 0) 542*f7fec032SZheng Liu retval = ret; 543*f7fec032SZheng Liu } 544729f52c6SZheng Liu if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 5454df3d265SAneesh Kumar K.V up_read((&EXT4_I(inode)->i_data_sem)); 546f5ab0d1fSMingming Cao 547e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 548*f7fec032SZheng Liu int ret = check_block_validity(inode, map); 5496fd058f7STheodore Ts'o if (ret != 0) 5506fd058f7STheodore Ts'o return ret; 5516fd058f7STheodore Ts'o } 5526fd058f7STheodore Ts'o 553f5ab0d1fSMingming Cao /* If it is only a block(s) look up */ 554c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 5554df3d265SAneesh Kumar K.V return retval; 5564df3d265SAneesh Kumar K.V 5574df3d265SAneesh Kumar K.V /* 558f5ab0d1fSMingming Cao * Returns if the blocks have already allocated 559f5ab0d1fSMingming Cao * 560f5ab0d1fSMingming Cao * Note that if blocks have been preallocated 561df3ab170STao Ma * ext4_ext_get_block() returns the create = 0 562f5ab0d1fSMingming Cao * with buffer head unmapped. 563f5ab0d1fSMingming Cao */ 564e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) 565f5ab0d1fSMingming Cao return retval; 566f5ab0d1fSMingming Cao 567f5ab0d1fSMingming Cao /* 568a25a4e1aSZheng Liu * Here we clear m_flags because after allocating an new extent, 569a25a4e1aSZheng Liu * it will be set again. 5702a8964d6SAneesh Kumar K.V */ 571a25a4e1aSZheng Liu map->m_flags &= ~EXT4_MAP_FLAGS; 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 } 617*f7fec032SZheng Liu if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 618f2321097STheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED); 619d2a17637SMingming Cao 620*f7fec032SZheng Liu if (retval > 0) { 62151865fdaSZheng Liu int ret; 622*f7fec032SZheng Liu unsigned long long status; 623*f7fec032SZheng Liu 624*f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 625*f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 626*f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 627*f7fec032SZheng Liu ext4_find_delalloc_range(inode, map->m_lblk, 628*f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 629*f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 630*f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 631*f7fec032SZheng Liu map->m_pblk, status); 63251865fdaSZheng Liu if (ret < 0) 63351865fdaSZheng Liu retval = ret; 63451865fdaSZheng Liu } 6355356f261SAditya Kali 6360e855ac8SAneesh Kumar K.V up_write((&EXT4_I(inode)->i_data_sem)); 637e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 638e29136f8STheodore Ts'o int ret = check_block_validity(inode, map); 6396fd058f7STheodore Ts'o if (ret != 0) 6406fd058f7STheodore Ts'o return ret; 6416fd058f7STheodore Ts'o } 6420e855ac8SAneesh Kumar K.V return retval; 6430e855ac8SAneesh Kumar K.V } 6440e855ac8SAneesh Kumar K.V 645f3bd1f3fSMingming Cao /* Maximum number of blocks we map for direct IO at once. */ 646f3bd1f3fSMingming Cao #define DIO_MAX_BLOCKS 4096 647f3bd1f3fSMingming Cao 6482ed88685STheodore Ts'o static int _ext4_get_block(struct inode *inode, sector_t iblock, 6492ed88685STheodore Ts'o struct buffer_head *bh, int flags) 650ac27a0ecSDave Kleikamp { 6513e4fdaf8SDmitriy Monakhov handle_t *handle = ext4_journal_current_handle(); 6522ed88685STheodore Ts'o struct ext4_map_blocks map; 6537fb5409dSJan Kara int ret = 0, started = 0; 654f3bd1f3fSMingming Cao int dio_credits; 655ac27a0ecSDave Kleikamp 65646c7f254STao Ma if (ext4_has_inline_data(inode)) 65746c7f254STao Ma return -ERANGE; 65846c7f254STao Ma 6592ed88685STheodore Ts'o map.m_lblk = iblock; 6602ed88685STheodore Ts'o map.m_len = bh->b_size >> inode->i_blkbits; 6612ed88685STheodore Ts'o 6628b0f165fSAnatol Pomozov if (flags && !(flags & EXT4_GET_BLOCKS_NO_LOCK) && !handle) { 6637fb5409dSJan Kara /* Direct IO write... */ 6642ed88685STheodore Ts'o if (map.m_len > DIO_MAX_BLOCKS) 6652ed88685STheodore Ts'o map.m_len = DIO_MAX_BLOCKS; 6662ed88685STheodore Ts'o dio_credits = ext4_chunk_trans_blocks(inode, map.m_len); 6679924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, 6689924a92aSTheodore Ts'o dio_credits); 6697fb5409dSJan Kara if (IS_ERR(handle)) { 670ac27a0ecSDave Kleikamp ret = PTR_ERR(handle); 6712ed88685STheodore Ts'o return ret; 6727fb5409dSJan Kara } 6737fb5409dSJan Kara started = 1; 674ac27a0ecSDave Kleikamp } 675ac27a0ecSDave Kleikamp 6762ed88685STheodore Ts'o ret = ext4_map_blocks(handle, inode, &map, flags); 677ac27a0ecSDave Kleikamp if (ret > 0) { 6782ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 6792ed88685STheodore Ts'o bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; 6802ed88685STheodore Ts'o bh->b_size = inode->i_sb->s_blocksize * map.m_len; 681ac27a0ecSDave Kleikamp ret = 0; 682ac27a0ecSDave Kleikamp } 6837fb5409dSJan Kara if (started) 6847fb5409dSJan Kara ext4_journal_stop(handle); 685ac27a0ecSDave Kleikamp return ret; 686ac27a0ecSDave Kleikamp } 687ac27a0ecSDave Kleikamp 6882ed88685STheodore Ts'o int ext4_get_block(struct inode *inode, sector_t iblock, 6892ed88685STheodore Ts'o struct buffer_head *bh, int create) 6902ed88685STheodore Ts'o { 6912ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh, 6922ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 6932ed88685STheodore Ts'o } 6942ed88685STheodore Ts'o 695ac27a0ecSDave Kleikamp /* 696ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 697ac27a0ecSDave Kleikamp */ 698617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 699725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *errp) 700ac27a0ecSDave Kleikamp { 7012ed88685STheodore Ts'o struct ext4_map_blocks map; 7022ed88685STheodore Ts'o struct buffer_head *bh; 703ac27a0ecSDave Kleikamp int fatal = 0, err; 704ac27a0ecSDave Kleikamp 705ac27a0ecSDave Kleikamp J_ASSERT(handle != NULL || create == 0); 706ac27a0ecSDave Kleikamp 7072ed88685STheodore Ts'o map.m_lblk = block; 7082ed88685STheodore Ts'o map.m_len = 1; 7092ed88685STheodore Ts'o err = ext4_map_blocks(handle, inode, &map, 7102ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 7112ed88685STheodore Ts'o 71290b0a973SCarlos Maiolino /* ensure we send some value back into *errp */ 71390b0a973SCarlos Maiolino *errp = 0; 71490b0a973SCarlos Maiolino 7150f70b406STheodore Ts'o if (create && err == 0) 7160f70b406STheodore Ts'o err = -ENOSPC; /* should never happen */ 7172ed88685STheodore Ts'o if (err < 0) 718ac27a0ecSDave Kleikamp *errp = err; 7192ed88685STheodore Ts'o if (err <= 0) 7202ed88685STheodore Ts'o return NULL; 7212ed88685STheodore Ts'o 7222ed88685STheodore Ts'o bh = sb_getblk(inode->i_sb, map.m_pblk); 723aebf0243SWang Shilong if (unlikely(!bh)) { 724860d21e2STheodore Ts'o *errp = -ENOMEM; 7252ed88685STheodore Ts'o return NULL; 726ac27a0ecSDave Kleikamp } 7272ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 728ac27a0ecSDave Kleikamp J_ASSERT(create != 0); 729ac39849dSAneesh Kumar K.V J_ASSERT(handle != NULL); 730ac27a0ecSDave Kleikamp 731ac27a0ecSDave Kleikamp /* 732ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 733ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 734ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 735617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 736ac27a0ecSDave Kleikamp * problem. 737ac27a0ecSDave Kleikamp */ 738ac27a0ecSDave Kleikamp lock_buffer(bh); 739ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 740617ba13bSMingming Cao fatal = ext4_journal_get_create_access(handle, bh); 741ac27a0ecSDave Kleikamp if (!fatal && !buffer_uptodate(bh)) { 742ac27a0ecSDave Kleikamp memset(bh->b_data, 0, inode->i_sb->s_blocksize); 743ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 744ac27a0ecSDave Kleikamp } 745ac27a0ecSDave Kleikamp unlock_buffer(bh); 7460390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 7470390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 748ac27a0ecSDave Kleikamp if (!fatal) 749ac27a0ecSDave Kleikamp fatal = err; 750ac27a0ecSDave Kleikamp } else { 751ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "not a new buffer"); 752ac27a0ecSDave Kleikamp } 753ac27a0ecSDave Kleikamp if (fatal) { 754ac27a0ecSDave Kleikamp *errp = fatal; 755ac27a0ecSDave Kleikamp brelse(bh); 756ac27a0ecSDave Kleikamp bh = NULL; 757ac27a0ecSDave Kleikamp } 758ac27a0ecSDave Kleikamp return bh; 759ac27a0ecSDave Kleikamp } 760ac27a0ecSDave Kleikamp 761617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 762725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *err) 763ac27a0ecSDave Kleikamp { 764ac27a0ecSDave Kleikamp struct buffer_head *bh; 765ac27a0ecSDave Kleikamp 766617ba13bSMingming Cao bh = ext4_getblk(handle, inode, block, create, err); 767ac27a0ecSDave Kleikamp if (!bh) 768ac27a0ecSDave Kleikamp return bh; 769ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 770ac27a0ecSDave Kleikamp return bh; 77165299a3bSChristoph Hellwig ll_rw_block(READ | REQ_META | REQ_PRIO, 1, &bh); 772ac27a0ecSDave Kleikamp wait_on_buffer(bh); 773ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 774ac27a0ecSDave Kleikamp return bh; 775ac27a0ecSDave Kleikamp put_bh(bh); 776ac27a0ecSDave Kleikamp *err = -EIO; 777ac27a0ecSDave Kleikamp return NULL; 778ac27a0ecSDave Kleikamp } 779ac27a0ecSDave Kleikamp 780f19d5870STao Ma int ext4_walk_page_buffers(handle_t *handle, 781ac27a0ecSDave Kleikamp struct buffer_head *head, 782ac27a0ecSDave Kleikamp unsigned from, 783ac27a0ecSDave Kleikamp unsigned to, 784ac27a0ecSDave Kleikamp int *partial, 785ac27a0ecSDave Kleikamp int (*fn)(handle_t *handle, 786ac27a0ecSDave Kleikamp struct buffer_head *bh)) 787ac27a0ecSDave Kleikamp { 788ac27a0ecSDave Kleikamp struct buffer_head *bh; 789ac27a0ecSDave Kleikamp unsigned block_start, block_end; 790ac27a0ecSDave Kleikamp unsigned blocksize = head->b_size; 791ac27a0ecSDave Kleikamp int err, ret = 0; 792ac27a0ecSDave Kleikamp struct buffer_head *next; 793ac27a0ecSDave Kleikamp 794ac27a0ecSDave Kleikamp for (bh = head, block_start = 0; 795ac27a0ecSDave Kleikamp ret == 0 && (bh != head || !block_start); 796de9a55b8STheodore Ts'o block_start = block_end, bh = next) { 797ac27a0ecSDave Kleikamp next = bh->b_this_page; 798ac27a0ecSDave Kleikamp block_end = block_start + blocksize; 799ac27a0ecSDave Kleikamp if (block_end <= from || block_start >= to) { 800ac27a0ecSDave Kleikamp if (partial && !buffer_uptodate(bh)) 801ac27a0ecSDave Kleikamp *partial = 1; 802ac27a0ecSDave Kleikamp continue; 803ac27a0ecSDave Kleikamp } 804ac27a0ecSDave Kleikamp err = (*fn)(handle, bh); 805ac27a0ecSDave Kleikamp if (!ret) 806ac27a0ecSDave Kleikamp ret = err; 807ac27a0ecSDave Kleikamp } 808ac27a0ecSDave Kleikamp return ret; 809ac27a0ecSDave Kleikamp } 810ac27a0ecSDave Kleikamp 811ac27a0ecSDave Kleikamp /* 812ac27a0ecSDave Kleikamp * To preserve ordering, it is essential that the hole instantiation and 813ac27a0ecSDave Kleikamp * the data write be encapsulated in a single transaction. We cannot 814617ba13bSMingming Cao * close off a transaction and start a new one between the ext4_get_block() 815dab291afSMingming Cao * and the commit_write(). So doing the jbd2_journal_start at the start of 816ac27a0ecSDave Kleikamp * prepare_write() is the right place. 817ac27a0ecSDave Kleikamp * 81836ade451SJan Kara * Also, this function can nest inside ext4_writepage(). In that case, we 81936ade451SJan Kara * *know* that ext4_writepage() has generated enough buffer credits to do the 82036ade451SJan Kara * whole page. So we won't block on the journal in that case, which is good, 82136ade451SJan Kara * because the caller may be PF_MEMALLOC. 822ac27a0ecSDave Kleikamp * 823617ba13bSMingming Cao * By accident, ext4 can be reentered when a transaction is open via 824ac27a0ecSDave Kleikamp * quota file writes. If we were to commit the transaction while thus 825ac27a0ecSDave Kleikamp * reentered, there can be a deadlock - we would be holding a quota 826ac27a0ecSDave Kleikamp * lock, and the commit would never complete if another thread had a 827ac27a0ecSDave Kleikamp * transaction open and was blocking on the quota lock - a ranking 828ac27a0ecSDave Kleikamp * violation. 829ac27a0ecSDave Kleikamp * 830dab291afSMingming Cao * So what we do is to rely on the fact that jbd2_journal_stop/journal_start 831ac27a0ecSDave Kleikamp * will _not_ run commit under these circumstances because handle->h_ref 832ac27a0ecSDave Kleikamp * is elevated. We'll still have enough credits for the tiny quotafile 833ac27a0ecSDave Kleikamp * write. 834ac27a0ecSDave Kleikamp */ 835f19d5870STao Ma int do_journal_get_write_access(handle_t *handle, 836ac27a0ecSDave Kleikamp struct buffer_head *bh) 837ac27a0ecSDave Kleikamp { 83856d35a4cSJan Kara int dirty = buffer_dirty(bh); 83956d35a4cSJan Kara int ret; 84056d35a4cSJan Kara 841ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 842ac27a0ecSDave Kleikamp return 0; 84356d35a4cSJan Kara /* 844ebdec241SChristoph Hellwig * __block_write_begin() could have dirtied some buffers. Clean 84556d35a4cSJan Kara * the dirty bit as jbd2_journal_get_write_access() could complain 84656d35a4cSJan Kara * otherwise about fs integrity issues. Setting of the dirty bit 847ebdec241SChristoph Hellwig * by __block_write_begin() isn't a real problem here as we clear 84856d35a4cSJan Kara * the bit before releasing a page lock and thus writeback cannot 84956d35a4cSJan Kara * ever write the buffer. 85056d35a4cSJan Kara */ 85156d35a4cSJan Kara if (dirty) 85256d35a4cSJan Kara clear_buffer_dirty(bh); 85356d35a4cSJan Kara ret = ext4_journal_get_write_access(handle, bh); 85456d35a4cSJan Kara if (!ret && dirty) 85556d35a4cSJan Kara ret = ext4_handle_dirty_metadata(handle, NULL, bh); 85656d35a4cSJan Kara return ret; 857ac27a0ecSDave Kleikamp } 858ac27a0ecSDave Kleikamp 8598b0f165fSAnatol Pomozov static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock, 8608b0f165fSAnatol Pomozov struct buffer_head *bh_result, int create); 861bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 862bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned flags, 863bfc1af65SNick Piggin struct page **pagep, void **fsdata) 864ac27a0ecSDave Kleikamp { 865bfc1af65SNick Piggin struct inode *inode = mapping->host; 8661938a150SAneesh Kumar K.V int ret, needed_blocks; 867ac27a0ecSDave Kleikamp handle_t *handle; 868ac27a0ecSDave Kleikamp int retries = 0; 869bfc1af65SNick Piggin struct page *page; 870bfc1af65SNick Piggin pgoff_t index; 871bfc1af65SNick Piggin unsigned from, to; 872bfc1af65SNick Piggin 8739bffad1eSTheodore Ts'o trace_ext4_write_begin(inode, pos, len, flags); 8741938a150SAneesh Kumar K.V /* 8751938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 8761938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 8771938a150SAneesh Kumar K.V */ 8781938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 879bfc1af65SNick Piggin index = pos >> PAGE_CACHE_SHIFT; 880bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 881bfc1af65SNick Piggin to = from + len; 882ac27a0ecSDave Kleikamp 883f19d5870STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 884f19d5870STao Ma ret = ext4_try_to_write_inline_data(mapping, inode, pos, len, 885f19d5870STao Ma flags, pagep); 886f19d5870STao Ma if (ret < 0) 88747564bfbSTheodore Ts'o return ret; 88847564bfbSTheodore Ts'o if (ret == 1) 88947564bfbSTheodore Ts'o return 0; 890f19d5870STao Ma } 891f19d5870STao Ma 89247564bfbSTheodore Ts'o /* 89347564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 89447564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 89547564bfbSTheodore Ts'o * is being written back. So grab it first before we start 89647564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 89747564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 89847564bfbSTheodore Ts'o */ 89947564bfbSTheodore Ts'o retry_grab: 90047564bfbSTheodore Ts'o page = grab_cache_page_write_begin(mapping, index, flags); 90147564bfbSTheodore Ts'o if (!page) 90247564bfbSTheodore Ts'o return -ENOMEM; 90347564bfbSTheodore Ts'o unlock_page(page); 90447564bfbSTheodore Ts'o 90547564bfbSTheodore Ts'o retry_journal: 9069924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks); 9077479d2b9SAndrew Morton if (IS_ERR(handle)) { 90847564bfbSTheodore Ts'o page_cache_release(page); 90947564bfbSTheodore Ts'o return PTR_ERR(handle); 9107479d2b9SAndrew Morton } 911ac27a0ecSDave Kleikamp 91247564bfbSTheodore Ts'o lock_page(page); 91347564bfbSTheodore Ts'o if (page->mapping != mapping) { 91447564bfbSTheodore Ts'o /* The page got truncated from under us */ 91547564bfbSTheodore Ts'o unlock_page(page); 91647564bfbSTheodore Ts'o page_cache_release(page); 917cf108bcaSJan Kara ext4_journal_stop(handle); 91847564bfbSTheodore Ts'o goto retry_grab; 919cf108bcaSJan Kara } 92047564bfbSTheodore Ts'o wait_on_page_writeback(page); 921cf108bcaSJan Kara 922744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 9236e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block_write); 924744692dcSJiaying Zhang else 9256e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block); 926bfc1af65SNick Piggin 927bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 928f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_buffers(page), 929f19d5870STao Ma from, to, NULL, 930f19d5870STao Ma do_journal_get_write_access); 931b46be050SAndrey Savochkin } 932bfc1af65SNick Piggin 933bfc1af65SNick Piggin if (ret) { 934bfc1af65SNick Piggin unlock_page(page); 935ae4d5372SAneesh Kumar K.V /* 9366e1db88dSChristoph Hellwig * __block_write_begin may have instantiated a few blocks 937ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 938ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 9391938a150SAneesh Kumar K.V * 9401938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 9411938a150SAneesh Kumar K.V * truncate finishes 942ae4d5372SAneesh Kumar K.V */ 943ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 9441938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 9451938a150SAneesh Kumar K.V 9461938a150SAneesh Kumar K.V ext4_journal_stop(handle); 9471938a150SAneesh Kumar K.V if (pos + len > inode->i_size) { 948b9a4207dSJan Kara ext4_truncate_failed_write(inode); 9491938a150SAneesh Kumar K.V /* 950ffacfa7aSJan Kara * If truncate failed early the inode might 9511938a150SAneesh Kumar K.V * still be on the orphan list; we need to 9521938a150SAneesh Kumar K.V * make sure the inode is removed from the 9531938a150SAneesh Kumar K.V * orphan list in that case. 9541938a150SAneesh Kumar K.V */ 9551938a150SAneesh Kumar K.V if (inode->i_nlink) 9561938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 9571938a150SAneesh Kumar K.V } 958bfc1af65SNick Piggin 95947564bfbSTheodore Ts'o if (ret == -ENOSPC && 96047564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 96147564bfbSTheodore Ts'o goto retry_journal; 96247564bfbSTheodore Ts'o page_cache_release(page); 96347564bfbSTheodore Ts'o return ret; 96447564bfbSTheodore Ts'o } 96547564bfbSTheodore Ts'o *pagep = page; 966ac27a0ecSDave Kleikamp return ret; 967ac27a0ecSDave Kleikamp } 968ac27a0ecSDave Kleikamp 969bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 970bfc1af65SNick Piggin static int write_end_fn(handle_t *handle, struct buffer_head *bh) 971ac27a0ecSDave Kleikamp { 972ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 973ac27a0ecSDave Kleikamp return 0; 974ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 9750390131bSFrank Mayhar return ext4_handle_dirty_metadata(handle, NULL, bh); 976ac27a0ecSDave Kleikamp } 977ac27a0ecSDave Kleikamp 978f8514083SAneesh Kumar K.V static int ext4_generic_write_end(struct file *file, 979f8514083SAneesh Kumar K.V struct address_space *mapping, 980f8514083SAneesh Kumar K.V loff_t pos, unsigned len, unsigned copied, 981f8514083SAneesh Kumar K.V struct page *page, void *fsdata) 982f8514083SAneesh Kumar K.V { 983f8514083SAneesh Kumar K.V int i_size_changed = 0; 984f8514083SAneesh Kumar K.V struct inode *inode = mapping->host; 985f8514083SAneesh Kumar K.V handle_t *handle = ext4_journal_current_handle(); 986f8514083SAneesh Kumar K.V 987f19d5870STao Ma if (ext4_has_inline_data(inode)) 988f19d5870STao Ma copied = ext4_write_inline_data_end(inode, pos, len, 989f19d5870STao Ma copied, page); 990f19d5870STao Ma else 991f19d5870STao Ma copied = block_write_end(file, mapping, pos, 992f19d5870STao Ma len, copied, page, fsdata); 993f8514083SAneesh Kumar K.V 994f8514083SAneesh Kumar K.V /* 995f8514083SAneesh Kumar K.V * No need to use i_size_read() here, the i_size 996f8514083SAneesh Kumar K.V * cannot change under us because we hold i_mutex. 997f8514083SAneesh Kumar K.V * 998f8514083SAneesh Kumar K.V * But it's important to update i_size while still holding page lock: 999f8514083SAneesh Kumar K.V * page writeout could otherwise come in and zero beyond i_size. 1000f8514083SAneesh Kumar K.V */ 1001f8514083SAneesh Kumar K.V if (pos + copied > inode->i_size) { 1002f8514083SAneesh Kumar K.V i_size_write(inode, pos + copied); 1003f8514083SAneesh Kumar K.V i_size_changed = 1; 1004f8514083SAneesh Kumar K.V } 1005f8514083SAneesh Kumar K.V 1006f8514083SAneesh Kumar K.V if (pos + copied > EXT4_I(inode)->i_disksize) { 1007f8514083SAneesh Kumar K.V /* We need to mark inode dirty even if 1008f8514083SAneesh Kumar K.V * new_i_size is less that inode->i_size 1009f8514083SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 1010f8514083SAneesh Kumar K.V */ 1011f8514083SAneesh Kumar K.V ext4_update_i_disksize(inode, (pos + copied)); 1012f8514083SAneesh Kumar K.V i_size_changed = 1; 1013f8514083SAneesh Kumar K.V } 1014f8514083SAneesh Kumar K.V unlock_page(page); 1015f8514083SAneesh Kumar K.V page_cache_release(page); 1016f8514083SAneesh Kumar K.V 1017f8514083SAneesh Kumar K.V /* 1018f8514083SAneesh Kumar K.V * Don't mark the inode dirty under page lock. First, it unnecessarily 1019f8514083SAneesh Kumar K.V * makes the holding time of page lock longer. Second, it forces lock 1020f8514083SAneesh Kumar K.V * ordering of page lock and transaction start for journaling 1021f8514083SAneesh Kumar K.V * filesystems. 1022f8514083SAneesh Kumar K.V */ 1023f8514083SAneesh Kumar K.V if (i_size_changed) 1024f8514083SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 1025f8514083SAneesh Kumar K.V 1026f8514083SAneesh Kumar K.V return copied; 1027f8514083SAneesh Kumar K.V } 1028f8514083SAneesh Kumar K.V 1029ac27a0ecSDave Kleikamp /* 1030ac27a0ecSDave Kleikamp * We need to pick up the new inode size which generic_commit_write gave us 1031ac27a0ecSDave Kleikamp * `file' can be NULL - eg, when called from page_symlink(). 1032ac27a0ecSDave Kleikamp * 1033617ba13bSMingming Cao * ext4 never places buffers on inode->i_mapping->private_list. metadata 1034ac27a0ecSDave Kleikamp * buffers are managed internally. 1035ac27a0ecSDave Kleikamp */ 1036bfc1af65SNick Piggin static int ext4_ordered_write_end(struct file *file, 1037bfc1af65SNick Piggin struct address_space *mapping, 1038bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1039bfc1af65SNick Piggin struct page *page, void *fsdata) 1040ac27a0ecSDave Kleikamp { 1041617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1042cf108bcaSJan Kara struct inode *inode = mapping->host; 1043ac27a0ecSDave Kleikamp int ret = 0, ret2; 1044ac27a0ecSDave Kleikamp 10459bffad1eSTheodore Ts'o trace_ext4_ordered_write_end(inode, pos, len, copied); 1046678aaf48SJan Kara ret = ext4_jbd2_file_inode(handle, inode); 1047ac27a0ecSDave Kleikamp 1048ac27a0ecSDave Kleikamp if (ret == 0) { 1049f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 1050bfc1af65SNick Piggin page, fsdata); 1051f8a87d89SRoel Kluin copied = ret2; 1052ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1053f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1054f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1055f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1056f8514083SAneesh Kumar K.V */ 1057f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1058f8a87d89SRoel Kluin if (ret2 < 0) 1059f8a87d89SRoel Kluin ret = ret2; 106009e0834fSAkira Fujita } else { 106109e0834fSAkira Fujita unlock_page(page); 106209e0834fSAkira Fujita page_cache_release(page); 1063ac27a0ecSDave Kleikamp } 106409e0834fSAkira Fujita 1065617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1066ac27a0ecSDave Kleikamp if (!ret) 1067ac27a0ecSDave Kleikamp ret = ret2; 1068bfc1af65SNick Piggin 1069f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1070b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1071f8514083SAneesh Kumar K.V /* 1072ffacfa7aSJan Kara * If truncate failed early the inode might still be 1073f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1074f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1075f8514083SAneesh Kumar K.V */ 1076f8514083SAneesh Kumar K.V if (inode->i_nlink) 1077f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1078f8514083SAneesh Kumar K.V } 1079f8514083SAneesh Kumar K.V 1080f8514083SAneesh Kumar K.V 1081bfc1af65SNick Piggin return ret ? ret : copied; 1082ac27a0ecSDave Kleikamp } 1083ac27a0ecSDave Kleikamp 1084bfc1af65SNick Piggin static int ext4_writeback_write_end(struct file *file, 1085bfc1af65SNick Piggin struct address_space *mapping, 1086bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1087bfc1af65SNick Piggin struct page *page, void *fsdata) 1088ac27a0ecSDave Kleikamp { 1089617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1090cf108bcaSJan Kara struct inode *inode = mapping->host; 1091ac27a0ecSDave Kleikamp int ret = 0, ret2; 1092ac27a0ecSDave Kleikamp 10939bffad1eSTheodore Ts'o trace_ext4_writeback_write_end(inode, pos, len, copied); 1094f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 1095bfc1af65SNick Piggin page, fsdata); 1096f8a87d89SRoel Kluin copied = ret2; 1097ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1098f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1099f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1100f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1101f8514083SAneesh Kumar K.V */ 1102f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1103f8514083SAneesh Kumar K.V 1104f8a87d89SRoel Kluin if (ret2 < 0) 1105f8a87d89SRoel Kluin ret = ret2; 1106ac27a0ecSDave Kleikamp 1107617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1108ac27a0ecSDave Kleikamp if (!ret) 1109ac27a0ecSDave Kleikamp ret = ret2; 1110bfc1af65SNick Piggin 1111f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1112b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1113f8514083SAneesh Kumar K.V /* 1114ffacfa7aSJan Kara * If truncate failed early the inode might still be 1115f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1116f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1117f8514083SAneesh Kumar K.V */ 1118f8514083SAneesh Kumar K.V if (inode->i_nlink) 1119f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1120f8514083SAneesh Kumar K.V } 1121f8514083SAneesh Kumar K.V 1122bfc1af65SNick Piggin return ret ? ret : copied; 1123ac27a0ecSDave Kleikamp } 1124ac27a0ecSDave Kleikamp 1125bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 1126bfc1af65SNick Piggin struct address_space *mapping, 1127bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1128bfc1af65SNick Piggin struct page *page, void *fsdata) 1129ac27a0ecSDave Kleikamp { 1130617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1131bfc1af65SNick Piggin struct inode *inode = mapping->host; 1132ac27a0ecSDave Kleikamp int ret = 0, ret2; 1133ac27a0ecSDave Kleikamp int partial = 0; 1134bfc1af65SNick Piggin unsigned from, to; 1135cf17fea6SAneesh Kumar K.V loff_t new_i_size; 1136ac27a0ecSDave Kleikamp 11379bffad1eSTheodore Ts'o trace_ext4_journalled_write_end(inode, pos, len, copied); 1138bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 1139bfc1af65SNick Piggin to = from + len; 1140bfc1af65SNick Piggin 1141441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1142441c8508SCurt Wohlgemuth 11433fdcfb66STao Ma if (ext4_has_inline_data(inode)) 11443fdcfb66STao Ma copied = ext4_write_inline_data_end(inode, pos, len, 11453fdcfb66STao Ma copied, page); 11463fdcfb66STao Ma else { 1147bfc1af65SNick Piggin if (copied < len) { 1148bfc1af65SNick Piggin if (!PageUptodate(page)) 1149bfc1af65SNick Piggin copied = 0; 1150bfc1af65SNick Piggin page_zero_new_buffers(page, from+copied, to); 1151bfc1af65SNick Piggin } 1152ac27a0ecSDave Kleikamp 1153f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_buffers(page), from, 1154bfc1af65SNick Piggin to, &partial, write_end_fn); 1155ac27a0ecSDave Kleikamp if (!partial) 1156ac27a0ecSDave Kleikamp SetPageUptodate(page); 11573fdcfb66STao Ma } 1158cf17fea6SAneesh Kumar K.V new_i_size = pos + copied; 1159cf17fea6SAneesh Kumar K.V if (new_i_size > inode->i_size) 1160bfc1af65SNick Piggin i_size_write(inode, pos+copied); 116119f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 11622d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 1163cf17fea6SAneesh Kumar K.V if (new_i_size > EXT4_I(inode)->i_disksize) { 1164cf17fea6SAneesh Kumar K.V ext4_update_i_disksize(inode, new_i_size); 1165617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1166ac27a0ecSDave Kleikamp if (!ret) 1167ac27a0ecSDave Kleikamp ret = ret2; 1168ac27a0ecSDave Kleikamp } 1169bfc1af65SNick Piggin 1170cf108bcaSJan Kara unlock_page(page); 1171f8514083SAneesh Kumar K.V page_cache_release(page); 1172ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1173f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1174f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1175f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1176f8514083SAneesh Kumar K.V */ 1177f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1178f8514083SAneesh Kumar K.V 1179617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1180ac27a0ecSDave Kleikamp if (!ret) 1181ac27a0ecSDave Kleikamp ret = ret2; 1182f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1183b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1184f8514083SAneesh Kumar K.V /* 1185ffacfa7aSJan Kara * If truncate failed early the inode might still be 1186f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1187f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1188f8514083SAneesh Kumar K.V */ 1189f8514083SAneesh Kumar K.V if (inode->i_nlink) 1190f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1191f8514083SAneesh Kumar K.V } 1192bfc1af65SNick Piggin 1193bfc1af65SNick Piggin return ret ? ret : copied; 1194ac27a0ecSDave Kleikamp } 1195d2a17637SMingming Cao 11969d0be502STheodore Ts'o /* 11977b415bf6SAditya Kali * Reserve a single cluster located at lblock 11989d0be502STheodore Ts'o */ 119901f49d0bSTheodore Ts'o static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock) 1200d2a17637SMingming Cao { 1201030ba6bcSAneesh Kumar K.V int retries = 0; 1202d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 12030637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 12047b415bf6SAditya Kali unsigned int md_needed; 12055dd4056dSChristoph Hellwig int ret; 120603179fe9STheodore Ts'o ext4_lblk_t save_last_lblock; 120703179fe9STheodore Ts'o int save_len; 1208d2a17637SMingming Cao 120960e58e0fSMingming Cao /* 121072b8ab9dSEric Sandeen * We will charge metadata quota at writeout time; this saves 121172b8ab9dSEric Sandeen * us from metadata over-estimation, though we may go over by 121272b8ab9dSEric Sandeen * a small amount in the end. Here we just reserve for data. 121360e58e0fSMingming Cao */ 12147b415bf6SAditya Kali ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1)); 12155dd4056dSChristoph Hellwig if (ret) 12165dd4056dSChristoph Hellwig return ret; 121703179fe9STheodore Ts'o 121803179fe9STheodore Ts'o /* 121903179fe9STheodore Ts'o * recalculate the amount of metadata blocks to reserve 122003179fe9STheodore Ts'o * in order to allocate nrblocks 122103179fe9STheodore Ts'o * worse case is one extent per block 122203179fe9STheodore Ts'o */ 122303179fe9STheodore Ts'o repeat: 122403179fe9STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 122503179fe9STheodore Ts'o /* 122603179fe9STheodore Ts'o * ext4_calc_metadata_amount() has side effects, which we have 122703179fe9STheodore Ts'o * to be prepared undo if we fail to claim space. 122803179fe9STheodore Ts'o */ 122903179fe9STheodore Ts'o save_len = ei->i_da_metadata_calc_len; 123003179fe9STheodore Ts'o save_last_lblock = ei->i_da_metadata_calc_last_lblock; 123103179fe9STheodore Ts'o md_needed = EXT4_NUM_B2C(sbi, 123203179fe9STheodore Ts'o ext4_calc_metadata_amount(inode, lblock)); 123303179fe9STheodore Ts'o trace_ext4_da_reserve_space(inode, md_needed); 123403179fe9STheodore Ts'o 123572b8ab9dSEric Sandeen /* 123672b8ab9dSEric Sandeen * We do still charge estimated metadata to the sb though; 123772b8ab9dSEric Sandeen * we cannot afford to run out of free blocks. 123872b8ab9dSEric Sandeen */ 1239e7d5f315STheodore Ts'o if (ext4_claim_free_clusters(sbi, md_needed + 1, 0)) { 124003179fe9STheodore Ts'o ei->i_da_metadata_calc_len = save_len; 124103179fe9STheodore Ts'o ei->i_da_metadata_calc_last_lblock = save_last_lblock; 124203179fe9STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 1243030ba6bcSAneesh Kumar K.V if (ext4_should_retry_alloc(inode->i_sb, &retries)) { 1244030ba6bcSAneesh Kumar K.V yield(); 1245030ba6bcSAneesh Kumar K.V goto repeat; 1246030ba6bcSAneesh Kumar K.V } 124703179fe9STheodore Ts'o dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1)); 1248d2a17637SMingming Cao return -ENOSPC; 1249d2a17637SMingming Cao } 12509d0be502STheodore Ts'o ei->i_reserved_data_blocks++; 12510637c6f4STheodore Ts'o ei->i_reserved_meta_blocks += md_needed; 12520637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 125339bc680aSDmitry Monakhov 1254d2a17637SMingming Cao return 0; /* success */ 1255d2a17637SMingming Cao } 1256d2a17637SMingming Cao 125712219aeaSAneesh Kumar K.V static void ext4_da_release_space(struct inode *inode, int to_free) 1258d2a17637SMingming Cao { 1259d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 12600637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 1261d2a17637SMingming Cao 1262cd213226SMingming Cao if (!to_free) 1263cd213226SMingming Cao return; /* Nothing to release, exit */ 1264cd213226SMingming Cao 1265d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1266cd213226SMingming Cao 12675a58ec87SLi Zefan trace_ext4_da_release_space(inode, to_free); 12680637c6f4STheodore Ts'o if (unlikely(to_free > ei->i_reserved_data_blocks)) { 1269cd213226SMingming Cao /* 12700637c6f4STheodore Ts'o * if there aren't enough reserved blocks, then the 12710637c6f4STheodore Ts'o * counter is messed up somewhere. Since this 12720637c6f4STheodore Ts'o * function is called from invalidate page, it's 12730637c6f4STheodore Ts'o * harmless to return without any action. 1274cd213226SMingming Cao */ 12758de5c325STheodore Ts'o ext4_warning(inode->i_sb, "ext4_da_release_space: " 12760637c6f4STheodore Ts'o "ino %lu, to_free %d with only %d reserved " 12771084f252STheodore Ts'o "data blocks", inode->i_ino, to_free, 12780637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 12790637c6f4STheodore Ts'o WARN_ON(1); 12800637c6f4STheodore Ts'o to_free = ei->i_reserved_data_blocks; 12810637c6f4STheodore Ts'o } 12820637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= to_free; 12830637c6f4STheodore Ts'o 12840637c6f4STheodore Ts'o if (ei->i_reserved_data_blocks == 0) { 12850637c6f4STheodore Ts'o /* 12860637c6f4STheodore Ts'o * We can release all of the reserved metadata blocks 12870637c6f4STheodore Ts'o * only when we have written all of the delayed 12880637c6f4STheodore Ts'o * allocation blocks. 12897b415bf6SAditya Kali * Note that in case of bigalloc, i_reserved_meta_blocks, 12907b415bf6SAditya Kali * i_reserved_data_blocks, etc. refer to number of clusters. 12910637c6f4STheodore Ts'o */ 129257042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 129372b8ab9dSEric Sandeen ei->i_reserved_meta_blocks); 1294ee5f4d9cSTheodore Ts'o ei->i_reserved_meta_blocks = 0; 12959d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 1296cd213226SMingming Cao } 1297cd213226SMingming Cao 129872b8ab9dSEric Sandeen /* update fs dirty data blocks counter */ 129957042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free); 1300d2a17637SMingming Cao 1301d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 130260e58e0fSMingming Cao 13037b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free)); 1304d2a17637SMingming Cao } 1305d2a17637SMingming Cao 1306d2a17637SMingming Cao static void ext4_da_page_release_reservation(struct page *page, 1307d2a17637SMingming Cao unsigned long offset) 1308d2a17637SMingming Cao { 1309d2a17637SMingming Cao int to_release = 0; 1310d2a17637SMingming Cao struct buffer_head *head, *bh; 1311d2a17637SMingming Cao unsigned int curr_off = 0; 13127b415bf6SAditya Kali struct inode *inode = page->mapping->host; 13137b415bf6SAditya Kali struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 13147b415bf6SAditya Kali int num_clusters; 131551865fdaSZheng Liu ext4_fsblk_t lblk; 1316d2a17637SMingming Cao 1317d2a17637SMingming Cao head = page_buffers(page); 1318d2a17637SMingming Cao bh = head; 1319d2a17637SMingming Cao do { 1320d2a17637SMingming Cao unsigned int next_off = curr_off + bh->b_size; 1321d2a17637SMingming Cao 1322d2a17637SMingming Cao if ((offset <= curr_off) && (buffer_delay(bh))) { 1323d2a17637SMingming Cao to_release++; 1324d2a17637SMingming Cao clear_buffer_delay(bh); 1325d2a17637SMingming Cao } 1326d2a17637SMingming Cao curr_off = next_off; 1327d2a17637SMingming Cao } while ((bh = bh->b_this_page) != head); 13287b415bf6SAditya Kali 132951865fdaSZheng Liu if (to_release) { 133051865fdaSZheng Liu lblk = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits); 133151865fdaSZheng Liu ext4_es_remove_extent(inode, lblk, to_release); 133251865fdaSZheng Liu } 133351865fdaSZheng Liu 13347b415bf6SAditya Kali /* If we have released all the blocks belonging to a cluster, then we 13357b415bf6SAditya Kali * need to release the reserved space for that cluster. */ 13367b415bf6SAditya Kali num_clusters = EXT4_NUM_B2C(sbi, to_release); 13377b415bf6SAditya Kali while (num_clusters > 0) { 13387b415bf6SAditya Kali lblk = (page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits)) + 13397b415bf6SAditya Kali ((num_clusters - 1) << sbi->s_cluster_bits); 13407b415bf6SAditya Kali if (sbi->s_cluster_ratio == 1 || 13417d1b1fbcSZheng Liu !ext4_find_delalloc_cluster(inode, lblk)) 13427b415bf6SAditya Kali ext4_da_release_space(inode, 1); 13437b415bf6SAditya Kali 13447b415bf6SAditya Kali num_clusters--; 13457b415bf6SAditya Kali } 1346d2a17637SMingming Cao } 1347ac27a0ecSDave Kleikamp 1348ac27a0ecSDave Kleikamp /* 134964769240SAlex Tomas * Delayed allocation stuff 135064769240SAlex Tomas */ 135164769240SAlex Tomas 135264769240SAlex Tomas /* 135364769240SAlex Tomas * mpage_da_submit_io - walks through extent of pages and try to write 1354a1d6cc56SAneesh Kumar K.V * them with writepage() call back 135564769240SAlex Tomas * 135664769240SAlex Tomas * @mpd->inode: inode 135764769240SAlex Tomas * @mpd->first_page: first page of the extent 135864769240SAlex Tomas * @mpd->next_page: page after the last page of the extent 135964769240SAlex Tomas * 136064769240SAlex Tomas * By the time mpage_da_submit_io() is called we expect all blocks 136164769240SAlex Tomas * to be allocated. this may be wrong if allocation failed. 136264769240SAlex Tomas * 136364769240SAlex Tomas * As pages are already locked by write_cache_pages(), we can't use it 136464769240SAlex Tomas */ 13651de3e3dfSTheodore Ts'o static int mpage_da_submit_io(struct mpage_da_data *mpd, 13661de3e3dfSTheodore Ts'o struct ext4_map_blocks *map) 136764769240SAlex Tomas { 1368791b7f08SAneesh Kumar K.V struct pagevec pvec; 1369791b7f08SAneesh Kumar K.V unsigned long index, end; 1370791b7f08SAneesh Kumar K.V int ret = 0, err, nr_pages, i; 1371791b7f08SAneesh Kumar K.V struct inode *inode = mpd->inode; 1372791b7f08SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 1373cb20d518STheodore Ts'o loff_t size = i_size_read(inode); 13743ecdb3a1STheodore Ts'o unsigned int len, block_start; 13753ecdb3a1STheodore Ts'o struct buffer_head *bh, *page_bufs = NULL; 13761de3e3dfSTheodore Ts'o sector_t pblock = 0, cur_logical = 0; 1377bd2d0210STheodore Ts'o struct ext4_io_submit io_submit; 137864769240SAlex Tomas 137964769240SAlex Tomas BUG_ON(mpd->next_page <= mpd->first_page); 1380bd2d0210STheodore Ts'o memset(&io_submit, 0, sizeof(io_submit)); 1381791b7f08SAneesh Kumar K.V /* 1382791b7f08SAneesh Kumar K.V * We need to start from the first_page to the next_page - 1 1383791b7f08SAneesh Kumar K.V * to make sure we also write the mapped dirty buffer_heads. 13848dc207c0STheodore Ts'o * If we look at mpd->b_blocknr we would only be looking 1385791b7f08SAneesh Kumar K.V * at the currently mapped buffer_heads. 1386791b7f08SAneesh Kumar K.V */ 138764769240SAlex Tomas index = mpd->first_page; 138864769240SAlex Tomas end = mpd->next_page - 1; 138964769240SAlex Tomas 1390791b7f08SAneesh Kumar K.V pagevec_init(&pvec, 0); 139164769240SAlex Tomas while (index <= end) { 1392791b7f08SAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 139364769240SAlex Tomas if (nr_pages == 0) 139464769240SAlex Tomas break; 139564769240SAlex Tomas for (i = 0; i < nr_pages; i++) { 1396f8bec370SJan Kara int skip_page = 0; 139764769240SAlex Tomas struct page *page = pvec.pages[i]; 139864769240SAlex Tomas 1399791b7f08SAneesh Kumar K.V index = page->index; 1400791b7f08SAneesh Kumar K.V if (index > end) 1401791b7f08SAneesh Kumar K.V break; 1402cb20d518STheodore Ts'o 1403cb20d518STheodore Ts'o if (index == size >> PAGE_CACHE_SHIFT) 1404cb20d518STheodore Ts'o len = size & ~PAGE_CACHE_MASK; 1405cb20d518STheodore Ts'o else 1406cb20d518STheodore Ts'o len = PAGE_CACHE_SIZE; 14071de3e3dfSTheodore Ts'o if (map) { 14081de3e3dfSTheodore Ts'o cur_logical = index << (PAGE_CACHE_SHIFT - 14091de3e3dfSTheodore Ts'o inode->i_blkbits); 14101de3e3dfSTheodore Ts'o pblock = map->m_pblk + (cur_logical - 14111de3e3dfSTheodore Ts'o map->m_lblk); 14121de3e3dfSTheodore Ts'o } 1413791b7f08SAneesh Kumar K.V index++; 1414791b7f08SAneesh Kumar K.V 1415791b7f08SAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1416791b7f08SAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1417791b7f08SAneesh Kumar K.V 14183ecdb3a1STheodore Ts'o bh = page_bufs = page_buffers(page); 14193ecdb3a1STheodore Ts'o block_start = 0; 14203ecdb3a1STheodore Ts'o do { 14211de3e3dfSTheodore Ts'o if (map && (cur_logical >= map->m_lblk) && 14221de3e3dfSTheodore Ts'o (cur_logical <= (map->m_lblk + 14231de3e3dfSTheodore Ts'o (map->m_len - 1)))) { 14241de3e3dfSTheodore Ts'o if (buffer_delay(bh)) { 14251de3e3dfSTheodore Ts'o clear_buffer_delay(bh); 14261de3e3dfSTheodore Ts'o bh->b_blocknr = pblock; 14271de3e3dfSTheodore Ts'o } 14281de3e3dfSTheodore Ts'o if (buffer_unwritten(bh) || 14291de3e3dfSTheodore Ts'o buffer_mapped(bh)) 14301de3e3dfSTheodore Ts'o BUG_ON(bh->b_blocknr != pblock); 14311de3e3dfSTheodore Ts'o if (map->m_flags & EXT4_MAP_UNINIT) 14321de3e3dfSTheodore Ts'o set_buffer_uninit(bh); 14331de3e3dfSTheodore Ts'o clear_buffer_unwritten(bh); 14341de3e3dfSTheodore Ts'o } 14351de3e3dfSTheodore Ts'o 143613a79a47SYongqiang Yang /* 143713a79a47SYongqiang Yang * skip page if block allocation undone and 143813a79a47SYongqiang Yang * block is dirty 143913a79a47SYongqiang Yang */ 144013a79a47SYongqiang Yang if (ext4_bh_delay_or_unwritten(NULL, bh)) 144197498956STheodore Ts'o skip_page = 1; 14423ecdb3a1STheodore Ts'o bh = bh->b_this_page; 14433ecdb3a1STheodore Ts'o block_start += bh->b_size; 14441de3e3dfSTheodore Ts'o cur_logical++; 14451de3e3dfSTheodore Ts'o pblock++; 14461de3e3dfSTheodore Ts'o } while (bh != page_bufs); 14471de3e3dfSTheodore Ts'o 1448f8bec370SJan Kara if (skip_page) { 1449f8bec370SJan Kara unlock_page(page); 1450f8bec370SJan Kara continue; 1451f8bec370SJan Kara } 1452cb20d518STheodore Ts'o 145397498956STheodore Ts'o clear_page_dirty_for_io(page); 1454fe089c77SJan Kara err = ext4_bio_write_page(&io_submit, page, len, 1455fe089c77SJan Kara mpd->wbc); 1456cb20d518STheodore Ts'o if (!err) 1457a1d6cc56SAneesh Kumar K.V mpd->pages_written++; 145864769240SAlex Tomas /* 145964769240SAlex Tomas * In error case, we have to continue because 146064769240SAlex Tomas * remaining pages are still locked 146164769240SAlex Tomas */ 146264769240SAlex Tomas if (ret == 0) 146364769240SAlex Tomas ret = err; 146464769240SAlex Tomas } 146564769240SAlex Tomas pagevec_release(&pvec); 146664769240SAlex Tomas } 1467bd2d0210STheodore Ts'o ext4_io_submit(&io_submit); 146864769240SAlex Tomas return ret; 146964769240SAlex Tomas } 147064769240SAlex Tomas 1471c7f5938aSCurt Wohlgemuth static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd) 1472c4a0c46eSAneesh Kumar K.V { 1473c4a0c46eSAneesh Kumar K.V int nr_pages, i; 1474c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 1475c4a0c46eSAneesh Kumar K.V struct pagevec pvec; 1476c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 1477c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 147851865fdaSZheng Liu ext4_lblk_t start, last; 1479c4a0c46eSAneesh Kumar K.V 1480c7f5938aSCurt Wohlgemuth index = mpd->first_page; 1481c7f5938aSCurt Wohlgemuth end = mpd->next_page - 1; 148251865fdaSZheng Liu 148351865fdaSZheng Liu start = index << (PAGE_CACHE_SHIFT - inode->i_blkbits); 148451865fdaSZheng Liu last = end << (PAGE_CACHE_SHIFT - inode->i_blkbits); 148551865fdaSZheng Liu ext4_es_remove_extent(inode, start, last - start + 1); 148651865fdaSZheng Liu 148766bea92cSEric Sandeen pagevec_init(&pvec, 0); 1488c4a0c46eSAneesh Kumar K.V while (index <= end) { 1489c4a0c46eSAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 1490c4a0c46eSAneesh Kumar K.V if (nr_pages == 0) 1491c4a0c46eSAneesh Kumar K.V break; 1492c4a0c46eSAneesh Kumar K.V for (i = 0; i < nr_pages; i++) { 1493c4a0c46eSAneesh Kumar K.V struct page *page = pvec.pages[i]; 14949b1d0998SJan Kara if (page->index > end) 1495c4a0c46eSAneesh Kumar K.V break; 1496c4a0c46eSAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1497c4a0c46eSAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1498c4a0c46eSAneesh Kumar K.V block_invalidatepage(page, 0); 1499c4a0c46eSAneesh Kumar K.V ClearPageUptodate(page); 1500c4a0c46eSAneesh Kumar K.V unlock_page(page); 1501c4a0c46eSAneesh Kumar K.V } 15029b1d0998SJan Kara index = pvec.pages[nr_pages - 1]->index + 1; 15039b1d0998SJan Kara pagevec_release(&pvec); 1504c4a0c46eSAneesh Kumar K.V } 1505c4a0c46eSAneesh Kumar K.V return; 1506c4a0c46eSAneesh Kumar K.V } 1507c4a0c46eSAneesh Kumar K.V 1508df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 1509df22291fSAneesh Kumar K.V { 1510df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 151192b97816STheodore Ts'o struct super_block *sb = inode->i_sb; 151292b97816STheodore Ts'o 151392b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld", 15145dee5437STheodore Ts'o EXT4_C2B(EXT4_SB(inode->i_sb), 15155dee5437STheodore Ts'o ext4_count_free_clusters(inode->i_sb))); 151692b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Free/Dirty block details"); 151792b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "free_blocks=%lld", 151857042651STheodore Ts'o (long long) EXT4_C2B(EXT4_SB(inode->i_sb), 151957042651STheodore Ts'o percpu_counter_sum(&sbi->s_freeclusters_counter))); 152092b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld", 15217b415bf6SAditya Kali (long long) EXT4_C2B(EXT4_SB(inode->i_sb), 15227b415bf6SAditya Kali percpu_counter_sum(&sbi->s_dirtyclusters_counter))); 152392b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Block reservation details"); 152492b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u", 1525df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_data_blocks); 152692b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_meta_blocks=%u", 1527df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_meta_blocks); 1528df22291fSAneesh Kumar K.V return; 1529df22291fSAneesh Kumar K.V } 1530df22291fSAneesh Kumar K.V 1531b920c755STheodore Ts'o /* 15325a87b7a5STheodore Ts'o * mpage_da_map_and_submit - go through given space, map them 15335a87b7a5STheodore Ts'o * if necessary, and then submit them for I/O 153464769240SAlex Tomas * 15358dc207c0STheodore Ts'o * @mpd - bh describing space 153664769240SAlex Tomas * 153764769240SAlex Tomas * The function skips space we know is already mapped to disk blocks. 153864769240SAlex Tomas * 153964769240SAlex Tomas */ 15405a87b7a5STheodore Ts'o static void mpage_da_map_and_submit(struct mpage_da_data *mpd) 154164769240SAlex Tomas { 15422ac3b6e0STheodore Ts'o int err, blks, get_blocks_flags; 15431de3e3dfSTheodore Ts'o struct ext4_map_blocks map, *mapp = NULL; 15442fa3cdfbSTheodore Ts'o sector_t next = mpd->b_blocknr; 15452fa3cdfbSTheodore Ts'o unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits; 15462fa3cdfbSTheodore Ts'o loff_t disksize = EXT4_I(mpd->inode)->i_disksize; 15472fa3cdfbSTheodore Ts'o handle_t *handle = NULL; 154864769240SAlex Tomas 154964769240SAlex Tomas /* 15505a87b7a5STheodore Ts'o * If the blocks are mapped already, or we couldn't accumulate 15515a87b7a5STheodore Ts'o * any blocks, then proceed immediately to the submission stage. 155264769240SAlex Tomas */ 15535a87b7a5STheodore Ts'o if ((mpd->b_size == 0) || 15545a87b7a5STheodore Ts'o ((mpd->b_state & (1 << BH_Mapped)) && 155529fa89d0SAneesh Kumar K.V !(mpd->b_state & (1 << BH_Delay)) && 15565a87b7a5STheodore Ts'o !(mpd->b_state & (1 << BH_Unwritten)))) 15575a87b7a5STheodore Ts'o goto submit_io; 15582fa3cdfbSTheodore Ts'o 15592fa3cdfbSTheodore Ts'o handle = ext4_journal_current_handle(); 15602fa3cdfbSTheodore Ts'o BUG_ON(!handle); 15612fa3cdfbSTheodore Ts'o 156279ffab34SAneesh Kumar K.V /* 156379e83036SEric Sandeen * Call ext4_map_blocks() to allocate any delayed allocation 15642ac3b6e0STheodore Ts'o * blocks, or to convert an uninitialized extent to be 15652ac3b6e0STheodore Ts'o * initialized (in the case where we have written into 15662ac3b6e0STheodore Ts'o * one or more preallocated blocks). 15672ac3b6e0STheodore Ts'o * 15682ac3b6e0STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to 15692ac3b6e0STheodore Ts'o * indicate that we are on the delayed allocation path. This 15702ac3b6e0STheodore Ts'o * affects functions in many different parts of the allocation 15712ac3b6e0STheodore Ts'o * call path. This flag exists primarily because we don't 157279e83036SEric Sandeen * want to change *many* call functions, so ext4_map_blocks() 1573f2321097STheodore Ts'o * will set the EXT4_STATE_DELALLOC_RESERVED flag once the 15742ac3b6e0STheodore Ts'o * inode's allocation semaphore is taken. 15752ac3b6e0STheodore Ts'o * 15762ac3b6e0STheodore Ts'o * If the blocks in questions were delalloc blocks, set 15772ac3b6e0STheodore Ts'o * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting 15782ac3b6e0STheodore Ts'o * variables are updated after the blocks have been allocated. 157979ffab34SAneesh Kumar K.V */ 15802ed88685STheodore Ts'o map.m_lblk = next; 15812ed88685STheodore Ts'o map.m_len = max_blocks; 15821296cc85SAneesh Kumar K.V get_blocks_flags = EXT4_GET_BLOCKS_CREATE; 1583744692dcSJiaying Zhang if (ext4_should_dioread_nolock(mpd->inode)) 1584744692dcSJiaying Zhang get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; 15852ac3b6e0STheodore Ts'o if (mpd->b_state & (1 << BH_Delay)) 15861296cc85SAneesh Kumar K.V get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; 15871296cc85SAneesh Kumar K.V 15882ed88685STheodore Ts'o blks = ext4_map_blocks(handle, mpd->inode, &map, get_blocks_flags); 15892fa3cdfbSTheodore Ts'o if (blks < 0) { 1590e3570639SEric Sandeen struct super_block *sb = mpd->inode->i_sb; 1591e3570639SEric Sandeen 15922fa3cdfbSTheodore Ts'o err = blks; 1593ed5bde0bSTheodore Ts'o /* 15945a87b7a5STheodore Ts'o * If get block returns EAGAIN or ENOSPC and there 159597498956STheodore Ts'o * appears to be free blocks we will just let 159697498956STheodore Ts'o * mpage_da_submit_io() unlock all of the pages. 1597c4a0c46eSAneesh Kumar K.V */ 1598c4a0c46eSAneesh Kumar K.V if (err == -EAGAIN) 15995a87b7a5STheodore Ts'o goto submit_io; 1600df22291fSAneesh Kumar K.V 16015dee5437STheodore Ts'o if (err == -ENOSPC && ext4_count_free_clusters(sb)) { 1602df22291fSAneesh Kumar K.V mpd->retval = err; 16035a87b7a5STheodore Ts'o goto submit_io; 1604df22291fSAneesh Kumar K.V } 1605df22291fSAneesh Kumar K.V 1606c4a0c46eSAneesh Kumar K.V /* 1607ed5bde0bSTheodore Ts'o * get block failure will cause us to loop in 1608ed5bde0bSTheodore Ts'o * writepages, because a_ops->writepage won't be able 1609ed5bde0bSTheodore Ts'o * to make progress. The page will be redirtied by 1610ed5bde0bSTheodore Ts'o * writepage and writepages will again try to write 1611ed5bde0bSTheodore Ts'o * the same. 1612c4a0c46eSAneesh Kumar K.V */ 1613e3570639SEric Sandeen if (!(EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) { 1614e3570639SEric Sandeen ext4_msg(sb, KERN_CRIT, 1615e3570639SEric Sandeen "delayed block allocation failed for inode %lu " 1616e3570639SEric Sandeen "at logical offset %llu with max blocks %zd " 1617e3570639SEric Sandeen "with error %d", mpd->inode->i_ino, 1618c4a0c46eSAneesh Kumar K.V (unsigned long long) next, 16198dc207c0STheodore Ts'o mpd->b_size >> mpd->inode->i_blkbits, err); 1620e3570639SEric Sandeen ext4_msg(sb, KERN_CRIT, 162101a523ebSTheodore Ts'o "This should not happen!! Data will be lost"); 1622e3570639SEric Sandeen if (err == -ENOSPC) 1623df22291fSAneesh Kumar K.V ext4_print_free_blocks(mpd->inode); 1624030ba6bcSAneesh Kumar K.V } 16252fa3cdfbSTheodore Ts'o /* invalidate all the pages */ 1626c7f5938aSCurt Wohlgemuth ext4_da_block_invalidatepages(mpd); 1627e0fd9b90SCurt Wohlgemuth 1628e0fd9b90SCurt Wohlgemuth /* Mark this page range as having been completed */ 1629e0fd9b90SCurt Wohlgemuth mpd->io_done = 1; 16305a87b7a5STheodore Ts'o return; 1631c4a0c46eSAneesh Kumar K.V } 16322fa3cdfbSTheodore Ts'o BUG_ON(blks == 0); 16332fa3cdfbSTheodore Ts'o 16341de3e3dfSTheodore Ts'o mapp = ↦ 16352ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 16362ed88685STheodore Ts'o struct block_device *bdev = mpd->inode->i_sb->s_bdev; 16372ed88685STheodore Ts'o int i; 163864769240SAlex Tomas 16392ed88685STheodore Ts'o for (i = 0; i < map.m_len; i++) 16402ed88685STheodore Ts'o unmap_underlying_metadata(bdev, map.m_pblk + i); 16412fa3cdfbSTheodore Ts'o } 16422fa3cdfbSTheodore Ts'o 16432fa3cdfbSTheodore Ts'o /* 164403f5d8bcSJan Kara * Update on-disk size along with block allocation. 16452fa3cdfbSTheodore Ts'o */ 16462fa3cdfbSTheodore Ts'o disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits; 16472fa3cdfbSTheodore Ts'o if (disksize > i_size_read(mpd->inode)) 16482fa3cdfbSTheodore Ts'o disksize = i_size_read(mpd->inode); 16492fa3cdfbSTheodore Ts'o if (disksize > EXT4_I(mpd->inode)->i_disksize) { 16502fa3cdfbSTheodore Ts'o ext4_update_i_disksize(mpd->inode, disksize); 16515a87b7a5STheodore Ts'o err = ext4_mark_inode_dirty(handle, mpd->inode); 16525a87b7a5STheodore Ts'o if (err) 16535a87b7a5STheodore Ts'o ext4_error(mpd->inode->i_sb, 16545a87b7a5STheodore Ts'o "Failed to mark inode %lu dirty", 16555a87b7a5STheodore Ts'o mpd->inode->i_ino); 16562fa3cdfbSTheodore Ts'o } 16572fa3cdfbSTheodore Ts'o 16585a87b7a5STheodore Ts'o submit_io: 16591de3e3dfSTheodore Ts'o mpage_da_submit_io(mpd, mapp); 16605a87b7a5STheodore Ts'o mpd->io_done = 1; 166164769240SAlex Tomas } 166264769240SAlex Tomas 1663bf068ee2SAneesh Kumar K.V #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \ 1664bf068ee2SAneesh Kumar K.V (1 << BH_Delay) | (1 << BH_Unwritten)) 166564769240SAlex Tomas 166664769240SAlex Tomas /* 166764769240SAlex Tomas * mpage_add_bh_to_extent - try to add one more block to extent of blocks 166864769240SAlex Tomas * 166964769240SAlex Tomas * @mpd->lbh - extent of blocks 167064769240SAlex Tomas * @logical - logical number of the block in the file 1671b6a8e62fSJan Kara * @b_state - b_state of the buffer head added 167264769240SAlex Tomas * 167364769240SAlex Tomas * the function is used to collect contig. blocks in same state 167464769240SAlex Tomas */ 1675b6a8e62fSJan Kara static void mpage_add_bh_to_extent(struct mpage_da_data *mpd, sector_t logical, 16768dc207c0STheodore Ts'o unsigned long b_state) 167764769240SAlex Tomas { 167864769240SAlex Tomas sector_t next; 1679b6a8e62fSJan Kara int blkbits = mpd->inode->i_blkbits; 1680b6a8e62fSJan Kara int nrblocks = mpd->b_size >> blkbits; 168164769240SAlex Tomas 1682c445e3e0SEric Sandeen /* 1683c445e3e0SEric Sandeen * XXX Don't go larger than mballoc is willing to allocate 1684c445e3e0SEric Sandeen * This is a stopgap solution. We eventually need to fold 1685c445e3e0SEric Sandeen * mpage_da_submit_io() into this function and then call 168679e83036SEric Sandeen * ext4_map_blocks() multiple times in a loop 1687c445e3e0SEric Sandeen */ 1688b6a8e62fSJan Kara if (nrblocks >= (8*1024*1024 >> blkbits)) 1689c445e3e0SEric Sandeen goto flush_it; 1690c445e3e0SEric Sandeen 1691525f4ed8SMingming Cao /* check if the reserved journal credits might overflow */ 1692b6a8e62fSJan Kara if (!ext4_test_inode_flag(mpd->inode, EXT4_INODE_EXTENTS)) { 1693525f4ed8SMingming Cao if (nrblocks >= EXT4_MAX_TRANS_DATA) { 1694525f4ed8SMingming Cao /* 1695525f4ed8SMingming Cao * With non-extent format we are limited by the journal 1696525f4ed8SMingming Cao * credit available. Total credit needed to insert 1697525f4ed8SMingming Cao * nrblocks contiguous blocks is dependent on the 1698525f4ed8SMingming Cao * nrblocks. So limit nrblocks. 1699525f4ed8SMingming Cao */ 1700525f4ed8SMingming Cao goto flush_it; 1701525f4ed8SMingming Cao } 1702525f4ed8SMingming Cao } 170364769240SAlex Tomas /* 170464769240SAlex Tomas * First block in the extent 170564769240SAlex Tomas */ 17068dc207c0STheodore Ts'o if (mpd->b_size == 0) { 17078dc207c0STheodore Ts'o mpd->b_blocknr = logical; 1708b6a8e62fSJan Kara mpd->b_size = 1 << blkbits; 17098dc207c0STheodore Ts'o mpd->b_state = b_state & BH_FLAGS; 171064769240SAlex Tomas return; 171164769240SAlex Tomas } 171264769240SAlex Tomas 17138dc207c0STheodore Ts'o next = mpd->b_blocknr + nrblocks; 171464769240SAlex Tomas /* 171564769240SAlex Tomas * Can we merge the block to our big extent? 171664769240SAlex Tomas */ 17178dc207c0STheodore Ts'o if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) { 1718b6a8e62fSJan Kara mpd->b_size += 1 << blkbits; 171964769240SAlex Tomas return; 172064769240SAlex Tomas } 172164769240SAlex Tomas 1722525f4ed8SMingming Cao flush_it: 172364769240SAlex Tomas /* 172464769240SAlex Tomas * We couldn't merge the block to our extent, so we 172564769240SAlex Tomas * need to flush current extent and start new one 172664769240SAlex Tomas */ 17275a87b7a5STheodore Ts'o mpage_da_map_and_submit(mpd); 1728a1d6cc56SAneesh Kumar K.V return; 172964769240SAlex Tomas } 173064769240SAlex Tomas 1731c364b22cSAneesh Kumar K.V static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh) 173229fa89d0SAneesh Kumar K.V { 1733c364b22cSAneesh Kumar K.V return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 173429fa89d0SAneesh Kumar K.V } 173529fa89d0SAneesh Kumar K.V 173664769240SAlex Tomas /* 17375356f261SAditya Kali * This function is grabs code from the very beginning of 17385356f261SAditya Kali * ext4_map_blocks, but assumes that the caller is from delayed write 17395356f261SAditya Kali * time. This function looks up the requested blocks and sets the 17405356f261SAditya Kali * buffer delay bit under the protection of i_data_sem. 17415356f261SAditya Kali */ 17425356f261SAditya Kali static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, 17435356f261SAditya Kali struct ext4_map_blocks *map, 17445356f261SAditya Kali struct buffer_head *bh) 17455356f261SAditya Kali { 17465356f261SAditya Kali int retval; 17475356f261SAditya Kali sector_t invalid_block = ~((sector_t) 0xffff); 17485356f261SAditya Kali 17495356f261SAditya Kali if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 17505356f261SAditya Kali invalid_block = ~0; 17515356f261SAditya Kali 17525356f261SAditya Kali map->m_flags = 0; 17535356f261SAditya Kali ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u," 17545356f261SAditya Kali "logical block %lu\n", inode->i_ino, map->m_len, 17555356f261SAditya Kali (unsigned long) map->m_lblk); 17565356f261SAditya Kali /* 17575356f261SAditya Kali * Try to see if we can get the block without requesting a new 17585356f261SAditya Kali * file system block. 17595356f261SAditya Kali */ 17605356f261SAditya Kali down_read((&EXT4_I(inode)->i_data_sem)); 17619c3569b5STao Ma if (ext4_has_inline_data(inode)) { 17629c3569b5STao Ma /* 17639c3569b5STao Ma * We will soon create blocks for this page, and let 17649c3569b5STao Ma * us pretend as if the blocks aren't allocated yet. 17659c3569b5STao Ma * In case of clusters, we have to handle the work 17669c3569b5STao Ma * of mapping from cluster so that the reserved space 17679c3569b5STao Ma * is calculated properly. 17689c3569b5STao Ma */ 17699c3569b5STao Ma if ((EXT4_SB(inode->i_sb)->s_cluster_ratio > 1) && 17709c3569b5STao Ma ext4_find_delalloc_cluster(inode, map->m_lblk)) 17719c3569b5STao Ma map->m_flags |= EXT4_MAP_FROM_CLUSTER; 17729c3569b5STao Ma retval = 0; 17739c3569b5STao Ma } else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 17745356f261SAditya Kali retval = ext4_ext_map_blocks(NULL, inode, map, 0); 17755356f261SAditya Kali else 17765356f261SAditya Kali retval = ext4_ind_map_blocks(NULL, inode, map, 0); 17775356f261SAditya Kali 17785356f261SAditya Kali if (retval == 0) { 1779*f7fec032SZheng Liu int ret; 17805356f261SAditya Kali /* 17815356f261SAditya Kali * XXX: __block_prepare_write() unmaps passed block, 17825356f261SAditya Kali * is it OK? 17835356f261SAditya Kali */ 17845356f261SAditya Kali /* If the block was allocated from previously allocated cluster, 17855356f261SAditya Kali * then we dont need to reserve it again. */ 17865356f261SAditya Kali if (!(map->m_flags & EXT4_MAP_FROM_CLUSTER)) { 1787*f7fec032SZheng Liu ret = ext4_da_reserve_space(inode, iblock); 1788*f7fec032SZheng Liu if (ret) { 17895356f261SAditya Kali /* not enough space to reserve */ 1790*f7fec032SZheng Liu retval = ret; 17915356f261SAditya Kali goto out_unlock; 17925356f261SAditya Kali } 1793*f7fec032SZheng Liu } 17945356f261SAditya Kali 1795*f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 1796fdc0212eSZheng Liu ~0, EXTENT_STATUS_DELAYED); 1797*f7fec032SZheng Liu if (ret) { 1798*f7fec032SZheng Liu retval = ret; 179951865fdaSZheng Liu goto out_unlock; 1800*f7fec032SZheng Liu } 180151865fdaSZheng Liu 18025356f261SAditya Kali /* Clear EXT4_MAP_FROM_CLUSTER flag since its purpose is served 18035356f261SAditya Kali * and it should not appear on the bh->b_state. 18045356f261SAditya Kali */ 18055356f261SAditya Kali map->m_flags &= ~EXT4_MAP_FROM_CLUSTER; 18065356f261SAditya Kali 18075356f261SAditya Kali map_bh(bh, inode->i_sb, invalid_block); 18085356f261SAditya Kali set_buffer_new(bh); 18095356f261SAditya Kali set_buffer_delay(bh); 1810*f7fec032SZheng Liu } else if (retval > 0) { 1811*f7fec032SZheng Liu int ret; 1812*f7fec032SZheng Liu unsigned long long status; 1813*f7fec032SZheng Liu 1814*f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 1815*f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 1816*f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 1817*f7fec032SZheng Liu map->m_pblk, status); 1818*f7fec032SZheng Liu if (ret != 0) 1819*f7fec032SZheng Liu retval = ret; 18205356f261SAditya Kali } 18215356f261SAditya Kali 18225356f261SAditya Kali out_unlock: 18235356f261SAditya Kali up_read((&EXT4_I(inode)->i_data_sem)); 18245356f261SAditya Kali 18255356f261SAditya Kali return retval; 18265356f261SAditya Kali } 18275356f261SAditya Kali 18285356f261SAditya Kali /* 1829b920c755STheodore Ts'o * This is a special get_blocks_t callback which is used by 1830b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 1831b920c755STheodore Ts'o * reserve space for a single block. 183229fa89d0SAneesh Kumar K.V * 183329fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 183429fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 183529fa89d0SAneesh Kumar K.V * 183629fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 183729fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 183829fa89d0SAneesh Kumar K.V * initialized properly. 183964769240SAlex Tomas */ 18409c3569b5STao Ma int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 18412ed88685STheodore Ts'o struct buffer_head *bh, int create) 184264769240SAlex Tomas { 18432ed88685STheodore Ts'o struct ext4_map_blocks map; 184464769240SAlex Tomas int ret = 0; 184564769240SAlex Tomas 184664769240SAlex Tomas BUG_ON(create == 0); 18472ed88685STheodore Ts'o BUG_ON(bh->b_size != inode->i_sb->s_blocksize); 18482ed88685STheodore Ts'o 18492ed88685STheodore Ts'o map.m_lblk = iblock; 18502ed88685STheodore Ts'o map.m_len = 1; 185164769240SAlex Tomas 185264769240SAlex Tomas /* 185364769240SAlex Tomas * first, we need to know whether the block is allocated already 185464769240SAlex Tomas * preallocated blocks are unmapped but should treated 185564769240SAlex Tomas * the same as allocated blocks. 185664769240SAlex Tomas */ 18575356f261SAditya Kali ret = ext4_da_map_blocks(inode, iblock, &map, bh); 18585356f261SAditya Kali if (ret <= 0) 18592ed88685STheodore Ts'o return ret; 186064769240SAlex Tomas 18612ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 18622ed88685STheodore Ts'o bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; 18632ed88685STheodore Ts'o 18642ed88685STheodore Ts'o if (buffer_unwritten(bh)) { 18652ed88685STheodore Ts'o /* A delayed write to unwritten bh should be marked 18662ed88685STheodore Ts'o * new and mapped. Mapped ensures that we don't do 18672ed88685STheodore Ts'o * get_block multiple times when we write to the same 18682ed88685STheodore Ts'o * offset and new ensures that we do proper zero out 18692ed88685STheodore Ts'o * for partial write. 18702ed88685STheodore Ts'o */ 18712ed88685STheodore Ts'o set_buffer_new(bh); 1872c8205636STheodore Ts'o set_buffer_mapped(bh); 18732ed88685STheodore Ts'o } 18742ed88685STheodore Ts'o return 0; 187564769240SAlex Tomas } 187661628a3fSMingming Cao 187762e086beSAneesh Kumar K.V static int bget_one(handle_t *handle, struct buffer_head *bh) 187862e086beSAneesh Kumar K.V { 187962e086beSAneesh Kumar K.V get_bh(bh); 188062e086beSAneesh Kumar K.V return 0; 188162e086beSAneesh Kumar K.V } 188262e086beSAneesh Kumar K.V 188362e086beSAneesh Kumar K.V static int bput_one(handle_t *handle, struct buffer_head *bh) 188462e086beSAneesh Kumar K.V { 188562e086beSAneesh Kumar K.V put_bh(bh); 188662e086beSAneesh Kumar K.V return 0; 188762e086beSAneesh Kumar K.V } 188862e086beSAneesh Kumar K.V 188962e086beSAneesh Kumar K.V static int __ext4_journalled_writepage(struct page *page, 189062e086beSAneesh Kumar K.V unsigned int len) 189162e086beSAneesh Kumar K.V { 189262e086beSAneesh Kumar K.V struct address_space *mapping = page->mapping; 189362e086beSAneesh Kumar K.V struct inode *inode = mapping->host; 18943fdcfb66STao Ma struct buffer_head *page_bufs = NULL; 189562e086beSAneesh Kumar K.V handle_t *handle = NULL; 18963fdcfb66STao Ma int ret = 0, err = 0; 18973fdcfb66STao Ma int inline_data = ext4_has_inline_data(inode); 18983fdcfb66STao Ma struct buffer_head *inode_bh = NULL; 189962e086beSAneesh Kumar K.V 1900cb20d518STheodore Ts'o ClearPageChecked(page); 19013fdcfb66STao Ma 19023fdcfb66STao Ma if (inline_data) { 19033fdcfb66STao Ma BUG_ON(page->index != 0); 19043fdcfb66STao Ma BUG_ON(len > ext4_get_max_inline_size(inode)); 19053fdcfb66STao Ma inode_bh = ext4_journalled_write_inline_data(inode, len, page); 19063fdcfb66STao Ma if (inode_bh == NULL) 19073fdcfb66STao Ma goto out; 19083fdcfb66STao Ma } else { 190962e086beSAneesh Kumar K.V page_bufs = page_buffers(page); 19103fdcfb66STao Ma if (!page_bufs) { 19113fdcfb66STao Ma BUG(); 19123fdcfb66STao Ma goto out; 19133fdcfb66STao Ma } 19143fdcfb66STao Ma ext4_walk_page_buffers(handle, page_bufs, 0, len, 19153fdcfb66STao Ma NULL, bget_one); 19163fdcfb66STao Ma } 191762e086beSAneesh Kumar K.V /* As soon as we unlock the page, it can go away, but we have 191862e086beSAneesh Kumar K.V * references to buffers so we are safe */ 191962e086beSAneesh Kumar K.V unlock_page(page); 192062e086beSAneesh Kumar K.V 19219924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 19229924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 192362e086beSAneesh Kumar K.V if (IS_ERR(handle)) { 192462e086beSAneesh Kumar K.V ret = PTR_ERR(handle); 192562e086beSAneesh Kumar K.V goto out; 192662e086beSAneesh Kumar K.V } 192762e086beSAneesh Kumar K.V 1928441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1929441c8508SCurt Wohlgemuth 19303fdcfb66STao Ma if (inline_data) { 19313fdcfb66STao Ma ret = ext4_journal_get_write_access(handle, inode_bh); 19323fdcfb66STao Ma 19333fdcfb66STao Ma err = ext4_handle_dirty_metadata(handle, inode, inode_bh); 19343fdcfb66STao Ma 19353fdcfb66STao Ma } else { 1936f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 193762e086beSAneesh Kumar K.V do_journal_get_write_access); 193862e086beSAneesh Kumar K.V 1939f19d5870STao Ma err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 194062e086beSAneesh Kumar K.V write_end_fn); 19413fdcfb66STao Ma } 194262e086beSAneesh Kumar K.V if (ret == 0) 194362e086beSAneesh Kumar K.V ret = err; 19442d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 194562e086beSAneesh Kumar K.V err = ext4_journal_stop(handle); 194662e086beSAneesh Kumar K.V if (!ret) 194762e086beSAneesh Kumar K.V ret = err; 194862e086beSAneesh Kumar K.V 19493fdcfb66STao Ma if (!ext4_has_inline_data(inode)) 19503fdcfb66STao Ma ext4_walk_page_buffers(handle, page_bufs, 0, len, 19513fdcfb66STao Ma NULL, bput_one); 195219f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 195362e086beSAneesh Kumar K.V out: 19543fdcfb66STao Ma brelse(inode_bh); 195562e086beSAneesh Kumar K.V return ret; 195662e086beSAneesh Kumar K.V } 195762e086beSAneesh Kumar K.V 195861628a3fSMingming Cao /* 195943ce1d23SAneesh Kumar K.V * Note that we don't need to start a transaction unless we're journaling data 196043ce1d23SAneesh Kumar K.V * because we should have holes filled from ext4_page_mkwrite(). We even don't 196143ce1d23SAneesh Kumar K.V * need to file the inode to the transaction's list in ordered mode because if 196243ce1d23SAneesh Kumar K.V * we are writing back data added by write(), the inode is already there and if 196343ce1d23SAneesh Kumar K.V * we are writing back data modified via mmap(), no one guarantees in which 196443ce1d23SAneesh Kumar K.V * transaction the data will hit the disk. In case we are journaling data, we 196543ce1d23SAneesh Kumar K.V * cannot start transaction directly because transaction start ranks above page 196643ce1d23SAneesh Kumar K.V * lock so we have to do some magic. 196743ce1d23SAneesh Kumar K.V * 1968b920c755STheodore Ts'o * This function can get called via... 1969b920c755STheodore Ts'o * - ext4_da_writepages after taking page lock (have journal handle) 1970b920c755STheodore Ts'o * - journal_submit_inode_data_buffers (no journal handle) 1971f6463b0dSArtem Bityutskiy * - shrink_page_list via the kswapd/direct reclaim (no journal handle) 1972b920c755STheodore Ts'o * - grab_page_cache when doing write_begin (have journal handle) 197343ce1d23SAneesh Kumar K.V * 197443ce1d23SAneesh Kumar K.V * We don't do any block allocation in this function. If we have page with 197543ce1d23SAneesh Kumar K.V * multiple blocks we need to write those buffer_heads that are mapped. This 197643ce1d23SAneesh Kumar K.V * is important for mmaped based write. So if we do with blocksize 1K 197743ce1d23SAneesh Kumar K.V * truncate(f, 1024); 197843ce1d23SAneesh Kumar K.V * a = mmap(f, 0, 4096); 197943ce1d23SAneesh Kumar K.V * a[0] = 'a'; 198043ce1d23SAneesh Kumar K.V * truncate(f, 4096); 198143ce1d23SAneesh Kumar K.V * we have in the page first buffer_head mapped via page_mkwrite call back 198290802ed9SPaul Bolle * but other buffer_heads would be unmapped but dirty (dirty done via the 198343ce1d23SAneesh Kumar K.V * do_wp_page). So writepage should write the first block. If we modify 198443ce1d23SAneesh Kumar K.V * the mmap area beyond 1024 we will again get a page_fault and the 198543ce1d23SAneesh Kumar K.V * page_mkwrite callback will do the block allocation and mark the 198643ce1d23SAneesh Kumar K.V * buffer_heads mapped. 198743ce1d23SAneesh Kumar K.V * 198843ce1d23SAneesh Kumar K.V * We redirty the page if we have any buffer_heads that is either delay or 198943ce1d23SAneesh Kumar K.V * unwritten in the page. 199043ce1d23SAneesh Kumar K.V * 199143ce1d23SAneesh Kumar K.V * We can get recursively called as show below. 199243ce1d23SAneesh Kumar K.V * 199343ce1d23SAneesh Kumar K.V * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> 199443ce1d23SAneesh Kumar K.V * ext4_writepage() 199543ce1d23SAneesh Kumar K.V * 199643ce1d23SAneesh Kumar K.V * But since we don't do any block allocation we should not deadlock. 199743ce1d23SAneesh Kumar K.V * Page also have the dirty flag cleared so we don't get recurive page_lock. 199861628a3fSMingming Cao */ 199943ce1d23SAneesh Kumar K.V static int ext4_writepage(struct page *page, 200064769240SAlex Tomas struct writeback_control *wbc) 200164769240SAlex Tomas { 2002f8bec370SJan Kara int ret = 0; 200361628a3fSMingming Cao loff_t size; 2004498e5f24STheodore Ts'o unsigned int len; 2005744692dcSJiaying Zhang struct buffer_head *page_bufs = NULL; 200661628a3fSMingming Cao struct inode *inode = page->mapping->host; 200736ade451SJan Kara struct ext4_io_submit io_submit; 200864769240SAlex Tomas 2009a9c667f8SLukas Czerner trace_ext4_writepage(page); 201061628a3fSMingming Cao size = i_size_read(inode); 201161628a3fSMingming Cao if (page->index == size >> PAGE_CACHE_SHIFT) 201261628a3fSMingming Cao len = size & ~PAGE_CACHE_MASK; 201361628a3fSMingming Cao else 201461628a3fSMingming Cao len = PAGE_CACHE_SIZE; 201561628a3fSMingming Cao 2016f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 2017fe386132SJan Kara /* 2018fe386132SJan Kara * We cannot do block allocation or other extent handling in this 2019fe386132SJan Kara * function. If there are buffers needing that, we have to redirty 2020fe386132SJan Kara * the page. But we may reach here when we do a journal commit via 2021fe386132SJan Kara * journal_submit_inode_data_buffers() and in that case we must write 2022fe386132SJan Kara * allocated buffers to achieve data=ordered mode guarantees. 2023fe386132SJan Kara */ 2024f19d5870STao Ma if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2025c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 2026f8bec370SJan Kara redirty_page_for_writepage(wbc, page); 2027fe386132SJan Kara if (current->flags & PF_MEMALLOC) { 2028fe386132SJan Kara /* 2029fe386132SJan Kara * For memory cleaning there's no point in writing only 2030fe386132SJan Kara * some buffers. So just bail out. Warn if we came here 2031fe386132SJan Kara * from direct reclaim. 2032fe386132SJan Kara */ 2033fe386132SJan Kara WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) 2034fe386132SJan Kara == PF_MEMALLOC); 2035f8bec370SJan Kara unlock_page(page); 2036f8bec370SJan Kara return 0; 2037f0e6c985SAneesh Kumar K.V } 2038fe386132SJan Kara } 203964769240SAlex Tomas 2040cb20d518STheodore Ts'o if (PageChecked(page) && ext4_should_journal_data(inode)) 204143ce1d23SAneesh Kumar K.V /* 204243ce1d23SAneesh Kumar K.V * It's mmapped pagecache. Add buffers and journal it. There 204343ce1d23SAneesh Kumar K.V * doesn't seem much point in redirtying the page here. 204443ce1d23SAneesh Kumar K.V */ 20453f0ca309SWu Fengguang return __ext4_journalled_writepage(page, len); 204643ce1d23SAneesh Kumar K.V 204736ade451SJan Kara memset(&io_submit, 0, sizeof(io_submit)); 204836ade451SJan Kara ret = ext4_bio_write_page(&io_submit, page, len, wbc); 204936ade451SJan Kara ext4_io_submit(&io_submit); 205064769240SAlex Tomas return ret; 205164769240SAlex Tomas } 205264769240SAlex Tomas 205361628a3fSMingming Cao /* 2054525f4ed8SMingming Cao * This is called via ext4_da_writepages() to 205525985edcSLucas De Marchi * calculate the total number of credits to reserve to fit 2056525f4ed8SMingming Cao * a single extent allocation into a single transaction, 2057525f4ed8SMingming Cao * ext4_da_writpeages() will loop calling this before 2058525f4ed8SMingming Cao * the block allocation. 205961628a3fSMingming Cao */ 2060525f4ed8SMingming Cao 2061525f4ed8SMingming Cao static int ext4_da_writepages_trans_blocks(struct inode *inode) 2062525f4ed8SMingming Cao { 2063525f4ed8SMingming Cao int max_blocks = EXT4_I(inode)->i_reserved_data_blocks; 2064525f4ed8SMingming Cao 2065525f4ed8SMingming Cao /* 2066525f4ed8SMingming Cao * With non-extent format the journal credit needed to 2067525f4ed8SMingming Cao * insert nrblocks contiguous block is dependent on 2068525f4ed8SMingming Cao * number of contiguous block. So we will limit 2069525f4ed8SMingming Cao * number of contiguous block to a sane value 2070525f4ed8SMingming Cao */ 207112e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) && 2072525f4ed8SMingming Cao (max_blocks > EXT4_MAX_TRANS_DATA)) 2073525f4ed8SMingming Cao max_blocks = EXT4_MAX_TRANS_DATA; 2074525f4ed8SMingming Cao 2075525f4ed8SMingming Cao return ext4_chunk_trans_blocks(inode, max_blocks); 2076525f4ed8SMingming Cao } 207761628a3fSMingming Cao 20788e48dcfbSTheodore Ts'o /* 20798e48dcfbSTheodore Ts'o * write_cache_pages_da - walk the list of dirty pages of the given 20808eb9e5ceSTheodore Ts'o * address space and accumulate pages that need writing, and call 2081168fc022STheodore Ts'o * mpage_da_map_and_submit to map a single contiguous memory region 2082168fc022STheodore Ts'o * and then write them. 20838e48dcfbSTheodore Ts'o */ 20849c3569b5STao Ma static int write_cache_pages_da(handle_t *handle, 20859c3569b5STao Ma struct address_space *mapping, 20868e48dcfbSTheodore Ts'o struct writeback_control *wbc, 208772f84e65SEric Sandeen struct mpage_da_data *mpd, 208872f84e65SEric Sandeen pgoff_t *done_index) 20898e48dcfbSTheodore Ts'o { 20908eb9e5ceSTheodore Ts'o struct buffer_head *bh, *head; 2091168fc022STheodore Ts'o struct inode *inode = mapping->host; 20928e48dcfbSTheodore Ts'o struct pagevec pvec; 20934f01b02cSTheodore Ts'o unsigned int nr_pages; 20944f01b02cSTheodore Ts'o sector_t logical; 20954f01b02cSTheodore Ts'o pgoff_t index, end; 20968e48dcfbSTheodore Ts'o long nr_to_write = wbc->nr_to_write; 20974f01b02cSTheodore Ts'o int i, tag, ret = 0; 20988e48dcfbSTheodore Ts'o 2099168fc022STheodore Ts'o memset(mpd, 0, sizeof(struct mpage_da_data)); 2100168fc022STheodore Ts'o mpd->wbc = wbc; 2101168fc022STheodore Ts'o mpd->inode = inode; 21028e48dcfbSTheodore Ts'o pagevec_init(&pvec, 0); 21038e48dcfbSTheodore Ts'o index = wbc->range_start >> PAGE_CACHE_SHIFT; 21048e48dcfbSTheodore Ts'o end = wbc->range_end >> PAGE_CACHE_SHIFT; 21058e48dcfbSTheodore Ts'o 21066e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 21075b41d924SEric Sandeen tag = PAGECACHE_TAG_TOWRITE; 21085b41d924SEric Sandeen else 21095b41d924SEric Sandeen tag = PAGECACHE_TAG_DIRTY; 21105b41d924SEric Sandeen 211172f84e65SEric Sandeen *done_index = index; 21124f01b02cSTheodore Ts'o while (index <= end) { 21135b41d924SEric Sandeen nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, 21148e48dcfbSTheodore Ts'o min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); 21158e48dcfbSTheodore Ts'o if (nr_pages == 0) 21164f01b02cSTheodore Ts'o return 0; 21178e48dcfbSTheodore Ts'o 21188e48dcfbSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 21198e48dcfbSTheodore Ts'o struct page *page = pvec.pages[i]; 21208e48dcfbSTheodore Ts'o 21218e48dcfbSTheodore Ts'o /* 21228e48dcfbSTheodore Ts'o * At this point, the page may be truncated or 21238e48dcfbSTheodore Ts'o * invalidated (changing page->mapping to NULL), or 21248e48dcfbSTheodore Ts'o * even swizzled back from swapper_space to tmpfs file 21258e48dcfbSTheodore Ts'o * mapping. However, page->index will not change 21268e48dcfbSTheodore Ts'o * because we have a reference on the page. 21278e48dcfbSTheodore Ts'o */ 21284f01b02cSTheodore Ts'o if (page->index > end) 21294f01b02cSTheodore Ts'o goto out; 21308e48dcfbSTheodore Ts'o 213172f84e65SEric Sandeen *done_index = page->index + 1; 213272f84e65SEric Sandeen 213378aaced3STheodore Ts'o /* 213478aaced3STheodore Ts'o * If we can't merge this page, and we have 213578aaced3STheodore Ts'o * accumulated an contiguous region, write it 213678aaced3STheodore Ts'o */ 213778aaced3STheodore Ts'o if ((mpd->next_page != page->index) && 213878aaced3STheodore Ts'o (mpd->next_page != mpd->first_page)) { 213978aaced3STheodore Ts'o mpage_da_map_and_submit(mpd); 214078aaced3STheodore Ts'o goto ret_extent_tail; 214178aaced3STheodore Ts'o } 214278aaced3STheodore Ts'o 21438e48dcfbSTheodore Ts'o lock_page(page); 21448e48dcfbSTheodore Ts'o 21458e48dcfbSTheodore Ts'o /* 21464f01b02cSTheodore Ts'o * If the page is no longer dirty, or its 21474f01b02cSTheodore Ts'o * mapping no longer corresponds to inode we 21484f01b02cSTheodore Ts'o * are writing (which means it has been 21494f01b02cSTheodore Ts'o * truncated or invalidated), or the page is 21504f01b02cSTheodore Ts'o * already under writeback and we are not 21514f01b02cSTheodore Ts'o * doing a data integrity writeback, skip the page 21528e48dcfbSTheodore Ts'o */ 21534f01b02cSTheodore Ts'o if (!PageDirty(page) || 21544f01b02cSTheodore Ts'o (PageWriteback(page) && 21554f01b02cSTheodore Ts'o (wbc->sync_mode == WB_SYNC_NONE)) || 21564f01b02cSTheodore Ts'o unlikely(page->mapping != mapping)) { 21578e48dcfbSTheodore Ts'o unlock_page(page); 21588e48dcfbSTheodore Ts'o continue; 21598e48dcfbSTheodore Ts'o } 21608e48dcfbSTheodore Ts'o 21618e48dcfbSTheodore Ts'o wait_on_page_writeback(page); 21628e48dcfbSTheodore Ts'o BUG_ON(PageWriteback(page)); 21638e48dcfbSTheodore Ts'o 21649c3569b5STao Ma /* 21659c3569b5STao Ma * If we have inline data and arrive here, it means that 21669c3569b5STao Ma * we will soon create the block for the 1st page, so 21679c3569b5STao Ma * we'd better clear the inline data here. 21689c3569b5STao Ma */ 21699c3569b5STao Ma if (ext4_has_inline_data(inode)) { 21709c3569b5STao Ma BUG_ON(ext4_test_inode_state(inode, 21719c3569b5STao Ma EXT4_STATE_MAY_INLINE_DATA)); 21729c3569b5STao Ma ext4_destroy_inline_data(handle, inode); 21739c3569b5STao Ma } 21749c3569b5STao Ma 2175168fc022STheodore Ts'o if (mpd->next_page != page->index) 21768eb9e5ceSTheodore Ts'o mpd->first_page = page->index; 21778eb9e5ceSTheodore Ts'o mpd->next_page = page->index + 1; 21788eb9e5ceSTheodore Ts'o logical = (sector_t) page->index << 21798eb9e5ceSTheodore Ts'o (PAGE_CACHE_SHIFT - inode->i_blkbits); 21808eb9e5ceSTheodore Ts'o 2181f8bec370SJan Kara /* Add all dirty buffers to mpd */ 21828eb9e5ceSTheodore Ts'o head = page_buffers(page); 21838eb9e5ceSTheodore Ts'o bh = head; 21848eb9e5ceSTheodore Ts'o do { 21858eb9e5ceSTheodore Ts'o BUG_ON(buffer_locked(bh)); 21868eb9e5ceSTheodore Ts'o /* 2187f8bec370SJan Kara * We need to try to allocate unmapped blocks 2188f8bec370SJan Kara * in the same page. Otherwise we won't make 2189f8bec370SJan Kara * progress with the page in ext4_writepage 21908eb9e5ceSTheodore Ts'o */ 21918eb9e5ceSTheodore Ts'o if (ext4_bh_delay_or_unwritten(NULL, bh)) { 21928eb9e5ceSTheodore Ts'o mpage_add_bh_to_extent(mpd, logical, 21938eb9e5ceSTheodore Ts'o bh->b_state); 21944f01b02cSTheodore Ts'o if (mpd->io_done) 21954f01b02cSTheodore Ts'o goto ret_extent_tail; 2196f8bec370SJan Kara } else if (buffer_dirty(bh) && 2197f8bec370SJan Kara buffer_mapped(bh)) { 21988eb9e5ceSTheodore Ts'o /* 2199f8bec370SJan Kara * mapped dirty buffer. We need to 2200f8bec370SJan Kara * update the b_state because we look 2201f8bec370SJan Kara * at b_state in mpage_da_map_blocks. 2202f8bec370SJan Kara * We don't update b_size because if we 2203f8bec370SJan Kara * find an unmapped buffer_head later 2204f8bec370SJan Kara * we need to use the b_state flag of 2205f8bec370SJan Kara * that buffer_head. 22068eb9e5ceSTheodore Ts'o */ 22078eb9e5ceSTheodore Ts'o if (mpd->b_size == 0) 2208f8bec370SJan Kara mpd->b_state = 2209f8bec370SJan Kara bh->b_state & BH_FLAGS; 22108e48dcfbSTheodore Ts'o } 22118eb9e5ceSTheodore Ts'o logical++; 22128eb9e5ceSTheodore Ts'o } while ((bh = bh->b_this_page) != head); 22138e48dcfbSTheodore Ts'o 22148e48dcfbSTheodore Ts'o if (nr_to_write > 0) { 22158e48dcfbSTheodore Ts'o nr_to_write--; 22168e48dcfbSTheodore Ts'o if (nr_to_write == 0 && 22174f01b02cSTheodore Ts'o wbc->sync_mode == WB_SYNC_NONE) 22188e48dcfbSTheodore Ts'o /* 22198e48dcfbSTheodore Ts'o * We stop writing back only if we are 22208e48dcfbSTheodore Ts'o * not doing integrity sync. In case of 22218e48dcfbSTheodore Ts'o * integrity sync we have to keep going 22228e48dcfbSTheodore Ts'o * because someone may be concurrently 22238e48dcfbSTheodore Ts'o * dirtying pages, and we might have 22248e48dcfbSTheodore Ts'o * synced a lot of newly appeared dirty 22258e48dcfbSTheodore Ts'o * pages, but have not synced all of the 22268e48dcfbSTheodore Ts'o * old dirty pages. 22278e48dcfbSTheodore Ts'o */ 22284f01b02cSTheodore Ts'o goto out; 22298e48dcfbSTheodore Ts'o } 22308e48dcfbSTheodore Ts'o } 22318e48dcfbSTheodore Ts'o pagevec_release(&pvec); 22328e48dcfbSTheodore Ts'o cond_resched(); 22338e48dcfbSTheodore Ts'o } 22344f01b02cSTheodore Ts'o return 0; 22354f01b02cSTheodore Ts'o ret_extent_tail: 22364f01b02cSTheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 22378eb9e5ceSTheodore Ts'o out: 22388eb9e5ceSTheodore Ts'o pagevec_release(&pvec); 22398eb9e5ceSTheodore Ts'o cond_resched(); 22408e48dcfbSTheodore Ts'o return ret; 22418e48dcfbSTheodore Ts'o } 22428e48dcfbSTheodore Ts'o 22438e48dcfbSTheodore Ts'o 224464769240SAlex Tomas static int ext4_da_writepages(struct address_space *mapping, 224564769240SAlex Tomas struct writeback_control *wbc) 224664769240SAlex Tomas { 224722208dedSAneesh Kumar K.V pgoff_t index; 224822208dedSAneesh Kumar K.V int range_whole = 0; 224961628a3fSMingming Cao handle_t *handle = NULL; 2250df22291fSAneesh Kumar K.V struct mpage_da_data mpd; 22515e745b04SAneesh Kumar K.V struct inode *inode = mapping->host; 2252498e5f24STheodore Ts'o int pages_written = 0; 225355138e0bSTheodore Ts'o unsigned int max_pages; 22542acf2c26SAneesh Kumar K.V int range_cyclic, cycled = 1, io_done = 0; 225555138e0bSTheodore Ts'o int needed_blocks, ret = 0; 225655138e0bSTheodore Ts'o long desired_nr_to_write, nr_to_writebump = 0; 2257de89de6eSTheodore Ts'o loff_t range_start = wbc->range_start; 22585e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 225972f84e65SEric Sandeen pgoff_t done_index = 0; 22605b41d924SEric Sandeen pgoff_t end; 22611bce63d1SShaohua Li struct blk_plug plug; 226261628a3fSMingming Cao 22639bffad1eSTheodore Ts'o trace_ext4_da_writepages(inode, wbc); 2264ba80b101STheodore Ts'o 226561628a3fSMingming Cao /* 226661628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 226761628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 226861628a3fSMingming Cao * because that could violate lock ordering on umount 226961628a3fSMingming Cao */ 2270a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 227161628a3fSMingming Cao return 0; 22722a21e37eSTheodore Ts'o 22732a21e37eSTheodore Ts'o /* 22742a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 22752a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 22762a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 22774ab2f15bSTheodore Ts'o * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because 22782a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 22792a21e37eSTheodore Ts'o * read-only, and in that case, ext4_da_writepages should 22802a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 22812a21e37eSTheodore Ts'o * the stack trace. 22822a21e37eSTheodore Ts'o */ 22834ab2f15bSTheodore Ts'o if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) 22842a21e37eSTheodore Ts'o return -EROFS; 22852a21e37eSTheodore Ts'o 228622208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 228722208dedSAneesh Kumar K.V range_whole = 1; 228861628a3fSMingming Cao 22892acf2c26SAneesh Kumar K.V range_cyclic = wbc->range_cyclic; 22902acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 229122208dedSAneesh Kumar K.V index = mapping->writeback_index; 22922acf2c26SAneesh Kumar K.V if (index) 22932acf2c26SAneesh Kumar K.V cycled = 0; 22942acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 22952acf2c26SAneesh Kumar K.V wbc->range_end = LLONG_MAX; 22962acf2c26SAneesh Kumar K.V wbc->range_cyclic = 0; 22975b41d924SEric Sandeen end = -1; 22985b41d924SEric Sandeen } else { 229922208dedSAneesh Kumar K.V index = wbc->range_start >> PAGE_CACHE_SHIFT; 23005b41d924SEric Sandeen end = wbc->range_end >> PAGE_CACHE_SHIFT; 23015b41d924SEric Sandeen } 2302a1d6cc56SAneesh Kumar K.V 230355138e0bSTheodore Ts'o /* 230455138e0bSTheodore Ts'o * This works around two forms of stupidity. The first is in 230555138e0bSTheodore Ts'o * the writeback code, which caps the maximum number of pages 230655138e0bSTheodore Ts'o * written to be 1024 pages. This is wrong on multiple 230755138e0bSTheodore Ts'o * levels; different architectues have a different page size, 230855138e0bSTheodore Ts'o * which changes the maximum amount of data which gets 230955138e0bSTheodore Ts'o * written. Secondly, 4 megabytes is way too small. XFS 231055138e0bSTheodore Ts'o * forces this value to be 16 megabytes by multiplying 231155138e0bSTheodore Ts'o * nr_to_write parameter by four, and then relies on its 231255138e0bSTheodore Ts'o * allocator to allocate larger extents to make them 231355138e0bSTheodore Ts'o * contiguous. Unfortunately this brings us to the second 231455138e0bSTheodore Ts'o * stupidity, which is that ext4's mballoc code only allocates 231555138e0bSTheodore Ts'o * at most 2048 blocks. So we force contiguous writes up to 231655138e0bSTheodore Ts'o * the number of dirty blocks in the inode, or 231755138e0bSTheodore Ts'o * sbi->max_writeback_mb_bump whichever is smaller. 231855138e0bSTheodore Ts'o */ 231955138e0bSTheodore Ts'o max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT); 2320b443e733SEric Sandeen if (!range_cyclic && range_whole) { 2321b443e733SEric Sandeen if (wbc->nr_to_write == LONG_MAX) 2322b443e733SEric Sandeen desired_nr_to_write = wbc->nr_to_write; 232355138e0bSTheodore Ts'o else 2324b443e733SEric Sandeen desired_nr_to_write = wbc->nr_to_write * 8; 2325b443e733SEric Sandeen } else 232655138e0bSTheodore Ts'o desired_nr_to_write = ext4_num_dirty_pages(inode, index, 232755138e0bSTheodore Ts'o max_pages); 232855138e0bSTheodore Ts'o if (desired_nr_to_write > max_pages) 232955138e0bSTheodore Ts'o desired_nr_to_write = max_pages; 233055138e0bSTheodore Ts'o 233155138e0bSTheodore Ts'o if (wbc->nr_to_write < desired_nr_to_write) { 233255138e0bSTheodore Ts'o nr_to_writebump = desired_nr_to_write - wbc->nr_to_write; 233355138e0bSTheodore Ts'o wbc->nr_to_write = desired_nr_to_write; 233455138e0bSTheodore Ts'o } 233555138e0bSTheodore Ts'o 23362acf2c26SAneesh Kumar K.V retry: 23376e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 23385b41d924SEric Sandeen tag_pages_for_writeback(mapping, index, end); 23395b41d924SEric Sandeen 23401bce63d1SShaohua Li blk_start_plug(&plug); 234122208dedSAneesh Kumar K.V while (!ret && wbc->nr_to_write > 0) { 2342a1d6cc56SAneesh Kumar K.V 2343a1d6cc56SAneesh Kumar K.V /* 2344a1d6cc56SAneesh Kumar K.V * we insert one extent at a time. So we need 2345a1d6cc56SAneesh Kumar K.V * credit needed for single extent allocation. 2346a1d6cc56SAneesh Kumar K.V * journalled mode is currently not supported 2347a1d6cc56SAneesh Kumar K.V * by delalloc 2348a1d6cc56SAneesh Kumar K.V */ 2349a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 2350525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 2351a1d6cc56SAneesh Kumar K.V 235261628a3fSMingming Cao /* start a new transaction*/ 23539924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 23549924a92aSTheodore Ts'o needed_blocks); 235561628a3fSMingming Cao if (IS_ERR(handle)) { 235661628a3fSMingming Cao ret = PTR_ERR(handle); 23571693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2358fbe845ddSCurt Wohlgemuth "%ld pages, ino %lu; err %d", __func__, 2359a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 23603c1fcb2cSNamjae Jeon blk_finish_plug(&plug); 236161628a3fSMingming Cao goto out_writepages; 236261628a3fSMingming Cao } 2363f63e6005STheodore Ts'o 2364f63e6005STheodore Ts'o /* 23658eb9e5ceSTheodore Ts'o * Now call write_cache_pages_da() to find the next 2366f63e6005STheodore Ts'o * contiguous region of logical blocks that need 23678eb9e5ceSTheodore Ts'o * blocks to be allocated by ext4 and submit them. 2368f63e6005STheodore Ts'o */ 23699c3569b5STao Ma ret = write_cache_pages_da(handle, mapping, 23709c3569b5STao Ma wbc, &mpd, &done_index); 2371f63e6005STheodore Ts'o /* 2372af901ca1SAndré Goddard Rosa * If we have a contiguous extent of pages and we 2373f63e6005STheodore Ts'o * haven't done the I/O yet, map the blocks and submit 2374f63e6005STheodore Ts'o * them for I/O. 2375f63e6005STheodore Ts'o */ 2376f63e6005STheodore Ts'o if (!mpd.io_done && mpd.next_page != mpd.first_page) { 23775a87b7a5STheodore Ts'o mpage_da_map_and_submit(&mpd); 2378f63e6005STheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 2379f63e6005STheodore Ts'o } 2380b3a3ca8cSTheodore Ts'o trace_ext4_da_write_pages(inode, &mpd); 2381f63e6005STheodore Ts'o wbc->nr_to_write -= mpd.pages_written; 2382df22291fSAneesh Kumar K.V 238361628a3fSMingming Cao ext4_journal_stop(handle); 2384df22291fSAneesh Kumar K.V 23858f64b32eSEric Sandeen if ((mpd.retval == -ENOSPC) && sbi->s_journal) { 238622208dedSAneesh Kumar K.V /* commit the transaction which would 238722208dedSAneesh Kumar K.V * free blocks released in the transaction 238822208dedSAneesh Kumar K.V * and try again 238922208dedSAneesh Kumar K.V */ 2390df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 239122208dedSAneesh Kumar K.V ret = 0; 239222208dedSAneesh Kumar K.V } else if (ret == MPAGE_DA_EXTENT_TAIL) { 2393a1d6cc56SAneesh Kumar K.V /* 23948de49e67SKazuya Mio * Got one extent now try with rest of the pages. 23958de49e67SKazuya Mio * If mpd.retval is set -EIO, journal is aborted. 23968de49e67SKazuya Mio * So we don't need to write any more. 2397a1d6cc56SAneesh Kumar K.V */ 239822208dedSAneesh Kumar K.V pages_written += mpd.pages_written; 23998de49e67SKazuya Mio ret = mpd.retval; 24002acf2c26SAneesh Kumar K.V io_done = 1; 240122208dedSAneesh Kumar K.V } else if (wbc->nr_to_write) 240261628a3fSMingming Cao /* 240361628a3fSMingming Cao * There is no more writeout needed 240461628a3fSMingming Cao * or we requested for a noblocking writeout 240561628a3fSMingming Cao * and we found the device congested 240661628a3fSMingming Cao */ 240761628a3fSMingming Cao break; 240861628a3fSMingming Cao } 24091bce63d1SShaohua Li blk_finish_plug(&plug); 24102acf2c26SAneesh Kumar K.V if (!io_done && !cycled) { 24112acf2c26SAneesh Kumar K.V cycled = 1; 24122acf2c26SAneesh Kumar K.V index = 0; 24132acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 24142acf2c26SAneesh Kumar K.V wbc->range_end = mapping->writeback_index - 1; 24152acf2c26SAneesh Kumar K.V goto retry; 24162acf2c26SAneesh Kumar K.V } 241761628a3fSMingming Cao 241822208dedSAneesh Kumar K.V /* Update index */ 24192acf2c26SAneesh Kumar K.V wbc->range_cyclic = range_cyclic; 242022208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 242122208dedSAneesh Kumar K.V /* 242222208dedSAneesh Kumar K.V * set the writeback_index so that range_cyclic 242322208dedSAneesh Kumar K.V * mode will write it back later 242422208dedSAneesh Kumar K.V */ 242572f84e65SEric Sandeen mapping->writeback_index = done_index; 2426a1d6cc56SAneesh Kumar K.V 242761628a3fSMingming Cao out_writepages: 242822208dedSAneesh Kumar K.V wbc->nr_to_write -= nr_to_writebump; 2429de89de6eSTheodore Ts'o wbc->range_start = range_start; 24309bffad1eSTheodore Ts'o trace_ext4_da_writepages_result(inode, wbc, ret, pages_written); 243161628a3fSMingming Cao return ret; 243264769240SAlex Tomas } 243364769240SAlex Tomas 243479f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 243579f0be8dSAneesh Kumar K.V { 243679f0be8dSAneesh Kumar K.V s64 free_blocks, dirty_blocks; 243779f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 243879f0be8dSAneesh Kumar K.V 243979f0be8dSAneesh Kumar K.V /* 244079f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 244179f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 2442179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 244379f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 244479f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 244579f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 244679f0be8dSAneesh Kumar K.V */ 244757042651STheodore Ts'o free_blocks = EXT4_C2B(sbi, 244857042651STheodore Ts'o percpu_counter_read_positive(&sbi->s_freeclusters_counter)); 244957042651STheodore Ts'o dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyclusters_counter); 245000d4e736STheodore Ts'o /* 245100d4e736STheodore Ts'o * Start pushing delalloc when 1/2 of free blocks are dirty. 245200d4e736STheodore Ts'o */ 245300d4e736STheodore Ts'o if (dirty_blocks && (free_blocks < 2 * dirty_blocks) && 245400d4e736STheodore Ts'o !writeback_in_progress(sb->s_bdi) && 245500d4e736STheodore Ts'o down_read_trylock(&sb->s_umount)) { 245600d4e736STheodore Ts'o writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE); 245700d4e736STheodore Ts'o up_read(&sb->s_umount); 245800d4e736STheodore Ts'o } 245900d4e736STheodore Ts'o 246079f0be8dSAneesh Kumar K.V if (2 * free_blocks < 3 * dirty_blocks || 2461df55c99dSTheodore Ts'o free_blocks < (dirty_blocks + EXT4_FREECLUSTERS_WATERMARK)) { 246279f0be8dSAneesh Kumar K.V /* 2463c8afb446SEric Sandeen * free block count is less than 150% of dirty blocks 2464c8afb446SEric Sandeen * or free blocks is less than watermark 246579f0be8dSAneesh Kumar K.V */ 246679f0be8dSAneesh Kumar K.V return 1; 246779f0be8dSAneesh Kumar K.V } 246879f0be8dSAneesh Kumar K.V return 0; 246979f0be8dSAneesh Kumar K.V } 247079f0be8dSAneesh Kumar K.V 247164769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 247264769240SAlex Tomas loff_t pos, unsigned len, unsigned flags, 247364769240SAlex Tomas struct page **pagep, void **fsdata) 247464769240SAlex Tomas { 247572b8ab9dSEric Sandeen int ret, retries = 0; 247664769240SAlex Tomas struct page *page; 247764769240SAlex Tomas pgoff_t index; 247864769240SAlex Tomas struct inode *inode = mapping->host; 247964769240SAlex Tomas handle_t *handle; 248064769240SAlex Tomas 248164769240SAlex Tomas index = pos >> PAGE_CACHE_SHIFT; 248279f0be8dSAneesh Kumar K.V 248379f0be8dSAneesh Kumar K.V if (ext4_nonda_switch(inode->i_sb)) { 248479f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 248579f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 248679f0be8dSAneesh Kumar K.V len, flags, pagep, fsdata); 248779f0be8dSAneesh Kumar K.V } 248879f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 24899bffad1eSTheodore Ts'o trace_ext4_da_write_begin(inode, pos, len, flags); 24909c3569b5STao Ma 24919c3569b5STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 24929c3569b5STao Ma ret = ext4_da_write_inline_data_begin(mapping, inode, 24939c3569b5STao Ma pos, len, flags, 24949c3569b5STao Ma pagep, fsdata); 24959c3569b5STao Ma if (ret < 0) 249647564bfbSTheodore Ts'o return ret; 249747564bfbSTheodore Ts'o if (ret == 1) 249847564bfbSTheodore Ts'o return 0; 24999c3569b5STao Ma } 25009c3569b5STao Ma 250147564bfbSTheodore Ts'o /* 250247564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 250347564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 250447564bfbSTheodore Ts'o * is being written back. So grab it first before we start 250547564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 250647564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 250747564bfbSTheodore Ts'o */ 250847564bfbSTheodore Ts'o retry_grab: 250947564bfbSTheodore Ts'o page = grab_cache_page_write_begin(mapping, index, flags); 251047564bfbSTheodore Ts'o if (!page) 251147564bfbSTheodore Ts'o return -ENOMEM; 251247564bfbSTheodore Ts'o unlock_page(page); 251347564bfbSTheodore Ts'o 251464769240SAlex Tomas /* 251564769240SAlex Tomas * With delayed allocation, we don't log the i_disksize update 251664769240SAlex Tomas * if there is delayed block allocation. But we still need 251764769240SAlex Tomas * to journalling the i_disksize update if writes to the end 251864769240SAlex Tomas * of file which has an already mapped buffer. 251964769240SAlex Tomas */ 252047564bfbSTheodore Ts'o retry_journal: 25219924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 1); 252264769240SAlex Tomas if (IS_ERR(handle)) { 252347564bfbSTheodore Ts'o page_cache_release(page); 252447564bfbSTheodore Ts'o return PTR_ERR(handle); 252564769240SAlex Tomas } 252664769240SAlex Tomas 252747564bfbSTheodore Ts'o lock_page(page); 252847564bfbSTheodore Ts'o if (page->mapping != mapping) { 252947564bfbSTheodore Ts'o /* The page got truncated from under us */ 253047564bfbSTheodore Ts'o unlock_page(page); 253147564bfbSTheodore Ts'o page_cache_release(page); 2532d5a0d4f7SEric Sandeen ext4_journal_stop(handle); 253347564bfbSTheodore Ts'o goto retry_grab; 2534d5a0d4f7SEric Sandeen } 253547564bfbSTheodore Ts'o /* In case writeback began while the page was unlocked */ 253647564bfbSTheodore Ts'o wait_on_page_writeback(page); 253764769240SAlex Tomas 25386e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep); 253964769240SAlex Tomas if (ret < 0) { 254064769240SAlex Tomas unlock_page(page); 254164769240SAlex Tomas ext4_journal_stop(handle); 2542ae4d5372SAneesh Kumar K.V /* 2543ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 2544ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 2545ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 2546ae4d5372SAneesh Kumar K.V */ 2547ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 2548b9a4207dSJan Kara ext4_truncate_failed_write(inode); 254947564bfbSTheodore Ts'o 255047564bfbSTheodore Ts'o if (ret == -ENOSPC && 255147564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 255247564bfbSTheodore Ts'o goto retry_journal; 255347564bfbSTheodore Ts'o 255447564bfbSTheodore Ts'o page_cache_release(page); 255547564bfbSTheodore Ts'o return ret; 255664769240SAlex Tomas } 255764769240SAlex Tomas 255847564bfbSTheodore Ts'o *pagep = page; 255964769240SAlex Tomas return ret; 256064769240SAlex Tomas } 256164769240SAlex Tomas 2562632eaeabSMingming Cao /* 2563632eaeabSMingming Cao * Check if we should update i_disksize 2564632eaeabSMingming Cao * when write to the end of file but not require block allocation 2565632eaeabSMingming Cao */ 2566632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page, 2567632eaeabSMingming Cao unsigned long offset) 2568632eaeabSMingming Cao { 2569632eaeabSMingming Cao struct buffer_head *bh; 2570632eaeabSMingming Cao struct inode *inode = page->mapping->host; 2571632eaeabSMingming Cao unsigned int idx; 2572632eaeabSMingming Cao int i; 2573632eaeabSMingming Cao 2574632eaeabSMingming Cao bh = page_buffers(page); 2575632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 2576632eaeabSMingming Cao 2577632eaeabSMingming Cao for (i = 0; i < idx; i++) 2578632eaeabSMingming Cao bh = bh->b_this_page; 2579632eaeabSMingming Cao 258029fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 2581632eaeabSMingming Cao return 0; 2582632eaeabSMingming Cao return 1; 2583632eaeabSMingming Cao } 2584632eaeabSMingming Cao 258564769240SAlex Tomas static int ext4_da_write_end(struct file *file, 258664769240SAlex Tomas struct address_space *mapping, 258764769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 258864769240SAlex Tomas struct page *page, void *fsdata) 258964769240SAlex Tomas { 259064769240SAlex Tomas struct inode *inode = mapping->host; 259164769240SAlex Tomas int ret = 0, ret2; 259264769240SAlex Tomas handle_t *handle = ext4_journal_current_handle(); 259364769240SAlex Tomas loff_t new_i_size; 2594632eaeabSMingming Cao unsigned long start, end; 259579f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 259679f0be8dSAneesh Kumar K.V 259779f0be8dSAneesh Kumar K.V if (write_mode == FALL_BACK_TO_NONDELALLOC) { 25983d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 25993d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 260079f0be8dSAneesh Kumar K.V return ext4_ordered_write_end(file, mapping, pos, 260179f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 26023d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 260379f0be8dSAneesh Kumar K.V return ext4_writeback_write_end(file, mapping, pos, 260479f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 26053d2b1582SLukas Czerner default: 260679f0be8dSAneesh Kumar K.V BUG(); 260779f0be8dSAneesh Kumar K.V } 260879f0be8dSAneesh Kumar K.V } 2609632eaeabSMingming Cao 26109bffad1eSTheodore Ts'o trace_ext4_da_write_end(inode, pos, len, copied); 2611632eaeabSMingming Cao start = pos & (PAGE_CACHE_SIZE - 1); 2612632eaeabSMingming Cao end = start + copied - 1; 261364769240SAlex Tomas 261464769240SAlex Tomas /* 261564769240SAlex Tomas * generic_write_end() will run mark_inode_dirty() if i_size 261664769240SAlex Tomas * changes. So let's piggyback the i_disksize mark_inode_dirty 261764769240SAlex Tomas * into that. 261864769240SAlex Tomas */ 261964769240SAlex Tomas new_i_size = pos + copied; 2620ea51d132SAndrea Arcangeli if (copied && new_i_size > EXT4_I(inode)->i_disksize) { 26219c3569b5STao Ma if (ext4_has_inline_data(inode) || 26229c3569b5STao Ma ext4_da_should_update_i_disksize(page, end)) { 2623632eaeabSMingming Cao down_write(&EXT4_I(inode)->i_data_sem); 2624f3b59291STheodore Ts'o if (new_i_size > EXT4_I(inode)->i_disksize) 262564769240SAlex Tomas EXT4_I(inode)->i_disksize = new_i_size; 2626632eaeabSMingming Cao up_write(&EXT4_I(inode)->i_data_sem); 2627cf17fea6SAneesh Kumar K.V /* We need to mark inode dirty even if 2628cf17fea6SAneesh Kumar K.V * new_i_size is less that inode->i_size 2629cf17fea6SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 2630cf17fea6SAneesh Kumar K.V */ 2631cf17fea6SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 2632632eaeabSMingming Cao } 2633632eaeabSMingming Cao } 26349c3569b5STao Ma 26359c3569b5STao Ma if (write_mode != CONVERT_INLINE_DATA && 26369c3569b5STao Ma ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) && 26379c3569b5STao Ma ext4_has_inline_data(inode)) 26389c3569b5STao Ma ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied, 26399c3569b5STao Ma page); 26409c3569b5STao Ma else 264164769240SAlex Tomas ret2 = generic_write_end(file, mapping, pos, len, copied, 264264769240SAlex Tomas page, fsdata); 26439c3569b5STao Ma 264464769240SAlex Tomas copied = ret2; 264564769240SAlex Tomas if (ret2 < 0) 264664769240SAlex Tomas ret = ret2; 264764769240SAlex Tomas ret2 = ext4_journal_stop(handle); 264864769240SAlex Tomas if (!ret) 264964769240SAlex Tomas ret = ret2; 265064769240SAlex Tomas 265164769240SAlex Tomas return ret ? ret : copied; 265264769240SAlex Tomas } 265364769240SAlex Tomas 265464769240SAlex Tomas static void ext4_da_invalidatepage(struct page *page, unsigned long offset) 265564769240SAlex Tomas { 265664769240SAlex Tomas /* 265764769240SAlex Tomas * Drop reserved blocks 265864769240SAlex Tomas */ 265964769240SAlex Tomas BUG_ON(!PageLocked(page)); 266064769240SAlex Tomas if (!page_has_buffers(page)) 266164769240SAlex Tomas goto out; 266264769240SAlex Tomas 2663d2a17637SMingming Cao ext4_da_page_release_reservation(page, offset); 266464769240SAlex Tomas 266564769240SAlex Tomas out: 266664769240SAlex Tomas ext4_invalidatepage(page, offset); 266764769240SAlex Tomas 266864769240SAlex Tomas return; 266964769240SAlex Tomas } 267064769240SAlex Tomas 2671ccd2506bSTheodore Ts'o /* 2672ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 2673ccd2506bSTheodore Ts'o */ 2674ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 2675ccd2506bSTheodore Ts'o { 2676fb40ba0dSTheodore Ts'o trace_ext4_alloc_da_blocks(inode); 2677fb40ba0dSTheodore Ts'o 2678ccd2506bSTheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks && 2679ccd2506bSTheodore Ts'o !EXT4_I(inode)->i_reserved_meta_blocks) 2680ccd2506bSTheodore Ts'o return 0; 2681ccd2506bSTheodore Ts'o 2682ccd2506bSTheodore Ts'o /* 2683ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 2684ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 2685ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 2686ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 2687ccd2506bSTheodore Ts'o * would require replicating code paths in: 2688ccd2506bSTheodore Ts'o * 2689ccd2506bSTheodore Ts'o * ext4_da_writepages() -> 2690ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 2691ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 2692ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 2693ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 2694ccd2506bSTheodore Ts'o * 2695ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 2696ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 2697ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 2698ccd2506bSTheodore Ts'o * doing I/O at all. 2699ccd2506bSTheodore Ts'o * 2700ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 2701380cf090SWu Fengguang * the pages by calling redirty_page_for_writepage() but that 2702ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 2703ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 270425985edcSLucas De Marchi * simplifying them because we wouldn't actually intend to 2705ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 2706ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 2707ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 2708ccd2506bSTheodore Ts'o * 2709ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 2710ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 2711ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 2712ccd2506bSTheodore Ts'o */ 2713ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 2714ccd2506bSTheodore Ts'o } 271564769240SAlex Tomas 271664769240SAlex Tomas /* 2717ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 2718ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 2719ac27a0ecSDave Kleikamp * 2720ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 2721617ba13bSMingming Cao * journal. If somebody makes a swapfile on an ext4 data-journaling 2722ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 2723ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 2724ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 2725ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 2726ac27a0ecSDave Kleikamp * 2727ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 2728ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 2729ac27a0ecSDave Kleikamp */ 2730617ba13bSMingming Cao static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 2731ac27a0ecSDave Kleikamp { 2732ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 2733ac27a0ecSDave Kleikamp journal_t *journal; 2734ac27a0ecSDave Kleikamp int err; 2735ac27a0ecSDave Kleikamp 273646c7f254STao Ma /* 273746c7f254STao Ma * We can get here for an inline file via the FIBMAP ioctl 273846c7f254STao Ma */ 273946c7f254STao Ma if (ext4_has_inline_data(inode)) 274046c7f254STao Ma return 0; 274146c7f254STao Ma 274264769240SAlex Tomas if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 274364769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) { 274464769240SAlex Tomas /* 274564769240SAlex Tomas * With delalloc we want to sync the file 274664769240SAlex Tomas * so that we can make sure we allocate 274764769240SAlex Tomas * blocks for file 274864769240SAlex Tomas */ 274964769240SAlex Tomas filemap_write_and_wait(mapping); 275064769240SAlex Tomas } 275164769240SAlex Tomas 275219f5fb7aSTheodore Ts'o if (EXT4_JOURNAL(inode) && 275319f5fb7aSTheodore Ts'o ext4_test_inode_state(inode, EXT4_STATE_JDATA)) { 2754ac27a0ecSDave Kleikamp /* 2755ac27a0ecSDave Kleikamp * This is a REALLY heavyweight approach, but the use of 2756ac27a0ecSDave Kleikamp * bmap on dirty files is expected to be extremely rare: 2757ac27a0ecSDave Kleikamp * only if we run lilo or swapon on a freshly made file 2758ac27a0ecSDave Kleikamp * do we expect this to happen. 2759ac27a0ecSDave Kleikamp * 2760ac27a0ecSDave Kleikamp * (bmap requires CAP_SYS_RAWIO so this does not 2761ac27a0ecSDave Kleikamp * represent an unprivileged user DOS attack --- we'd be 2762ac27a0ecSDave Kleikamp * in trouble if mortal users could trigger this path at 2763ac27a0ecSDave Kleikamp * will.) 2764ac27a0ecSDave Kleikamp * 2765617ba13bSMingming Cao * NB. EXT4_STATE_JDATA is not set on files other than 2766ac27a0ecSDave Kleikamp * regular files. If somebody wants to bmap a directory 2767ac27a0ecSDave Kleikamp * or symlink and gets confused because the buffer 2768ac27a0ecSDave Kleikamp * hasn't yet been flushed to disk, they deserve 2769ac27a0ecSDave Kleikamp * everything they get. 2770ac27a0ecSDave Kleikamp */ 2771ac27a0ecSDave Kleikamp 277219f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_JDATA); 2773617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 2774dab291afSMingming Cao jbd2_journal_lock_updates(journal); 2775dab291afSMingming Cao err = jbd2_journal_flush(journal); 2776dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 2777ac27a0ecSDave Kleikamp 2778ac27a0ecSDave Kleikamp if (err) 2779ac27a0ecSDave Kleikamp return 0; 2780ac27a0ecSDave Kleikamp } 2781ac27a0ecSDave Kleikamp 2782617ba13bSMingming Cao return generic_block_bmap(mapping, block, ext4_get_block); 2783ac27a0ecSDave Kleikamp } 2784ac27a0ecSDave Kleikamp 2785617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page) 2786ac27a0ecSDave Kleikamp { 278746c7f254STao Ma int ret = -EAGAIN; 278846c7f254STao Ma struct inode *inode = page->mapping->host; 278946c7f254STao Ma 27900562e0baSJiaying Zhang trace_ext4_readpage(page); 279146c7f254STao Ma 279246c7f254STao Ma if (ext4_has_inline_data(inode)) 279346c7f254STao Ma ret = ext4_readpage_inline(inode, page); 279446c7f254STao Ma 279546c7f254STao Ma if (ret == -EAGAIN) 2796617ba13bSMingming Cao return mpage_readpage(page, ext4_get_block); 279746c7f254STao Ma 279846c7f254STao Ma return ret; 2799ac27a0ecSDave Kleikamp } 2800ac27a0ecSDave Kleikamp 2801ac27a0ecSDave Kleikamp static int 2802617ba13bSMingming Cao ext4_readpages(struct file *file, struct address_space *mapping, 2803ac27a0ecSDave Kleikamp struct list_head *pages, unsigned nr_pages) 2804ac27a0ecSDave Kleikamp { 280546c7f254STao Ma struct inode *inode = mapping->host; 280646c7f254STao Ma 280746c7f254STao Ma /* If the file has inline data, no need to do readpages. */ 280846c7f254STao Ma if (ext4_has_inline_data(inode)) 280946c7f254STao Ma return 0; 281046c7f254STao Ma 2811617ba13bSMingming Cao return mpage_readpages(mapping, pages, nr_pages, ext4_get_block); 2812ac27a0ecSDave Kleikamp } 2813ac27a0ecSDave Kleikamp 2814617ba13bSMingming Cao static void ext4_invalidatepage(struct page *page, unsigned long offset) 2815ac27a0ecSDave Kleikamp { 28160562e0baSJiaying Zhang trace_ext4_invalidatepage(page, offset); 28170562e0baSJiaying Zhang 28184520fb3cSJan Kara /* No journalling happens on data buffers when this function is used */ 28194520fb3cSJan Kara WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page))); 28204520fb3cSJan Kara 28214520fb3cSJan Kara block_invalidatepage(page, offset); 28224520fb3cSJan Kara } 28234520fb3cSJan Kara 282453e87268SJan Kara static int __ext4_journalled_invalidatepage(struct page *page, 28254520fb3cSJan Kara unsigned long offset) 28264520fb3cSJan Kara { 28274520fb3cSJan Kara journal_t *journal = EXT4_JOURNAL(page->mapping->host); 28284520fb3cSJan Kara 28294520fb3cSJan Kara trace_ext4_journalled_invalidatepage(page, offset); 28304520fb3cSJan Kara 2831744692dcSJiaying Zhang /* 2832ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 2833ac27a0ecSDave Kleikamp */ 2834ac27a0ecSDave Kleikamp if (offset == 0) 2835ac27a0ecSDave Kleikamp ClearPageChecked(page); 2836ac27a0ecSDave Kleikamp 283753e87268SJan Kara return jbd2_journal_invalidatepage(journal, page, offset); 283853e87268SJan Kara } 283953e87268SJan Kara 284053e87268SJan Kara /* Wrapper for aops... */ 284153e87268SJan Kara static void ext4_journalled_invalidatepage(struct page *page, 284253e87268SJan Kara unsigned long offset) 284353e87268SJan Kara { 284453e87268SJan Kara WARN_ON(__ext4_journalled_invalidatepage(page, offset) < 0); 2845ac27a0ecSDave Kleikamp } 2846ac27a0ecSDave Kleikamp 2847617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait) 2848ac27a0ecSDave Kleikamp { 2849617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 2850ac27a0ecSDave Kleikamp 28510562e0baSJiaying Zhang trace_ext4_releasepage(page); 28520562e0baSJiaying Zhang 2853ac27a0ecSDave Kleikamp WARN_ON(PageChecked(page)); 2854ac27a0ecSDave Kleikamp if (!page_has_buffers(page)) 2855ac27a0ecSDave Kleikamp return 0; 28560390131bSFrank Mayhar if (journal) 2857dab291afSMingming Cao return jbd2_journal_try_to_free_buffers(journal, page, wait); 28580390131bSFrank Mayhar else 28590390131bSFrank Mayhar return try_to_free_buffers(page); 2860ac27a0ecSDave Kleikamp } 2861ac27a0ecSDave Kleikamp 2862ac27a0ecSDave Kleikamp /* 28632ed88685STheodore Ts'o * ext4_get_block used when preparing for a DIO write or buffer write. 28642ed88685STheodore Ts'o * We allocate an uinitialized extent if blocks haven't been allocated. 28652ed88685STheodore Ts'o * The extent will be converted to initialized after the IO is complete. 28662ed88685STheodore Ts'o */ 2867f19d5870STao Ma int ext4_get_block_write(struct inode *inode, sector_t iblock, 28684c0425ffSMingming Cao struct buffer_head *bh_result, int create) 28694c0425ffSMingming Cao { 2870c7064ef1SJiaying Zhang ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n", 28718d5d02e6SMingming Cao inode->i_ino, create); 28722ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh_result, 28732ed88685STheodore Ts'o EXT4_GET_BLOCKS_IO_CREATE_EXT); 28744c0425ffSMingming Cao } 28754c0425ffSMingming Cao 2876729f52c6SZheng Liu static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock, 28778b0f165fSAnatol Pomozov struct buffer_head *bh_result, int create) 2878729f52c6SZheng Liu { 28798b0f165fSAnatol Pomozov ext4_debug("ext4_get_block_write_nolock: inode %lu, create flag %d\n", 28808b0f165fSAnatol Pomozov inode->i_ino, create); 28818b0f165fSAnatol Pomozov return _ext4_get_block(inode, iblock, bh_result, 28828b0f165fSAnatol Pomozov EXT4_GET_BLOCKS_NO_LOCK); 2883729f52c6SZheng Liu } 2884729f52c6SZheng Liu 28854c0425ffSMingming Cao static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset, 2886552ef802SChristoph Hellwig ssize_t size, void *private, int ret, 2887552ef802SChristoph Hellwig bool is_async) 28884c0425ffSMingming Cao { 288972c5052dSChristoph Hellwig struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode; 28904c0425ffSMingming Cao ext4_io_end_t *io_end = iocb->private; 28914c0425ffSMingming Cao 28924b70df18SMingming /* if not async direct IO or dio with 0 bytes write, just return */ 28934b70df18SMingming if (!io_end || !size) 2894552ef802SChristoph Hellwig goto out; 28954b70df18SMingming 28968d5d02e6SMingming Cao ext_debug("ext4_end_io_dio(): io_end 0x%p " 2897ace36ad4SJoe Perches "for inode %lu, iocb 0x%p, offset %llu, size %zd\n", 28988d5d02e6SMingming Cao iocb->private, io_end->inode->i_ino, iocb, offset, 28998d5d02e6SMingming Cao size); 29008d5d02e6SMingming Cao 2901b5a7e970STheodore Ts'o iocb->private = NULL; 2902b5a7e970STheodore Ts'o 29038d5d02e6SMingming Cao /* if not aio dio with unwritten extents, just free io and return */ 2904bd2d0210STheodore Ts'o if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) { 29058d5d02e6SMingming Cao ext4_free_io_end(io_end); 29065b3ff237Sjiayingz@google.com (Jiaying Zhang) out: 2907091e26dfSJan Kara inode_dio_done(inode); 29085b3ff237Sjiayingz@google.com (Jiaying Zhang) if (is_async) 29095b3ff237Sjiayingz@google.com (Jiaying Zhang) aio_complete(iocb, ret, 0); 29105b3ff237Sjiayingz@google.com (Jiaying Zhang) return; 29118d5d02e6SMingming Cao } 29128d5d02e6SMingming Cao 29134c0425ffSMingming Cao io_end->offset = offset; 29144c0425ffSMingming Cao io_end->size = size; 29155b3ff237Sjiayingz@google.com (Jiaying Zhang) if (is_async) { 29165b3ff237Sjiayingz@google.com (Jiaying Zhang) io_end->iocb = iocb; 29175b3ff237Sjiayingz@google.com (Jiaying Zhang) io_end->result = ret; 29185b3ff237Sjiayingz@google.com (Jiaying Zhang) } 29194c0425ffSMingming Cao 292028a535f9SDmitry Monakhov ext4_add_complete_io(io_end); 29214c0425ffSMingming Cao } 2922c7064ef1SJiaying Zhang 29234c0425ffSMingming Cao /* 29244c0425ffSMingming Cao * For ext4 extent files, ext4 will do direct-io write to holes, 29254c0425ffSMingming Cao * preallocated extents, and those write extend the file, no need to 29264c0425ffSMingming Cao * fall back to buffered IO. 29274c0425ffSMingming Cao * 2928b595076aSUwe Kleine-König * For holes, we fallocate those blocks, mark them as uninitialized 292969c499d1STheodore Ts'o * If those blocks were preallocated, we mark sure they are split, but 2930b595076aSUwe Kleine-König * still keep the range to write as uninitialized. 29314c0425ffSMingming Cao * 293269c499d1STheodore Ts'o * The unwritten extents will be converted to written when DIO is completed. 29338d5d02e6SMingming Cao * For async direct IO, since the IO may still pending when return, we 293425985edcSLucas De Marchi * set up an end_io call back function, which will do the conversion 29358d5d02e6SMingming Cao * when async direct IO completed. 29364c0425ffSMingming Cao * 29374c0425ffSMingming Cao * If the O_DIRECT write will extend the file then add this inode to the 29384c0425ffSMingming Cao * orphan list. So recovery will truncate it back to the original size 29394c0425ffSMingming Cao * if the machine crashes during the write. 29404c0425ffSMingming Cao * 29414c0425ffSMingming Cao */ 29424c0425ffSMingming Cao static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb, 29434c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 29444c0425ffSMingming Cao unsigned long nr_segs) 29454c0425ffSMingming Cao { 29464c0425ffSMingming Cao struct file *file = iocb->ki_filp; 29474c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 29484c0425ffSMingming Cao ssize_t ret; 29494c0425ffSMingming Cao size_t count = iov_length(iov, nr_segs); 2950729f52c6SZheng Liu int overwrite = 0; 29518b0f165fSAnatol Pomozov get_block_t *get_block_func = NULL; 29528b0f165fSAnatol Pomozov int dio_flags = 0; 295369c499d1STheodore Ts'o loff_t final_size = offset + count; 295469c499d1STheodore Ts'o 295569c499d1STheodore Ts'o /* Use the old path for reads and writes beyond i_size. */ 295669c499d1STheodore Ts'o if (rw != WRITE || final_size > inode->i_size) 295769c499d1STheodore Ts'o return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 2958729f52c6SZheng Liu 29594bd809dbSZheng Liu BUG_ON(iocb->private == NULL); 29604bd809dbSZheng Liu 29614bd809dbSZheng Liu /* If we do a overwrite dio, i_mutex locking can be released */ 29624bd809dbSZheng Liu overwrite = *((int *)iocb->private); 29634bd809dbSZheng Liu 29644bd809dbSZheng Liu if (overwrite) { 29651f555cfaSDmitry Monakhov atomic_inc(&inode->i_dio_count); 29664bd809dbSZheng Liu down_read(&EXT4_I(inode)->i_data_sem); 29674bd809dbSZheng Liu mutex_unlock(&inode->i_mutex); 29684bd809dbSZheng Liu } 29694bd809dbSZheng Liu 29704c0425ffSMingming Cao /* 29718d5d02e6SMingming Cao * We could direct write to holes and fallocate. 29728d5d02e6SMingming Cao * 297369c499d1STheodore Ts'o * Allocated blocks to fill the hole are marked as 297469c499d1STheodore Ts'o * uninitialized to prevent parallel buffered read to expose 297569c499d1STheodore Ts'o * the stale data before DIO complete the data IO. 29768d5d02e6SMingming Cao * 297769c499d1STheodore Ts'o * As to previously fallocated extents, ext4 get_block will 297869c499d1STheodore Ts'o * just simply mark the buffer mapped but still keep the 297969c499d1STheodore Ts'o * extents uninitialized. 29804c0425ffSMingming Cao * 298169c499d1STheodore Ts'o * For non AIO case, we will convert those unwritten extents 29828d5d02e6SMingming Cao * to written after return back from blockdev_direct_IO. 29834c0425ffSMingming Cao * 298469c499d1STheodore Ts'o * For async DIO, the conversion needs to be deferred when the 298569c499d1STheodore Ts'o * IO is completed. The ext4 end_io callback function will be 298669c499d1STheodore Ts'o * called to take care of the conversion work. Here for async 298769c499d1STheodore Ts'o * case, we allocate an io_end structure to hook to the iocb. 29884c0425ffSMingming Cao */ 29898d5d02e6SMingming Cao iocb->private = NULL; 2990f45ee3a1SDmitry Monakhov ext4_inode_aio_set(inode, NULL); 29918d5d02e6SMingming Cao if (!is_sync_kiocb(iocb)) { 299269c499d1STheodore Ts'o ext4_io_end_t *io_end = ext4_init_io_end(inode, GFP_NOFS); 29934bd809dbSZheng Liu if (!io_end) { 29944bd809dbSZheng Liu ret = -ENOMEM; 29954bd809dbSZheng Liu goto retake_lock; 29964bd809dbSZheng Liu } 2997266991b1SJeff Moyer io_end->flag |= EXT4_IO_END_DIRECT; 2998266991b1SJeff Moyer iocb->private = io_end; 29998d5d02e6SMingming Cao /* 300069c499d1STheodore Ts'o * we save the io structure for current async direct 300169c499d1STheodore Ts'o * IO, so that later ext4_map_blocks() could flag the 300269c499d1STheodore Ts'o * io structure whether there is a unwritten extents 300369c499d1STheodore Ts'o * needs to be converted when IO is completed. 30048d5d02e6SMingming Cao */ 3005f45ee3a1SDmitry Monakhov ext4_inode_aio_set(inode, io_end); 30068d5d02e6SMingming Cao } 30078d5d02e6SMingming Cao 30088b0f165fSAnatol Pomozov if (overwrite) { 30098b0f165fSAnatol Pomozov get_block_func = ext4_get_block_write_nolock; 30108b0f165fSAnatol Pomozov } else { 30118b0f165fSAnatol Pomozov get_block_func = ext4_get_block_write; 30128b0f165fSAnatol Pomozov dio_flags = DIO_LOCKING; 30138b0f165fSAnatol Pomozov } 3014729f52c6SZheng Liu ret = __blockdev_direct_IO(rw, iocb, inode, 3015729f52c6SZheng Liu inode->i_sb->s_bdev, iov, 3016729f52c6SZheng Liu offset, nr_segs, 30178b0f165fSAnatol Pomozov get_block_func, 3018729f52c6SZheng Liu ext4_end_io_dio, 3019729f52c6SZheng Liu NULL, 30208b0f165fSAnatol Pomozov dio_flags); 30218b0f165fSAnatol Pomozov 30228d5d02e6SMingming Cao if (iocb->private) 3023f45ee3a1SDmitry Monakhov ext4_inode_aio_set(inode, NULL); 30248d5d02e6SMingming Cao /* 302569c499d1STheodore Ts'o * The io_end structure takes a reference to the inode, that 302669c499d1STheodore Ts'o * structure needs to be destroyed and the reference to the 302769c499d1STheodore Ts'o * inode need to be dropped, when IO is complete, even with 0 302869c499d1STheodore Ts'o * byte write, or failed. 30298d5d02e6SMingming Cao * 303069c499d1STheodore Ts'o * In the successful AIO DIO case, the io_end structure will 303169c499d1STheodore Ts'o * be destroyed and the reference to the inode will be dropped 30328d5d02e6SMingming Cao * after the end_io call back function is called. 30338d5d02e6SMingming Cao * 303469c499d1STheodore Ts'o * In the case there is 0 byte write, or error case, since VFS 303569c499d1STheodore Ts'o * direct IO won't invoke the end_io call back function, we 303669c499d1STheodore Ts'o * need to free the end_io structure here. 30378d5d02e6SMingming Cao */ 30388d5d02e6SMingming Cao if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) { 30398d5d02e6SMingming Cao ext4_free_io_end(iocb->private); 30408d5d02e6SMingming Cao iocb->private = NULL; 3041729f52c6SZheng Liu } else if (ret > 0 && !overwrite && ext4_test_inode_state(inode, 30425f524950SMingming EXT4_STATE_DIO_UNWRITTEN)) { 3043109f5565SMingming int err; 30448d5d02e6SMingming Cao /* 30458d5d02e6SMingming Cao * for non AIO case, since the IO is already 304625985edcSLucas De Marchi * completed, we could do the conversion right here 30478d5d02e6SMingming Cao */ 3048109f5565SMingming err = ext4_convert_unwritten_extents(inode, 30498d5d02e6SMingming Cao offset, ret); 3050109f5565SMingming if (err < 0) 3051109f5565SMingming ret = err; 305219f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN); 3053109f5565SMingming } 30544bd809dbSZheng Liu 30554bd809dbSZheng Liu retake_lock: 30564bd809dbSZheng Liu /* take i_mutex locking again if we do a ovewrite dio */ 30574bd809dbSZheng Liu if (overwrite) { 30581f555cfaSDmitry Monakhov inode_dio_done(inode); 30594bd809dbSZheng Liu up_read(&EXT4_I(inode)->i_data_sem); 30604bd809dbSZheng Liu mutex_lock(&inode->i_mutex); 30614bd809dbSZheng Liu } 30624bd809dbSZheng Liu 30634c0425ffSMingming Cao return ret; 30644c0425ffSMingming Cao } 30658d5d02e6SMingming Cao 30664c0425ffSMingming Cao static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb, 30674c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 30684c0425ffSMingming Cao unsigned long nr_segs) 30694c0425ffSMingming Cao { 30704c0425ffSMingming Cao struct file *file = iocb->ki_filp; 30714c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 30720562e0baSJiaying Zhang ssize_t ret; 30734c0425ffSMingming Cao 307484ebd795STheodore Ts'o /* 307584ebd795STheodore Ts'o * If we are doing data journalling we don't support O_DIRECT 307684ebd795STheodore Ts'o */ 307784ebd795STheodore Ts'o if (ext4_should_journal_data(inode)) 307884ebd795STheodore Ts'o return 0; 307984ebd795STheodore Ts'o 308046c7f254STao Ma /* Let buffer I/O handle the inline data case. */ 308146c7f254STao Ma if (ext4_has_inline_data(inode)) 308246c7f254STao Ma return 0; 308346c7f254STao Ma 30840562e0baSJiaying Zhang trace_ext4_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw); 308512e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 30860562e0baSJiaying Zhang ret = ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs); 30870562e0baSJiaying Zhang else 30880562e0baSJiaying Zhang ret = ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 30890562e0baSJiaying Zhang trace_ext4_direct_IO_exit(inode, offset, 30900562e0baSJiaying Zhang iov_length(iov, nr_segs), rw, ret); 30910562e0baSJiaying Zhang return ret; 30924c0425ffSMingming Cao } 30934c0425ffSMingming Cao 3094ac27a0ecSDave Kleikamp /* 3095617ba13bSMingming Cao * Pages can be marked dirty completely asynchronously from ext4's journalling 3096ac27a0ecSDave Kleikamp * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do 3097ac27a0ecSDave Kleikamp * much here because ->set_page_dirty is called under VFS locks. The page is 3098ac27a0ecSDave Kleikamp * not necessarily locked. 3099ac27a0ecSDave Kleikamp * 3100ac27a0ecSDave Kleikamp * We cannot just dirty the page and leave attached buffers clean, because the 3101ac27a0ecSDave Kleikamp * buffers' dirty state is "definitive". We cannot just set the buffers dirty 3102ac27a0ecSDave Kleikamp * or jbddirty because all the journalling code will explode. 3103ac27a0ecSDave Kleikamp * 3104ac27a0ecSDave Kleikamp * So what we do is to mark the page "pending dirty" and next time writepage 3105ac27a0ecSDave Kleikamp * is called, propagate that into the buffers appropriately. 3106ac27a0ecSDave Kleikamp */ 3107617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page) 3108ac27a0ecSDave Kleikamp { 3109ac27a0ecSDave Kleikamp SetPageChecked(page); 3110ac27a0ecSDave Kleikamp return __set_page_dirty_nobuffers(page); 3111ac27a0ecSDave Kleikamp } 3112ac27a0ecSDave Kleikamp 3113617ba13bSMingming Cao static const struct address_space_operations ext4_ordered_aops = { 3114617ba13bSMingming Cao .readpage = ext4_readpage, 3115617ba13bSMingming Cao .readpages = ext4_readpages, 311643ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3117bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3118bfc1af65SNick Piggin .write_end = ext4_ordered_write_end, 3119617ba13bSMingming Cao .bmap = ext4_bmap, 3120617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3121617ba13bSMingming Cao .releasepage = ext4_releasepage, 3122617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 3123ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 31248ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3125aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3126ac27a0ecSDave Kleikamp }; 3127ac27a0ecSDave Kleikamp 3128617ba13bSMingming Cao static const struct address_space_operations ext4_writeback_aops = { 3129617ba13bSMingming Cao .readpage = ext4_readpage, 3130617ba13bSMingming Cao .readpages = ext4_readpages, 313143ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3132bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3133bfc1af65SNick Piggin .write_end = ext4_writeback_write_end, 3134617ba13bSMingming Cao .bmap = ext4_bmap, 3135617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3136617ba13bSMingming Cao .releasepage = ext4_releasepage, 3137617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 3138ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 31398ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3140aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3141ac27a0ecSDave Kleikamp }; 3142ac27a0ecSDave Kleikamp 3143617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 3144617ba13bSMingming Cao .readpage = ext4_readpage, 3145617ba13bSMingming Cao .readpages = ext4_readpages, 314643ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3147bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3148bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 3149617ba13bSMingming Cao .set_page_dirty = ext4_journalled_set_page_dirty, 3150617ba13bSMingming Cao .bmap = ext4_bmap, 31514520fb3cSJan Kara .invalidatepage = ext4_journalled_invalidatepage, 3152617ba13bSMingming Cao .releasepage = ext4_releasepage, 315384ebd795STheodore Ts'o .direct_IO = ext4_direct_IO, 31548ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3155aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3156ac27a0ecSDave Kleikamp }; 3157ac27a0ecSDave Kleikamp 315864769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 315964769240SAlex Tomas .readpage = ext4_readpage, 316064769240SAlex Tomas .readpages = ext4_readpages, 316143ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 316264769240SAlex Tomas .writepages = ext4_da_writepages, 316364769240SAlex Tomas .write_begin = ext4_da_write_begin, 316464769240SAlex Tomas .write_end = ext4_da_write_end, 316564769240SAlex Tomas .bmap = ext4_bmap, 316664769240SAlex Tomas .invalidatepage = ext4_da_invalidatepage, 316764769240SAlex Tomas .releasepage = ext4_releasepage, 316864769240SAlex Tomas .direct_IO = ext4_direct_IO, 316964769240SAlex Tomas .migratepage = buffer_migrate_page, 31708ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3171aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 317264769240SAlex Tomas }; 317364769240SAlex Tomas 3174617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 3175ac27a0ecSDave Kleikamp { 31763d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 31773d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 31783d2b1582SLukas Czerner if (test_opt(inode->i_sb, DELALLOC)) 3179cd1aac32SAneesh Kumar K.V inode->i_mapping->a_ops = &ext4_da_aops; 3180ac27a0ecSDave Kleikamp else 31813d2b1582SLukas Czerner inode->i_mapping->a_ops = &ext4_ordered_aops; 31823d2b1582SLukas Czerner break; 31833d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 31843d2b1582SLukas Czerner if (test_opt(inode->i_sb, DELALLOC)) 31853d2b1582SLukas Czerner inode->i_mapping->a_ops = &ext4_da_aops; 31863d2b1582SLukas Czerner else 31873d2b1582SLukas Czerner inode->i_mapping->a_ops = &ext4_writeback_aops; 31883d2b1582SLukas Czerner break; 31893d2b1582SLukas Czerner case EXT4_INODE_JOURNAL_DATA_MODE: 3190617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 31913d2b1582SLukas Czerner break; 31923d2b1582SLukas Czerner default: 31933d2b1582SLukas Czerner BUG(); 31943d2b1582SLukas Czerner } 3195ac27a0ecSDave Kleikamp } 3196ac27a0ecSDave Kleikamp 31974e96b2dbSAllison Henderson 31984e96b2dbSAllison Henderson /* 31994e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers() 32004e96b2dbSAllison Henderson * Wrapper function for ext4_discard_partial_page_buffers_no_lock. 32014e96b2dbSAllison Henderson * This function finds and locks the page containing the offset 32024e96b2dbSAllison Henderson * "from" and passes it to ext4_discard_partial_page_buffers_no_lock. 32034e96b2dbSAllison Henderson * Calling functions that already have the page locked should call 32044e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers_no_lock directly. 32054e96b2dbSAllison Henderson */ 32064e96b2dbSAllison Henderson int ext4_discard_partial_page_buffers(handle_t *handle, 32074e96b2dbSAllison Henderson struct address_space *mapping, loff_t from, 32084e96b2dbSAllison Henderson loff_t length, int flags) 32094e96b2dbSAllison Henderson { 32104e96b2dbSAllison Henderson struct inode *inode = mapping->host; 32114e96b2dbSAllison Henderson struct page *page; 32124e96b2dbSAllison Henderson int err = 0; 32134e96b2dbSAllison Henderson 32144e96b2dbSAllison Henderson page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT, 32154e96b2dbSAllison Henderson mapping_gfp_mask(mapping) & ~__GFP_FS); 32164e96b2dbSAllison Henderson if (!page) 32175129d05fSYongqiang Yang return -ENOMEM; 32184e96b2dbSAllison Henderson 32194e96b2dbSAllison Henderson err = ext4_discard_partial_page_buffers_no_lock(handle, inode, page, 32204e96b2dbSAllison Henderson from, length, flags); 32214e96b2dbSAllison Henderson 32224e96b2dbSAllison Henderson unlock_page(page); 32234e96b2dbSAllison Henderson page_cache_release(page); 32244e96b2dbSAllison Henderson return err; 32254e96b2dbSAllison Henderson } 32264e96b2dbSAllison Henderson 32274e96b2dbSAllison Henderson /* 32284e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers_no_lock() 32294e96b2dbSAllison Henderson * Zeros a page range of length 'length' starting from offset 'from'. 32304e96b2dbSAllison Henderson * Buffer heads that correspond to the block aligned regions of the 32314e96b2dbSAllison Henderson * zeroed range will be unmapped. Unblock aligned regions 32324e96b2dbSAllison Henderson * will have the corresponding buffer head mapped if needed so that 32334e96b2dbSAllison Henderson * that region of the page can be updated with the partial zero out. 32344e96b2dbSAllison Henderson * 32354e96b2dbSAllison Henderson * This function assumes that the page has already been locked. The 32364e96b2dbSAllison Henderson * The range to be discarded must be contained with in the given page. 32374e96b2dbSAllison Henderson * If the specified range exceeds the end of the page it will be shortened 32384e96b2dbSAllison Henderson * to the end of the page that corresponds to 'from'. This function is 32394e96b2dbSAllison Henderson * appropriate for updating a page and it buffer heads to be unmapped and 32404e96b2dbSAllison Henderson * zeroed for blocks that have been either released, or are going to be 32414e96b2dbSAllison Henderson * released. 32424e96b2dbSAllison Henderson * 32434e96b2dbSAllison Henderson * handle: The journal handle 32444e96b2dbSAllison Henderson * inode: The files inode 32454e96b2dbSAllison Henderson * page: A locked page that contains the offset "from" 32464907cb7bSAnatol Pomozov * from: The starting byte offset (from the beginning of the file) 32474e96b2dbSAllison Henderson * to begin discarding 32484e96b2dbSAllison Henderson * len: The length of bytes to discard 32494e96b2dbSAllison Henderson * flags: Optional flags that may be used: 32504e96b2dbSAllison Henderson * 32514e96b2dbSAllison Henderson * EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED 32524e96b2dbSAllison Henderson * Only zero the regions of the page whose buffer heads 32534e96b2dbSAllison Henderson * have already been unmapped. This flag is appropriate 32544907cb7bSAnatol Pomozov * for updating the contents of a page whose blocks may 32554e96b2dbSAllison Henderson * have already been released, and we only want to zero 32564e96b2dbSAllison Henderson * out the regions that correspond to those released blocks. 32574e96b2dbSAllison Henderson * 32584907cb7bSAnatol Pomozov * Returns zero on success or negative on failure. 32594e96b2dbSAllison Henderson */ 32605f163cc7SEric Sandeen static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle, 32614e96b2dbSAllison Henderson struct inode *inode, struct page *page, loff_t from, 32624e96b2dbSAllison Henderson loff_t length, int flags) 32634e96b2dbSAllison Henderson { 32644e96b2dbSAllison Henderson ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; 32654e96b2dbSAllison Henderson unsigned int offset = from & (PAGE_CACHE_SIZE-1); 32664e96b2dbSAllison Henderson unsigned int blocksize, max, pos; 32674e96b2dbSAllison Henderson ext4_lblk_t iblock; 32684e96b2dbSAllison Henderson struct buffer_head *bh; 32694e96b2dbSAllison Henderson int err = 0; 32704e96b2dbSAllison Henderson 32714e96b2dbSAllison Henderson blocksize = inode->i_sb->s_blocksize; 32724e96b2dbSAllison Henderson max = PAGE_CACHE_SIZE - offset; 32734e96b2dbSAllison Henderson 32744e96b2dbSAllison Henderson if (index != page->index) 32754e96b2dbSAllison Henderson return -EINVAL; 32764e96b2dbSAllison Henderson 32774e96b2dbSAllison Henderson /* 32784e96b2dbSAllison Henderson * correct length if it does not fall between 32794e96b2dbSAllison Henderson * 'from' and the end of the page 32804e96b2dbSAllison Henderson */ 32814e96b2dbSAllison Henderson if (length > max || length < 0) 32824e96b2dbSAllison Henderson length = max; 32834e96b2dbSAllison Henderson 32844e96b2dbSAllison Henderson iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); 32854e96b2dbSAllison Henderson 3286093e6e36SYongqiang Yang if (!page_has_buffers(page)) 32874e96b2dbSAllison Henderson create_empty_buffers(page, blocksize, 0); 32884e96b2dbSAllison Henderson 32894e96b2dbSAllison Henderson /* Find the buffer that contains "offset" */ 32904e96b2dbSAllison Henderson bh = page_buffers(page); 32914e96b2dbSAllison Henderson pos = blocksize; 32924e96b2dbSAllison Henderson while (offset >= pos) { 32934e96b2dbSAllison Henderson bh = bh->b_this_page; 32944e96b2dbSAllison Henderson iblock++; 32954e96b2dbSAllison Henderson pos += blocksize; 32964e96b2dbSAllison Henderson } 32974e96b2dbSAllison Henderson 32984e96b2dbSAllison Henderson pos = offset; 32994e96b2dbSAllison Henderson while (pos < offset + length) { 3300e260daf2SYongqiang Yang unsigned int end_of_block, range_to_discard; 3301e260daf2SYongqiang Yang 33024e96b2dbSAllison Henderson err = 0; 33034e96b2dbSAllison Henderson 33044e96b2dbSAllison Henderson /* The length of space left to zero and unmap */ 33054e96b2dbSAllison Henderson range_to_discard = offset + length - pos; 33064e96b2dbSAllison Henderson 33074e96b2dbSAllison Henderson /* The length of space until the end of the block */ 33084e96b2dbSAllison Henderson end_of_block = blocksize - (pos & (blocksize-1)); 33094e96b2dbSAllison Henderson 33104e96b2dbSAllison Henderson /* 33114e96b2dbSAllison Henderson * Do not unmap or zero past end of block 33124e96b2dbSAllison Henderson * for this buffer head 33134e96b2dbSAllison Henderson */ 33144e96b2dbSAllison Henderson if (range_to_discard > end_of_block) 33154e96b2dbSAllison Henderson range_to_discard = end_of_block; 33164e96b2dbSAllison Henderson 33174e96b2dbSAllison Henderson 33184e96b2dbSAllison Henderson /* 33194e96b2dbSAllison Henderson * Skip this buffer head if we are only zeroing unampped 33204e96b2dbSAllison Henderson * regions of the page 33214e96b2dbSAllison Henderson */ 33224e96b2dbSAllison Henderson if (flags & EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED && 33234e96b2dbSAllison Henderson buffer_mapped(bh)) 33244e96b2dbSAllison Henderson goto next; 33254e96b2dbSAllison Henderson 33264e96b2dbSAllison Henderson /* If the range is block aligned, unmap */ 33274e96b2dbSAllison Henderson if (range_to_discard == blocksize) { 33284e96b2dbSAllison Henderson clear_buffer_dirty(bh); 33294e96b2dbSAllison Henderson bh->b_bdev = NULL; 33304e96b2dbSAllison Henderson clear_buffer_mapped(bh); 33314e96b2dbSAllison Henderson clear_buffer_req(bh); 33324e96b2dbSAllison Henderson clear_buffer_new(bh); 33334e96b2dbSAllison Henderson clear_buffer_delay(bh); 33344e96b2dbSAllison Henderson clear_buffer_unwritten(bh); 33354e96b2dbSAllison Henderson clear_buffer_uptodate(bh); 33364e96b2dbSAllison Henderson zero_user(page, pos, range_to_discard); 33374e96b2dbSAllison Henderson BUFFER_TRACE(bh, "Buffer discarded"); 33384e96b2dbSAllison Henderson goto next; 33394e96b2dbSAllison Henderson } 33404e96b2dbSAllison Henderson 33414e96b2dbSAllison Henderson /* 33424e96b2dbSAllison Henderson * If this block is not completely contained in the range 33434e96b2dbSAllison Henderson * to be discarded, then it is not going to be released. Because 33444e96b2dbSAllison Henderson * we need to keep this block, we need to make sure this part 33454e96b2dbSAllison Henderson * of the page is uptodate before we modify it by writeing 33464e96b2dbSAllison Henderson * partial zeros on it. 33474e96b2dbSAllison Henderson */ 33484e96b2dbSAllison Henderson if (!buffer_mapped(bh)) { 33494e96b2dbSAllison Henderson /* 33504e96b2dbSAllison Henderson * Buffer head must be mapped before we can read 33514e96b2dbSAllison Henderson * from the block 33524e96b2dbSAllison Henderson */ 33534e96b2dbSAllison Henderson BUFFER_TRACE(bh, "unmapped"); 33544e96b2dbSAllison Henderson ext4_get_block(inode, iblock, bh, 0); 33554e96b2dbSAllison Henderson /* unmapped? It's a hole - nothing to do */ 33564e96b2dbSAllison Henderson if (!buffer_mapped(bh)) { 33574e96b2dbSAllison Henderson BUFFER_TRACE(bh, "still unmapped"); 33584e96b2dbSAllison Henderson goto next; 33594e96b2dbSAllison Henderson } 33604e96b2dbSAllison Henderson } 33614e96b2dbSAllison Henderson 33624e96b2dbSAllison Henderson /* Ok, it's mapped. Make sure it's up-to-date */ 33634e96b2dbSAllison Henderson if (PageUptodate(page)) 33644e96b2dbSAllison Henderson set_buffer_uptodate(bh); 33654e96b2dbSAllison Henderson 33664e96b2dbSAllison Henderson if (!buffer_uptodate(bh)) { 33674e96b2dbSAllison Henderson err = -EIO; 33684e96b2dbSAllison Henderson ll_rw_block(READ, 1, &bh); 33694e96b2dbSAllison Henderson wait_on_buffer(bh); 33704e96b2dbSAllison Henderson /* Uhhuh. Read error. Complain and punt.*/ 33714e96b2dbSAllison Henderson if (!buffer_uptodate(bh)) 33724e96b2dbSAllison Henderson goto next; 33734e96b2dbSAllison Henderson } 33744e96b2dbSAllison Henderson 33754e96b2dbSAllison Henderson if (ext4_should_journal_data(inode)) { 33764e96b2dbSAllison Henderson BUFFER_TRACE(bh, "get write access"); 33774e96b2dbSAllison Henderson err = ext4_journal_get_write_access(handle, bh); 33784e96b2dbSAllison Henderson if (err) 33794e96b2dbSAllison Henderson goto next; 33804e96b2dbSAllison Henderson } 33814e96b2dbSAllison Henderson 33824e96b2dbSAllison Henderson zero_user(page, pos, range_to_discard); 33834e96b2dbSAllison Henderson 33844e96b2dbSAllison Henderson err = 0; 33854e96b2dbSAllison Henderson if (ext4_should_journal_data(inode)) { 33864e96b2dbSAllison Henderson err = ext4_handle_dirty_metadata(handle, inode, bh); 3387decbd919STheodore Ts'o } else 33884e96b2dbSAllison Henderson mark_buffer_dirty(bh); 33894e96b2dbSAllison Henderson 33904e96b2dbSAllison Henderson BUFFER_TRACE(bh, "Partial buffer zeroed"); 33914e96b2dbSAllison Henderson next: 33924e96b2dbSAllison Henderson bh = bh->b_this_page; 33934e96b2dbSAllison Henderson iblock++; 33944e96b2dbSAllison Henderson pos += range_to_discard; 33954e96b2dbSAllison Henderson } 33964e96b2dbSAllison Henderson 33974e96b2dbSAllison Henderson return err; 33984e96b2dbSAllison Henderson } 33994e96b2dbSAllison Henderson 340091ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 340191ef4cafSDuane Griffin { 340291ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 340391ef4cafSDuane Griffin return 1; 340491ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 340591ef4cafSDuane Griffin return 1; 340691ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 340791ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 340891ef4cafSDuane Griffin return 0; 340991ef4cafSDuane Griffin } 341091ef4cafSDuane Griffin 3411ac27a0ecSDave Kleikamp /* 3412a4bb6b64SAllison Henderson * ext4_punch_hole: punches a hole in a file by releaseing the blocks 3413a4bb6b64SAllison Henderson * associated with the given offset and length 3414a4bb6b64SAllison Henderson * 3415a4bb6b64SAllison Henderson * @inode: File inode 3416a4bb6b64SAllison Henderson * @offset: The offset where the hole will begin 3417a4bb6b64SAllison Henderson * @len: The length of the hole 3418a4bb6b64SAllison Henderson * 34194907cb7bSAnatol Pomozov * Returns: 0 on success or negative on failure 3420a4bb6b64SAllison Henderson */ 3421a4bb6b64SAllison Henderson 3422a4bb6b64SAllison Henderson int ext4_punch_hole(struct file *file, loff_t offset, loff_t length) 3423a4bb6b64SAllison Henderson { 3424a4bb6b64SAllison Henderson struct inode *inode = file->f_path.dentry->d_inode; 3425a4bb6b64SAllison Henderson if (!S_ISREG(inode->i_mode)) 342673355192SAllison Henderson return -EOPNOTSUPP; 3427a4bb6b64SAllison Henderson 34288bad6fc8SZheng Liu if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 34298bad6fc8SZheng Liu return ext4_ind_punch_hole(file, offset, length); 3430a4bb6b64SAllison Henderson 3431bab08ab9STheodore Ts'o if (EXT4_SB(inode->i_sb)->s_cluster_ratio > 1) { 3432bab08ab9STheodore Ts'o /* TODO: Add support for bigalloc file systems */ 343373355192SAllison Henderson return -EOPNOTSUPP; 3434bab08ab9STheodore Ts'o } 3435bab08ab9STheodore Ts'o 3436aaddea81SZheng Liu trace_ext4_punch_hole(inode, offset, length); 3437aaddea81SZheng Liu 3438a4bb6b64SAllison Henderson return ext4_ext_punch_hole(file, offset, length); 3439a4bb6b64SAllison Henderson } 3440a4bb6b64SAllison Henderson 3441a4bb6b64SAllison Henderson /* 3442617ba13bSMingming Cao * ext4_truncate() 3443ac27a0ecSDave Kleikamp * 3444617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 3445617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 3446ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 3447ac27a0ecSDave Kleikamp * 344842b2aa86SJustin P. Mattock * As we work through the truncate and commit bits of it to the journal there 3449ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 3450ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 3451ac27a0ecSDave Kleikamp * 3452ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 3453ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 3454ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 3455ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 3456ac27a0ecSDave Kleikamp * left-to-right works OK too). 3457ac27a0ecSDave Kleikamp * 3458ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 3459ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 3460ac27a0ecSDave Kleikamp * 3461ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 3462617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 3463ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 3464617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 3465617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 3466ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 3467617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 3468ac27a0ecSDave Kleikamp */ 3469617ba13bSMingming Cao void ext4_truncate(struct inode *inode) 3470ac27a0ecSDave Kleikamp { 34710562e0baSJiaying Zhang trace_ext4_truncate_enter(inode); 34720562e0baSJiaying Zhang 347391ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 3474ac27a0ecSDave Kleikamp return; 3475ac27a0ecSDave Kleikamp 347612e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS); 3477c8d46e41SJiaying Zhang 34785534fb5bSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 347919f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE); 34807d8f9f7dSTheodore Ts'o 3481aef1c851STao Ma if (ext4_has_inline_data(inode)) { 3482aef1c851STao Ma int has_inline = 1; 3483aef1c851STao Ma 3484aef1c851STao Ma ext4_inline_data_truncate(inode, &has_inline); 3485aef1c851STao Ma if (has_inline) 3486aef1c851STao Ma return; 3487aef1c851STao Ma } 3488aef1c851STao Ma 3489ff9893dcSAmir Goldstein if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3490cf108bcaSJan Kara ext4_ext_truncate(inode); 3491ff9893dcSAmir Goldstein else 3492ff9893dcSAmir Goldstein ext4_ind_truncate(inode); 3493a86c6181SAlex Tomas 34940562e0baSJiaying Zhang trace_ext4_truncate_exit(inode); 3495ac27a0ecSDave Kleikamp } 3496ac27a0ecSDave Kleikamp 3497ac27a0ecSDave Kleikamp /* 3498617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 3499ac27a0ecSDave Kleikamp * underlying buffer_head on success. If 'in_mem' is true, we have all 3500ac27a0ecSDave Kleikamp * data in memory that is needed to recreate the on-disk version of this 3501ac27a0ecSDave Kleikamp * inode. 3502ac27a0ecSDave Kleikamp */ 3503617ba13bSMingming Cao static int __ext4_get_inode_loc(struct inode *inode, 3504617ba13bSMingming Cao struct ext4_iloc *iloc, int in_mem) 3505ac27a0ecSDave Kleikamp { 3506240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 3507ac27a0ecSDave Kleikamp struct buffer_head *bh; 3508240799cdSTheodore Ts'o struct super_block *sb = inode->i_sb; 3509240799cdSTheodore Ts'o ext4_fsblk_t block; 3510240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 3511ac27a0ecSDave Kleikamp 35123a06d778SAneesh Kumar K.V iloc->bh = NULL; 3513240799cdSTheodore Ts'o if (!ext4_valid_inum(sb, inode->i_ino)) 3514ac27a0ecSDave Kleikamp return -EIO; 3515ac27a0ecSDave Kleikamp 3516240799cdSTheodore Ts'o iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb); 3517240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 3518240799cdSTheodore Ts'o if (!gdp) 3519240799cdSTheodore Ts'o return -EIO; 3520240799cdSTheodore Ts'o 3521240799cdSTheodore Ts'o /* 3522240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 3523240799cdSTheodore Ts'o */ 352400d09882STao Ma inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 3525240799cdSTheodore Ts'o inode_offset = ((inode->i_ino - 1) % 3526240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 3527240799cdSTheodore Ts'o block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); 3528240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 3529240799cdSTheodore Ts'o 3530240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 3531aebf0243SWang Shilong if (unlikely(!bh)) 3532860d21e2STheodore Ts'o return -ENOMEM; 3533ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 3534ac27a0ecSDave Kleikamp lock_buffer(bh); 35359c83a923SHidehiro Kawai 35369c83a923SHidehiro Kawai /* 35379c83a923SHidehiro Kawai * If the buffer has the write error flag, we have failed 35389c83a923SHidehiro Kawai * to write out another inode in the same block. In this 35399c83a923SHidehiro Kawai * case, we don't have to read the block because we may 35409c83a923SHidehiro Kawai * read the old inode data successfully. 35419c83a923SHidehiro Kawai */ 35429c83a923SHidehiro Kawai if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) 35439c83a923SHidehiro Kawai set_buffer_uptodate(bh); 35449c83a923SHidehiro Kawai 3545ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) { 3546ac27a0ecSDave Kleikamp /* someone brought it uptodate while we waited */ 3547ac27a0ecSDave Kleikamp unlock_buffer(bh); 3548ac27a0ecSDave Kleikamp goto has_buffer; 3549ac27a0ecSDave Kleikamp } 3550ac27a0ecSDave Kleikamp 3551ac27a0ecSDave Kleikamp /* 3552ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 3553ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 3554ac27a0ecSDave Kleikamp * block. 3555ac27a0ecSDave Kleikamp */ 3556ac27a0ecSDave Kleikamp if (in_mem) { 3557ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 3558240799cdSTheodore Ts'o int i, start; 3559ac27a0ecSDave Kleikamp 3560240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 3561ac27a0ecSDave Kleikamp 3562ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 3563240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 3564aebf0243SWang Shilong if (unlikely(!bitmap_bh)) 3565ac27a0ecSDave Kleikamp goto make_io; 3566ac27a0ecSDave Kleikamp 3567ac27a0ecSDave Kleikamp /* 3568ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 3569ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 3570ac27a0ecSDave Kleikamp * of one, so skip it. 3571ac27a0ecSDave Kleikamp */ 3572ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 3573ac27a0ecSDave Kleikamp brelse(bitmap_bh); 3574ac27a0ecSDave Kleikamp goto make_io; 3575ac27a0ecSDave Kleikamp } 3576240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 3577ac27a0ecSDave Kleikamp if (i == inode_offset) 3578ac27a0ecSDave Kleikamp continue; 3579617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 3580ac27a0ecSDave Kleikamp break; 3581ac27a0ecSDave Kleikamp } 3582ac27a0ecSDave Kleikamp brelse(bitmap_bh); 3583240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 3584ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 3585ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 3586ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 3587ac27a0ecSDave Kleikamp unlock_buffer(bh); 3588ac27a0ecSDave Kleikamp goto has_buffer; 3589ac27a0ecSDave Kleikamp } 3590ac27a0ecSDave Kleikamp } 3591ac27a0ecSDave Kleikamp 3592ac27a0ecSDave Kleikamp make_io: 3593ac27a0ecSDave Kleikamp /* 3594240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 3595240799cdSTheodore Ts'o * blocks from the inode table. 3596240799cdSTheodore Ts'o */ 3597240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 3598240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 3599240799cdSTheodore Ts'o unsigned num; 3600240799cdSTheodore Ts'o 3601240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 3602b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 3603240799cdSTheodore Ts'o b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1); 3604240799cdSTheodore Ts'o if (table > b) 3605240799cdSTheodore Ts'o b = table; 3606240799cdSTheodore Ts'o end = b + EXT4_SB(sb)->s_inode_readahead_blks; 3607240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 3608feb0ab32SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) 3609560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 3610240799cdSTheodore Ts'o table += num / inodes_per_block; 3611240799cdSTheodore Ts'o if (end > table) 3612240799cdSTheodore Ts'o end = table; 3613240799cdSTheodore Ts'o while (b <= end) 3614240799cdSTheodore Ts'o sb_breadahead(sb, b++); 3615240799cdSTheodore Ts'o } 3616240799cdSTheodore Ts'o 3617240799cdSTheodore Ts'o /* 3618ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 3619ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 3620ac27a0ecSDave Kleikamp * Read the block from disk. 3621ac27a0ecSDave Kleikamp */ 36220562e0baSJiaying Zhang trace_ext4_load_inode(inode); 3623ac27a0ecSDave Kleikamp get_bh(bh); 3624ac27a0ecSDave Kleikamp bh->b_end_io = end_buffer_read_sync; 362565299a3bSChristoph Hellwig submit_bh(READ | REQ_META | REQ_PRIO, bh); 3626ac27a0ecSDave Kleikamp wait_on_buffer(bh); 3627ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 3628c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, block, 3629c398eda0STheodore Ts'o "unable to read itable block"); 3630ac27a0ecSDave Kleikamp brelse(bh); 3631ac27a0ecSDave Kleikamp return -EIO; 3632ac27a0ecSDave Kleikamp } 3633ac27a0ecSDave Kleikamp } 3634ac27a0ecSDave Kleikamp has_buffer: 3635ac27a0ecSDave Kleikamp iloc->bh = bh; 3636ac27a0ecSDave Kleikamp return 0; 3637ac27a0ecSDave Kleikamp } 3638ac27a0ecSDave Kleikamp 3639617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 3640ac27a0ecSDave Kleikamp { 3641ac27a0ecSDave Kleikamp /* We have all inode data except xattrs in memory here. */ 3642617ba13bSMingming Cao return __ext4_get_inode_loc(inode, iloc, 364319f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_XATTR)); 3644ac27a0ecSDave Kleikamp } 3645ac27a0ecSDave Kleikamp 3646617ba13bSMingming Cao void ext4_set_inode_flags(struct inode *inode) 3647ac27a0ecSDave Kleikamp { 3648617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 3649ac27a0ecSDave Kleikamp 3650ac27a0ecSDave Kleikamp inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); 3651617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 3652ac27a0ecSDave Kleikamp inode->i_flags |= S_SYNC; 3653617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 3654ac27a0ecSDave Kleikamp inode->i_flags |= S_APPEND; 3655617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 3656ac27a0ecSDave Kleikamp inode->i_flags |= S_IMMUTABLE; 3657617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 3658ac27a0ecSDave Kleikamp inode->i_flags |= S_NOATIME; 3659617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 3660ac27a0ecSDave Kleikamp inode->i_flags |= S_DIRSYNC; 3661ac27a0ecSDave Kleikamp } 3662ac27a0ecSDave Kleikamp 3663ff9ddf7eSJan Kara /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */ 3664ff9ddf7eSJan Kara void ext4_get_inode_flags(struct ext4_inode_info *ei) 3665ff9ddf7eSJan Kara { 366684a8dce2SDmitry Monakhov unsigned int vfs_fl; 366784a8dce2SDmitry Monakhov unsigned long old_fl, new_fl; 3668ff9ddf7eSJan Kara 366984a8dce2SDmitry Monakhov do { 367084a8dce2SDmitry Monakhov vfs_fl = ei->vfs_inode.i_flags; 367184a8dce2SDmitry Monakhov old_fl = ei->i_flags; 367284a8dce2SDmitry Monakhov new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL| 367384a8dce2SDmitry Monakhov EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL| 367484a8dce2SDmitry Monakhov EXT4_DIRSYNC_FL); 367584a8dce2SDmitry Monakhov if (vfs_fl & S_SYNC) 367684a8dce2SDmitry Monakhov new_fl |= EXT4_SYNC_FL; 367784a8dce2SDmitry Monakhov if (vfs_fl & S_APPEND) 367884a8dce2SDmitry Monakhov new_fl |= EXT4_APPEND_FL; 367984a8dce2SDmitry Monakhov if (vfs_fl & S_IMMUTABLE) 368084a8dce2SDmitry Monakhov new_fl |= EXT4_IMMUTABLE_FL; 368184a8dce2SDmitry Monakhov if (vfs_fl & S_NOATIME) 368284a8dce2SDmitry Monakhov new_fl |= EXT4_NOATIME_FL; 368384a8dce2SDmitry Monakhov if (vfs_fl & S_DIRSYNC) 368484a8dce2SDmitry Monakhov new_fl |= EXT4_DIRSYNC_FL; 368584a8dce2SDmitry Monakhov } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl); 3686ff9ddf7eSJan Kara } 3687de9a55b8STheodore Ts'o 36880fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 36890fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 36900fc1b451SAneesh Kumar K.V { 36910fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 36928180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 36938180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 36940fc1b451SAneesh Kumar K.V 36950fc1b451SAneesh Kumar K.V if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 36960fc1b451SAneesh Kumar K.V EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) { 36970fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 36980fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 36990fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 370007a03824STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) { 37018180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 37028180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 37038180a562SAneesh Kumar K.V } else { 37040fc1b451SAneesh Kumar K.V return i_blocks; 37058180a562SAneesh Kumar K.V } 37060fc1b451SAneesh Kumar K.V } else { 37070fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 37080fc1b451SAneesh Kumar K.V } 37090fc1b451SAneesh Kumar K.V } 3710ff9ddf7eSJan Kara 3711152a7b0aSTao Ma static inline void ext4_iget_extra_inode(struct inode *inode, 3712152a7b0aSTao Ma struct ext4_inode *raw_inode, 3713152a7b0aSTao Ma struct ext4_inode_info *ei) 3714152a7b0aSTao Ma { 3715152a7b0aSTao Ma __le32 *magic = (void *)raw_inode + 3716152a7b0aSTao Ma EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize; 371767cf5b09STao Ma if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) { 3718152a7b0aSTao Ma ext4_set_inode_state(inode, EXT4_STATE_XATTR); 371967cf5b09STao Ma ext4_find_inline_data_nolock(inode); 3720f19d5870STao Ma } else 3721f19d5870STao Ma EXT4_I(inode)->i_inline_off = 0; 3722152a7b0aSTao Ma } 3723152a7b0aSTao Ma 37241d1fe1eeSDavid Howells struct inode *ext4_iget(struct super_block *sb, unsigned long ino) 3725ac27a0ecSDave Kleikamp { 3726617ba13bSMingming Cao struct ext4_iloc iloc; 3727617ba13bSMingming Cao struct ext4_inode *raw_inode; 37281d1fe1eeSDavid Howells struct ext4_inode_info *ei; 37291d1fe1eeSDavid Howells struct inode *inode; 3730b436b9beSJan Kara journal_t *journal = EXT4_SB(sb)->s_journal; 37311d1fe1eeSDavid Howells long ret; 3732ac27a0ecSDave Kleikamp int block; 373308cefc7aSEric W. Biederman uid_t i_uid; 373408cefc7aSEric W. Biederman gid_t i_gid; 3735ac27a0ecSDave Kleikamp 37361d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 37371d1fe1eeSDavid Howells if (!inode) 37381d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 37391d1fe1eeSDavid Howells if (!(inode->i_state & I_NEW)) 37401d1fe1eeSDavid Howells return inode; 37411d1fe1eeSDavid Howells 37421d1fe1eeSDavid Howells ei = EXT4_I(inode); 37437dc57615SPeter Huewe iloc.bh = NULL; 3744ac27a0ecSDave Kleikamp 37451d1fe1eeSDavid Howells ret = __ext4_get_inode_loc(inode, &iloc, 0); 37461d1fe1eeSDavid Howells if (ret < 0) 3747ac27a0ecSDave Kleikamp goto bad_inode; 3748617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 3749814525f4SDarrick J. Wong 3750814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 3751814525f4SDarrick J. Wong ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 3752814525f4SDarrick J. Wong if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 3753814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)) { 3754814525f4SDarrick J. Wong EXT4_ERROR_INODE(inode, "bad extra_isize (%u != %u)", 3755814525f4SDarrick J. Wong EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize, 3756814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)); 3757814525f4SDarrick J. Wong ret = -EIO; 3758814525f4SDarrick J. Wong goto bad_inode; 3759814525f4SDarrick J. Wong } 3760814525f4SDarrick J. Wong } else 3761814525f4SDarrick J. Wong ei->i_extra_isize = 0; 3762814525f4SDarrick J. Wong 3763814525f4SDarrick J. Wong /* Precompute checksum seed for inode metadata */ 3764814525f4SDarrick J. Wong if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 3765814525f4SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) { 3766814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3767814525f4SDarrick J. Wong __u32 csum; 3768814525f4SDarrick J. Wong __le32 inum = cpu_to_le32(inode->i_ino); 3769814525f4SDarrick J. Wong __le32 gen = raw_inode->i_generation; 3770814525f4SDarrick J. Wong csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, 3771814525f4SDarrick J. Wong sizeof(inum)); 3772814525f4SDarrick J. Wong ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, 3773814525f4SDarrick J. Wong sizeof(gen)); 3774814525f4SDarrick J. Wong } 3775814525f4SDarrick J. Wong 3776814525f4SDarrick J. Wong if (!ext4_inode_csum_verify(inode, raw_inode, ei)) { 3777814525f4SDarrick J. Wong EXT4_ERROR_INODE(inode, "checksum invalid"); 3778814525f4SDarrick J. Wong ret = -EIO; 3779814525f4SDarrick J. Wong goto bad_inode; 3780814525f4SDarrick J. Wong } 3781814525f4SDarrick J. Wong 3782ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 378308cefc7aSEric W. Biederman i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 378408cefc7aSEric W. Biederman i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 3785ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 378608cefc7aSEric W. Biederman i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 378708cefc7aSEric W. Biederman i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 3788ac27a0ecSDave Kleikamp } 378908cefc7aSEric W. Biederman i_uid_write(inode, i_uid); 379008cefc7aSEric W. Biederman i_gid_write(inode, i_gid); 3791bfe86848SMiklos Szeredi set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); 3792ac27a0ecSDave Kleikamp 3793353eb83cSTheodore Ts'o ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 379467cf5b09STao Ma ei->i_inline_off = 0; 3795ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 3796ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 3797ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 3798ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 3799ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 3800ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 3801ac27a0ecSDave Kleikamp */ 3802ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 3803ac27a0ecSDave Kleikamp if (inode->i_mode == 0 || 3804617ba13bSMingming Cao !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) { 3805ac27a0ecSDave Kleikamp /* this inode is deleted */ 38061d1fe1eeSDavid Howells ret = -ESTALE; 3807ac27a0ecSDave Kleikamp goto bad_inode; 3808ac27a0ecSDave Kleikamp } 3809ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 3810ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 3811ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 3812ac27a0ecSDave Kleikamp * the process of deleting those. */ 3813ac27a0ecSDave Kleikamp } 3814ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 38150fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 38167973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 3817a9e81742STheodore Ts'o if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) 3818a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 3819a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 3820a48380f7SAneesh Kumar K.V inode->i_size = ext4_isize(raw_inode); 3821ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 3822a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 3823a9e7f447SDmitry Monakhov ei->i_reserved_quota = 0; 3824a9e7f447SDmitry Monakhov #endif 3825ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 3826ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 3827a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 3828ac27a0ecSDave Kleikamp /* 3829ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 3830ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 3831ac27a0ecSDave Kleikamp */ 3832617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 3833ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 3834ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 3835ac27a0ecSDave Kleikamp 3836b436b9beSJan Kara /* 3837b436b9beSJan Kara * Set transaction id's of transactions that have to be committed 3838b436b9beSJan Kara * to finish f[data]sync. We set them to currently running transaction 3839b436b9beSJan Kara * as we cannot be sure that the inode or some of its metadata isn't 3840b436b9beSJan Kara * part of the transaction - the inode could have been reclaimed and 3841b436b9beSJan Kara * now it is reread from disk. 3842b436b9beSJan Kara */ 3843b436b9beSJan Kara if (journal) { 3844b436b9beSJan Kara transaction_t *transaction; 3845b436b9beSJan Kara tid_t tid; 3846b436b9beSJan Kara 3847a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 3848b436b9beSJan Kara if (journal->j_running_transaction) 3849b436b9beSJan Kara transaction = journal->j_running_transaction; 3850b436b9beSJan Kara else 3851b436b9beSJan Kara transaction = journal->j_committing_transaction; 3852b436b9beSJan Kara if (transaction) 3853b436b9beSJan Kara tid = transaction->t_tid; 3854b436b9beSJan Kara else 3855b436b9beSJan Kara tid = journal->j_commit_sequence; 3856a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 3857b436b9beSJan Kara ei->i_sync_tid = tid; 3858b436b9beSJan Kara ei->i_datasync_tid = tid; 3859b436b9beSJan Kara } 3860b436b9beSJan Kara 38610040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 3862ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 3863ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 3864617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 3865617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 3866ac27a0ecSDave Kleikamp } else { 3867152a7b0aSTao Ma ext4_iget_extra_inode(inode, raw_inode, ei); 3868ac27a0ecSDave Kleikamp } 3869814525f4SDarrick J. Wong } 3870ac27a0ecSDave Kleikamp 3871ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 3872ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 3873ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 3874ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 3875ef7f3835SKalpak Shah 387625ec56b5SJean Noel Cordenner inode->i_version = le32_to_cpu(raw_inode->i_disk_version); 387725ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 387825ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 387925ec56b5SJean Noel Cordenner inode->i_version |= 388025ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 388125ec56b5SJean Noel Cordenner } 388225ec56b5SJean Noel Cordenner 3883c4b5a614STheodore Ts'o ret = 0; 3884485c26ecSTheodore Ts'o if (ei->i_file_acl && 38851032988cSTheodore Ts'o !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) { 388624676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "bad extended attribute block %llu", 388724676da4STheodore Ts'o ei->i_file_acl); 3888485c26ecSTheodore Ts'o ret = -EIO; 3889485c26ecSTheodore Ts'o goto bad_inode; 3890f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 3891f19d5870STao Ma if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 3892f19d5870STao Ma if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 3893c4b5a614STheodore Ts'o (S_ISLNK(inode->i_mode) && 3894f19d5870STao Ma !ext4_inode_is_fast_symlink(inode)))) 38957a262f7cSAneesh Kumar K.V /* Validate extent which is part of inode */ 38967a262f7cSAneesh Kumar K.V ret = ext4_ext_check_inode(inode); 3897fe2c8191SThiemo Nagel } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 3898fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 3899fe2c8191SThiemo Nagel !ext4_inode_is_fast_symlink(inode))) { 3900fe2c8191SThiemo Nagel /* Validate block references which are part of inode */ 39011f7d1e77STheodore Ts'o ret = ext4_ind_check_inode(inode); 3902fe2c8191SThiemo Nagel } 3903f19d5870STao Ma } 3904567f3e9aSTheodore Ts'o if (ret) 39057a262f7cSAneesh Kumar K.V goto bad_inode; 39067a262f7cSAneesh Kumar K.V 3907ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 3908617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 3909617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 3910617ba13bSMingming Cao ext4_set_aops(inode); 3911ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 3912617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 3913617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 3914ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 3915e83c1397SDuane Griffin if (ext4_inode_is_fast_symlink(inode)) { 3916617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 3917e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 3918e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 3919e83c1397SDuane Griffin } else { 3920617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 3921617ba13bSMingming Cao ext4_set_aops(inode); 3922ac27a0ecSDave Kleikamp } 3923563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 3924563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 3925617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 3926ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 3927ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 3928ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 3929ac27a0ecSDave Kleikamp else 3930ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 3931ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 3932563bdd61STheodore Ts'o } else { 3933563bdd61STheodore Ts'o ret = -EIO; 393424676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode); 3935563bdd61STheodore Ts'o goto bad_inode; 3936ac27a0ecSDave Kleikamp } 3937ac27a0ecSDave Kleikamp brelse(iloc.bh); 3938617ba13bSMingming Cao ext4_set_inode_flags(inode); 39391d1fe1eeSDavid Howells unlock_new_inode(inode); 39401d1fe1eeSDavid Howells return inode; 3941ac27a0ecSDave Kleikamp 3942ac27a0ecSDave Kleikamp bad_inode: 3943567f3e9aSTheodore Ts'o brelse(iloc.bh); 39441d1fe1eeSDavid Howells iget_failed(inode); 39451d1fe1eeSDavid Howells return ERR_PTR(ret); 3946ac27a0ecSDave Kleikamp } 3947ac27a0ecSDave Kleikamp 39480fc1b451SAneesh Kumar K.V static int ext4_inode_blocks_set(handle_t *handle, 39490fc1b451SAneesh Kumar K.V struct ext4_inode *raw_inode, 39500fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 39510fc1b451SAneesh Kumar K.V { 39520fc1b451SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 39530fc1b451SAneesh Kumar K.V u64 i_blocks = inode->i_blocks; 39540fc1b451SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 39550fc1b451SAneesh Kumar K.V 39560fc1b451SAneesh Kumar K.V if (i_blocks <= ~0U) { 39570fc1b451SAneesh Kumar K.V /* 39584907cb7bSAnatol Pomozov * i_blocks can be represented in a 32 bit variable 39590fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 39600fc1b451SAneesh Kumar K.V */ 39618180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 39620fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = 0; 396384a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 3964f287a1a5STheodore Ts'o return 0; 3965f287a1a5STheodore Ts'o } 3966f287a1a5STheodore Ts'o if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) 3967f287a1a5STheodore Ts'o return -EFBIG; 3968f287a1a5STheodore Ts'o 3969f287a1a5STheodore Ts'o if (i_blocks <= 0xffffffffffffULL) { 39700fc1b451SAneesh Kumar K.V /* 39710fc1b451SAneesh Kumar K.V * i_blocks can be represented in a 48 bit variable 39720fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 39730fc1b451SAneesh Kumar K.V */ 39748180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 39750fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 397684a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 39770fc1b451SAneesh Kumar K.V } else { 397884a8dce2SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE); 39798180a562SAneesh Kumar K.V /* i_block is stored in file system block size */ 39808180a562SAneesh Kumar K.V i_blocks = i_blocks >> (inode->i_blkbits - 9); 39818180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 39828180a562SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 39830fc1b451SAneesh Kumar K.V } 3984f287a1a5STheodore Ts'o return 0; 39850fc1b451SAneesh Kumar K.V } 39860fc1b451SAneesh Kumar K.V 3987ac27a0ecSDave Kleikamp /* 3988ac27a0ecSDave Kleikamp * Post the struct inode info into an on-disk inode location in the 3989ac27a0ecSDave Kleikamp * buffer-cache. This gobbles the caller's reference to the 3990ac27a0ecSDave Kleikamp * buffer_head in the inode location struct. 3991ac27a0ecSDave Kleikamp * 3992ac27a0ecSDave Kleikamp * The caller must have write access to iloc->bh. 3993ac27a0ecSDave Kleikamp */ 3994617ba13bSMingming Cao static int ext4_do_update_inode(handle_t *handle, 3995ac27a0ecSDave Kleikamp struct inode *inode, 3996830156c7SFrank Mayhar struct ext4_iloc *iloc) 3997ac27a0ecSDave Kleikamp { 3998617ba13bSMingming Cao struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 3999617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 4000ac27a0ecSDave Kleikamp struct buffer_head *bh = iloc->bh; 4001ac27a0ecSDave Kleikamp int err = 0, rc, block; 4002b71fc079SJan Kara int need_datasync = 0; 400308cefc7aSEric W. Biederman uid_t i_uid; 400408cefc7aSEric W. Biederman gid_t i_gid; 4005ac27a0ecSDave Kleikamp 4006ac27a0ecSDave Kleikamp /* For fields not not tracking in the in-memory inode, 4007ac27a0ecSDave Kleikamp * initialise them to zero for new inodes. */ 400819f5fb7aSTheodore Ts'o if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) 4009617ba13bSMingming Cao memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 4010ac27a0ecSDave Kleikamp 4011ff9ddf7eSJan Kara ext4_get_inode_flags(ei); 4012ac27a0ecSDave Kleikamp raw_inode->i_mode = cpu_to_le16(inode->i_mode); 401308cefc7aSEric W. Biederman i_uid = i_uid_read(inode); 401408cefc7aSEric W. Biederman i_gid = i_gid_read(inode); 4015ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 401608cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid)); 401708cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid)); 4018ac27a0ecSDave Kleikamp /* 4019ac27a0ecSDave Kleikamp * Fix up interoperability with old kernels. Otherwise, old inodes get 4020ac27a0ecSDave Kleikamp * re-used with the upper 16 bits of the uid/gid intact 4021ac27a0ecSDave Kleikamp */ 4022ac27a0ecSDave Kleikamp if (!ei->i_dtime) { 4023ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 402408cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_uid)); 4025ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 402608cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_gid)); 4027ac27a0ecSDave Kleikamp } else { 4028ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4029ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4030ac27a0ecSDave Kleikamp } 4031ac27a0ecSDave Kleikamp } else { 403208cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid)); 403308cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid)); 4034ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4035ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4036ac27a0ecSDave Kleikamp } 4037ac27a0ecSDave Kleikamp raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 4038ef7f3835SKalpak Shah 4039ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 4040ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 4041ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 4042ef7f3835SKalpak Shah EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 4043ef7f3835SKalpak Shah 40440fc1b451SAneesh Kumar K.V if (ext4_inode_blocks_set(handle, raw_inode, ei)) 40450fc1b451SAneesh Kumar K.V goto out_brelse; 4046ac27a0ecSDave Kleikamp raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 4047353eb83cSTheodore Ts'o raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF); 40489b8f1f01SMingming Cao if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 40499b8f1f01SMingming Cao cpu_to_le32(EXT4_OS_HURD)) 4050a1ddeb7eSBadari Pulavarty raw_inode->i_file_acl_high = 4051a1ddeb7eSBadari Pulavarty cpu_to_le16(ei->i_file_acl >> 32); 40527973c0c1SAneesh Kumar K.V raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 4053b71fc079SJan Kara if (ei->i_disksize != ext4_isize(raw_inode)) { 4054a48380f7SAneesh Kumar K.V ext4_isize_set(raw_inode, ei->i_disksize); 4055b71fc079SJan Kara need_datasync = 1; 4056b71fc079SJan Kara } 4057ac27a0ecSDave Kleikamp if (ei->i_disksize > 0x7fffffffULL) { 4058ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 4059617ba13bSMingming Cao if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, 4060617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE) || 4061617ba13bSMingming Cao EXT4_SB(sb)->s_es->s_rev_level == 4062617ba13bSMingming Cao cpu_to_le32(EXT4_GOOD_OLD_REV)) { 4063ac27a0ecSDave Kleikamp /* If this is the first large file 4064ac27a0ecSDave Kleikamp * created, add a flag to the superblock. 4065ac27a0ecSDave Kleikamp */ 4066617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, 4067617ba13bSMingming Cao EXT4_SB(sb)->s_sbh); 4068ac27a0ecSDave Kleikamp if (err) 4069ac27a0ecSDave Kleikamp goto out_brelse; 4070617ba13bSMingming Cao ext4_update_dynamic_rev(sb); 4071617ba13bSMingming Cao EXT4_SET_RO_COMPAT_FEATURE(sb, 4072617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE); 40730390131bSFrank Mayhar ext4_handle_sync(handle); 4074b50924c2SArtem Bityutskiy err = ext4_handle_dirty_super(handle, sb); 4075ac27a0ecSDave Kleikamp } 4076ac27a0ecSDave Kleikamp } 4077ac27a0ecSDave Kleikamp raw_inode->i_generation = cpu_to_le32(inode->i_generation); 4078ac27a0ecSDave Kleikamp if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 4079ac27a0ecSDave Kleikamp if (old_valid_dev(inode->i_rdev)) { 4080ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 4081ac27a0ecSDave Kleikamp cpu_to_le32(old_encode_dev(inode->i_rdev)); 4082ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 0; 4083ac27a0ecSDave Kleikamp } else { 4084ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 0; 4085ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 4086ac27a0ecSDave Kleikamp cpu_to_le32(new_encode_dev(inode->i_rdev)); 4087ac27a0ecSDave Kleikamp raw_inode->i_block[2] = 0; 4088ac27a0ecSDave Kleikamp } 4089f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 4090de9a55b8STheodore Ts'o for (block = 0; block < EXT4_N_BLOCKS; block++) 4091ac27a0ecSDave Kleikamp raw_inode->i_block[block] = ei->i_data[block]; 4092f19d5870STao Ma } 4093ac27a0ecSDave Kleikamp 409425ec56b5SJean Noel Cordenner raw_inode->i_disk_version = cpu_to_le32(inode->i_version); 409525ec56b5SJean Noel Cordenner if (ei->i_extra_isize) { 409625ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 409725ec56b5SJean Noel Cordenner raw_inode->i_version_hi = 409825ec56b5SJean Noel Cordenner cpu_to_le32(inode->i_version >> 32); 4099ac27a0ecSDave Kleikamp raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize); 410025ec56b5SJean Noel Cordenner } 410125ec56b5SJean Noel Cordenner 4102814525f4SDarrick J. Wong ext4_inode_csum_set(inode, raw_inode, ei); 4103814525f4SDarrick J. Wong 41040390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 410573b50c1cSCurt Wohlgemuth rc = ext4_handle_dirty_metadata(handle, NULL, bh); 4106ac27a0ecSDave Kleikamp if (!err) 4107ac27a0ecSDave Kleikamp err = rc; 410819f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_NEW); 4109ac27a0ecSDave Kleikamp 4110b71fc079SJan Kara ext4_update_inode_fsync_trans(handle, inode, need_datasync); 4111ac27a0ecSDave Kleikamp out_brelse: 4112ac27a0ecSDave Kleikamp brelse(bh); 4113617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4114ac27a0ecSDave Kleikamp return err; 4115ac27a0ecSDave Kleikamp } 4116ac27a0ecSDave Kleikamp 4117ac27a0ecSDave Kleikamp /* 4118617ba13bSMingming Cao * ext4_write_inode() 4119ac27a0ecSDave Kleikamp * 4120ac27a0ecSDave Kleikamp * We are called from a few places: 4121ac27a0ecSDave Kleikamp * 4122ac27a0ecSDave Kleikamp * - Within generic_file_write() for O_SYNC files. 4123ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 41244907cb7bSAnatol Pomozov * transaction to commit. 4125ac27a0ecSDave Kleikamp * 4126ac27a0ecSDave Kleikamp * - Within sys_sync(), kupdate and such. 4127ac27a0ecSDave Kleikamp * We wait on commit, if tol to. 4128ac27a0ecSDave Kleikamp * 4129ac27a0ecSDave Kleikamp * - Within prune_icache() (PF_MEMALLOC == true) 4130ac27a0ecSDave Kleikamp * Here we simply return. We can't afford to block kswapd on the 4131ac27a0ecSDave Kleikamp * journal commit. 4132ac27a0ecSDave Kleikamp * 4133ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 4134ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 4135617ba13bSMingming Cao * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for 4136ac27a0ecSDave Kleikamp * knfsd. 4137ac27a0ecSDave Kleikamp * 4138ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 4139ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 4140ac27a0ecSDave Kleikamp * which we are interested. 4141ac27a0ecSDave Kleikamp * 4142ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 4143ac27a0ecSDave Kleikamp * 4144ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 4145ac27a0ecSDave Kleikamp * stuff(); 4146ac27a0ecSDave Kleikamp * inode->i_size = expr; 4147ac27a0ecSDave Kleikamp * 4148ac27a0ecSDave Kleikamp * is in error because a kswapd-driven write_inode() could occur while 4149ac27a0ecSDave Kleikamp * `stuff()' is running, and the new i_size will be lost. Plus the inode 4150ac27a0ecSDave Kleikamp * will no longer be on the superblock's dirty inode list. 4151ac27a0ecSDave Kleikamp */ 4152a9185b41SChristoph Hellwig int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) 4153ac27a0ecSDave Kleikamp { 415491ac6f43SFrank Mayhar int err; 415591ac6f43SFrank Mayhar 4156ac27a0ecSDave Kleikamp if (current->flags & PF_MEMALLOC) 4157ac27a0ecSDave Kleikamp return 0; 4158ac27a0ecSDave Kleikamp 415991ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal) { 4160617ba13bSMingming Cao if (ext4_journal_current_handle()) { 4161b38bd33aSMingming Cao jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); 4162ac27a0ecSDave Kleikamp dump_stack(); 4163ac27a0ecSDave Kleikamp return -EIO; 4164ac27a0ecSDave Kleikamp } 4165ac27a0ecSDave Kleikamp 4166a9185b41SChristoph Hellwig if (wbc->sync_mode != WB_SYNC_ALL) 4167ac27a0ecSDave Kleikamp return 0; 4168ac27a0ecSDave Kleikamp 416991ac6f43SFrank Mayhar err = ext4_force_commit(inode->i_sb); 417091ac6f43SFrank Mayhar } else { 417191ac6f43SFrank Mayhar struct ext4_iloc iloc; 417291ac6f43SFrank Mayhar 41738b472d73SCurt Wohlgemuth err = __ext4_get_inode_loc(inode, &iloc, 0); 417491ac6f43SFrank Mayhar if (err) 417591ac6f43SFrank Mayhar return err; 4176a9185b41SChristoph Hellwig if (wbc->sync_mode == WB_SYNC_ALL) 4177830156c7SFrank Mayhar sync_dirty_buffer(iloc.bh); 4178830156c7SFrank Mayhar if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { 4179c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr, 4180c398eda0STheodore Ts'o "IO error syncing inode"); 4181830156c7SFrank Mayhar err = -EIO; 4182830156c7SFrank Mayhar } 4183fd2dd9fbSCurt Wohlgemuth brelse(iloc.bh); 418491ac6f43SFrank Mayhar } 418591ac6f43SFrank Mayhar return err; 4186ac27a0ecSDave Kleikamp } 4187ac27a0ecSDave Kleikamp 4188ac27a0ecSDave Kleikamp /* 418953e87268SJan Kara * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate 419053e87268SJan Kara * buffers that are attached to a page stradding i_size and are undergoing 419153e87268SJan Kara * commit. In that case we have to wait for commit to finish and try again. 419253e87268SJan Kara */ 419353e87268SJan Kara static void ext4_wait_for_tail_page_commit(struct inode *inode) 419453e87268SJan Kara { 419553e87268SJan Kara struct page *page; 419653e87268SJan Kara unsigned offset; 419753e87268SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 419853e87268SJan Kara tid_t commit_tid = 0; 419953e87268SJan Kara int ret; 420053e87268SJan Kara 420153e87268SJan Kara offset = inode->i_size & (PAGE_CACHE_SIZE - 1); 420253e87268SJan Kara /* 420353e87268SJan Kara * All buffers in the last page remain valid? Then there's nothing to 420453e87268SJan Kara * do. We do the check mainly to optimize the common PAGE_CACHE_SIZE == 420553e87268SJan Kara * blocksize case 420653e87268SJan Kara */ 420753e87268SJan Kara if (offset > PAGE_CACHE_SIZE - (1 << inode->i_blkbits)) 420853e87268SJan Kara return; 420953e87268SJan Kara while (1) { 421053e87268SJan Kara page = find_lock_page(inode->i_mapping, 421153e87268SJan Kara inode->i_size >> PAGE_CACHE_SHIFT); 421253e87268SJan Kara if (!page) 421353e87268SJan Kara return; 421453e87268SJan Kara ret = __ext4_journalled_invalidatepage(page, offset); 421553e87268SJan Kara unlock_page(page); 421653e87268SJan Kara page_cache_release(page); 421753e87268SJan Kara if (ret != -EBUSY) 421853e87268SJan Kara return; 421953e87268SJan Kara commit_tid = 0; 422053e87268SJan Kara read_lock(&journal->j_state_lock); 422153e87268SJan Kara if (journal->j_committing_transaction) 422253e87268SJan Kara commit_tid = journal->j_committing_transaction->t_tid; 422353e87268SJan Kara read_unlock(&journal->j_state_lock); 422453e87268SJan Kara if (commit_tid) 422553e87268SJan Kara jbd2_log_wait_commit(journal, commit_tid); 422653e87268SJan Kara } 422753e87268SJan Kara } 422853e87268SJan Kara 422953e87268SJan Kara /* 4230617ba13bSMingming Cao * ext4_setattr() 4231ac27a0ecSDave Kleikamp * 4232ac27a0ecSDave Kleikamp * Called from notify_change. 4233ac27a0ecSDave Kleikamp * 4234ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 4235ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 4236ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 4237ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 4238ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 4239ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 4240ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 4241ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 4242ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 4243ac27a0ecSDave Kleikamp * 4244678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 4245678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 4246678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 4247678aaf48SJan Kara * This way we are sure that all the data written in the previous 4248678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 4249678aaf48SJan Kara * writeback). 4250678aaf48SJan Kara * 4251678aaf48SJan Kara * Called with inode->i_mutex down. 4252ac27a0ecSDave Kleikamp */ 4253617ba13bSMingming Cao int ext4_setattr(struct dentry *dentry, struct iattr *attr) 4254ac27a0ecSDave Kleikamp { 4255ac27a0ecSDave Kleikamp struct inode *inode = dentry->d_inode; 4256ac27a0ecSDave Kleikamp int error, rc = 0; 42573d287de3SDmitry Monakhov int orphan = 0; 4258ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 4259ac27a0ecSDave Kleikamp 4260ac27a0ecSDave Kleikamp error = inode_change_ok(inode, attr); 4261ac27a0ecSDave Kleikamp if (error) 4262ac27a0ecSDave Kleikamp return error; 4263ac27a0ecSDave Kleikamp 426412755627SDmitry Monakhov if (is_quota_modification(inode, attr)) 4265871a2931SChristoph Hellwig dquot_initialize(inode); 426608cefc7aSEric W. Biederman if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) || 426708cefc7aSEric W. Biederman (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) { 4268ac27a0ecSDave Kleikamp handle_t *handle; 4269ac27a0ecSDave Kleikamp 4270ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 4271ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 42729924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 42739924a92aSTheodore Ts'o (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) + 4274194074acSDmitry Monakhov EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3); 4275ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 4276ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 4277ac27a0ecSDave Kleikamp goto err_out; 4278ac27a0ecSDave Kleikamp } 4279b43fa828SChristoph Hellwig error = dquot_transfer(inode, attr); 4280ac27a0ecSDave Kleikamp if (error) { 4281617ba13bSMingming Cao ext4_journal_stop(handle); 4282ac27a0ecSDave Kleikamp return error; 4283ac27a0ecSDave Kleikamp } 4284ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 4285ac27a0ecSDave Kleikamp * one transaction */ 4286ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_UID) 4287ac27a0ecSDave Kleikamp inode->i_uid = attr->ia_uid; 4288ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_GID) 4289ac27a0ecSDave Kleikamp inode->i_gid = attr->ia_gid; 4290617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 4291617ba13bSMingming Cao ext4_journal_stop(handle); 4292ac27a0ecSDave Kleikamp } 4293ac27a0ecSDave Kleikamp 4294e2b46574SEric Sandeen if (attr->ia_valid & ATTR_SIZE) { 4295562c72aaSChristoph Hellwig 429612e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 4297e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4298e2b46574SEric Sandeen 42990c095c7fSTheodore Ts'o if (attr->ia_size > sbi->s_bitmap_maxbytes) 43000c095c7fSTheodore Ts'o return -EFBIG; 4301e2b46574SEric Sandeen } 4302e2b46574SEric Sandeen } 4303e2b46574SEric Sandeen 4304ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode) && 4305c8d46e41SJiaying Zhang attr->ia_valid & ATTR_SIZE && 4306072bd7eaSTheodore Ts'o (attr->ia_size < inode->i_size)) { 4307ac27a0ecSDave Kleikamp handle_t *handle; 4308ac27a0ecSDave Kleikamp 43099924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 3); 4310ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 4311ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 4312ac27a0ecSDave Kleikamp goto err_out; 4313ac27a0ecSDave Kleikamp } 43143d287de3SDmitry Monakhov if (ext4_handle_valid(handle)) { 4315617ba13bSMingming Cao error = ext4_orphan_add(handle, inode); 43163d287de3SDmitry Monakhov orphan = 1; 43173d287de3SDmitry Monakhov } 4318617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 4319617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 4320ac27a0ecSDave Kleikamp if (!error) 4321ac27a0ecSDave Kleikamp error = rc; 4322617ba13bSMingming Cao ext4_journal_stop(handle); 4323678aaf48SJan Kara 4324678aaf48SJan Kara if (ext4_should_order_data(inode)) { 4325678aaf48SJan Kara error = ext4_begin_ordered_truncate(inode, 4326678aaf48SJan Kara attr->ia_size); 4327678aaf48SJan Kara if (error) { 4328678aaf48SJan Kara /* Do as much error cleanup as possible */ 43299924a92aSTheodore Ts'o handle = ext4_journal_start(inode, 43309924a92aSTheodore Ts'o EXT4_HT_INODE, 3); 4331678aaf48SJan Kara if (IS_ERR(handle)) { 4332678aaf48SJan Kara ext4_orphan_del(NULL, inode); 4333678aaf48SJan Kara goto err_out; 4334678aaf48SJan Kara } 4335678aaf48SJan Kara ext4_orphan_del(handle, inode); 43363d287de3SDmitry Monakhov orphan = 0; 4337678aaf48SJan Kara ext4_journal_stop(handle); 4338678aaf48SJan Kara goto err_out; 4339678aaf48SJan Kara } 4340678aaf48SJan Kara } 4341ac27a0ecSDave Kleikamp } 4342ac27a0ecSDave Kleikamp 4343072bd7eaSTheodore Ts'o if (attr->ia_valid & ATTR_SIZE) { 434453e87268SJan Kara if (attr->ia_size != inode->i_size) { 434553e87268SJan Kara loff_t oldsize = inode->i_size; 434653e87268SJan Kara 434753e87268SJan Kara i_size_write(inode, attr->ia_size); 434853e87268SJan Kara /* 434953e87268SJan Kara * Blocks are going to be removed from the inode. Wait 435053e87268SJan Kara * for dio in flight. Temporarily disable 435153e87268SJan Kara * dioread_nolock to prevent livelock. 435253e87268SJan Kara */ 43531b65007eSDmitry Monakhov if (orphan) { 435453e87268SJan Kara if (!ext4_should_journal_data(inode)) { 43551b65007eSDmitry Monakhov ext4_inode_block_unlocked_dio(inode); 43561c9114f9SDmitry Monakhov inode_dio_wait(inode); 43571b65007eSDmitry Monakhov ext4_inode_resume_unlocked_dio(inode); 435853e87268SJan Kara } else 435953e87268SJan Kara ext4_wait_for_tail_page_commit(inode); 43601b65007eSDmitry Monakhov } 436153e87268SJan Kara /* 436253e87268SJan Kara * Truncate pagecache after we've waited for commit 436353e87268SJan Kara * in data=journal mode to make pages freeable. 436453e87268SJan Kara */ 436553e87268SJan Kara truncate_pagecache(inode, oldsize, inode->i_size); 43661c9114f9SDmitry Monakhov } 4367072bd7eaSTheodore Ts'o ext4_truncate(inode); 4368072bd7eaSTheodore Ts'o } 4369ac27a0ecSDave Kleikamp 43701025774cSChristoph Hellwig if (!rc) { 43711025774cSChristoph Hellwig setattr_copy(inode, attr); 43721025774cSChristoph Hellwig mark_inode_dirty(inode); 43731025774cSChristoph Hellwig } 43741025774cSChristoph Hellwig 43751025774cSChristoph Hellwig /* 43761025774cSChristoph Hellwig * If the call to ext4_truncate failed to get a transaction handle at 43771025774cSChristoph Hellwig * all, we need to clean up the in-core orphan list manually. 43781025774cSChristoph Hellwig */ 43793d287de3SDmitry Monakhov if (orphan && inode->i_nlink) 4380617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 4381ac27a0ecSDave Kleikamp 4382ac27a0ecSDave Kleikamp if (!rc && (ia_valid & ATTR_MODE)) 4383617ba13bSMingming Cao rc = ext4_acl_chmod(inode); 4384ac27a0ecSDave Kleikamp 4385ac27a0ecSDave Kleikamp err_out: 4386617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 4387ac27a0ecSDave Kleikamp if (!error) 4388ac27a0ecSDave Kleikamp error = rc; 4389ac27a0ecSDave Kleikamp return error; 4390ac27a0ecSDave Kleikamp } 4391ac27a0ecSDave Kleikamp 43923e3398a0SMingming Cao int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry, 43933e3398a0SMingming Cao struct kstat *stat) 43943e3398a0SMingming Cao { 43953e3398a0SMingming Cao struct inode *inode; 43963e3398a0SMingming Cao unsigned long delalloc_blocks; 43973e3398a0SMingming Cao 43983e3398a0SMingming Cao inode = dentry->d_inode; 43993e3398a0SMingming Cao generic_fillattr(inode, stat); 44003e3398a0SMingming Cao 44013e3398a0SMingming Cao /* 44023e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 44033e3398a0SMingming Cao * otherwise in the case of system crash before the real block 44043e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 44053e3398a0SMingming Cao * on-disk file blocks. 44063e3398a0SMingming Cao * We always keep i_blocks updated together with real 44073e3398a0SMingming Cao * allocation. But to not confuse with user, stat 44083e3398a0SMingming Cao * will return the blocks that include the delayed allocation 44093e3398a0SMingming Cao * blocks for this file. 44103e3398a0SMingming Cao */ 441196607551STao Ma delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb), 441296607551STao Ma EXT4_I(inode)->i_reserved_data_blocks); 44133e3398a0SMingming Cao 44143e3398a0SMingming Cao stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9; 44153e3398a0SMingming Cao return 0; 44163e3398a0SMingming Cao } 4417ac27a0ecSDave Kleikamp 4418a02908f1SMingming Cao static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4419a02908f1SMingming Cao { 442012e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 44218bb2b247SAmir Goldstein return ext4_ind_trans_blocks(inode, nrblocks, chunk); 4422ac51d837STheodore Ts'o return ext4_ext_index_trans_blocks(inode, nrblocks, chunk); 4423a02908f1SMingming Cao } 4424ac51d837STheodore Ts'o 4425a02908f1SMingming Cao /* 4426a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 4427a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 4428a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 4429a02908f1SMingming Cao * 4430a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 44314907cb7bSAnatol Pomozov * different block groups too. If they are contiguous, with flexbg, 4432a02908f1SMingming Cao * they could still across block group boundary. 4433a02908f1SMingming Cao * 4434a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 4435a02908f1SMingming Cao */ 44361f109d5aSTheodore Ts'o static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4437a02908f1SMingming Cao { 44388df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 44398df9675fSTheodore Ts'o int gdpblocks; 4440a02908f1SMingming Cao int idxblocks; 4441a02908f1SMingming Cao int ret = 0; 4442a02908f1SMingming Cao 4443a02908f1SMingming Cao /* 4444a02908f1SMingming Cao * How many index blocks need to touch to modify nrblocks? 4445a02908f1SMingming Cao * The "Chunk" flag indicating whether the nrblocks is 4446a02908f1SMingming Cao * physically contiguous on disk 4447a02908f1SMingming Cao * 4448a02908f1SMingming Cao * For Direct IO and fallocate, they calls get_block to allocate 4449a02908f1SMingming Cao * one single extent at a time, so they could set the "Chunk" flag 4450a02908f1SMingming Cao */ 4451a02908f1SMingming Cao idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk); 4452a02908f1SMingming Cao 4453a02908f1SMingming Cao ret = idxblocks; 4454a02908f1SMingming Cao 4455a02908f1SMingming Cao /* 4456a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 4457a02908f1SMingming Cao * to account 4458a02908f1SMingming Cao */ 4459a02908f1SMingming Cao groups = idxblocks; 4460a02908f1SMingming Cao if (chunk) 4461a02908f1SMingming Cao groups += 1; 4462ac27a0ecSDave Kleikamp else 4463a02908f1SMingming Cao groups += nrblocks; 4464ac27a0ecSDave Kleikamp 4465a02908f1SMingming Cao gdpblocks = groups; 44668df9675fSTheodore Ts'o if (groups > ngroups) 44678df9675fSTheodore Ts'o groups = ngroups; 4468a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 4469a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 4470a02908f1SMingming Cao 4471a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 4472a02908f1SMingming Cao ret += groups + gdpblocks; 4473a02908f1SMingming Cao 4474a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 4475a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 4476ac27a0ecSDave Kleikamp 4477ac27a0ecSDave Kleikamp return ret; 4478ac27a0ecSDave Kleikamp } 4479ac27a0ecSDave Kleikamp 4480ac27a0ecSDave Kleikamp /* 448125985edcSLucas De Marchi * Calculate the total number of credits to reserve to fit 4482f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 4483f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 4484a02908f1SMingming Cao * 4485525f4ed8SMingming Cao * This could be called via ext4_write_begin() 4486a02908f1SMingming Cao * 4487525f4ed8SMingming Cao * We need to consider the worse case, when 4488a02908f1SMingming Cao * one new block per extent. 4489a02908f1SMingming Cao */ 4490a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 4491a02908f1SMingming Cao { 4492a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 4493a02908f1SMingming Cao int ret; 4494a02908f1SMingming Cao 4495a02908f1SMingming Cao ret = ext4_meta_trans_blocks(inode, bpp, 0); 4496a02908f1SMingming Cao 4497a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 4498a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 4499a02908f1SMingming Cao ret += bpp; 4500a02908f1SMingming Cao return ret; 4501a02908f1SMingming Cao } 4502f3bd1f3fSMingming Cao 4503f3bd1f3fSMingming Cao /* 4504f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 4505f3bd1f3fSMingming Cao * 4506f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 450779e83036SEric Sandeen * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks. 4508f3bd1f3fSMingming Cao * 4509f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 4510f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 4511f3bd1f3fSMingming Cao */ 4512f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 4513f3bd1f3fSMingming Cao { 4514f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 4515f3bd1f3fSMingming Cao } 4516f3bd1f3fSMingming Cao 4517a02908f1SMingming Cao /* 4518617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 4519ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 4520ac27a0ecSDave Kleikamp */ 4521617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 4522617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 4523ac27a0ecSDave Kleikamp { 4524ac27a0ecSDave Kleikamp int err = 0; 4525ac27a0ecSDave Kleikamp 4526c64db50eSTheodore Ts'o if (IS_I_VERSION(inode)) 452725ec56b5SJean Noel Cordenner inode_inc_iversion(inode); 452825ec56b5SJean Noel Cordenner 4529ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 4530ac27a0ecSDave Kleikamp get_bh(iloc->bh); 4531ac27a0ecSDave Kleikamp 4532dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 4533830156c7SFrank Mayhar err = ext4_do_update_inode(handle, inode, iloc); 4534ac27a0ecSDave Kleikamp put_bh(iloc->bh); 4535ac27a0ecSDave Kleikamp return err; 4536ac27a0ecSDave Kleikamp } 4537ac27a0ecSDave Kleikamp 4538ac27a0ecSDave Kleikamp /* 4539ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 4540ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 4541ac27a0ecSDave Kleikamp */ 4542ac27a0ecSDave Kleikamp 4543ac27a0ecSDave Kleikamp int 4544617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 4545617ba13bSMingming Cao struct ext4_iloc *iloc) 4546ac27a0ecSDave Kleikamp { 45470390131bSFrank Mayhar int err; 45480390131bSFrank Mayhar 4549617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 4550ac27a0ecSDave Kleikamp if (!err) { 4551ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 4552617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, iloc->bh); 4553ac27a0ecSDave Kleikamp if (err) { 4554ac27a0ecSDave Kleikamp brelse(iloc->bh); 4555ac27a0ecSDave Kleikamp iloc->bh = NULL; 4556ac27a0ecSDave Kleikamp } 4557ac27a0ecSDave Kleikamp } 4558617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4559ac27a0ecSDave Kleikamp return err; 4560ac27a0ecSDave Kleikamp } 4561ac27a0ecSDave Kleikamp 4562ac27a0ecSDave Kleikamp /* 45636dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 45646dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 45656dd4ee7cSKalpak Shah */ 45661d03ec98SAneesh Kumar K.V static int ext4_expand_extra_isize(struct inode *inode, 45671d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 45681d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 45691d03ec98SAneesh Kumar K.V handle_t *handle) 45706dd4ee7cSKalpak Shah { 45716dd4ee7cSKalpak Shah struct ext4_inode *raw_inode; 45726dd4ee7cSKalpak Shah struct ext4_xattr_ibody_header *header; 45736dd4ee7cSKalpak Shah 45746dd4ee7cSKalpak Shah if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) 45756dd4ee7cSKalpak Shah return 0; 45766dd4ee7cSKalpak Shah 45776dd4ee7cSKalpak Shah raw_inode = ext4_raw_inode(&iloc); 45786dd4ee7cSKalpak Shah 45796dd4ee7cSKalpak Shah header = IHDR(inode, raw_inode); 45806dd4ee7cSKalpak Shah 45816dd4ee7cSKalpak Shah /* No extended attributes present */ 458219f5fb7aSTheodore Ts'o if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) || 45836dd4ee7cSKalpak Shah header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 45846dd4ee7cSKalpak Shah memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0, 45856dd4ee7cSKalpak Shah new_extra_isize); 45866dd4ee7cSKalpak Shah EXT4_I(inode)->i_extra_isize = new_extra_isize; 45876dd4ee7cSKalpak Shah return 0; 45886dd4ee7cSKalpak Shah } 45896dd4ee7cSKalpak Shah 45906dd4ee7cSKalpak Shah /* try to expand with EAs present */ 45916dd4ee7cSKalpak Shah return ext4_expand_extra_isize_ea(inode, new_extra_isize, 45926dd4ee7cSKalpak Shah raw_inode, handle); 45936dd4ee7cSKalpak Shah } 45946dd4ee7cSKalpak Shah 45956dd4ee7cSKalpak Shah /* 4596ac27a0ecSDave Kleikamp * What we do here is to mark the in-core inode as clean with respect to inode 4597ac27a0ecSDave Kleikamp * dirtiness (it may still be data-dirty). 4598ac27a0ecSDave Kleikamp * This means that the in-core inode may be reaped by prune_icache 4599ac27a0ecSDave Kleikamp * without having to perform any I/O. This is a very good thing, 4600ac27a0ecSDave Kleikamp * because *any* task may call prune_icache - even ones which 4601ac27a0ecSDave Kleikamp * have a transaction open against a different journal. 4602ac27a0ecSDave Kleikamp * 4603ac27a0ecSDave Kleikamp * Is this cheating? Not really. Sure, we haven't written the 4604ac27a0ecSDave Kleikamp * inode out, but prune_icache isn't a user-visible syncing function. 4605ac27a0ecSDave Kleikamp * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 4606ac27a0ecSDave Kleikamp * we start and wait on commits. 4607ac27a0ecSDave Kleikamp */ 4608617ba13bSMingming Cao int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode) 4609ac27a0ecSDave Kleikamp { 4610617ba13bSMingming Cao struct ext4_iloc iloc; 46116dd4ee7cSKalpak Shah struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 46126dd4ee7cSKalpak Shah static unsigned int mnt_count; 46136dd4ee7cSKalpak Shah int err, ret; 4614ac27a0ecSDave Kleikamp 4615ac27a0ecSDave Kleikamp might_sleep(); 46167ff9c073STheodore Ts'o trace_ext4_mark_inode_dirty(inode, _RET_IP_); 4617617ba13bSMingming Cao err = ext4_reserve_inode_write(handle, inode, &iloc); 46180390131bSFrank Mayhar if (ext4_handle_valid(handle) && 46190390131bSFrank Mayhar EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize && 462019f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) { 46216dd4ee7cSKalpak Shah /* 46226dd4ee7cSKalpak Shah * We need extra buffer credits since we may write into EA block 46236dd4ee7cSKalpak Shah * with this same handle. If journal_extend fails, then it will 46246dd4ee7cSKalpak Shah * only result in a minor loss of functionality for that inode. 46256dd4ee7cSKalpak Shah * If this is felt to be critical, then e2fsck should be run to 46266dd4ee7cSKalpak Shah * force a large enough s_min_extra_isize. 46276dd4ee7cSKalpak Shah */ 46286dd4ee7cSKalpak Shah if ((jbd2_journal_extend(handle, 46296dd4ee7cSKalpak Shah EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) { 46306dd4ee7cSKalpak Shah ret = ext4_expand_extra_isize(inode, 46316dd4ee7cSKalpak Shah sbi->s_want_extra_isize, 46326dd4ee7cSKalpak Shah iloc, handle); 46336dd4ee7cSKalpak Shah if (ret) { 463419f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, 463519f5fb7aSTheodore Ts'o EXT4_STATE_NO_EXPAND); 4636c1bddad9SAneesh Kumar K.V if (mnt_count != 4637c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count)) { 463812062dddSEric Sandeen ext4_warning(inode->i_sb, 46396dd4ee7cSKalpak Shah "Unable to expand inode %lu. Delete" 46406dd4ee7cSKalpak Shah " some EAs or run e2fsck.", 46416dd4ee7cSKalpak Shah inode->i_ino); 4642c1bddad9SAneesh Kumar K.V mnt_count = 4643c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count); 46446dd4ee7cSKalpak Shah } 46456dd4ee7cSKalpak Shah } 46466dd4ee7cSKalpak Shah } 46476dd4ee7cSKalpak Shah } 4648ac27a0ecSDave Kleikamp if (!err) 4649617ba13bSMingming Cao err = ext4_mark_iloc_dirty(handle, inode, &iloc); 4650ac27a0ecSDave Kleikamp return err; 4651ac27a0ecSDave Kleikamp } 4652ac27a0ecSDave Kleikamp 4653ac27a0ecSDave Kleikamp /* 4654617ba13bSMingming Cao * ext4_dirty_inode() is called from __mark_inode_dirty() 4655ac27a0ecSDave Kleikamp * 4656ac27a0ecSDave Kleikamp * We're really interested in the case where a file is being extended. 4657ac27a0ecSDave Kleikamp * i_size has been changed by generic_commit_write() and we thus need 4658ac27a0ecSDave Kleikamp * to include the updated inode in the current transaction. 4659ac27a0ecSDave Kleikamp * 46605dd4056dSChristoph Hellwig * Also, dquot_alloc_block() will always dirty the inode when blocks 4661ac27a0ecSDave Kleikamp * are allocated to the file. 4662ac27a0ecSDave Kleikamp * 4663ac27a0ecSDave Kleikamp * If the inode is marked synchronous, we don't honour that here - doing 4664ac27a0ecSDave Kleikamp * so would cause a commit on atime updates, which we don't bother doing. 4665ac27a0ecSDave Kleikamp * We handle synchronous inodes at the highest possible level. 4666ac27a0ecSDave Kleikamp */ 4667aa385729SChristoph Hellwig void ext4_dirty_inode(struct inode *inode, int flags) 4668ac27a0ecSDave Kleikamp { 4669ac27a0ecSDave Kleikamp handle_t *handle; 4670ac27a0ecSDave Kleikamp 46719924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 2); 4672ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 4673ac27a0ecSDave Kleikamp goto out; 4674f3dc272fSCurt Wohlgemuth 4675617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 4676f3dc272fSCurt Wohlgemuth 4677617ba13bSMingming Cao ext4_journal_stop(handle); 4678ac27a0ecSDave Kleikamp out: 4679ac27a0ecSDave Kleikamp return; 4680ac27a0ecSDave Kleikamp } 4681ac27a0ecSDave Kleikamp 4682ac27a0ecSDave Kleikamp #if 0 4683ac27a0ecSDave Kleikamp /* 4684ac27a0ecSDave Kleikamp * Bind an inode's backing buffer_head into this transaction, to prevent 4685ac27a0ecSDave Kleikamp * it from being flushed to disk early. Unlike 4686617ba13bSMingming Cao * ext4_reserve_inode_write, this leaves behind no bh reference and 4687ac27a0ecSDave Kleikamp * returns no iloc structure, so the caller needs to repeat the iloc 4688ac27a0ecSDave Kleikamp * lookup to mark the inode dirty later. 4689ac27a0ecSDave Kleikamp */ 4690617ba13bSMingming Cao static int ext4_pin_inode(handle_t *handle, struct inode *inode) 4691ac27a0ecSDave Kleikamp { 4692617ba13bSMingming Cao struct ext4_iloc iloc; 4693ac27a0ecSDave Kleikamp 4694ac27a0ecSDave Kleikamp int err = 0; 4695ac27a0ecSDave Kleikamp if (handle) { 4696617ba13bSMingming Cao err = ext4_get_inode_loc(inode, &iloc); 4697ac27a0ecSDave Kleikamp if (!err) { 4698ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc.bh, "get_write_access"); 4699dab291afSMingming Cao err = jbd2_journal_get_write_access(handle, iloc.bh); 4700ac27a0ecSDave Kleikamp if (!err) 47010390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, 470273b50c1cSCurt Wohlgemuth NULL, 4703ac27a0ecSDave Kleikamp iloc.bh); 4704ac27a0ecSDave Kleikamp brelse(iloc.bh); 4705ac27a0ecSDave Kleikamp } 4706ac27a0ecSDave Kleikamp } 4707617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4708ac27a0ecSDave Kleikamp return err; 4709ac27a0ecSDave Kleikamp } 4710ac27a0ecSDave Kleikamp #endif 4711ac27a0ecSDave Kleikamp 4712617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val) 4713ac27a0ecSDave Kleikamp { 4714ac27a0ecSDave Kleikamp journal_t *journal; 4715ac27a0ecSDave Kleikamp handle_t *handle; 4716ac27a0ecSDave Kleikamp int err; 4717ac27a0ecSDave Kleikamp 4718ac27a0ecSDave Kleikamp /* 4719ac27a0ecSDave Kleikamp * We have to be very careful here: changing a data block's 4720ac27a0ecSDave Kleikamp * journaling status dynamically is dangerous. If we write a 4721ac27a0ecSDave Kleikamp * data block to the journal, change the status and then delete 4722ac27a0ecSDave Kleikamp * that block, we risk forgetting to revoke the old log record 4723ac27a0ecSDave Kleikamp * from the journal and so a subsequent replay can corrupt data. 4724ac27a0ecSDave Kleikamp * So, first we make sure that the journal is empty and that 4725ac27a0ecSDave Kleikamp * nobody is changing anything. 4726ac27a0ecSDave Kleikamp */ 4727ac27a0ecSDave Kleikamp 4728617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 47290390131bSFrank Mayhar if (!journal) 47300390131bSFrank Mayhar return 0; 4731d699594dSDave Hansen if (is_journal_aborted(journal)) 4732ac27a0ecSDave Kleikamp return -EROFS; 47332aff57b0SYongqiang Yang /* We have to allocate physical blocks for delalloc blocks 47342aff57b0SYongqiang Yang * before flushing journal. otherwise delalloc blocks can not 47352aff57b0SYongqiang Yang * be allocated any more. even more truncate on delalloc blocks 47362aff57b0SYongqiang Yang * could trigger BUG by flushing delalloc blocks in journal. 47372aff57b0SYongqiang Yang * There is no delalloc block in non-journal data mode. 47382aff57b0SYongqiang Yang */ 47392aff57b0SYongqiang Yang if (val && test_opt(inode->i_sb, DELALLOC)) { 47402aff57b0SYongqiang Yang err = ext4_alloc_da_blocks(inode); 47412aff57b0SYongqiang Yang if (err < 0) 47422aff57b0SYongqiang Yang return err; 47432aff57b0SYongqiang Yang } 4744ac27a0ecSDave Kleikamp 474517335dccSDmitry Monakhov /* Wait for all existing dio workers */ 474617335dccSDmitry Monakhov ext4_inode_block_unlocked_dio(inode); 474717335dccSDmitry Monakhov inode_dio_wait(inode); 474817335dccSDmitry Monakhov 4749dab291afSMingming Cao jbd2_journal_lock_updates(journal); 4750ac27a0ecSDave Kleikamp 4751ac27a0ecSDave Kleikamp /* 4752ac27a0ecSDave Kleikamp * OK, there are no updates running now, and all cached data is 4753ac27a0ecSDave Kleikamp * synced to disk. We are now in a completely consistent state 4754ac27a0ecSDave Kleikamp * which doesn't have anything in the journal, and we know that 4755ac27a0ecSDave Kleikamp * no filesystem updates are running, so it is safe to modify 4756ac27a0ecSDave Kleikamp * the inode's in-core data-journaling state flag now. 4757ac27a0ecSDave Kleikamp */ 4758ac27a0ecSDave Kleikamp 4759ac27a0ecSDave Kleikamp if (val) 476012e9b892SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 47615872ddaaSYongqiang Yang else { 47625872ddaaSYongqiang Yang jbd2_journal_flush(journal); 476312e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 47645872ddaaSYongqiang Yang } 4765617ba13bSMingming Cao ext4_set_aops(inode); 4766ac27a0ecSDave Kleikamp 4767dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 476817335dccSDmitry Monakhov ext4_inode_resume_unlocked_dio(inode); 4769ac27a0ecSDave Kleikamp 4770ac27a0ecSDave Kleikamp /* Finally we can mark the inode as dirty. */ 4771ac27a0ecSDave Kleikamp 47729924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 4773ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 4774ac27a0ecSDave Kleikamp return PTR_ERR(handle); 4775ac27a0ecSDave Kleikamp 4776617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 47770390131bSFrank Mayhar ext4_handle_sync(handle); 4778617ba13bSMingming Cao ext4_journal_stop(handle); 4779617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4780ac27a0ecSDave Kleikamp 4781ac27a0ecSDave Kleikamp return err; 4782ac27a0ecSDave Kleikamp } 47832e9ee850SAneesh Kumar K.V 47842e9ee850SAneesh Kumar K.V static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) 47852e9ee850SAneesh Kumar K.V { 47862e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 47872e9ee850SAneesh Kumar K.V } 47882e9ee850SAneesh Kumar K.V 4789c2ec175cSNick Piggin int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) 47902e9ee850SAneesh Kumar K.V { 4791c2ec175cSNick Piggin struct page *page = vmf->page; 47922e9ee850SAneesh Kumar K.V loff_t size; 47932e9ee850SAneesh Kumar K.V unsigned long len; 47949ea7df53SJan Kara int ret; 47952e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 47962e9ee850SAneesh Kumar K.V struct inode *inode = file->f_path.dentry->d_inode; 47972e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 47989ea7df53SJan Kara handle_t *handle; 47999ea7df53SJan Kara get_block_t *get_block; 48009ea7df53SJan Kara int retries = 0; 48012e9ee850SAneesh Kumar K.V 48028e8ad8a5SJan Kara sb_start_pagefault(inode->i_sb); 4803041bbb6dSTheodore Ts'o file_update_time(vma->vm_file); 48049ea7df53SJan Kara /* Delalloc case is easy... */ 48059ea7df53SJan Kara if (test_opt(inode->i_sb, DELALLOC) && 48069ea7df53SJan Kara !ext4_should_journal_data(inode) && 48079ea7df53SJan Kara !ext4_nonda_switch(inode->i_sb)) { 48089ea7df53SJan Kara do { 48099ea7df53SJan Kara ret = __block_page_mkwrite(vma, vmf, 48109ea7df53SJan Kara ext4_da_get_block_prep); 48119ea7df53SJan Kara } while (ret == -ENOSPC && 48129ea7df53SJan Kara ext4_should_retry_alloc(inode->i_sb, &retries)); 48139ea7df53SJan Kara goto out_ret; 48142e9ee850SAneesh Kumar K.V } 48150e499890SDarrick J. Wong 48160e499890SDarrick J. Wong lock_page(page); 48179ea7df53SJan Kara size = i_size_read(inode); 48189ea7df53SJan Kara /* Page got truncated from under us? */ 48199ea7df53SJan Kara if (page->mapping != mapping || page_offset(page) > size) { 48209ea7df53SJan Kara unlock_page(page); 48219ea7df53SJan Kara ret = VM_FAULT_NOPAGE; 48229ea7df53SJan Kara goto out; 48230e499890SDarrick J. Wong } 48242e9ee850SAneesh Kumar K.V 48252e9ee850SAneesh Kumar K.V if (page->index == size >> PAGE_CACHE_SHIFT) 48262e9ee850SAneesh Kumar K.V len = size & ~PAGE_CACHE_MASK; 48272e9ee850SAneesh Kumar K.V else 48282e9ee850SAneesh Kumar K.V len = PAGE_CACHE_SIZE; 4829a827eaffSAneesh Kumar K.V /* 48309ea7df53SJan Kara * Return if we have all the buffers mapped. This avoids the need to do 48319ea7df53SJan Kara * journal_start/journal_stop which can block and take a long time 4832a827eaffSAneesh Kumar K.V */ 48332e9ee850SAneesh Kumar K.V if (page_has_buffers(page)) { 4834f19d5870STao Ma if (!ext4_walk_page_buffers(NULL, page_buffers(page), 4835f19d5870STao Ma 0, len, NULL, 4836a827eaffSAneesh Kumar K.V ext4_bh_unmapped)) { 48379ea7df53SJan Kara /* Wait so that we don't change page under IO */ 48389ea7df53SJan Kara wait_on_page_writeback(page); 48399ea7df53SJan Kara ret = VM_FAULT_LOCKED; 48409ea7df53SJan Kara goto out; 48412e9ee850SAneesh Kumar K.V } 4842a827eaffSAneesh Kumar K.V } 4843a827eaffSAneesh Kumar K.V unlock_page(page); 48449ea7df53SJan Kara /* OK, we need to fill the hole... */ 48459ea7df53SJan Kara if (ext4_should_dioread_nolock(inode)) 48469ea7df53SJan Kara get_block = ext4_get_block_write; 48479ea7df53SJan Kara else 48489ea7df53SJan Kara get_block = ext4_get_block; 48499ea7df53SJan Kara retry_alloc: 48509924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 48519924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 48529ea7df53SJan Kara if (IS_ERR(handle)) { 4853c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 48549ea7df53SJan Kara goto out; 48559ea7df53SJan Kara } 48569ea7df53SJan Kara ret = __block_page_mkwrite(vma, vmf, get_block); 48579ea7df53SJan Kara if (!ret && ext4_should_journal_data(inode)) { 4858f19d5870STao Ma if (ext4_walk_page_buffers(handle, page_buffers(page), 0, 48599ea7df53SJan Kara PAGE_CACHE_SIZE, NULL, do_journal_get_write_access)) { 48609ea7df53SJan Kara unlock_page(page); 48619ea7df53SJan Kara ret = VM_FAULT_SIGBUS; 4862fcbb5515SYongqiang Yang ext4_journal_stop(handle); 48639ea7df53SJan Kara goto out; 48649ea7df53SJan Kara } 48659ea7df53SJan Kara ext4_set_inode_state(inode, EXT4_STATE_JDATA); 48669ea7df53SJan Kara } 48679ea7df53SJan Kara ext4_journal_stop(handle); 48689ea7df53SJan Kara if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 48699ea7df53SJan Kara goto retry_alloc; 48709ea7df53SJan Kara out_ret: 48719ea7df53SJan Kara ret = block_page_mkwrite_return(ret); 48729ea7df53SJan Kara out: 48738e8ad8a5SJan Kara sb_end_pagefault(inode->i_sb); 48742e9ee850SAneesh Kumar K.V return ret; 48752e9ee850SAneesh Kumar K.V } 4876