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 1880930fcc1SAl Viro if (inode->i_nlink) { 1892d859db3SJan Kara /* 1902d859db3SJan Kara * When journalling data dirty buffers are tracked only in the 1912d859db3SJan Kara * journal. So although mm thinks everything is clean and 1922d859db3SJan Kara * ready for reaping the inode might still have some pages to 1932d859db3SJan Kara * write in the running transaction or waiting to be 1942d859db3SJan Kara * checkpointed. Thus calling jbd2_journal_invalidatepage() 1952d859db3SJan Kara * (via truncate_inode_pages()) to discard these buffers can 1962d859db3SJan Kara * cause data loss. Also even if we did not discard these 1972d859db3SJan Kara * buffers, we would have no way to find them after the inode 1982d859db3SJan Kara * is reaped and thus user could see stale data if he tries to 1992d859db3SJan Kara * read them before the transaction is checkpointed. So be 2002d859db3SJan Kara * careful and force everything to disk here... We use 2012d859db3SJan Kara * ei->i_datasync_tid to store the newest transaction 2022d859db3SJan Kara * containing inode's data. 2032d859db3SJan Kara * 2042d859db3SJan Kara * Note that directories do not have this problem because they 2052d859db3SJan Kara * don't use page cache. 2062d859db3SJan Kara */ 2072d859db3SJan Kara if (ext4_should_journal_data(inode) && 208*2b405bfaSTheodore Ts'o (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode)) && 209*2b405bfaSTheodore Ts'o inode->i_ino != EXT4_JOURNAL_INO) { 2102d859db3SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 2112d859db3SJan Kara tid_t commit_tid = EXT4_I(inode)->i_datasync_tid; 2122d859db3SJan Kara 2132d859db3SJan Kara jbd2_log_start_commit(journal, commit_tid); 2142d859db3SJan Kara jbd2_log_wait_commit(journal, commit_tid); 2152d859db3SJan Kara filemap_write_and_wait(&inode->i_data); 2162d859db3SJan Kara } 2170930fcc1SAl Viro truncate_inode_pages(&inode->i_data, 0); 2181ada47d9STheodore Ts'o ext4_ioend_shutdown(inode); 2190930fcc1SAl Viro goto no_delete; 2200930fcc1SAl Viro } 2210930fcc1SAl Viro 222907f4554SChristoph Hellwig if (!is_bad_inode(inode)) 223871a2931SChristoph Hellwig dquot_initialize(inode); 224907f4554SChristoph Hellwig 225678aaf48SJan Kara if (ext4_should_order_data(inode)) 226678aaf48SJan Kara ext4_begin_ordered_truncate(inode, 0); 227ac27a0ecSDave Kleikamp truncate_inode_pages(&inode->i_data, 0); 2281ada47d9STheodore Ts'o ext4_ioend_shutdown(inode); 229ac27a0ecSDave Kleikamp 230ac27a0ecSDave Kleikamp if (is_bad_inode(inode)) 231ac27a0ecSDave Kleikamp goto no_delete; 232ac27a0ecSDave Kleikamp 2338e8ad8a5SJan Kara /* 2348e8ad8a5SJan Kara * Protect us against freezing - iput() caller didn't have to have any 2358e8ad8a5SJan Kara * protection against it 2368e8ad8a5SJan Kara */ 2378e8ad8a5SJan Kara sb_start_intwrite(inode->i_sb); 2389924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, 2399924a92aSTheodore Ts'o ext4_blocks_for_truncate(inode)+3); 240ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 241bc965ab3STheodore Ts'o ext4_std_error(inode->i_sb, PTR_ERR(handle)); 242ac27a0ecSDave Kleikamp /* 243ac27a0ecSDave Kleikamp * If we're going to skip the normal cleanup, we still need to 244ac27a0ecSDave Kleikamp * make sure that the in-core orphan linked list is properly 245ac27a0ecSDave Kleikamp * cleaned up. 246ac27a0ecSDave Kleikamp */ 247617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 2488e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 249ac27a0ecSDave Kleikamp goto no_delete; 250ac27a0ecSDave Kleikamp } 251ac27a0ecSDave Kleikamp 252ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 2530390131bSFrank Mayhar ext4_handle_sync(handle); 254ac27a0ecSDave Kleikamp inode->i_size = 0; 255bc965ab3STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 256bc965ab3STheodore Ts'o if (err) { 25712062dddSEric Sandeen ext4_warning(inode->i_sb, 258bc965ab3STheodore Ts'o "couldn't mark inode dirty (err %d)", err); 259bc965ab3STheodore Ts'o goto stop_handle; 260bc965ab3STheodore Ts'o } 261ac27a0ecSDave Kleikamp if (inode->i_blocks) 262617ba13bSMingming Cao ext4_truncate(inode); 263bc965ab3STheodore Ts'o 264bc965ab3STheodore Ts'o /* 265bc965ab3STheodore Ts'o * ext4_ext_truncate() doesn't reserve any slop when it 266bc965ab3STheodore Ts'o * restarts journal transactions; therefore there may not be 267bc965ab3STheodore Ts'o * enough credits left in the handle to remove the inode from 268bc965ab3STheodore Ts'o * the orphan list and set the dtime field. 269bc965ab3STheodore Ts'o */ 2700390131bSFrank Mayhar if (!ext4_handle_has_enough_credits(handle, 3)) { 271bc965ab3STheodore Ts'o err = ext4_journal_extend(handle, 3); 272bc965ab3STheodore Ts'o if (err > 0) 273bc965ab3STheodore Ts'o err = ext4_journal_restart(handle, 3); 274bc965ab3STheodore Ts'o if (err != 0) { 27512062dddSEric Sandeen ext4_warning(inode->i_sb, 276bc965ab3STheodore Ts'o "couldn't extend journal (err %d)", err); 277bc965ab3STheodore Ts'o stop_handle: 278bc965ab3STheodore Ts'o ext4_journal_stop(handle); 27945388219STheodore Ts'o ext4_orphan_del(NULL, inode); 2808e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 281bc965ab3STheodore Ts'o goto no_delete; 282bc965ab3STheodore Ts'o } 283bc965ab3STheodore Ts'o } 284bc965ab3STheodore Ts'o 285ac27a0ecSDave Kleikamp /* 286617ba13bSMingming Cao * Kill off the orphan record which ext4_truncate created. 287ac27a0ecSDave Kleikamp * AKPM: I think this can be inside the above `if'. 288617ba13bSMingming Cao * Note that ext4_orphan_del() has to be able to cope with the 289ac27a0ecSDave Kleikamp * deletion of a non-existent orphan - this is because we don't 290617ba13bSMingming Cao * know if ext4_truncate() actually created an orphan record. 291ac27a0ecSDave Kleikamp * (Well, we could do this if we need to, but heck - it works) 292ac27a0ecSDave Kleikamp */ 293617ba13bSMingming Cao ext4_orphan_del(handle, inode); 294617ba13bSMingming Cao EXT4_I(inode)->i_dtime = get_seconds(); 295ac27a0ecSDave Kleikamp 296ac27a0ecSDave Kleikamp /* 297ac27a0ecSDave Kleikamp * One subtle ordering requirement: if anything has gone wrong 298ac27a0ecSDave Kleikamp * (transaction abort, IO errors, whatever), then we can still 299ac27a0ecSDave Kleikamp * do these next steps (the fs will already have been marked as 300ac27a0ecSDave Kleikamp * having errors), but we can't free the inode if the mark_dirty 301ac27a0ecSDave Kleikamp * fails. 302ac27a0ecSDave Kleikamp */ 303617ba13bSMingming Cao if (ext4_mark_inode_dirty(handle, inode)) 304ac27a0ecSDave Kleikamp /* If that failed, just do the required in-core inode clear. */ 3050930fcc1SAl Viro ext4_clear_inode(inode); 306ac27a0ecSDave Kleikamp else 307617ba13bSMingming Cao ext4_free_inode(handle, inode); 308617ba13bSMingming Cao ext4_journal_stop(handle); 3098e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 310ac27a0ecSDave Kleikamp return; 311ac27a0ecSDave Kleikamp no_delete: 3120930fcc1SAl Viro ext4_clear_inode(inode); /* We must guarantee clearing of inode... */ 313ac27a0ecSDave Kleikamp } 314ac27a0ecSDave Kleikamp 315a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 316a9e7f447SDmitry Monakhov qsize_t *ext4_get_reserved_space(struct inode *inode) 31760e58e0fSMingming Cao { 318a9e7f447SDmitry Monakhov return &EXT4_I(inode)->i_reserved_quota; 31960e58e0fSMingming Cao } 320a9e7f447SDmitry Monakhov #endif 3219d0be502STheodore Ts'o 32212219aeaSAneesh Kumar K.V /* 32312219aeaSAneesh Kumar K.V * Calculate the number of metadata blocks need to reserve 3249d0be502STheodore Ts'o * to allocate a block located at @lblock 32512219aeaSAneesh Kumar K.V */ 32601f49d0bSTheodore Ts'o static int ext4_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock) 32712219aeaSAneesh Kumar K.V { 32812e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3299d0be502STheodore Ts'o return ext4_ext_calc_metadata_amount(inode, lblock); 33012219aeaSAneesh Kumar K.V 3318bb2b247SAmir Goldstein return ext4_ind_calc_metadata_amount(inode, lblock); 33212219aeaSAneesh Kumar K.V } 33312219aeaSAneesh Kumar K.V 3340637c6f4STheodore Ts'o /* 3350637c6f4STheodore Ts'o * Called with i_data_sem down, which is important since we can call 3360637c6f4STheodore Ts'o * ext4_discard_preallocations() from here. 3370637c6f4STheodore Ts'o */ 3385f634d06SAneesh Kumar K.V void ext4_da_update_reserve_space(struct inode *inode, 3395f634d06SAneesh Kumar K.V int used, int quota_claim) 34012219aeaSAneesh Kumar K.V { 34112219aeaSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3420637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 34312219aeaSAneesh Kumar K.V 3440637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 345d8990240SAditya Kali trace_ext4_da_update_reserve_space(inode, used, quota_claim); 3460637c6f4STheodore Ts'o if (unlikely(used > ei->i_reserved_data_blocks)) { 3478de5c325STheodore Ts'o ext4_warning(inode->i_sb, "%s: ino %lu, used %d " 3481084f252STheodore Ts'o "with only %d reserved data blocks", 3490637c6f4STheodore Ts'o __func__, inode->i_ino, used, 3500637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 3510637c6f4STheodore Ts'o WARN_ON(1); 3520637c6f4STheodore Ts'o used = ei->i_reserved_data_blocks; 3536bc6e63fSAneesh Kumar K.V } 35412219aeaSAneesh Kumar K.V 35597795d2aSBrian Foster if (unlikely(ei->i_allocated_meta_blocks > ei->i_reserved_meta_blocks)) { 35601a523ebSTheodore Ts'o ext4_warning(inode->i_sb, "ino %lu, allocated %d " 35701a523ebSTheodore Ts'o "with only %d reserved metadata blocks " 35801a523ebSTheodore Ts'o "(releasing %d blocks with reserved %d data blocks)", 35997795d2aSBrian Foster inode->i_ino, ei->i_allocated_meta_blocks, 36001a523ebSTheodore Ts'o ei->i_reserved_meta_blocks, used, 36101a523ebSTheodore Ts'o ei->i_reserved_data_blocks); 36297795d2aSBrian Foster WARN_ON(1); 36397795d2aSBrian Foster ei->i_allocated_meta_blocks = ei->i_reserved_meta_blocks; 36497795d2aSBrian Foster } 36597795d2aSBrian Foster 3660637c6f4STheodore Ts'o /* Update per-inode reservations */ 3670637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= used; 3680637c6f4STheodore Ts'o ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks; 36957042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 37072b8ab9dSEric Sandeen used + ei->i_allocated_meta_blocks); 3710637c6f4STheodore Ts'o ei->i_allocated_meta_blocks = 0; 3720637c6f4STheodore Ts'o 3730637c6f4STheodore Ts'o if (ei->i_reserved_data_blocks == 0) { 3740637c6f4STheodore Ts'o /* 3750637c6f4STheodore Ts'o * We can release all of the reserved metadata blocks 3760637c6f4STheodore Ts'o * only when we have written all of the delayed 3770637c6f4STheodore Ts'o * allocation blocks. 3780637c6f4STheodore Ts'o */ 37957042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 38072b8ab9dSEric Sandeen ei->i_reserved_meta_blocks); 381ee5f4d9cSTheodore Ts'o ei->i_reserved_meta_blocks = 0; 3829d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 3830637c6f4STheodore Ts'o } 38412219aeaSAneesh Kumar K.V spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 38560e58e0fSMingming Cao 38672b8ab9dSEric Sandeen /* Update quota subsystem for data blocks */ 38772b8ab9dSEric Sandeen if (quota_claim) 3887b415bf6SAditya Kali dquot_claim_block(inode, EXT4_C2B(sbi, used)); 38972b8ab9dSEric Sandeen else { 3905f634d06SAneesh Kumar K.V /* 3915f634d06SAneesh Kumar K.V * We did fallocate with an offset that is already delayed 3925f634d06SAneesh Kumar K.V * allocated. So on delayed allocated writeback we should 39372b8ab9dSEric Sandeen * not re-claim the quota for fallocated blocks. 3945f634d06SAneesh Kumar K.V */ 3957b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, used)); 3965f634d06SAneesh Kumar K.V } 397d6014301SAneesh Kumar K.V 398d6014301SAneesh Kumar K.V /* 399d6014301SAneesh Kumar K.V * If we have done all the pending block allocations and if 400d6014301SAneesh Kumar K.V * there aren't any writers on the inode, we can discard the 401d6014301SAneesh Kumar K.V * inode's preallocations. 402d6014301SAneesh Kumar K.V */ 4030637c6f4STheodore Ts'o if ((ei->i_reserved_data_blocks == 0) && 4040637c6f4STheodore Ts'o (atomic_read(&inode->i_writecount) == 0)) 405d6014301SAneesh Kumar K.V ext4_discard_preallocations(inode); 40612219aeaSAneesh Kumar K.V } 40712219aeaSAneesh Kumar K.V 408e29136f8STheodore Ts'o static int __check_block_validity(struct inode *inode, const char *func, 409c398eda0STheodore Ts'o unsigned int line, 41024676da4STheodore Ts'o struct ext4_map_blocks *map) 4116fd058f7STheodore Ts'o { 41224676da4STheodore Ts'o if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk, 41324676da4STheodore Ts'o map->m_len)) { 414c398eda0STheodore Ts'o ext4_error_inode(inode, func, line, map->m_pblk, 415c398eda0STheodore Ts'o "lblock %lu mapped to illegal pblock " 41624676da4STheodore Ts'o "(length %d)", (unsigned long) map->m_lblk, 417c398eda0STheodore Ts'o map->m_len); 4186fd058f7STheodore Ts'o return -EIO; 4196fd058f7STheodore Ts'o } 4206fd058f7STheodore Ts'o return 0; 4216fd058f7STheodore Ts'o } 4226fd058f7STheodore Ts'o 423e29136f8STheodore Ts'o #define check_block_validity(inode, map) \ 424c398eda0STheodore Ts'o __check_block_validity((inode), __func__, __LINE__, (map)) 425e29136f8STheodore Ts'o 426f5ab0d1fSMingming Cao /* 4271f94533dSTheodore Ts'o * Return the number of contiguous dirty pages in a given inode 4281f94533dSTheodore Ts'o * starting at page frame idx. 42955138e0bSTheodore Ts'o */ 43055138e0bSTheodore Ts'o static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx, 43155138e0bSTheodore Ts'o unsigned int max_pages) 43255138e0bSTheodore Ts'o { 43355138e0bSTheodore Ts'o struct address_space *mapping = inode->i_mapping; 43455138e0bSTheodore Ts'o pgoff_t index; 43555138e0bSTheodore Ts'o struct pagevec pvec; 43655138e0bSTheodore Ts'o pgoff_t num = 0; 43755138e0bSTheodore Ts'o int i, nr_pages, done = 0; 43855138e0bSTheodore Ts'o 43955138e0bSTheodore Ts'o if (max_pages == 0) 44055138e0bSTheodore Ts'o return 0; 44155138e0bSTheodore Ts'o pagevec_init(&pvec, 0); 44255138e0bSTheodore Ts'o while (!done) { 44355138e0bSTheodore Ts'o index = idx; 44455138e0bSTheodore Ts'o nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 44555138e0bSTheodore Ts'o PAGECACHE_TAG_DIRTY, 44655138e0bSTheodore Ts'o (pgoff_t)PAGEVEC_SIZE); 44755138e0bSTheodore Ts'o if (nr_pages == 0) 44855138e0bSTheodore Ts'o break; 44955138e0bSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 45055138e0bSTheodore Ts'o struct page *page = pvec.pages[i]; 45155138e0bSTheodore Ts'o struct buffer_head *bh, *head; 45255138e0bSTheodore Ts'o 45355138e0bSTheodore Ts'o lock_page(page); 45455138e0bSTheodore Ts'o if (unlikely(page->mapping != mapping) || 45555138e0bSTheodore Ts'o !PageDirty(page) || 45655138e0bSTheodore Ts'o PageWriteback(page) || 45755138e0bSTheodore Ts'o page->index != idx) { 45855138e0bSTheodore Ts'o done = 1; 45955138e0bSTheodore Ts'o unlock_page(page); 46055138e0bSTheodore Ts'o break; 46155138e0bSTheodore Ts'o } 4621f94533dSTheodore Ts'o if (page_has_buffers(page)) { 4631f94533dSTheodore Ts'o bh = head = page_buffers(page); 46455138e0bSTheodore Ts'o do { 46555138e0bSTheodore Ts'o if (!buffer_delay(bh) && 4661f94533dSTheodore Ts'o !buffer_unwritten(bh)) 46755138e0bSTheodore Ts'o done = 1; 4681f94533dSTheodore Ts'o bh = bh->b_this_page; 4691f94533dSTheodore Ts'o } while (!done && (bh != head)); 47055138e0bSTheodore Ts'o } 47155138e0bSTheodore Ts'o unlock_page(page); 47255138e0bSTheodore Ts'o if (done) 47355138e0bSTheodore Ts'o break; 47455138e0bSTheodore Ts'o idx++; 47555138e0bSTheodore Ts'o num++; 476659c6009SEric Sandeen if (num >= max_pages) { 477659c6009SEric Sandeen done = 1; 47855138e0bSTheodore Ts'o break; 47955138e0bSTheodore Ts'o } 480659c6009SEric Sandeen } 48155138e0bSTheodore Ts'o pagevec_release(&pvec); 48255138e0bSTheodore Ts'o } 48355138e0bSTheodore Ts'o return num; 48455138e0bSTheodore Ts'o } 48555138e0bSTheodore Ts'o 486921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 487921f266bSDmitry Monakhov static void ext4_map_blocks_es_recheck(handle_t *handle, 488921f266bSDmitry Monakhov struct inode *inode, 489921f266bSDmitry Monakhov struct ext4_map_blocks *es_map, 490921f266bSDmitry Monakhov struct ext4_map_blocks *map, 491921f266bSDmitry Monakhov int flags) 492921f266bSDmitry Monakhov { 493921f266bSDmitry Monakhov int retval; 494921f266bSDmitry Monakhov 495921f266bSDmitry Monakhov map->m_flags = 0; 496921f266bSDmitry Monakhov /* 497921f266bSDmitry Monakhov * There is a race window that the result is not the same. 498921f266bSDmitry Monakhov * e.g. xfstests #223 when dioread_nolock enables. The reason 499921f266bSDmitry Monakhov * is that we lookup a block mapping in extent status tree with 500921f266bSDmitry Monakhov * out taking i_data_sem. So at the time the unwritten extent 501921f266bSDmitry Monakhov * could be converted. 502921f266bSDmitry Monakhov */ 503921f266bSDmitry Monakhov if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 504921f266bSDmitry Monakhov down_read((&EXT4_I(inode)->i_data_sem)); 505921f266bSDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 506921f266bSDmitry Monakhov retval = ext4_ext_map_blocks(handle, inode, map, flags & 507921f266bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 508921f266bSDmitry Monakhov } else { 509921f266bSDmitry Monakhov retval = ext4_ind_map_blocks(handle, inode, map, flags & 510921f266bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 511921f266bSDmitry Monakhov } 512921f266bSDmitry Monakhov if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 513921f266bSDmitry Monakhov up_read((&EXT4_I(inode)->i_data_sem)); 514921f266bSDmitry Monakhov /* 515921f266bSDmitry Monakhov * Clear EXT4_MAP_FROM_CLUSTER and EXT4_MAP_BOUNDARY flag 516921f266bSDmitry Monakhov * because it shouldn't be marked in es_map->m_flags. 517921f266bSDmitry Monakhov */ 518921f266bSDmitry Monakhov map->m_flags &= ~(EXT4_MAP_FROM_CLUSTER | EXT4_MAP_BOUNDARY); 519921f266bSDmitry Monakhov 520921f266bSDmitry Monakhov /* 521921f266bSDmitry Monakhov * We don't check m_len because extent will be collpased in status 522921f266bSDmitry Monakhov * tree. So the m_len might not equal. 523921f266bSDmitry Monakhov */ 524921f266bSDmitry Monakhov if (es_map->m_lblk != map->m_lblk || 525921f266bSDmitry Monakhov es_map->m_flags != map->m_flags || 526921f266bSDmitry Monakhov es_map->m_pblk != map->m_pblk) { 527921f266bSDmitry Monakhov printk("ES cache assertation failed for inode: %lu " 528921f266bSDmitry Monakhov "es_cached ex [%d/%d/%llu/%x] != " 529921f266bSDmitry Monakhov "found ex [%d/%d/%llu/%x] retval %d flags %x\n", 530921f266bSDmitry Monakhov inode->i_ino, es_map->m_lblk, es_map->m_len, 531921f266bSDmitry Monakhov es_map->m_pblk, es_map->m_flags, map->m_lblk, 532921f266bSDmitry Monakhov map->m_len, map->m_pblk, map->m_flags, 533921f266bSDmitry Monakhov retval, flags); 534921f266bSDmitry Monakhov } 535921f266bSDmitry Monakhov } 536921f266bSDmitry Monakhov #endif /* ES_AGGRESSIVE_TEST */ 537921f266bSDmitry Monakhov 53855138e0bSTheodore Ts'o /* 539e35fd660STheodore Ts'o * The ext4_map_blocks() function tries to look up the requested blocks, 5402b2d6d01STheodore Ts'o * and returns if the blocks are already mapped. 541f5ab0d1fSMingming Cao * 542f5ab0d1fSMingming Cao * Otherwise it takes the write lock of the i_data_sem and allocate blocks 543f5ab0d1fSMingming Cao * and store the allocated blocks in the result buffer head and mark it 544f5ab0d1fSMingming Cao * mapped. 545f5ab0d1fSMingming Cao * 546e35fd660STheodore Ts'o * If file type is extents based, it will call ext4_ext_map_blocks(), 547e35fd660STheodore Ts'o * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping 548f5ab0d1fSMingming Cao * based files 549f5ab0d1fSMingming Cao * 550f5ab0d1fSMingming Cao * On success, it returns the number of blocks being mapped or allocate. 551f5ab0d1fSMingming Cao * if create==0 and the blocks are pre-allocated and uninitialized block, 552f5ab0d1fSMingming Cao * the result buffer head is unmapped. If the create ==1, it will make sure 553f5ab0d1fSMingming Cao * the buffer head is mapped. 554f5ab0d1fSMingming Cao * 555f5ab0d1fSMingming Cao * It returns 0 if plain look up failed (blocks have not been allocated), in 556df3ab170STao Ma * that case, buffer head is unmapped 557f5ab0d1fSMingming Cao * 558f5ab0d1fSMingming Cao * It returns the error in case of allocation failure. 559f5ab0d1fSMingming Cao */ 560e35fd660STheodore Ts'o int ext4_map_blocks(handle_t *handle, struct inode *inode, 561e35fd660STheodore Ts'o struct ext4_map_blocks *map, int flags) 5620e855ac8SAneesh Kumar K.V { 563d100eef2SZheng Liu struct extent_status es; 5640e855ac8SAneesh Kumar K.V int retval; 565921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 566921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 567921f266bSDmitry Monakhov 568921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 569921f266bSDmitry Monakhov #endif 570f5ab0d1fSMingming Cao 571e35fd660STheodore Ts'o map->m_flags = 0; 572e35fd660STheodore Ts'o ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u," 573e35fd660STheodore Ts'o "logical block %lu\n", inode->i_ino, flags, map->m_len, 574e35fd660STheodore Ts'o (unsigned long) map->m_lblk); 575d100eef2SZheng Liu 576d100eef2SZheng Liu /* Lookup extent status tree firstly */ 577d100eef2SZheng Liu if (ext4_es_lookup_extent(inode, map->m_lblk, &es)) { 578d100eef2SZheng Liu if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) { 579d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + 580d100eef2SZheng Liu map->m_lblk - es.es_lblk; 581d100eef2SZheng Liu map->m_flags |= ext4_es_is_written(&es) ? 582d100eef2SZheng Liu EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN; 583d100eef2SZheng Liu retval = es.es_len - (map->m_lblk - es.es_lblk); 584d100eef2SZheng Liu if (retval > map->m_len) 585d100eef2SZheng Liu retval = map->m_len; 586d100eef2SZheng Liu map->m_len = retval; 587d100eef2SZheng Liu } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) { 588d100eef2SZheng Liu retval = 0; 589d100eef2SZheng Liu } else { 590d100eef2SZheng Liu BUG_ON(1); 591d100eef2SZheng Liu } 592921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 593921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(handle, inode, map, 594921f266bSDmitry Monakhov &orig_map, flags); 595921f266bSDmitry Monakhov #endif 596d100eef2SZheng Liu goto found; 597d100eef2SZheng Liu } 598d100eef2SZheng Liu 5994df3d265SAneesh Kumar K.V /* 600b920c755STheodore Ts'o * Try to see if we can get the block without requesting a new 601b920c755STheodore Ts'o * file system block. 6024df3d265SAneesh Kumar K.V */ 603729f52c6SZheng Liu if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 6040e855ac8SAneesh Kumar K.V down_read((&EXT4_I(inode)->i_data_sem)); 60512e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 606a4e5d88bSDmitry Monakhov retval = ext4_ext_map_blocks(handle, inode, map, flags & 607a4e5d88bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 6084df3d265SAneesh Kumar K.V } else { 609a4e5d88bSDmitry Monakhov retval = ext4_ind_map_blocks(handle, inode, map, flags & 610a4e5d88bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 6110e855ac8SAneesh Kumar K.V } 612f7fec032SZheng Liu if (retval > 0) { 613f7fec032SZheng Liu int ret; 614f7fec032SZheng Liu unsigned long long status; 615f7fec032SZheng Liu 616921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 617921f266bSDmitry Monakhov if (retval != map->m_len) { 618921f266bSDmitry Monakhov printk("ES len assertation failed for inode: %lu " 619921f266bSDmitry Monakhov "retval %d != map->m_len %d " 620921f266bSDmitry Monakhov "in %s (lookup)\n", inode->i_ino, retval, 621921f266bSDmitry Monakhov map->m_len, __func__); 622921f266bSDmitry Monakhov } 623921f266bSDmitry Monakhov #endif 624921f266bSDmitry Monakhov 625f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 626f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 627f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 628f7fec032SZheng Liu ext4_find_delalloc_range(inode, map->m_lblk, 629f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 630f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 631f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, 632f7fec032SZheng Liu map->m_len, map->m_pblk, status); 633f7fec032SZheng Liu if (ret < 0) 634f7fec032SZheng Liu retval = ret; 635f7fec032SZheng Liu } 636729f52c6SZheng Liu if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 6374df3d265SAneesh Kumar K.V up_read((&EXT4_I(inode)->i_data_sem)); 638f5ab0d1fSMingming Cao 639d100eef2SZheng Liu found: 640e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 641f7fec032SZheng Liu int ret = check_block_validity(inode, map); 6426fd058f7STheodore Ts'o if (ret != 0) 6436fd058f7STheodore Ts'o return ret; 6446fd058f7STheodore Ts'o } 6456fd058f7STheodore Ts'o 646f5ab0d1fSMingming Cao /* If it is only a block(s) look up */ 647c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 6484df3d265SAneesh Kumar K.V return retval; 6494df3d265SAneesh Kumar K.V 6504df3d265SAneesh Kumar K.V /* 651f5ab0d1fSMingming Cao * Returns if the blocks have already allocated 652f5ab0d1fSMingming Cao * 653f5ab0d1fSMingming Cao * Note that if blocks have been preallocated 654df3ab170STao Ma * ext4_ext_get_block() returns the create = 0 655f5ab0d1fSMingming Cao * with buffer head unmapped. 656f5ab0d1fSMingming Cao */ 657e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) 658f5ab0d1fSMingming Cao return retval; 659f5ab0d1fSMingming Cao 660f5ab0d1fSMingming Cao /* 661a25a4e1aSZheng Liu * Here we clear m_flags because after allocating an new extent, 662a25a4e1aSZheng Liu * it will be set again. 6632a8964d6SAneesh Kumar K.V */ 664a25a4e1aSZheng Liu map->m_flags &= ~EXT4_MAP_FLAGS; 6652a8964d6SAneesh Kumar K.V 6662a8964d6SAneesh Kumar K.V /* 667f5ab0d1fSMingming Cao * New blocks allocate and/or writing to uninitialized extent 668f5ab0d1fSMingming Cao * will possibly result in updating i_data, so we take 669f5ab0d1fSMingming Cao * the write lock of i_data_sem, and call get_blocks() 670f5ab0d1fSMingming Cao * with create == 1 flag. 6714df3d265SAneesh Kumar K.V */ 6724df3d265SAneesh Kumar K.V down_write((&EXT4_I(inode)->i_data_sem)); 673d2a17637SMingming Cao 674d2a17637SMingming Cao /* 675d2a17637SMingming Cao * if the caller is from delayed allocation writeout path 676d2a17637SMingming Cao * we have already reserved fs blocks for allocation 677d2a17637SMingming Cao * let the underlying get_block() function know to 678d2a17637SMingming Cao * avoid double accounting 679d2a17637SMingming Cao */ 680c2177057STheodore Ts'o if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 681f2321097STheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED); 6824df3d265SAneesh Kumar K.V /* 6834df3d265SAneesh Kumar K.V * We need to check for EXT4 here because migrate 6844df3d265SAneesh Kumar K.V * could have changed the inode type in between 6854df3d265SAneesh Kumar K.V */ 68612e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 687e35fd660STheodore Ts'o retval = ext4_ext_map_blocks(handle, inode, map, flags); 6880e855ac8SAneesh Kumar K.V } else { 689e35fd660STheodore Ts'o retval = ext4_ind_map_blocks(handle, inode, map, flags); 690267e4db9SAneesh Kumar K.V 691e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_NEW) { 692267e4db9SAneesh Kumar K.V /* 693267e4db9SAneesh Kumar K.V * We allocated new blocks which will result in 694267e4db9SAneesh Kumar K.V * i_data's format changing. Force the migrate 695267e4db9SAneesh Kumar K.V * to fail by clearing migrate flags 696267e4db9SAneesh Kumar K.V */ 69719f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE); 698267e4db9SAneesh Kumar K.V } 6992ac3b6e0STheodore Ts'o 700d2a17637SMingming Cao /* 7012ac3b6e0STheodore Ts'o * Update reserved blocks/metadata blocks after successful 7025f634d06SAneesh Kumar K.V * block allocation which had been deferred till now. We don't 7035f634d06SAneesh Kumar K.V * support fallocate for non extent files. So we can update 7045f634d06SAneesh Kumar K.V * reserve space here. 705d2a17637SMingming Cao */ 7065f634d06SAneesh Kumar K.V if ((retval > 0) && 7071296cc85SAneesh Kumar K.V (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)) 7085f634d06SAneesh Kumar K.V ext4_da_update_reserve_space(inode, retval, 1); 7095f634d06SAneesh Kumar K.V } 710f7fec032SZheng Liu if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 711f2321097STheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED); 712d2a17637SMingming Cao 713f7fec032SZheng Liu if (retval > 0) { 71451865fdaSZheng Liu int ret; 715f7fec032SZheng Liu unsigned long long status; 716f7fec032SZheng Liu 717921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 718921f266bSDmitry Monakhov if (retval != map->m_len) { 719921f266bSDmitry Monakhov printk("ES len assertation failed for inode: %lu " 720921f266bSDmitry Monakhov "retval %d != map->m_len %d " 721921f266bSDmitry Monakhov "in %s (allocation)\n", inode->i_ino, retval, 722921f266bSDmitry Monakhov map->m_len, __func__); 723921f266bSDmitry Monakhov } 724921f266bSDmitry Monakhov #endif 725921f266bSDmitry Monakhov 726adb23551SZheng Liu /* 727adb23551SZheng Liu * If the extent has been zeroed out, we don't need to update 728adb23551SZheng Liu * extent status tree. 729adb23551SZheng Liu */ 730adb23551SZheng Liu if ((flags & EXT4_GET_BLOCKS_PRE_IO) && 731adb23551SZheng Liu ext4_es_lookup_extent(inode, map->m_lblk, &es)) { 732adb23551SZheng Liu if (ext4_es_is_written(&es)) 733adb23551SZheng Liu goto has_zeroout; 734adb23551SZheng Liu } 735f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 736f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 737f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 738f7fec032SZheng Liu ext4_find_delalloc_range(inode, map->m_lblk, 739f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 740f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 741f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 742f7fec032SZheng Liu map->m_pblk, status); 74351865fdaSZheng Liu if (ret < 0) 74451865fdaSZheng Liu retval = ret; 74551865fdaSZheng Liu } 7465356f261SAditya Kali 747adb23551SZheng Liu has_zeroout: 7480e855ac8SAneesh Kumar K.V up_write((&EXT4_I(inode)->i_data_sem)); 749e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 750e29136f8STheodore Ts'o int ret = check_block_validity(inode, map); 7516fd058f7STheodore Ts'o if (ret != 0) 7526fd058f7STheodore Ts'o return ret; 7536fd058f7STheodore Ts'o } 7540e855ac8SAneesh Kumar K.V return retval; 7550e855ac8SAneesh Kumar K.V } 7560e855ac8SAneesh Kumar K.V 757f3bd1f3fSMingming Cao /* Maximum number of blocks we map for direct IO at once. */ 758f3bd1f3fSMingming Cao #define DIO_MAX_BLOCKS 4096 759f3bd1f3fSMingming Cao 7602ed88685STheodore Ts'o static int _ext4_get_block(struct inode *inode, sector_t iblock, 7612ed88685STheodore Ts'o struct buffer_head *bh, int flags) 762ac27a0ecSDave Kleikamp { 7633e4fdaf8SDmitriy Monakhov handle_t *handle = ext4_journal_current_handle(); 7642ed88685STheodore Ts'o struct ext4_map_blocks map; 7657fb5409dSJan Kara int ret = 0, started = 0; 766f3bd1f3fSMingming Cao int dio_credits; 767ac27a0ecSDave Kleikamp 76846c7f254STao Ma if (ext4_has_inline_data(inode)) 76946c7f254STao Ma return -ERANGE; 77046c7f254STao Ma 7712ed88685STheodore Ts'o map.m_lblk = iblock; 7722ed88685STheodore Ts'o map.m_len = bh->b_size >> inode->i_blkbits; 7732ed88685STheodore Ts'o 7748b0f165fSAnatol Pomozov if (flags && !(flags & EXT4_GET_BLOCKS_NO_LOCK) && !handle) { 7757fb5409dSJan Kara /* Direct IO write... */ 7762ed88685STheodore Ts'o if (map.m_len > DIO_MAX_BLOCKS) 7772ed88685STheodore Ts'o map.m_len = DIO_MAX_BLOCKS; 7782ed88685STheodore Ts'o dio_credits = ext4_chunk_trans_blocks(inode, map.m_len); 7799924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, 7809924a92aSTheodore Ts'o dio_credits); 7817fb5409dSJan Kara if (IS_ERR(handle)) { 782ac27a0ecSDave Kleikamp ret = PTR_ERR(handle); 7832ed88685STheodore Ts'o return ret; 7847fb5409dSJan Kara } 7857fb5409dSJan Kara started = 1; 786ac27a0ecSDave Kleikamp } 787ac27a0ecSDave Kleikamp 7882ed88685STheodore Ts'o ret = ext4_map_blocks(handle, inode, &map, flags); 789ac27a0ecSDave Kleikamp if (ret > 0) { 7902ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 7912ed88685STheodore Ts'o bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; 7922ed88685STheodore Ts'o bh->b_size = inode->i_sb->s_blocksize * map.m_len; 793ac27a0ecSDave Kleikamp ret = 0; 794ac27a0ecSDave Kleikamp } 7957fb5409dSJan Kara if (started) 7967fb5409dSJan Kara ext4_journal_stop(handle); 797ac27a0ecSDave Kleikamp return ret; 798ac27a0ecSDave Kleikamp } 799ac27a0ecSDave Kleikamp 8002ed88685STheodore Ts'o int ext4_get_block(struct inode *inode, sector_t iblock, 8012ed88685STheodore Ts'o struct buffer_head *bh, int create) 8022ed88685STheodore Ts'o { 8032ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh, 8042ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 8052ed88685STheodore Ts'o } 8062ed88685STheodore Ts'o 807ac27a0ecSDave Kleikamp /* 808ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 809ac27a0ecSDave Kleikamp */ 810617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 811725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *errp) 812ac27a0ecSDave Kleikamp { 8132ed88685STheodore Ts'o struct ext4_map_blocks map; 8142ed88685STheodore Ts'o struct buffer_head *bh; 815ac27a0ecSDave Kleikamp int fatal = 0, err; 816ac27a0ecSDave Kleikamp 817ac27a0ecSDave Kleikamp J_ASSERT(handle != NULL || create == 0); 818ac27a0ecSDave Kleikamp 8192ed88685STheodore Ts'o map.m_lblk = block; 8202ed88685STheodore Ts'o map.m_len = 1; 8212ed88685STheodore Ts'o err = ext4_map_blocks(handle, inode, &map, 8222ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 8232ed88685STheodore Ts'o 82490b0a973SCarlos Maiolino /* ensure we send some value back into *errp */ 82590b0a973SCarlos Maiolino *errp = 0; 82690b0a973SCarlos Maiolino 8270f70b406STheodore Ts'o if (create && err == 0) 8280f70b406STheodore Ts'o err = -ENOSPC; /* should never happen */ 8292ed88685STheodore Ts'o if (err < 0) 830ac27a0ecSDave Kleikamp *errp = err; 8312ed88685STheodore Ts'o if (err <= 0) 8322ed88685STheodore Ts'o return NULL; 8332ed88685STheodore Ts'o 8342ed88685STheodore Ts'o bh = sb_getblk(inode->i_sb, map.m_pblk); 835aebf0243SWang Shilong if (unlikely(!bh)) { 836860d21e2STheodore Ts'o *errp = -ENOMEM; 8372ed88685STheodore Ts'o return NULL; 838ac27a0ecSDave Kleikamp } 8392ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 840ac27a0ecSDave Kleikamp J_ASSERT(create != 0); 841ac39849dSAneesh Kumar K.V J_ASSERT(handle != NULL); 842ac27a0ecSDave Kleikamp 843ac27a0ecSDave Kleikamp /* 844ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 845ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 846ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 847617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 848ac27a0ecSDave Kleikamp * problem. 849ac27a0ecSDave Kleikamp */ 850ac27a0ecSDave Kleikamp lock_buffer(bh); 851ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 852617ba13bSMingming Cao fatal = ext4_journal_get_create_access(handle, bh); 853ac27a0ecSDave Kleikamp if (!fatal && !buffer_uptodate(bh)) { 854ac27a0ecSDave Kleikamp memset(bh->b_data, 0, inode->i_sb->s_blocksize); 855ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 856ac27a0ecSDave Kleikamp } 857ac27a0ecSDave Kleikamp unlock_buffer(bh); 8580390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 8590390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 860ac27a0ecSDave Kleikamp if (!fatal) 861ac27a0ecSDave Kleikamp fatal = err; 862ac27a0ecSDave Kleikamp } else { 863ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "not a new buffer"); 864ac27a0ecSDave Kleikamp } 865ac27a0ecSDave Kleikamp if (fatal) { 866ac27a0ecSDave Kleikamp *errp = fatal; 867ac27a0ecSDave Kleikamp brelse(bh); 868ac27a0ecSDave Kleikamp bh = NULL; 869ac27a0ecSDave Kleikamp } 870ac27a0ecSDave Kleikamp return bh; 871ac27a0ecSDave Kleikamp } 872ac27a0ecSDave Kleikamp 873617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 874725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *err) 875ac27a0ecSDave Kleikamp { 876ac27a0ecSDave Kleikamp struct buffer_head *bh; 877ac27a0ecSDave Kleikamp 878617ba13bSMingming Cao bh = ext4_getblk(handle, inode, block, create, err); 879ac27a0ecSDave Kleikamp if (!bh) 880ac27a0ecSDave Kleikamp return bh; 881ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 882ac27a0ecSDave Kleikamp return bh; 88365299a3bSChristoph Hellwig ll_rw_block(READ | REQ_META | REQ_PRIO, 1, &bh); 884ac27a0ecSDave Kleikamp wait_on_buffer(bh); 885ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 886ac27a0ecSDave Kleikamp return bh; 887ac27a0ecSDave Kleikamp put_bh(bh); 888ac27a0ecSDave Kleikamp *err = -EIO; 889ac27a0ecSDave Kleikamp return NULL; 890ac27a0ecSDave Kleikamp } 891ac27a0ecSDave Kleikamp 892f19d5870STao Ma int ext4_walk_page_buffers(handle_t *handle, 893ac27a0ecSDave Kleikamp struct buffer_head *head, 894ac27a0ecSDave Kleikamp unsigned from, 895ac27a0ecSDave Kleikamp unsigned to, 896ac27a0ecSDave Kleikamp int *partial, 897ac27a0ecSDave Kleikamp int (*fn)(handle_t *handle, 898ac27a0ecSDave Kleikamp struct buffer_head *bh)) 899ac27a0ecSDave Kleikamp { 900ac27a0ecSDave Kleikamp struct buffer_head *bh; 901ac27a0ecSDave Kleikamp unsigned block_start, block_end; 902ac27a0ecSDave Kleikamp unsigned blocksize = head->b_size; 903ac27a0ecSDave Kleikamp int err, ret = 0; 904ac27a0ecSDave Kleikamp struct buffer_head *next; 905ac27a0ecSDave Kleikamp 906ac27a0ecSDave Kleikamp for (bh = head, block_start = 0; 907ac27a0ecSDave Kleikamp ret == 0 && (bh != head || !block_start); 908de9a55b8STheodore Ts'o block_start = block_end, bh = next) { 909ac27a0ecSDave Kleikamp next = bh->b_this_page; 910ac27a0ecSDave Kleikamp block_end = block_start + blocksize; 911ac27a0ecSDave Kleikamp if (block_end <= from || block_start >= to) { 912ac27a0ecSDave Kleikamp if (partial && !buffer_uptodate(bh)) 913ac27a0ecSDave Kleikamp *partial = 1; 914ac27a0ecSDave Kleikamp continue; 915ac27a0ecSDave Kleikamp } 916ac27a0ecSDave Kleikamp err = (*fn)(handle, bh); 917ac27a0ecSDave Kleikamp if (!ret) 918ac27a0ecSDave Kleikamp ret = err; 919ac27a0ecSDave Kleikamp } 920ac27a0ecSDave Kleikamp return ret; 921ac27a0ecSDave Kleikamp } 922ac27a0ecSDave Kleikamp 923ac27a0ecSDave Kleikamp /* 924ac27a0ecSDave Kleikamp * To preserve ordering, it is essential that the hole instantiation and 925ac27a0ecSDave Kleikamp * the data write be encapsulated in a single transaction. We cannot 926617ba13bSMingming Cao * close off a transaction and start a new one between the ext4_get_block() 927dab291afSMingming Cao * and the commit_write(). So doing the jbd2_journal_start at the start of 928ac27a0ecSDave Kleikamp * prepare_write() is the right place. 929ac27a0ecSDave Kleikamp * 93036ade451SJan Kara * Also, this function can nest inside ext4_writepage(). In that case, we 93136ade451SJan Kara * *know* that ext4_writepage() has generated enough buffer credits to do the 93236ade451SJan Kara * whole page. So we won't block on the journal in that case, which is good, 93336ade451SJan Kara * because the caller may be PF_MEMALLOC. 934ac27a0ecSDave Kleikamp * 935617ba13bSMingming Cao * By accident, ext4 can be reentered when a transaction is open via 936ac27a0ecSDave Kleikamp * quota file writes. If we were to commit the transaction while thus 937ac27a0ecSDave Kleikamp * reentered, there can be a deadlock - we would be holding a quota 938ac27a0ecSDave Kleikamp * lock, and the commit would never complete if another thread had a 939ac27a0ecSDave Kleikamp * transaction open and was blocking on the quota lock - a ranking 940ac27a0ecSDave Kleikamp * violation. 941ac27a0ecSDave Kleikamp * 942dab291afSMingming Cao * So what we do is to rely on the fact that jbd2_journal_stop/journal_start 943ac27a0ecSDave Kleikamp * will _not_ run commit under these circumstances because handle->h_ref 944ac27a0ecSDave Kleikamp * is elevated. We'll still have enough credits for the tiny quotafile 945ac27a0ecSDave Kleikamp * write. 946ac27a0ecSDave Kleikamp */ 947f19d5870STao Ma int do_journal_get_write_access(handle_t *handle, 948ac27a0ecSDave Kleikamp struct buffer_head *bh) 949ac27a0ecSDave Kleikamp { 95056d35a4cSJan Kara int dirty = buffer_dirty(bh); 95156d35a4cSJan Kara int ret; 95256d35a4cSJan Kara 953ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 954ac27a0ecSDave Kleikamp return 0; 95556d35a4cSJan Kara /* 956ebdec241SChristoph Hellwig * __block_write_begin() could have dirtied some buffers. Clean 95756d35a4cSJan Kara * the dirty bit as jbd2_journal_get_write_access() could complain 95856d35a4cSJan Kara * otherwise about fs integrity issues. Setting of the dirty bit 959ebdec241SChristoph Hellwig * by __block_write_begin() isn't a real problem here as we clear 96056d35a4cSJan Kara * the bit before releasing a page lock and thus writeback cannot 96156d35a4cSJan Kara * ever write the buffer. 96256d35a4cSJan Kara */ 96356d35a4cSJan Kara if (dirty) 96456d35a4cSJan Kara clear_buffer_dirty(bh); 96556d35a4cSJan Kara ret = ext4_journal_get_write_access(handle, bh); 96656d35a4cSJan Kara if (!ret && dirty) 96756d35a4cSJan Kara ret = ext4_handle_dirty_metadata(handle, NULL, bh); 96856d35a4cSJan Kara return ret; 969ac27a0ecSDave Kleikamp } 970ac27a0ecSDave Kleikamp 9718b0f165fSAnatol Pomozov static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock, 9728b0f165fSAnatol Pomozov struct buffer_head *bh_result, int create); 973bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 974bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned flags, 975bfc1af65SNick Piggin struct page **pagep, void **fsdata) 976ac27a0ecSDave Kleikamp { 977bfc1af65SNick Piggin struct inode *inode = mapping->host; 9781938a150SAneesh Kumar K.V int ret, needed_blocks; 979ac27a0ecSDave Kleikamp handle_t *handle; 980ac27a0ecSDave Kleikamp int retries = 0; 981bfc1af65SNick Piggin struct page *page; 982bfc1af65SNick Piggin pgoff_t index; 983bfc1af65SNick Piggin unsigned from, to; 984bfc1af65SNick Piggin 9859bffad1eSTheodore Ts'o trace_ext4_write_begin(inode, pos, len, flags); 9861938a150SAneesh Kumar K.V /* 9871938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 9881938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 9891938a150SAneesh Kumar K.V */ 9901938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 991bfc1af65SNick Piggin index = pos >> PAGE_CACHE_SHIFT; 992bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 993bfc1af65SNick Piggin to = from + len; 994ac27a0ecSDave Kleikamp 995f19d5870STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 996f19d5870STao Ma ret = ext4_try_to_write_inline_data(mapping, inode, pos, len, 997f19d5870STao Ma flags, pagep); 998f19d5870STao Ma if (ret < 0) 99947564bfbSTheodore Ts'o return ret; 100047564bfbSTheodore Ts'o if (ret == 1) 100147564bfbSTheodore Ts'o return 0; 1002f19d5870STao Ma } 1003f19d5870STao Ma 100447564bfbSTheodore Ts'o /* 100547564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 100647564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 100747564bfbSTheodore Ts'o * is being written back. So grab it first before we start 100847564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 100947564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 101047564bfbSTheodore Ts'o */ 101147564bfbSTheodore Ts'o retry_grab: 101247564bfbSTheodore Ts'o page = grab_cache_page_write_begin(mapping, index, flags); 101347564bfbSTheodore Ts'o if (!page) 101447564bfbSTheodore Ts'o return -ENOMEM; 101547564bfbSTheodore Ts'o unlock_page(page); 101647564bfbSTheodore Ts'o 101747564bfbSTheodore Ts'o retry_journal: 10189924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks); 10197479d2b9SAndrew Morton if (IS_ERR(handle)) { 102047564bfbSTheodore Ts'o page_cache_release(page); 102147564bfbSTheodore Ts'o return PTR_ERR(handle); 10227479d2b9SAndrew Morton } 1023ac27a0ecSDave Kleikamp 102447564bfbSTheodore Ts'o lock_page(page); 102547564bfbSTheodore Ts'o if (page->mapping != mapping) { 102647564bfbSTheodore Ts'o /* The page got truncated from under us */ 102747564bfbSTheodore Ts'o unlock_page(page); 102847564bfbSTheodore Ts'o page_cache_release(page); 1029cf108bcaSJan Kara ext4_journal_stop(handle); 103047564bfbSTheodore Ts'o goto retry_grab; 1031cf108bcaSJan Kara } 103247564bfbSTheodore Ts'o wait_on_page_writeback(page); 1033cf108bcaSJan Kara 1034744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 10356e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block_write); 1036744692dcSJiaying Zhang else 10376e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block); 1038bfc1af65SNick Piggin 1039bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 1040f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_buffers(page), 1041f19d5870STao Ma from, to, NULL, 1042f19d5870STao Ma do_journal_get_write_access); 1043b46be050SAndrey Savochkin } 1044bfc1af65SNick Piggin 1045bfc1af65SNick Piggin if (ret) { 1046bfc1af65SNick Piggin unlock_page(page); 1047ae4d5372SAneesh Kumar K.V /* 10486e1db88dSChristoph Hellwig * __block_write_begin may have instantiated a few blocks 1049ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 1050ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 10511938a150SAneesh Kumar K.V * 10521938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 10531938a150SAneesh Kumar K.V * truncate finishes 1054ae4d5372SAneesh Kumar K.V */ 1055ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 10561938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 10571938a150SAneesh Kumar K.V 10581938a150SAneesh Kumar K.V ext4_journal_stop(handle); 10591938a150SAneesh Kumar K.V if (pos + len > inode->i_size) { 1060b9a4207dSJan Kara ext4_truncate_failed_write(inode); 10611938a150SAneesh Kumar K.V /* 1062ffacfa7aSJan Kara * If truncate failed early the inode might 10631938a150SAneesh Kumar K.V * still be on the orphan list; we need to 10641938a150SAneesh Kumar K.V * make sure the inode is removed from the 10651938a150SAneesh Kumar K.V * orphan list in that case. 10661938a150SAneesh Kumar K.V */ 10671938a150SAneesh Kumar K.V if (inode->i_nlink) 10681938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 10691938a150SAneesh Kumar K.V } 1070bfc1af65SNick Piggin 107147564bfbSTheodore Ts'o if (ret == -ENOSPC && 107247564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 107347564bfbSTheodore Ts'o goto retry_journal; 107447564bfbSTheodore Ts'o page_cache_release(page); 107547564bfbSTheodore Ts'o return ret; 107647564bfbSTheodore Ts'o } 107747564bfbSTheodore Ts'o *pagep = page; 1078ac27a0ecSDave Kleikamp return ret; 1079ac27a0ecSDave Kleikamp } 1080ac27a0ecSDave Kleikamp 1081bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 1082bfc1af65SNick Piggin static int write_end_fn(handle_t *handle, struct buffer_head *bh) 1083ac27a0ecSDave Kleikamp { 1084ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1085ac27a0ecSDave Kleikamp return 0; 1086ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 10870390131bSFrank Mayhar return ext4_handle_dirty_metadata(handle, NULL, bh); 1088ac27a0ecSDave Kleikamp } 1089ac27a0ecSDave Kleikamp 1090f8514083SAneesh Kumar K.V static int ext4_generic_write_end(struct file *file, 1091f8514083SAneesh Kumar K.V struct address_space *mapping, 1092f8514083SAneesh Kumar K.V loff_t pos, unsigned len, unsigned copied, 1093f8514083SAneesh Kumar K.V struct page *page, void *fsdata) 1094f8514083SAneesh Kumar K.V { 1095f8514083SAneesh Kumar K.V int i_size_changed = 0; 1096f8514083SAneesh Kumar K.V struct inode *inode = mapping->host; 1097f8514083SAneesh Kumar K.V handle_t *handle = ext4_journal_current_handle(); 1098f8514083SAneesh Kumar K.V 1099f19d5870STao Ma if (ext4_has_inline_data(inode)) 1100f19d5870STao Ma copied = ext4_write_inline_data_end(inode, pos, len, 1101f19d5870STao Ma copied, page); 1102f19d5870STao Ma else 1103f19d5870STao Ma copied = block_write_end(file, mapping, pos, 1104f19d5870STao Ma len, copied, page, fsdata); 1105f8514083SAneesh Kumar K.V 1106f8514083SAneesh Kumar K.V /* 1107f8514083SAneesh Kumar K.V * No need to use i_size_read() here, the i_size 1108f8514083SAneesh Kumar K.V * cannot change under us because we hold i_mutex. 1109f8514083SAneesh Kumar K.V * 1110f8514083SAneesh Kumar K.V * But it's important to update i_size while still holding page lock: 1111f8514083SAneesh Kumar K.V * page writeout could otherwise come in and zero beyond i_size. 1112f8514083SAneesh Kumar K.V */ 1113f8514083SAneesh Kumar K.V if (pos + copied > inode->i_size) { 1114f8514083SAneesh Kumar K.V i_size_write(inode, pos + copied); 1115f8514083SAneesh Kumar K.V i_size_changed = 1; 1116f8514083SAneesh Kumar K.V } 1117f8514083SAneesh Kumar K.V 1118f8514083SAneesh Kumar K.V if (pos + copied > EXT4_I(inode)->i_disksize) { 1119f8514083SAneesh Kumar K.V /* We need to mark inode dirty even if 1120f8514083SAneesh Kumar K.V * new_i_size is less that inode->i_size 1121f8514083SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 1122f8514083SAneesh Kumar K.V */ 1123f8514083SAneesh Kumar K.V ext4_update_i_disksize(inode, (pos + copied)); 1124f8514083SAneesh Kumar K.V i_size_changed = 1; 1125f8514083SAneesh Kumar K.V } 1126f8514083SAneesh Kumar K.V unlock_page(page); 1127f8514083SAneesh Kumar K.V page_cache_release(page); 1128f8514083SAneesh Kumar K.V 1129f8514083SAneesh Kumar K.V /* 1130f8514083SAneesh Kumar K.V * Don't mark the inode dirty under page lock. First, it unnecessarily 1131f8514083SAneesh Kumar K.V * makes the holding time of page lock longer. Second, it forces lock 1132f8514083SAneesh Kumar K.V * ordering of page lock and transaction start for journaling 1133f8514083SAneesh Kumar K.V * filesystems. 1134f8514083SAneesh Kumar K.V */ 1135f8514083SAneesh Kumar K.V if (i_size_changed) 1136f8514083SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 1137f8514083SAneesh Kumar K.V 1138f8514083SAneesh Kumar K.V return copied; 1139f8514083SAneesh Kumar K.V } 1140f8514083SAneesh Kumar K.V 1141ac27a0ecSDave Kleikamp /* 1142ac27a0ecSDave Kleikamp * We need to pick up the new inode size which generic_commit_write gave us 1143ac27a0ecSDave Kleikamp * `file' can be NULL - eg, when called from page_symlink(). 1144ac27a0ecSDave Kleikamp * 1145617ba13bSMingming Cao * ext4 never places buffers on inode->i_mapping->private_list. metadata 1146ac27a0ecSDave Kleikamp * buffers are managed internally. 1147ac27a0ecSDave Kleikamp */ 1148bfc1af65SNick Piggin static int ext4_ordered_write_end(struct file *file, 1149bfc1af65SNick Piggin struct address_space *mapping, 1150bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1151bfc1af65SNick Piggin struct page *page, void *fsdata) 1152ac27a0ecSDave Kleikamp { 1153617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1154cf108bcaSJan Kara struct inode *inode = mapping->host; 1155ac27a0ecSDave Kleikamp int ret = 0, ret2; 1156ac27a0ecSDave Kleikamp 11579bffad1eSTheodore Ts'o trace_ext4_ordered_write_end(inode, pos, len, copied); 1158678aaf48SJan Kara ret = ext4_jbd2_file_inode(handle, inode); 1159ac27a0ecSDave Kleikamp 1160ac27a0ecSDave Kleikamp if (ret == 0) { 1161f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 1162bfc1af65SNick Piggin page, fsdata); 1163f8a87d89SRoel Kluin copied = ret2; 1164ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1165f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1166f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1167f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1168f8514083SAneesh Kumar K.V */ 1169f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1170f8a87d89SRoel Kluin if (ret2 < 0) 1171f8a87d89SRoel Kluin ret = ret2; 117209e0834fSAkira Fujita } else { 117309e0834fSAkira Fujita unlock_page(page); 117409e0834fSAkira Fujita page_cache_release(page); 1175ac27a0ecSDave Kleikamp } 117609e0834fSAkira Fujita 1177617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1178ac27a0ecSDave Kleikamp if (!ret) 1179ac27a0ecSDave Kleikamp ret = ret2; 1180bfc1af65SNick Piggin 1181f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1182b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1183f8514083SAneesh Kumar K.V /* 1184ffacfa7aSJan Kara * If truncate failed early the inode might still be 1185f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1186f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1187f8514083SAneesh Kumar K.V */ 1188f8514083SAneesh Kumar K.V if (inode->i_nlink) 1189f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1190f8514083SAneesh Kumar K.V } 1191f8514083SAneesh Kumar K.V 1192f8514083SAneesh Kumar K.V 1193bfc1af65SNick Piggin return ret ? ret : copied; 1194ac27a0ecSDave Kleikamp } 1195ac27a0ecSDave Kleikamp 1196bfc1af65SNick Piggin static int ext4_writeback_write_end(struct file *file, 1197bfc1af65SNick Piggin struct address_space *mapping, 1198bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1199bfc1af65SNick Piggin struct page *page, void *fsdata) 1200ac27a0ecSDave Kleikamp { 1201617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1202cf108bcaSJan Kara struct inode *inode = mapping->host; 1203ac27a0ecSDave Kleikamp int ret = 0, ret2; 1204ac27a0ecSDave Kleikamp 12059bffad1eSTheodore Ts'o trace_ext4_writeback_write_end(inode, pos, len, copied); 1206f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 1207bfc1af65SNick Piggin page, fsdata); 1208f8a87d89SRoel Kluin copied = ret2; 1209ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1210f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1211f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1212f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1213f8514083SAneesh Kumar K.V */ 1214f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1215f8514083SAneesh Kumar K.V 1216f8a87d89SRoel Kluin if (ret2 < 0) 1217f8a87d89SRoel Kluin ret = ret2; 1218ac27a0ecSDave Kleikamp 1219617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1220ac27a0ecSDave Kleikamp if (!ret) 1221ac27a0ecSDave Kleikamp ret = ret2; 1222bfc1af65SNick Piggin 1223f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1224b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1225f8514083SAneesh Kumar K.V /* 1226ffacfa7aSJan Kara * If truncate failed early the inode might still be 1227f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1228f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1229f8514083SAneesh Kumar K.V */ 1230f8514083SAneesh Kumar K.V if (inode->i_nlink) 1231f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1232f8514083SAneesh Kumar K.V } 1233f8514083SAneesh Kumar K.V 1234bfc1af65SNick Piggin return ret ? ret : copied; 1235ac27a0ecSDave Kleikamp } 1236ac27a0ecSDave Kleikamp 1237bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 1238bfc1af65SNick Piggin struct address_space *mapping, 1239bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1240bfc1af65SNick Piggin struct page *page, void *fsdata) 1241ac27a0ecSDave Kleikamp { 1242617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1243bfc1af65SNick Piggin struct inode *inode = mapping->host; 1244ac27a0ecSDave Kleikamp int ret = 0, ret2; 1245ac27a0ecSDave Kleikamp int partial = 0; 1246bfc1af65SNick Piggin unsigned from, to; 1247cf17fea6SAneesh Kumar K.V loff_t new_i_size; 1248ac27a0ecSDave Kleikamp 12499bffad1eSTheodore Ts'o trace_ext4_journalled_write_end(inode, pos, len, copied); 1250bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 1251bfc1af65SNick Piggin to = from + len; 1252bfc1af65SNick Piggin 1253441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1254441c8508SCurt Wohlgemuth 12553fdcfb66STao Ma if (ext4_has_inline_data(inode)) 12563fdcfb66STao Ma copied = ext4_write_inline_data_end(inode, pos, len, 12573fdcfb66STao Ma copied, page); 12583fdcfb66STao Ma else { 1259bfc1af65SNick Piggin if (copied < len) { 1260bfc1af65SNick Piggin if (!PageUptodate(page)) 1261bfc1af65SNick Piggin copied = 0; 1262bfc1af65SNick Piggin page_zero_new_buffers(page, from+copied, to); 1263bfc1af65SNick Piggin } 1264ac27a0ecSDave Kleikamp 1265f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_buffers(page), from, 1266bfc1af65SNick Piggin to, &partial, write_end_fn); 1267ac27a0ecSDave Kleikamp if (!partial) 1268ac27a0ecSDave Kleikamp SetPageUptodate(page); 12693fdcfb66STao Ma } 1270cf17fea6SAneesh Kumar K.V new_i_size = pos + copied; 1271cf17fea6SAneesh Kumar K.V if (new_i_size > inode->i_size) 1272bfc1af65SNick Piggin i_size_write(inode, pos+copied); 127319f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 12742d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 1275cf17fea6SAneesh Kumar K.V if (new_i_size > EXT4_I(inode)->i_disksize) { 1276cf17fea6SAneesh Kumar K.V ext4_update_i_disksize(inode, new_i_size); 1277617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1278ac27a0ecSDave Kleikamp if (!ret) 1279ac27a0ecSDave Kleikamp ret = ret2; 1280ac27a0ecSDave Kleikamp } 1281bfc1af65SNick Piggin 1282cf108bcaSJan Kara unlock_page(page); 1283f8514083SAneesh Kumar K.V page_cache_release(page); 1284ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1285f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1286f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1287f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1288f8514083SAneesh Kumar K.V */ 1289f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1290f8514083SAneesh Kumar K.V 1291617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1292ac27a0ecSDave Kleikamp if (!ret) 1293ac27a0ecSDave Kleikamp ret = ret2; 1294f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1295b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1296f8514083SAneesh Kumar K.V /* 1297ffacfa7aSJan Kara * If truncate failed early the inode might still be 1298f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1299f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1300f8514083SAneesh Kumar K.V */ 1301f8514083SAneesh Kumar K.V if (inode->i_nlink) 1302f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1303f8514083SAneesh Kumar K.V } 1304bfc1af65SNick Piggin 1305bfc1af65SNick Piggin return ret ? ret : copied; 1306ac27a0ecSDave Kleikamp } 1307d2a17637SMingming Cao 13089d0be502STheodore Ts'o /* 1309386ad67cSLukas Czerner * Reserve a metadata for a single block located at lblock 1310386ad67cSLukas Czerner */ 1311386ad67cSLukas Czerner static int ext4_da_reserve_metadata(struct inode *inode, ext4_lblk_t lblock) 1312386ad67cSLukas Czerner { 1313386ad67cSLukas Czerner int retries = 0; 1314386ad67cSLukas Czerner struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 1315386ad67cSLukas Czerner struct ext4_inode_info *ei = EXT4_I(inode); 1316386ad67cSLukas Czerner unsigned int md_needed; 1317386ad67cSLukas Czerner ext4_lblk_t save_last_lblock; 1318386ad67cSLukas Czerner int save_len; 1319386ad67cSLukas Czerner 1320386ad67cSLukas Czerner /* 1321386ad67cSLukas Czerner * recalculate the amount of metadata blocks to reserve 1322386ad67cSLukas Czerner * in order to allocate nrblocks 1323386ad67cSLukas Czerner * worse case is one extent per block 1324386ad67cSLukas Czerner */ 1325386ad67cSLukas Czerner repeat: 1326386ad67cSLukas Czerner spin_lock(&ei->i_block_reservation_lock); 1327386ad67cSLukas Czerner /* 1328386ad67cSLukas Czerner * ext4_calc_metadata_amount() has side effects, which we have 1329386ad67cSLukas Czerner * to be prepared undo if we fail to claim space. 1330386ad67cSLukas Czerner */ 1331386ad67cSLukas Czerner save_len = ei->i_da_metadata_calc_len; 1332386ad67cSLukas Czerner save_last_lblock = ei->i_da_metadata_calc_last_lblock; 1333386ad67cSLukas Czerner md_needed = EXT4_NUM_B2C(sbi, 1334386ad67cSLukas Czerner ext4_calc_metadata_amount(inode, lblock)); 1335386ad67cSLukas Czerner trace_ext4_da_reserve_space(inode, md_needed); 1336386ad67cSLukas Czerner 1337386ad67cSLukas Czerner /* 1338386ad67cSLukas Czerner * We do still charge estimated metadata to the sb though; 1339386ad67cSLukas Czerner * we cannot afford to run out of free blocks. 1340386ad67cSLukas Czerner */ 1341386ad67cSLukas Czerner if (ext4_claim_free_clusters(sbi, md_needed, 0)) { 1342386ad67cSLukas Czerner ei->i_da_metadata_calc_len = save_len; 1343386ad67cSLukas Czerner ei->i_da_metadata_calc_last_lblock = save_last_lblock; 1344386ad67cSLukas Czerner spin_unlock(&ei->i_block_reservation_lock); 1345386ad67cSLukas Czerner if (ext4_should_retry_alloc(inode->i_sb, &retries)) { 1346386ad67cSLukas Czerner cond_resched(); 1347386ad67cSLukas Czerner goto repeat; 1348386ad67cSLukas Czerner } 1349386ad67cSLukas Czerner return -ENOSPC; 1350386ad67cSLukas Czerner } 1351386ad67cSLukas Czerner ei->i_reserved_meta_blocks += md_needed; 1352386ad67cSLukas Czerner spin_unlock(&ei->i_block_reservation_lock); 1353386ad67cSLukas Czerner 1354386ad67cSLukas Czerner return 0; /* success */ 1355386ad67cSLukas Czerner } 1356386ad67cSLukas Czerner 1357386ad67cSLukas Czerner /* 13587b415bf6SAditya Kali * Reserve a single cluster located at lblock 13599d0be502STheodore Ts'o */ 136001f49d0bSTheodore Ts'o static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock) 1361d2a17637SMingming Cao { 1362030ba6bcSAneesh Kumar K.V int retries = 0; 1363d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 13640637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 13657b415bf6SAditya Kali unsigned int md_needed; 13665dd4056dSChristoph Hellwig int ret; 136703179fe9STheodore Ts'o ext4_lblk_t save_last_lblock; 136803179fe9STheodore Ts'o int save_len; 1369d2a17637SMingming Cao 137060e58e0fSMingming Cao /* 137172b8ab9dSEric Sandeen * We will charge metadata quota at writeout time; this saves 137272b8ab9dSEric Sandeen * us from metadata over-estimation, though we may go over by 137372b8ab9dSEric Sandeen * a small amount in the end. Here we just reserve for data. 137460e58e0fSMingming Cao */ 13757b415bf6SAditya Kali ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1)); 13765dd4056dSChristoph Hellwig if (ret) 13775dd4056dSChristoph Hellwig return ret; 137803179fe9STheodore Ts'o 137903179fe9STheodore Ts'o /* 138003179fe9STheodore Ts'o * recalculate the amount of metadata blocks to reserve 138103179fe9STheodore Ts'o * in order to allocate nrblocks 138203179fe9STheodore Ts'o * worse case is one extent per block 138303179fe9STheodore Ts'o */ 138403179fe9STheodore Ts'o repeat: 138503179fe9STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 138603179fe9STheodore Ts'o /* 138703179fe9STheodore Ts'o * ext4_calc_metadata_amount() has side effects, which we have 138803179fe9STheodore Ts'o * to be prepared undo if we fail to claim space. 138903179fe9STheodore Ts'o */ 139003179fe9STheodore Ts'o save_len = ei->i_da_metadata_calc_len; 139103179fe9STheodore Ts'o save_last_lblock = ei->i_da_metadata_calc_last_lblock; 139203179fe9STheodore Ts'o md_needed = EXT4_NUM_B2C(sbi, 139303179fe9STheodore Ts'o ext4_calc_metadata_amount(inode, lblock)); 139403179fe9STheodore Ts'o trace_ext4_da_reserve_space(inode, md_needed); 139503179fe9STheodore Ts'o 139672b8ab9dSEric Sandeen /* 139772b8ab9dSEric Sandeen * We do still charge estimated metadata to the sb though; 139872b8ab9dSEric Sandeen * we cannot afford to run out of free blocks. 139972b8ab9dSEric Sandeen */ 1400e7d5f315STheodore Ts'o if (ext4_claim_free_clusters(sbi, md_needed + 1, 0)) { 140103179fe9STheodore Ts'o ei->i_da_metadata_calc_len = save_len; 140203179fe9STheodore Ts'o ei->i_da_metadata_calc_last_lblock = save_last_lblock; 140303179fe9STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 1404030ba6bcSAneesh Kumar K.V if (ext4_should_retry_alloc(inode->i_sb, &retries)) { 1405bb8b20edSLukas Czerner cond_resched(); 1406030ba6bcSAneesh Kumar K.V goto repeat; 1407030ba6bcSAneesh Kumar K.V } 140803179fe9STheodore Ts'o dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1)); 1409d2a17637SMingming Cao return -ENOSPC; 1410d2a17637SMingming Cao } 14119d0be502STheodore Ts'o ei->i_reserved_data_blocks++; 14120637c6f4STheodore Ts'o ei->i_reserved_meta_blocks += md_needed; 14130637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 141439bc680aSDmitry Monakhov 1415d2a17637SMingming Cao return 0; /* success */ 1416d2a17637SMingming Cao } 1417d2a17637SMingming Cao 141812219aeaSAneesh Kumar K.V static void ext4_da_release_space(struct inode *inode, int to_free) 1419d2a17637SMingming Cao { 1420d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 14210637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 1422d2a17637SMingming Cao 1423cd213226SMingming Cao if (!to_free) 1424cd213226SMingming Cao return; /* Nothing to release, exit */ 1425cd213226SMingming Cao 1426d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1427cd213226SMingming Cao 14285a58ec87SLi Zefan trace_ext4_da_release_space(inode, to_free); 14290637c6f4STheodore Ts'o if (unlikely(to_free > ei->i_reserved_data_blocks)) { 1430cd213226SMingming Cao /* 14310637c6f4STheodore Ts'o * if there aren't enough reserved blocks, then the 14320637c6f4STheodore Ts'o * counter is messed up somewhere. Since this 14330637c6f4STheodore Ts'o * function is called from invalidate page, it's 14340637c6f4STheodore Ts'o * harmless to return without any action. 1435cd213226SMingming Cao */ 14368de5c325STheodore Ts'o ext4_warning(inode->i_sb, "ext4_da_release_space: " 14370637c6f4STheodore Ts'o "ino %lu, to_free %d with only %d reserved " 14381084f252STheodore Ts'o "data blocks", inode->i_ino, to_free, 14390637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 14400637c6f4STheodore Ts'o WARN_ON(1); 14410637c6f4STheodore Ts'o to_free = ei->i_reserved_data_blocks; 14420637c6f4STheodore Ts'o } 14430637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= to_free; 14440637c6f4STheodore Ts'o 14450637c6f4STheodore Ts'o if (ei->i_reserved_data_blocks == 0) { 14460637c6f4STheodore Ts'o /* 14470637c6f4STheodore Ts'o * We can release all of the reserved metadata blocks 14480637c6f4STheodore Ts'o * only when we have written all of the delayed 14490637c6f4STheodore Ts'o * allocation blocks. 14507b415bf6SAditya Kali * Note that in case of bigalloc, i_reserved_meta_blocks, 14517b415bf6SAditya Kali * i_reserved_data_blocks, etc. refer to number of clusters. 14520637c6f4STheodore Ts'o */ 145357042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 145472b8ab9dSEric Sandeen ei->i_reserved_meta_blocks); 1455ee5f4d9cSTheodore Ts'o ei->i_reserved_meta_blocks = 0; 14569d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 1457cd213226SMingming Cao } 1458cd213226SMingming Cao 145972b8ab9dSEric Sandeen /* update fs dirty data blocks counter */ 146057042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free); 1461d2a17637SMingming Cao 1462d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 146360e58e0fSMingming Cao 14647b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free)); 1465d2a17637SMingming Cao } 1466d2a17637SMingming Cao 1467d2a17637SMingming Cao static void ext4_da_page_release_reservation(struct page *page, 1468d2a17637SMingming Cao unsigned long offset) 1469d2a17637SMingming Cao { 1470d2a17637SMingming Cao int to_release = 0; 1471d2a17637SMingming Cao struct buffer_head *head, *bh; 1472d2a17637SMingming Cao unsigned int curr_off = 0; 14737b415bf6SAditya Kali struct inode *inode = page->mapping->host; 14747b415bf6SAditya Kali struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 14757b415bf6SAditya Kali int num_clusters; 147651865fdaSZheng Liu ext4_fsblk_t lblk; 1477d2a17637SMingming Cao 1478d2a17637SMingming Cao head = page_buffers(page); 1479d2a17637SMingming Cao bh = head; 1480d2a17637SMingming Cao do { 1481d2a17637SMingming Cao unsigned int next_off = curr_off + bh->b_size; 1482d2a17637SMingming Cao 1483d2a17637SMingming Cao if ((offset <= curr_off) && (buffer_delay(bh))) { 1484d2a17637SMingming Cao to_release++; 1485d2a17637SMingming Cao clear_buffer_delay(bh); 1486d2a17637SMingming Cao } 1487d2a17637SMingming Cao curr_off = next_off; 1488d2a17637SMingming Cao } while ((bh = bh->b_this_page) != head); 14897b415bf6SAditya Kali 149051865fdaSZheng Liu if (to_release) { 149151865fdaSZheng Liu lblk = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits); 149251865fdaSZheng Liu ext4_es_remove_extent(inode, lblk, to_release); 149351865fdaSZheng Liu } 149451865fdaSZheng Liu 14957b415bf6SAditya Kali /* If we have released all the blocks belonging to a cluster, then we 14967b415bf6SAditya Kali * need to release the reserved space for that cluster. */ 14977b415bf6SAditya Kali num_clusters = EXT4_NUM_B2C(sbi, to_release); 14987b415bf6SAditya Kali while (num_clusters > 0) { 14997b415bf6SAditya Kali lblk = (page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits)) + 15007b415bf6SAditya Kali ((num_clusters - 1) << sbi->s_cluster_bits); 15017b415bf6SAditya Kali if (sbi->s_cluster_ratio == 1 || 15027d1b1fbcSZheng Liu !ext4_find_delalloc_cluster(inode, lblk)) 15037b415bf6SAditya Kali ext4_da_release_space(inode, 1); 15047b415bf6SAditya Kali 15057b415bf6SAditya Kali num_clusters--; 15067b415bf6SAditya Kali } 1507d2a17637SMingming Cao } 1508ac27a0ecSDave Kleikamp 1509ac27a0ecSDave Kleikamp /* 151064769240SAlex Tomas * Delayed allocation stuff 151164769240SAlex Tomas */ 151264769240SAlex Tomas 151364769240SAlex Tomas /* 151464769240SAlex Tomas * mpage_da_submit_io - walks through extent of pages and try to write 1515a1d6cc56SAneesh Kumar K.V * them with writepage() call back 151664769240SAlex Tomas * 151764769240SAlex Tomas * @mpd->inode: inode 151864769240SAlex Tomas * @mpd->first_page: first page of the extent 151964769240SAlex Tomas * @mpd->next_page: page after the last page of the extent 152064769240SAlex Tomas * 152164769240SAlex Tomas * By the time mpage_da_submit_io() is called we expect all blocks 152264769240SAlex Tomas * to be allocated. this may be wrong if allocation failed. 152364769240SAlex Tomas * 152464769240SAlex Tomas * As pages are already locked by write_cache_pages(), we can't use it 152564769240SAlex Tomas */ 15261de3e3dfSTheodore Ts'o static int mpage_da_submit_io(struct mpage_da_data *mpd, 15271de3e3dfSTheodore Ts'o struct ext4_map_blocks *map) 152864769240SAlex Tomas { 1529791b7f08SAneesh Kumar K.V struct pagevec pvec; 1530791b7f08SAneesh Kumar K.V unsigned long index, end; 1531791b7f08SAneesh Kumar K.V int ret = 0, err, nr_pages, i; 1532791b7f08SAneesh Kumar K.V struct inode *inode = mpd->inode; 1533791b7f08SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 1534cb20d518STheodore Ts'o loff_t size = i_size_read(inode); 15353ecdb3a1STheodore Ts'o unsigned int len, block_start; 15363ecdb3a1STheodore Ts'o struct buffer_head *bh, *page_bufs = NULL; 15371de3e3dfSTheodore Ts'o sector_t pblock = 0, cur_logical = 0; 1538bd2d0210STheodore Ts'o struct ext4_io_submit io_submit; 153964769240SAlex Tomas 154064769240SAlex Tomas BUG_ON(mpd->next_page <= mpd->first_page); 1541bd2d0210STheodore Ts'o memset(&io_submit, 0, sizeof(io_submit)); 1542791b7f08SAneesh Kumar K.V /* 1543791b7f08SAneesh Kumar K.V * We need to start from the first_page to the next_page - 1 1544791b7f08SAneesh Kumar K.V * to make sure we also write the mapped dirty buffer_heads. 15458dc207c0STheodore Ts'o * If we look at mpd->b_blocknr we would only be looking 1546791b7f08SAneesh Kumar K.V * at the currently mapped buffer_heads. 1547791b7f08SAneesh Kumar K.V */ 154864769240SAlex Tomas index = mpd->first_page; 154964769240SAlex Tomas end = mpd->next_page - 1; 155064769240SAlex Tomas 1551791b7f08SAneesh Kumar K.V pagevec_init(&pvec, 0); 155264769240SAlex Tomas while (index <= end) { 1553791b7f08SAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 155464769240SAlex Tomas if (nr_pages == 0) 155564769240SAlex Tomas break; 155664769240SAlex Tomas for (i = 0; i < nr_pages; i++) { 1557f8bec370SJan Kara int skip_page = 0; 155864769240SAlex Tomas struct page *page = pvec.pages[i]; 155964769240SAlex Tomas 1560791b7f08SAneesh Kumar K.V index = page->index; 1561791b7f08SAneesh Kumar K.V if (index > end) 1562791b7f08SAneesh Kumar K.V break; 1563cb20d518STheodore Ts'o 1564cb20d518STheodore Ts'o if (index == size >> PAGE_CACHE_SHIFT) 1565cb20d518STheodore Ts'o len = size & ~PAGE_CACHE_MASK; 1566cb20d518STheodore Ts'o else 1567cb20d518STheodore Ts'o len = PAGE_CACHE_SIZE; 15681de3e3dfSTheodore Ts'o if (map) { 15691de3e3dfSTheodore Ts'o cur_logical = index << (PAGE_CACHE_SHIFT - 15701de3e3dfSTheodore Ts'o inode->i_blkbits); 15711de3e3dfSTheodore Ts'o pblock = map->m_pblk + (cur_logical - 15721de3e3dfSTheodore Ts'o map->m_lblk); 15731de3e3dfSTheodore Ts'o } 1574791b7f08SAneesh Kumar K.V index++; 1575791b7f08SAneesh Kumar K.V 1576791b7f08SAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1577791b7f08SAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1578791b7f08SAneesh Kumar K.V 15793ecdb3a1STheodore Ts'o bh = page_bufs = page_buffers(page); 15803ecdb3a1STheodore Ts'o block_start = 0; 15813ecdb3a1STheodore Ts'o do { 15821de3e3dfSTheodore Ts'o if (map && (cur_logical >= map->m_lblk) && 15831de3e3dfSTheodore Ts'o (cur_logical <= (map->m_lblk + 15841de3e3dfSTheodore Ts'o (map->m_len - 1)))) { 15851de3e3dfSTheodore Ts'o if (buffer_delay(bh)) { 15861de3e3dfSTheodore Ts'o clear_buffer_delay(bh); 15871de3e3dfSTheodore Ts'o bh->b_blocknr = pblock; 15881de3e3dfSTheodore Ts'o } 15891de3e3dfSTheodore Ts'o if (buffer_unwritten(bh) || 15901de3e3dfSTheodore Ts'o buffer_mapped(bh)) 15911de3e3dfSTheodore Ts'o BUG_ON(bh->b_blocknr != pblock); 15921de3e3dfSTheodore Ts'o if (map->m_flags & EXT4_MAP_UNINIT) 15931de3e3dfSTheodore Ts'o set_buffer_uninit(bh); 15941de3e3dfSTheodore Ts'o clear_buffer_unwritten(bh); 15951de3e3dfSTheodore Ts'o } 15961de3e3dfSTheodore Ts'o 159713a79a47SYongqiang Yang /* 159813a79a47SYongqiang Yang * skip page if block allocation undone and 159913a79a47SYongqiang Yang * block is dirty 160013a79a47SYongqiang Yang */ 160113a79a47SYongqiang Yang if (ext4_bh_delay_or_unwritten(NULL, bh)) 160297498956STheodore Ts'o skip_page = 1; 16033ecdb3a1STheodore Ts'o bh = bh->b_this_page; 16043ecdb3a1STheodore Ts'o block_start += bh->b_size; 16051de3e3dfSTheodore Ts'o cur_logical++; 16061de3e3dfSTheodore Ts'o pblock++; 16071de3e3dfSTheodore Ts'o } while (bh != page_bufs); 16081de3e3dfSTheodore Ts'o 1609f8bec370SJan Kara if (skip_page) { 1610f8bec370SJan Kara unlock_page(page); 1611f8bec370SJan Kara continue; 1612f8bec370SJan Kara } 1613cb20d518STheodore Ts'o 161497498956STheodore Ts'o clear_page_dirty_for_io(page); 1615fe089c77SJan Kara err = ext4_bio_write_page(&io_submit, page, len, 1616fe089c77SJan Kara mpd->wbc); 1617cb20d518STheodore Ts'o if (!err) 1618a1d6cc56SAneesh Kumar K.V mpd->pages_written++; 161964769240SAlex Tomas /* 162064769240SAlex Tomas * In error case, we have to continue because 162164769240SAlex Tomas * remaining pages are still locked 162264769240SAlex Tomas */ 162364769240SAlex Tomas if (ret == 0) 162464769240SAlex Tomas ret = err; 162564769240SAlex Tomas } 162664769240SAlex Tomas pagevec_release(&pvec); 162764769240SAlex Tomas } 1628bd2d0210STheodore Ts'o ext4_io_submit(&io_submit); 162964769240SAlex Tomas return ret; 163064769240SAlex Tomas } 163164769240SAlex Tomas 1632c7f5938aSCurt Wohlgemuth static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd) 1633c4a0c46eSAneesh Kumar K.V { 1634c4a0c46eSAneesh Kumar K.V int nr_pages, i; 1635c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 1636c4a0c46eSAneesh Kumar K.V struct pagevec pvec; 1637c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 1638c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 163951865fdaSZheng Liu ext4_lblk_t start, last; 1640c4a0c46eSAneesh Kumar K.V 1641c7f5938aSCurt Wohlgemuth index = mpd->first_page; 1642c7f5938aSCurt Wohlgemuth end = mpd->next_page - 1; 164351865fdaSZheng Liu 164451865fdaSZheng Liu start = index << (PAGE_CACHE_SHIFT - inode->i_blkbits); 164551865fdaSZheng Liu last = end << (PAGE_CACHE_SHIFT - inode->i_blkbits); 164651865fdaSZheng Liu ext4_es_remove_extent(inode, start, last - start + 1); 164751865fdaSZheng Liu 164866bea92cSEric Sandeen pagevec_init(&pvec, 0); 1649c4a0c46eSAneesh Kumar K.V while (index <= end) { 1650c4a0c46eSAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 1651c4a0c46eSAneesh Kumar K.V if (nr_pages == 0) 1652c4a0c46eSAneesh Kumar K.V break; 1653c4a0c46eSAneesh Kumar K.V for (i = 0; i < nr_pages; i++) { 1654c4a0c46eSAneesh Kumar K.V struct page *page = pvec.pages[i]; 16559b1d0998SJan Kara if (page->index > end) 1656c4a0c46eSAneesh Kumar K.V break; 1657c4a0c46eSAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1658c4a0c46eSAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1659c4a0c46eSAneesh Kumar K.V block_invalidatepage(page, 0); 1660c4a0c46eSAneesh Kumar K.V ClearPageUptodate(page); 1661c4a0c46eSAneesh Kumar K.V unlock_page(page); 1662c4a0c46eSAneesh Kumar K.V } 16639b1d0998SJan Kara index = pvec.pages[nr_pages - 1]->index + 1; 16649b1d0998SJan Kara pagevec_release(&pvec); 1665c4a0c46eSAneesh Kumar K.V } 1666c4a0c46eSAneesh Kumar K.V return; 1667c4a0c46eSAneesh Kumar K.V } 1668c4a0c46eSAneesh Kumar K.V 1669df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 1670df22291fSAneesh Kumar K.V { 1671df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 167292b97816STheodore Ts'o struct super_block *sb = inode->i_sb; 167392b97816STheodore Ts'o 167492b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld", 16755dee5437STheodore Ts'o EXT4_C2B(EXT4_SB(inode->i_sb), 16765dee5437STheodore Ts'o ext4_count_free_clusters(inode->i_sb))); 167792b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Free/Dirty block details"); 167892b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "free_blocks=%lld", 167957042651STheodore Ts'o (long long) EXT4_C2B(EXT4_SB(inode->i_sb), 168057042651STheodore Ts'o percpu_counter_sum(&sbi->s_freeclusters_counter))); 168192b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld", 16827b415bf6SAditya Kali (long long) EXT4_C2B(EXT4_SB(inode->i_sb), 16837b415bf6SAditya Kali percpu_counter_sum(&sbi->s_dirtyclusters_counter))); 168492b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Block reservation details"); 168592b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u", 1686df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_data_blocks); 168792b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_meta_blocks=%u", 1688df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_meta_blocks); 1689df22291fSAneesh Kumar K.V return; 1690df22291fSAneesh Kumar K.V } 1691df22291fSAneesh Kumar K.V 1692b920c755STheodore Ts'o /* 16935a87b7a5STheodore Ts'o * mpage_da_map_and_submit - go through given space, map them 16945a87b7a5STheodore Ts'o * if necessary, and then submit them for I/O 169564769240SAlex Tomas * 16968dc207c0STheodore Ts'o * @mpd - bh describing space 169764769240SAlex Tomas * 169864769240SAlex Tomas * The function skips space we know is already mapped to disk blocks. 169964769240SAlex Tomas * 170064769240SAlex Tomas */ 17015a87b7a5STheodore Ts'o static void mpage_da_map_and_submit(struct mpage_da_data *mpd) 170264769240SAlex Tomas { 17032ac3b6e0STheodore Ts'o int err, blks, get_blocks_flags; 17041de3e3dfSTheodore Ts'o struct ext4_map_blocks map, *mapp = NULL; 17052fa3cdfbSTheodore Ts'o sector_t next = mpd->b_blocknr; 17062fa3cdfbSTheodore Ts'o unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits; 17072fa3cdfbSTheodore Ts'o loff_t disksize = EXT4_I(mpd->inode)->i_disksize; 17082fa3cdfbSTheodore Ts'o handle_t *handle = NULL; 170964769240SAlex Tomas 171064769240SAlex Tomas /* 17115a87b7a5STheodore Ts'o * If the blocks are mapped already, or we couldn't accumulate 17125a87b7a5STheodore Ts'o * any blocks, then proceed immediately to the submission stage. 171364769240SAlex Tomas */ 17145a87b7a5STheodore Ts'o if ((mpd->b_size == 0) || 17155a87b7a5STheodore Ts'o ((mpd->b_state & (1 << BH_Mapped)) && 171629fa89d0SAneesh Kumar K.V !(mpd->b_state & (1 << BH_Delay)) && 17175a87b7a5STheodore Ts'o !(mpd->b_state & (1 << BH_Unwritten)))) 17185a87b7a5STheodore Ts'o goto submit_io; 17192fa3cdfbSTheodore Ts'o 17202fa3cdfbSTheodore Ts'o handle = ext4_journal_current_handle(); 17212fa3cdfbSTheodore Ts'o BUG_ON(!handle); 17222fa3cdfbSTheodore Ts'o 172379ffab34SAneesh Kumar K.V /* 172479e83036SEric Sandeen * Call ext4_map_blocks() to allocate any delayed allocation 17252ac3b6e0STheodore Ts'o * blocks, or to convert an uninitialized extent to be 17262ac3b6e0STheodore Ts'o * initialized (in the case where we have written into 17272ac3b6e0STheodore Ts'o * one or more preallocated blocks). 17282ac3b6e0STheodore Ts'o * 17292ac3b6e0STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to 17302ac3b6e0STheodore Ts'o * indicate that we are on the delayed allocation path. This 17312ac3b6e0STheodore Ts'o * affects functions in many different parts of the allocation 17322ac3b6e0STheodore Ts'o * call path. This flag exists primarily because we don't 173379e83036SEric Sandeen * want to change *many* call functions, so ext4_map_blocks() 1734f2321097STheodore Ts'o * will set the EXT4_STATE_DELALLOC_RESERVED flag once the 17352ac3b6e0STheodore Ts'o * inode's allocation semaphore is taken. 17362ac3b6e0STheodore Ts'o * 17372ac3b6e0STheodore Ts'o * If the blocks in questions were delalloc blocks, set 17382ac3b6e0STheodore Ts'o * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting 17392ac3b6e0STheodore Ts'o * variables are updated after the blocks have been allocated. 174079ffab34SAneesh Kumar K.V */ 17412ed88685STheodore Ts'o map.m_lblk = next; 17422ed88685STheodore Ts'o map.m_len = max_blocks; 17431296cc85SAneesh Kumar K.V get_blocks_flags = EXT4_GET_BLOCKS_CREATE; 1744744692dcSJiaying Zhang if (ext4_should_dioread_nolock(mpd->inode)) 1745744692dcSJiaying Zhang get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; 17462ac3b6e0STheodore Ts'o if (mpd->b_state & (1 << BH_Delay)) 17471296cc85SAneesh Kumar K.V get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; 17481296cc85SAneesh Kumar K.V 17492ed88685STheodore Ts'o blks = ext4_map_blocks(handle, mpd->inode, &map, get_blocks_flags); 17502fa3cdfbSTheodore Ts'o if (blks < 0) { 1751e3570639SEric Sandeen struct super_block *sb = mpd->inode->i_sb; 1752e3570639SEric Sandeen 17532fa3cdfbSTheodore Ts'o err = blks; 1754ed5bde0bSTheodore Ts'o /* 17555a87b7a5STheodore Ts'o * If get block returns EAGAIN or ENOSPC and there 175697498956STheodore Ts'o * appears to be free blocks we will just let 175797498956STheodore Ts'o * mpage_da_submit_io() unlock all of the pages. 1758c4a0c46eSAneesh Kumar K.V */ 1759c4a0c46eSAneesh Kumar K.V if (err == -EAGAIN) 17605a87b7a5STheodore Ts'o goto submit_io; 1761df22291fSAneesh Kumar K.V 17625dee5437STheodore Ts'o if (err == -ENOSPC && ext4_count_free_clusters(sb)) { 1763df22291fSAneesh Kumar K.V mpd->retval = err; 17645a87b7a5STheodore Ts'o goto submit_io; 1765df22291fSAneesh Kumar K.V } 1766df22291fSAneesh Kumar K.V 1767c4a0c46eSAneesh Kumar K.V /* 1768ed5bde0bSTheodore Ts'o * get block failure will cause us to loop in 1769ed5bde0bSTheodore Ts'o * writepages, because a_ops->writepage won't be able 1770ed5bde0bSTheodore Ts'o * to make progress. The page will be redirtied by 1771ed5bde0bSTheodore Ts'o * writepage and writepages will again try to write 1772ed5bde0bSTheodore Ts'o * the same. 1773c4a0c46eSAneesh Kumar K.V */ 1774e3570639SEric Sandeen if (!(EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) { 1775e3570639SEric Sandeen ext4_msg(sb, KERN_CRIT, 1776e3570639SEric Sandeen "delayed block allocation failed for inode %lu " 1777e3570639SEric Sandeen "at logical offset %llu with max blocks %zd " 1778e3570639SEric Sandeen "with error %d", mpd->inode->i_ino, 1779c4a0c46eSAneesh Kumar K.V (unsigned long long) next, 17808dc207c0STheodore Ts'o mpd->b_size >> mpd->inode->i_blkbits, err); 1781e3570639SEric Sandeen ext4_msg(sb, KERN_CRIT, 178201a523ebSTheodore Ts'o "This should not happen!! Data will be lost"); 1783e3570639SEric Sandeen if (err == -ENOSPC) 1784df22291fSAneesh Kumar K.V ext4_print_free_blocks(mpd->inode); 1785030ba6bcSAneesh Kumar K.V } 17862fa3cdfbSTheodore Ts'o /* invalidate all the pages */ 1787c7f5938aSCurt Wohlgemuth ext4_da_block_invalidatepages(mpd); 1788e0fd9b90SCurt Wohlgemuth 1789e0fd9b90SCurt Wohlgemuth /* Mark this page range as having been completed */ 1790e0fd9b90SCurt Wohlgemuth mpd->io_done = 1; 17915a87b7a5STheodore Ts'o return; 1792c4a0c46eSAneesh Kumar K.V } 17932fa3cdfbSTheodore Ts'o BUG_ON(blks == 0); 17942fa3cdfbSTheodore Ts'o 17951de3e3dfSTheodore Ts'o mapp = ↦ 17962ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 17972ed88685STheodore Ts'o struct block_device *bdev = mpd->inode->i_sb->s_bdev; 17982ed88685STheodore Ts'o int i; 179964769240SAlex Tomas 18002ed88685STheodore Ts'o for (i = 0; i < map.m_len; i++) 18012ed88685STheodore Ts'o unmap_underlying_metadata(bdev, map.m_pblk + i); 18022fa3cdfbSTheodore Ts'o } 18032fa3cdfbSTheodore Ts'o 18042fa3cdfbSTheodore Ts'o /* 180503f5d8bcSJan Kara * Update on-disk size along with block allocation. 18062fa3cdfbSTheodore Ts'o */ 18072fa3cdfbSTheodore Ts'o disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits; 18082fa3cdfbSTheodore Ts'o if (disksize > i_size_read(mpd->inode)) 18092fa3cdfbSTheodore Ts'o disksize = i_size_read(mpd->inode); 18102fa3cdfbSTheodore Ts'o if (disksize > EXT4_I(mpd->inode)->i_disksize) { 18112fa3cdfbSTheodore Ts'o ext4_update_i_disksize(mpd->inode, disksize); 18125a87b7a5STheodore Ts'o err = ext4_mark_inode_dirty(handle, mpd->inode); 18135a87b7a5STheodore Ts'o if (err) 18145a87b7a5STheodore Ts'o ext4_error(mpd->inode->i_sb, 18155a87b7a5STheodore Ts'o "Failed to mark inode %lu dirty", 18165a87b7a5STheodore Ts'o mpd->inode->i_ino); 18172fa3cdfbSTheodore Ts'o } 18182fa3cdfbSTheodore Ts'o 18195a87b7a5STheodore Ts'o submit_io: 18201de3e3dfSTheodore Ts'o mpage_da_submit_io(mpd, mapp); 18215a87b7a5STheodore Ts'o mpd->io_done = 1; 182264769240SAlex Tomas } 182364769240SAlex Tomas 1824bf068ee2SAneesh Kumar K.V #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \ 1825bf068ee2SAneesh Kumar K.V (1 << BH_Delay) | (1 << BH_Unwritten)) 182664769240SAlex Tomas 182764769240SAlex Tomas /* 182864769240SAlex Tomas * mpage_add_bh_to_extent - try to add one more block to extent of blocks 182964769240SAlex Tomas * 183064769240SAlex Tomas * @mpd->lbh - extent of blocks 183164769240SAlex Tomas * @logical - logical number of the block in the file 1832b6a8e62fSJan Kara * @b_state - b_state of the buffer head added 183364769240SAlex Tomas * 183464769240SAlex Tomas * the function is used to collect contig. blocks in same state 183564769240SAlex Tomas */ 1836b6a8e62fSJan Kara static void mpage_add_bh_to_extent(struct mpage_da_data *mpd, sector_t logical, 18378dc207c0STheodore Ts'o unsigned long b_state) 183864769240SAlex Tomas { 183964769240SAlex Tomas sector_t next; 1840b6a8e62fSJan Kara int blkbits = mpd->inode->i_blkbits; 1841b6a8e62fSJan Kara int nrblocks = mpd->b_size >> blkbits; 184264769240SAlex Tomas 1843c445e3e0SEric Sandeen /* 1844c445e3e0SEric Sandeen * XXX Don't go larger than mballoc is willing to allocate 1845c445e3e0SEric Sandeen * This is a stopgap solution. We eventually need to fold 1846c445e3e0SEric Sandeen * mpage_da_submit_io() into this function and then call 184779e83036SEric Sandeen * ext4_map_blocks() multiple times in a loop 1848c445e3e0SEric Sandeen */ 1849b6a8e62fSJan Kara if (nrblocks >= (8*1024*1024 >> blkbits)) 1850c445e3e0SEric Sandeen goto flush_it; 1851c445e3e0SEric Sandeen 1852525f4ed8SMingming Cao /* check if the reserved journal credits might overflow */ 1853b6a8e62fSJan Kara if (!ext4_test_inode_flag(mpd->inode, EXT4_INODE_EXTENTS)) { 1854525f4ed8SMingming Cao if (nrblocks >= EXT4_MAX_TRANS_DATA) { 1855525f4ed8SMingming Cao /* 1856525f4ed8SMingming Cao * With non-extent format we are limited by the journal 1857525f4ed8SMingming Cao * credit available. Total credit needed to insert 1858525f4ed8SMingming Cao * nrblocks contiguous blocks is dependent on the 1859525f4ed8SMingming Cao * nrblocks. So limit nrblocks. 1860525f4ed8SMingming Cao */ 1861525f4ed8SMingming Cao goto flush_it; 1862525f4ed8SMingming Cao } 1863525f4ed8SMingming Cao } 186464769240SAlex Tomas /* 186564769240SAlex Tomas * First block in the extent 186664769240SAlex Tomas */ 18678dc207c0STheodore Ts'o if (mpd->b_size == 0) { 18688dc207c0STheodore Ts'o mpd->b_blocknr = logical; 1869b6a8e62fSJan Kara mpd->b_size = 1 << blkbits; 18708dc207c0STheodore Ts'o mpd->b_state = b_state & BH_FLAGS; 187164769240SAlex Tomas return; 187264769240SAlex Tomas } 187364769240SAlex Tomas 18748dc207c0STheodore Ts'o next = mpd->b_blocknr + nrblocks; 187564769240SAlex Tomas /* 187664769240SAlex Tomas * Can we merge the block to our big extent? 187764769240SAlex Tomas */ 18788dc207c0STheodore Ts'o if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) { 1879b6a8e62fSJan Kara mpd->b_size += 1 << blkbits; 188064769240SAlex Tomas return; 188164769240SAlex Tomas } 188264769240SAlex Tomas 1883525f4ed8SMingming Cao flush_it: 188464769240SAlex Tomas /* 188564769240SAlex Tomas * We couldn't merge the block to our extent, so we 188664769240SAlex Tomas * need to flush current extent and start new one 188764769240SAlex Tomas */ 18885a87b7a5STheodore Ts'o mpage_da_map_and_submit(mpd); 1889a1d6cc56SAneesh Kumar K.V return; 189064769240SAlex Tomas } 189164769240SAlex Tomas 1892c364b22cSAneesh Kumar K.V static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh) 189329fa89d0SAneesh Kumar K.V { 1894c364b22cSAneesh Kumar K.V return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 189529fa89d0SAneesh Kumar K.V } 189629fa89d0SAneesh Kumar K.V 189764769240SAlex Tomas /* 18985356f261SAditya Kali * This function is grabs code from the very beginning of 18995356f261SAditya Kali * ext4_map_blocks, but assumes that the caller is from delayed write 19005356f261SAditya Kali * time. This function looks up the requested blocks and sets the 19015356f261SAditya Kali * buffer delay bit under the protection of i_data_sem. 19025356f261SAditya Kali */ 19035356f261SAditya Kali static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, 19045356f261SAditya Kali struct ext4_map_blocks *map, 19055356f261SAditya Kali struct buffer_head *bh) 19065356f261SAditya Kali { 1907d100eef2SZheng Liu struct extent_status es; 19085356f261SAditya Kali int retval; 19095356f261SAditya Kali sector_t invalid_block = ~((sector_t) 0xffff); 1910921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1911921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 1912921f266bSDmitry Monakhov 1913921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 1914921f266bSDmitry Monakhov #endif 19155356f261SAditya Kali 19165356f261SAditya Kali if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 19175356f261SAditya Kali invalid_block = ~0; 19185356f261SAditya Kali 19195356f261SAditya Kali map->m_flags = 0; 19205356f261SAditya Kali ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u," 19215356f261SAditya Kali "logical block %lu\n", inode->i_ino, map->m_len, 19225356f261SAditya Kali (unsigned long) map->m_lblk); 1923d100eef2SZheng Liu 1924d100eef2SZheng Liu /* Lookup extent status tree firstly */ 1925d100eef2SZheng Liu if (ext4_es_lookup_extent(inode, iblock, &es)) { 1926d100eef2SZheng Liu 1927d100eef2SZheng Liu if (ext4_es_is_hole(&es)) { 1928d100eef2SZheng Liu retval = 0; 1929d100eef2SZheng Liu down_read((&EXT4_I(inode)->i_data_sem)); 1930d100eef2SZheng Liu goto add_delayed; 1931d100eef2SZheng Liu } 1932d100eef2SZheng Liu 1933d100eef2SZheng Liu /* 1934d100eef2SZheng Liu * Delayed extent could be allocated by fallocate. 1935d100eef2SZheng Liu * So we need to check it. 1936d100eef2SZheng Liu */ 1937d100eef2SZheng Liu if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) { 1938d100eef2SZheng Liu map_bh(bh, inode->i_sb, invalid_block); 1939d100eef2SZheng Liu set_buffer_new(bh); 1940d100eef2SZheng Liu set_buffer_delay(bh); 1941d100eef2SZheng Liu return 0; 1942d100eef2SZheng Liu } 1943d100eef2SZheng Liu 1944d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk; 1945d100eef2SZheng Liu retval = es.es_len - (iblock - es.es_lblk); 1946d100eef2SZheng Liu if (retval > map->m_len) 1947d100eef2SZheng Liu retval = map->m_len; 1948d100eef2SZheng Liu map->m_len = retval; 1949d100eef2SZheng Liu if (ext4_es_is_written(&es)) 1950d100eef2SZheng Liu map->m_flags |= EXT4_MAP_MAPPED; 1951d100eef2SZheng Liu else if (ext4_es_is_unwritten(&es)) 1952d100eef2SZheng Liu map->m_flags |= EXT4_MAP_UNWRITTEN; 1953d100eef2SZheng Liu else 1954d100eef2SZheng Liu BUG_ON(1); 1955d100eef2SZheng Liu 1956921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1957921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0); 1958921f266bSDmitry Monakhov #endif 1959d100eef2SZheng Liu return retval; 1960d100eef2SZheng Liu } 1961d100eef2SZheng Liu 19625356f261SAditya Kali /* 19635356f261SAditya Kali * Try to see if we can get the block without requesting a new 19645356f261SAditya Kali * file system block. 19655356f261SAditya Kali */ 19665356f261SAditya Kali down_read((&EXT4_I(inode)->i_data_sem)); 19679c3569b5STao Ma if (ext4_has_inline_data(inode)) { 19689c3569b5STao Ma /* 19699c3569b5STao Ma * We will soon create blocks for this page, and let 19709c3569b5STao Ma * us pretend as if the blocks aren't allocated yet. 19719c3569b5STao Ma * In case of clusters, we have to handle the work 19729c3569b5STao Ma * of mapping from cluster so that the reserved space 19739c3569b5STao Ma * is calculated properly. 19749c3569b5STao Ma */ 19759c3569b5STao Ma if ((EXT4_SB(inode->i_sb)->s_cluster_ratio > 1) && 19769c3569b5STao Ma ext4_find_delalloc_cluster(inode, map->m_lblk)) 19779c3569b5STao Ma map->m_flags |= EXT4_MAP_FROM_CLUSTER; 19789c3569b5STao Ma retval = 0; 19799c3569b5STao Ma } else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 1980d100eef2SZheng Liu retval = ext4_ext_map_blocks(NULL, inode, map, 1981d100eef2SZheng Liu EXT4_GET_BLOCKS_NO_PUT_HOLE); 19825356f261SAditya Kali else 1983d100eef2SZheng Liu retval = ext4_ind_map_blocks(NULL, inode, map, 1984d100eef2SZheng Liu EXT4_GET_BLOCKS_NO_PUT_HOLE); 19855356f261SAditya Kali 1986d100eef2SZheng Liu add_delayed: 19875356f261SAditya Kali if (retval == 0) { 1988f7fec032SZheng Liu int ret; 19895356f261SAditya Kali /* 19905356f261SAditya Kali * XXX: __block_prepare_write() unmaps passed block, 19915356f261SAditya Kali * is it OK? 19925356f261SAditya Kali */ 1993386ad67cSLukas Czerner /* 1994386ad67cSLukas Czerner * If the block was allocated from previously allocated cluster, 1995386ad67cSLukas Czerner * then we don't need to reserve it again. However we still need 1996386ad67cSLukas Czerner * to reserve metadata for every block we're going to write. 1997386ad67cSLukas Czerner */ 19985356f261SAditya Kali if (!(map->m_flags & EXT4_MAP_FROM_CLUSTER)) { 1999f7fec032SZheng Liu ret = ext4_da_reserve_space(inode, iblock); 2000f7fec032SZheng Liu if (ret) { 20015356f261SAditya Kali /* not enough space to reserve */ 2002f7fec032SZheng Liu retval = ret; 20035356f261SAditya Kali goto out_unlock; 20045356f261SAditya Kali } 2005386ad67cSLukas Czerner } else { 2006386ad67cSLukas Czerner ret = ext4_da_reserve_metadata(inode, iblock); 2007386ad67cSLukas Czerner if (ret) { 2008386ad67cSLukas Czerner /* not enough space to reserve */ 2009386ad67cSLukas Czerner retval = ret; 2010386ad67cSLukas Czerner goto out_unlock; 2011386ad67cSLukas Czerner } 2012f7fec032SZheng Liu } 20135356f261SAditya Kali 2014f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 2015fdc0212eSZheng Liu ~0, EXTENT_STATUS_DELAYED); 2016f7fec032SZheng Liu if (ret) { 2017f7fec032SZheng Liu retval = ret; 201851865fdaSZheng Liu goto out_unlock; 2019f7fec032SZheng Liu } 202051865fdaSZheng Liu 20215356f261SAditya Kali /* Clear EXT4_MAP_FROM_CLUSTER flag since its purpose is served 20225356f261SAditya Kali * and it should not appear on the bh->b_state. 20235356f261SAditya Kali */ 20245356f261SAditya Kali map->m_flags &= ~EXT4_MAP_FROM_CLUSTER; 20255356f261SAditya Kali 20265356f261SAditya Kali map_bh(bh, inode->i_sb, invalid_block); 20275356f261SAditya Kali set_buffer_new(bh); 20285356f261SAditya Kali set_buffer_delay(bh); 2029f7fec032SZheng Liu } else if (retval > 0) { 2030f7fec032SZheng Liu int ret; 2031f7fec032SZheng Liu unsigned long long status; 2032f7fec032SZheng Liu 2033921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 2034921f266bSDmitry Monakhov if (retval != map->m_len) { 2035921f266bSDmitry Monakhov printk("ES len assertation failed for inode: %lu " 2036921f266bSDmitry Monakhov "retval %d != map->m_len %d " 2037921f266bSDmitry Monakhov "in %s (lookup)\n", inode->i_ino, retval, 2038921f266bSDmitry Monakhov map->m_len, __func__); 2039921f266bSDmitry Monakhov } 2040921f266bSDmitry Monakhov #endif 2041921f266bSDmitry Monakhov 2042f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 2043f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 2044f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 2045f7fec032SZheng Liu map->m_pblk, status); 2046f7fec032SZheng Liu if (ret != 0) 2047f7fec032SZheng Liu retval = ret; 20485356f261SAditya Kali } 20495356f261SAditya Kali 20505356f261SAditya Kali out_unlock: 20515356f261SAditya Kali up_read((&EXT4_I(inode)->i_data_sem)); 20525356f261SAditya Kali 20535356f261SAditya Kali return retval; 20545356f261SAditya Kali } 20555356f261SAditya Kali 20565356f261SAditya Kali /* 2057b920c755STheodore Ts'o * This is a special get_blocks_t callback which is used by 2058b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 2059b920c755STheodore Ts'o * reserve space for a single block. 206029fa89d0SAneesh Kumar K.V * 206129fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 206229fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 206329fa89d0SAneesh Kumar K.V * 206429fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 206529fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 206629fa89d0SAneesh Kumar K.V * initialized properly. 206764769240SAlex Tomas */ 20689c3569b5STao Ma int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 20692ed88685STheodore Ts'o struct buffer_head *bh, int create) 207064769240SAlex Tomas { 20712ed88685STheodore Ts'o struct ext4_map_blocks map; 207264769240SAlex Tomas int ret = 0; 207364769240SAlex Tomas 207464769240SAlex Tomas BUG_ON(create == 0); 20752ed88685STheodore Ts'o BUG_ON(bh->b_size != inode->i_sb->s_blocksize); 20762ed88685STheodore Ts'o 20772ed88685STheodore Ts'o map.m_lblk = iblock; 20782ed88685STheodore Ts'o map.m_len = 1; 207964769240SAlex Tomas 208064769240SAlex Tomas /* 208164769240SAlex Tomas * first, we need to know whether the block is allocated already 208264769240SAlex Tomas * preallocated blocks are unmapped but should treated 208364769240SAlex Tomas * the same as allocated blocks. 208464769240SAlex Tomas */ 20855356f261SAditya Kali ret = ext4_da_map_blocks(inode, iblock, &map, bh); 20865356f261SAditya Kali if (ret <= 0) 20872ed88685STheodore Ts'o return ret; 208864769240SAlex Tomas 20892ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 20902ed88685STheodore Ts'o bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; 20912ed88685STheodore Ts'o 20922ed88685STheodore Ts'o if (buffer_unwritten(bh)) { 20932ed88685STheodore Ts'o /* A delayed write to unwritten bh should be marked 20942ed88685STheodore Ts'o * new and mapped. Mapped ensures that we don't do 20952ed88685STheodore Ts'o * get_block multiple times when we write to the same 20962ed88685STheodore Ts'o * offset and new ensures that we do proper zero out 20972ed88685STheodore Ts'o * for partial write. 20982ed88685STheodore Ts'o */ 20992ed88685STheodore Ts'o set_buffer_new(bh); 2100c8205636STheodore Ts'o set_buffer_mapped(bh); 21012ed88685STheodore Ts'o } 21022ed88685STheodore Ts'o return 0; 210364769240SAlex Tomas } 210461628a3fSMingming Cao 210562e086beSAneesh Kumar K.V static int bget_one(handle_t *handle, struct buffer_head *bh) 210662e086beSAneesh Kumar K.V { 210762e086beSAneesh Kumar K.V get_bh(bh); 210862e086beSAneesh Kumar K.V return 0; 210962e086beSAneesh Kumar K.V } 211062e086beSAneesh Kumar K.V 211162e086beSAneesh Kumar K.V static int bput_one(handle_t *handle, struct buffer_head *bh) 211262e086beSAneesh Kumar K.V { 211362e086beSAneesh Kumar K.V put_bh(bh); 211462e086beSAneesh Kumar K.V return 0; 211562e086beSAneesh Kumar K.V } 211662e086beSAneesh Kumar K.V 211762e086beSAneesh Kumar K.V static int __ext4_journalled_writepage(struct page *page, 211862e086beSAneesh Kumar K.V unsigned int len) 211962e086beSAneesh Kumar K.V { 212062e086beSAneesh Kumar K.V struct address_space *mapping = page->mapping; 212162e086beSAneesh Kumar K.V struct inode *inode = mapping->host; 21223fdcfb66STao Ma struct buffer_head *page_bufs = NULL; 212362e086beSAneesh Kumar K.V handle_t *handle = NULL; 21243fdcfb66STao Ma int ret = 0, err = 0; 21253fdcfb66STao Ma int inline_data = ext4_has_inline_data(inode); 21263fdcfb66STao Ma struct buffer_head *inode_bh = NULL; 212762e086beSAneesh Kumar K.V 2128cb20d518STheodore Ts'o ClearPageChecked(page); 21293fdcfb66STao Ma 21303fdcfb66STao Ma if (inline_data) { 21313fdcfb66STao Ma BUG_ON(page->index != 0); 21323fdcfb66STao Ma BUG_ON(len > ext4_get_max_inline_size(inode)); 21333fdcfb66STao Ma inode_bh = ext4_journalled_write_inline_data(inode, len, page); 21343fdcfb66STao Ma if (inode_bh == NULL) 21353fdcfb66STao Ma goto out; 21363fdcfb66STao Ma } else { 213762e086beSAneesh Kumar K.V page_bufs = page_buffers(page); 21383fdcfb66STao Ma if (!page_bufs) { 21393fdcfb66STao Ma BUG(); 21403fdcfb66STao Ma goto out; 21413fdcfb66STao Ma } 21423fdcfb66STao Ma ext4_walk_page_buffers(handle, page_bufs, 0, len, 21433fdcfb66STao Ma NULL, bget_one); 21443fdcfb66STao Ma } 214562e086beSAneesh Kumar K.V /* As soon as we unlock the page, it can go away, but we have 214662e086beSAneesh Kumar K.V * references to buffers so we are safe */ 214762e086beSAneesh Kumar K.V unlock_page(page); 214862e086beSAneesh Kumar K.V 21499924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 21509924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 215162e086beSAneesh Kumar K.V if (IS_ERR(handle)) { 215262e086beSAneesh Kumar K.V ret = PTR_ERR(handle); 215362e086beSAneesh Kumar K.V goto out; 215462e086beSAneesh Kumar K.V } 215562e086beSAneesh Kumar K.V 2156441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 2157441c8508SCurt Wohlgemuth 21583fdcfb66STao Ma if (inline_data) { 21593fdcfb66STao Ma ret = ext4_journal_get_write_access(handle, inode_bh); 21603fdcfb66STao Ma 21613fdcfb66STao Ma err = ext4_handle_dirty_metadata(handle, inode, inode_bh); 21623fdcfb66STao Ma 21633fdcfb66STao Ma } else { 2164f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 216562e086beSAneesh Kumar K.V do_journal_get_write_access); 216662e086beSAneesh Kumar K.V 2167f19d5870STao Ma err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 216862e086beSAneesh Kumar K.V write_end_fn); 21693fdcfb66STao Ma } 217062e086beSAneesh Kumar K.V if (ret == 0) 217162e086beSAneesh Kumar K.V ret = err; 21722d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 217362e086beSAneesh Kumar K.V err = ext4_journal_stop(handle); 217462e086beSAneesh Kumar K.V if (!ret) 217562e086beSAneesh Kumar K.V ret = err; 217662e086beSAneesh Kumar K.V 21773fdcfb66STao Ma if (!ext4_has_inline_data(inode)) 21783fdcfb66STao Ma ext4_walk_page_buffers(handle, page_bufs, 0, len, 21793fdcfb66STao Ma NULL, bput_one); 218019f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 218162e086beSAneesh Kumar K.V out: 21823fdcfb66STao Ma brelse(inode_bh); 218362e086beSAneesh Kumar K.V return ret; 218462e086beSAneesh Kumar K.V } 218562e086beSAneesh Kumar K.V 218661628a3fSMingming Cao /* 218743ce1d23SAneesh Kumar K.V * Note that we don't need to start a transaction unless we're journaling data 218843ce1d23SAneesh Kumar K.V * because we should have holes filled from ext4_page_mkwrite(). We even don't 218943ce1d23SAneesh Kumar K.V * need to file the inode to the transaction's list in ordered mode because if 219043ce1d23SAneesh Kumar K.V * we are writing back data added by write(), the inode is already there and if 219143ce1d23SAneesh Kumar K.V * we are writing back data modified via mmap(), no one guarantees in which 219243ce1d23SAneesh Kumar K.V * transaction the data will hit the disk. In case we are journaling data, we 219343ce1d23SAneesh Kumar K.V * cannot start transaction directly because transaction start ranks above page 219443ce1d23SAneesh Kumar K.V * lock so we have to do some magic. 219543ce1d23SAneesh Kumar K.V * 2196b920c755STheodore Ts'o * This function can get called via... 2197b920c755STheodore Ts'o * - ext4_da_writepages after taking page lock (have journal handle) 2198b920c755STheodore Ts'o * - journal_submit_inode_data_buffers (no journal handle) 2199f6463b0dSArtem Bityutskiy * - shrink_page_list via the kswapd/direct reclaim (no journal handle) 2200b920c755STheodore Ts'o * - grab_page_cache when doing write_begin (have journal handle) 220143ce1d23SAneesh Kumar K.V * 220243ce1d23SAneesh Kumar K.V * We don't do any block allocation in this function. If we have page with 220343ce1d23SAneesh Kumar K.V * multiple blocks we need to write those buffer_heads that are mapped. This 220443ce1d23SAneesh Kumar K.V * is important for mmaped based write. So if we do with blocksize 1K 220543ce1d23SAneesh Kumar K.V * truncate(f, 1024); 220643ce1d23SAneesh Kumar K.V * a = mmap(f, 0, 4096); 220743ce1d23SAneesh Kumar K.V * a[0] = 'a'; 220843ce1d23SAneesh Kumar K.V * truncate(f, 4096); 220943ce1d23SAneesh Kumar K.V * we have in the page first buffer_head mapped via page_mkwrite call back 221090802ed9SPaul Bolle * but other buffer_heads would be unmapped but dirty (dirty done via the 221143ce1d23SAneesh Kumar K.V * do_wp_page). So writepage should write the first block. If we modify 221243ce1d23SAneesh Kumar K.V * the mmap area beyond 1024 we will again get a page_fault and the 221343ce1d23SAneesh Kumar K.V * page_mkwrite callback will do the block allocation and mark the 221443ce1d23SAneesh Kumar K.V * buffer_heads mapped. 221543ce1d23SAneesh Kumar K.V * 221643ce1d23SAneesh Kumar K.V * We redirty the page if we have any buffer_heads that is either delay or 221743ce1d23SAneesh Kumar K.V * unwritten in the page. 221843ce1d23SAneesh Kumar K.V * 221943ce1d23SAneesh Kumar K.V * We can get recursively called as show below. 222043ce1d23SAneesh Kumar K.V * 222143ce1d23SAneesh Kumar K.V * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> 222243ce1d23SAneesh Kumar K.V * ext4_writepage() 222343ce1d23SAneesh Kumar K.V * 222443ce1d23SAneesh Kumar K.V * But since we don't do any block allocation we should not deadlock. 222543ce1d23SAneesh Kumar K.V * Page also have the dirty flag cleared so we don't get recurive page_lock. 222661628a3fSMingming Cao */ 222743ce1d23SAneesh Kumar K.V static int ext4_writepage(struct page *page, 222864769240SAlex Tomas struct writeback_control *wbc) 222964769240SAlex Tomas { 2230f8bec370SJan Kara int ret = 0; 223161628a3fSMingming Cao loff_t size; 2232498e5f24STheodore Ts'o unsigned int len; 2233744692dcSJiaying Zhang struct buffer_head *page_bufs = NULL; 223461628a3fSMingming Cao struct inode *inode = page->mapping->host; 223536ade451SJan Kara struct ext4_io_submit io_submit; 223664769240SAlex Tomas 2237a9c667f8SLukas Czerner trace_ext4_writepage(page); 223861628a3fSMingming Cao size = i_size_read(inode); 223961628a3fSMingming Cao if (page->index == size >> PAGE_CACHE_SHIFT) 224061628a3fSMingming Cao len = size & ~PAGE_CACHE_MASK; 224161628a3fSMingming Cao else 224261628a3fSMingming Cao len = PAGE_CACHE_SIZE; 224361628a3fSMingming Cao 2244f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 2245fe386132SJan Kara /* 2246fe386132SJan Kara * We cannot do block allocation or other extent handling in this 2247fe386132SJan Kara * function. If there are buffers needing that, we have to redirty 2248fe386132SJan Kara * the page. But we may reach here when we do a journal commit via 2249fe386132SJan Kara * journal_submit_inode_data_buffers() and in that case we must write 2250fe386132SJan Kara * allocated buffers to achieve data=ordered mode guarantees. 2251fe386132SJan Kara */ 2252f19d5870STao Ma if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2253c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 2254f8bec370SJan Kara redirty_page_for_writepage(wbc, page); 2255fe386132SJan Kara if (current->flags & PF_MEMALLOC) { 2256fe386132SJan Kara /* 2257fe386132SJan Kara * For memory cleaning there's no point in writing only 2258fe386132SJan Kara * some buffers. So just bail out. Warn if we came here 2259fe386132SJan Kara * from direct reclaim. 2260fe386132SJan Kara */ 2261fe386132SJan Kara WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) 2262fe386132SJan Kara == PF_MEMALLOC); 2263f8bec370SJan Kara unlock_page(page); 2264f8bec370SJan Kara return 0; 2265f0e6c985SAneesh Kumar K.V } 2266fe386132SJan Kara } 226764769240SAlex Tomas 2268cb20d518STheodore Ts'o if (PageChecked(page) && ext4_should_journal_data(inode)) 226943ce1d23SAneesh Kumar K.V /* 227043ce1d23SAneesh Kumar K.V * It's mmapped pagecache. Add buffers and journal it. There 227143ce1d23SAneesh Kumar K.V * doesn't seem much point in redirtying the page here. 227243ce1d23SAneesh Kumar K.V */ 22733f0ca309SWu Fengguang return __ext4_journalled_writepage(page, len); 227443ce1d23SAneesh Kumar K.V 227536ade451SJan Kara memset(&io_submit, 0, sizeof(io_submit)); 227636ade451SJan Kara ret = ext4_bio_write_page(&io_submit, page, len, wbc); 227736ade451SJan Kara ext4_io_submit(&io_submit); 227864769240SAlex Tomas return ret; 227964769240SAlex Tomas } 228064769240SAlex Tomas 228161628a3fSMingming Cao /* 2282525f4ed8SMingming Cao * This is called via ext4_da_writepages() to 228325985edcSLucas De Marchi * calculate the total number of credits to reserve to fit 2284525f4ed8SMingming Cao * a single extent allocation into a single transaction, 2285525f4ed8SMingming Cao * ext4_da_writpeages() will loop calling this before 2286525f4ed8SMingming Cao * the block allocation. 228761628a3fSMingming Cao */ 2288525f4ed8SMingming Cao 2289525f4ed8SMingming Cao static int ext4_da_writepages_trans_blocks(struct inode *inode) 2290525f4ed8SMingming Cao { 2291525f4ed8SMingming Cao int max_blocks = EXT4_I(inode)->i_reserved_data_blocks; 2292525f4ed8SMingming Cao 2293525f4ed8SMingming Cao /* 2294525f4ed8SMingming Cao * With non-extent format the journal credit needed to 2295525f4ed8SMingming Cao * insert nrblocks contiguous block is dependent on 2296525f4ed8SMingming Cao * number of contiguous block. So we will limit 2297525f4ed8SMingming Cao * number of contiguous block to a sane value 2298525f4ed8SMingming Cao */ 229912e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) && 2300525f4ed8SMingming Cao (max_blocks > EXT4_MAX_TRANS_DATA)) 2301525f4ed8SMingming Cao max_blocks = EXT4_MAX_TRANS_DATA; 2302525f4ed8SMingming Cao 2303525f4ed8SMingming Cao return ext4_chunk_trans_blocks(inode, max_blocks); 2304525f4ed8SMingming Cao } 230561628a3fSMingming Cao 23068e48dcfbSTheodore Ts'o /* 23078e48dcfbSTheodore Ts'o * write_cache_pages_da - walk the list of dirty pages of the given 23088eb9e5ceSTheodore Ts'o * address space and accumulate pages that need writing, and call 2309168fc022STheodore Ts'o * mpage_da_map_and_submit to map a single contiguous memory region 2310168fc022STheodore Ts'o * and then write them. 23118e48dcfbSTheodore Ts'o */ 23129c3569b5STao Ma static int write_cache_pages_da(handle_t *handle, 23139c3569b5STao Ma struct address_space *mapping, 23148e48dcfbSTheodore Ts'o struct writeback_control *wbc, 231572f84e65SEric Sandeen struct mpage_da_data *mpd, 231672f84e65SEric Sandeen pgoff_t *done_index) 23178e48dcfbSTheodore Ts'o { 23188eb9e5ceSTheodore Ts'o struct buffer_head *bh, *head; 2319168fc022STheodore Ts'o struct inode *inode = mapping->host; 23208e48dcfbSTheodore Ts'o struct pagevec pvec; 23214f01b02cSTheodore Ts'o unsigned int nr_pages; 23224f01b02cSTheodore Ts'o sector_t logical; 23234f01b02cSTheodore Ts'o pgoff_t index, end; 23248e48dcfbSTheodore Ts'o long nr_to_write = wbc->nr_to_write; 23254f01b02cSTheodore Ts'o int i, tag, ret = 0; 23268e48dcfbSTheodore Ts'o 2327168fc022STheodore Ts'o memset(mpd, 0, sizeof(struct mpage_da_data)); 2328168fc022STheodore Ts'o mpd->wbc = wbc; 2329168fc022STheodore Ts'o mpd->inode = inode; 23308e48dcfbSTheodore Ts'o pagevec_init(&pvec, 0); 23318e48dcfbSTheodore Ts'o index = wbc->range_start >> PAGE_CACHE_SHIFT; 23328e48dcfbSTheodore Ts'o end = wbc->range_end >> PAGE_CACHE_SHIFT; 23338e48dcfbSTheodore Ts'o 23346e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 23355b41d924SEric Sandeen tag = PAGECACHE_TAG_TOWRITE; 23365b41d924SEric Sandeen else 23375b41d924SEric Sandeen tag = PAGECACHE_TAG_DIRTY; 23385b41d924SEric Sandeen 233972f84e65SEric Sandeen *done_index = index; 23404f01b02cSTheodore Ts'o while (index <= end) { 23415b41d924SEric Sandeen nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, 23428e48dcfbSTheodore Ts'o min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); 23438e48dcfbSTheodore Ts'o if (nr_pages == 0) 23444f01b02cSTheodore Ts'o return 0; 23458e48dcfbSTheodore Ts'o 23468e48dcfbSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 23478e48dcfbSTheodore Ts'o struct page *page = pvec.pages[i]; 23488e48dcfbSTheodore Ts'o 23498e48dcfbSTheodore Ts'o /* 23508e48dcfbSTheodore Ts'o * At this point, the page may be truncated or 23518e48dcfbSTheodore Ts'o * invalidated (changing page->mapping to NULL), or 23528e48dcfbSTheodore Ts'o * even swizzled back from swapper_space to tmpfs file 23538e48dcfbSTheodore Ts'o * mapping. However, page->index will not change 23548e48dcfbSTheodore Ts'o * because we have a reference on the page. 23558e48dcfbSTheodore Ts'o */ 23564f01b02cSTheodore Ts'o if (page->index > end) 23574f01b02cSTheodore Ts'o goto out; 23588e48dcfbSTheodore Ts'o 235972f84e65SEric Sandeen *done_index = page->index + 1; 236072f84e65SEric Sandeen 236178aaced3STheodore Ts'o /* 236278aaced3STheodore Ts'o * If we can't merge this page, and we have 236378aaced3STheodore Ts'o * accumulated an contiguous region, write it 236478aaced3STheodore Ts'o */ 236578aaced3STheodore Ts'o if ((mpd->next_page != page->index) && 236678aaced3STheodore Ts'o (mpd->next_page != mpd->first_page)) { 236778aaced3STheodore Ts'o mpage_da_map_and_submit(mpd); 236878aaced3STheodore Ts'o goto ret_extent_tail; 236978aaced3STheodore Ts'o } 237078aaced3STheodore Ts'o 23718e48dcfbSTheodore Ts'o lock_page(page); 23728e48dcfbSTheodore Ts'o 23738e48dcfbSTheodore Ts'o /* 23744f01b02cSTheodore Ts'o * If the page is no longer dirty, or its 23754f01b02cSTheodore Ts'o * mapping no longer corresponds to inode we 23764f01b02cSTheodore Ts'o * are writing (which means it has been 23774f01b02cSTheodore Ts'o * truncated or invalidated), or the page is 23784f01b02cSTheodore Ts'o * already under writeback and we are not 23794f01b02cSTheodore Ts'o * doing a data integrity writeback, skip the page 23808e48dcfbSTheodore Ts'o */ 23814f01b02cSTheodore Ts'o if (!PageDirty(page) || 23824f01b02cSTheodore Ts'o (PageWriteback(page) && 23834f01b02cSTheodore Ts'o (wbc->sync_mode == WB_SYNC_NONE)) || 23844f01b02cSTheodore Ts'o unlikely(page->mapping != mapping)) { 23858e48dcfbSTheodore Ts'o unlock_page(page); 23868e48dcfbSTheodore Ts'o continue; 23878e48dcfbSTheodore Ts'o } 23888e48dcfbSTheodore Ts'o 23898e48dcfbSTheodore Ts'o wait_on_page_writeback(page); 23908e48dcfbSTheodore Ts'o BUG_ON(PageWriteback(page)); 23918e48dcfbSTheodore Ts'o 23929c3569b5STao Ma /* 23939c3569b5STao Ma * If we have inline data and arrive here, it means that 23949c3569b5STao Ma * we will soon create the block for the 1st page, so 23959c3569b5STao Ma * we'd better clear the inline data here. 23969c3569b5STao Ma */ 23979c3569b5STao Ma if (ext4_has_inline_data(inode)) { 23989c3569b5STao Ma BUG_ON(ext4_test_inode_state(inode, 23999c3569b5STao Ma EXT4_STATE_MAY_INLINE_DATA)); 24009c3569b5STao Ma ext4_destroy_inline_data(handle, inode); 24019c3569b5STao Ma } 24029c3569b5STao Ma 2403168fc022STheodore Ts'o if (mpd->next_page != page->index) 24048eb9e5ceSTheodore Ts'o mpd->first_page = page->index; 24058eb9e5ceSTheodore Ts'o mpd->next_page = page->index + 1; 24068eb9e5ceSTheodore Ts'o logical = (sector_t) page->index << 24078eb9e5ceSTheodore Ts'o (PAGE_CACHE_SHIFT - inode->i_blkbits); 24088eb9e5ceSTheodore Ts'o 2409f8bec370SJan Kara /* Add all dirty buffers to mpd */ 24108eb9e5ceSTheodore Ts'o head = page_buffers(page); 24118eb9e5ceSTheodore Ts'o bh = head; 24128eb9e5ceSTheodore Ts'o do { 24138eb9e5ceSTheodore Ts'o BUG_ON(buffer_locked(bh)); 24148eb9e5ceSTheodore Ts'o /* 2415f8bec370SJan Kara * We need to try to allocate unmapped blocks 2416f8bec370SJan Kara * in the same page. Otherwise we won't make 2417f8bec370SJan Kara * progress with the page in ext4_writepage 24188eb9e5ceSTheodore Ts'o */ 24198eb9e5ceSTheodore Ts'o if (ext4_bh_delay_or_unwritten(NULL, bh)) { 24208eb9e5ceSTheodore Ts'o mpage_add_bh_to_extent(mpd, logical, 24218eb9e5ceSTheodore Ts'o bh->b_state); 24224f01b02cSTheodore Ts'o if (mpd->io_done) 24234f01b02cSTheodore Ts'o goto ret_extent_tail; 2424f8bec370SJan Kara } else if (buffer_dirty(bh) && 2425f8bec370SJan Kara buffer_mapped(bh)) { 24268eb9e5ceSTheodore Ts'o /* 2427f8bec370SJan Kara * mapped dirty buffer. We need to 2428f8bec370SJan Kara * update the b_state because we look 2429f8bec370SJan Kara * at b_state in mpage_da_map_blocks. 2430f8bec370SJan Kara * We don't update b_size because if we 2431f8bec370SJan Kara * find an unmapped buffer_head later 2432f8bec370SJan Kara * we need to use the b_state flag of 2433f8bec370SJan Kara * that buffer_head. 24348eb9e5ceSTheodore Ts'o */ 24358eb9e5ceSTheodore Ts'o if (mpd->b_size == 0) 2436f8bec370SJan Kara mpd->b_state = 2437f8bec370SJan Kara bh->b_state & BH_FLAGS; 24388e48dcfbSTheodore Ts'o } 24398eb9e5ceSTheodore Ts'o logical++; 24408eb9e5ceSTheodore Ts'o } while ((bh = bh->b_this_page) != head); 24418e48dcfbSTheodore Ts'o 24428e48dcfbSTheodore Ts'o if (nr_to_write > 0) { 24438e48dcfbSTheodore Ts'o nr_to_write--; 24448e48dcfbSTheodore Ts'o if (nr_to_write == 0 && 24454f01b02cSTheodore Ts'o wbc->sync_mode == WB_SYNC_NONE) 24468e48dcfbSTheodore Ts'o /* 24478e48dcfbSTheodore Ts'o * We stop writing back only if we are 24488e48dcfbSTheodore Ts'o * not doing integrity sync. In case of 24498e48dcfbSTheodore Ts'o * integrity sync we have to keep going 24508e48dcfbSTheodore Ts'o * because someone may be concurrently 24518e48dcfbSTheodore Ts'o * dirtying pages, and we might have 24528e48dcfbSTheodore Ts'o * synced a lot of newly appeared dirty 24538e48dcfbSTheodore Ts'o * pages, but have not synced all of the 24548e48dcfbSTheodore Ts'o * old dirty pages. 24558e48dcfbSTheodore Ts'o */ 24564f01b02cSTheodore Ts'o goto out; 24578e48dcfbSTheodore Ts'o } 24588e48dcfbSTheodore Ts'o } 24598e48dcfbSTheodore Ts'o pagevec_release(&pvec); 24608e48dcfbSTheodore Ts'o cond_resched(); 24618e48dcfbSTheodore Ts'o } 24624f01b02cSTheodore Ts'o return 0; 24634f01b02cSTheodore Ts'o ret_extent_tail: 24644f01b02cSTheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 24658eb9e5ceSTheodore Ts'o out: 24668eb9e5ceSTheodore Ts'o pagevec_release(&pvec); 24678eb9e5ceSTheodore Ts'o cond_resched(); 24688e48dcfbSTheodore Ts'o return ret; 24698e48dcfbSTheodore Ts'o } 24708e48dcfbSTheodore Ts'o 24718e48dcfbSTheodore Ts'o 247264769240SAlex Tomas static int ext4_da_writepages(struct address_space *mapping, 247364769240SAlex Tomas struct writeback_control *wbc) 247464769240SAlex Tomas { 247522208dedSAneesh Kumar K.V pgoff_t index; 247622208dedSAneesh Kumar K.V int range_whole = 0; 247761628a3fSMingming Cao handle_t *handle = NULL; 2478df22291fSAneesh Kumar K.V struct mpage_da_data mpd; 24795e745b04SAneesh Kumar K.V struct inode *inode = mapping->host; 2480498e5f24STheodore Ts'o int pages_written = 0; 248155138e0bSTheodore Ts'o unsigned int max_pages; 24822acf2c26SAneesh Kumar K.V int range_cyclic, cycled = 1, io_done = 0; 248355138e0bSTheodore Ts'o int needed_blocks, ret = 0; 248455138e0bSTheodore Ts'o long desired_nr_to_write, nr_to_writebump = 0; 2485de89de6eSTheodore Ts'o loff_t range_start = wbc->range_start; 24865e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 248772f84e65SEric Sandeen pgoff_t done_index = 0; 24885b41d924SEric Sandeen pgoff_t end; 24891bce63d1SShaohua Li struct blk_plug plug; 249061628a3fSMingming Cao 24919bffad1eSTheodore Ts'o trace_ext4_da_writepages(inode, wbc); 2492ba80b101STheodore Ts'o 249361628a3fSMingming Cao /* 249461628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 249561628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 249661628a3fSMingming Cao * because that could violate lock ordering on umount 249761628a3fSMingming Cao */ 2498a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 249961628a3fSMingming Cao return 0; 25002a21e37eSTheodore Ts'o 25012a21e37eSTheodore Ts'o /* 25022a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 25032a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 25042a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 25054ab2f15bSTheodore Ts'o * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because 25062a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 25072a21e37eSTheodore Ts'o * read-only, and in that case, ext4_da_writepages should 25082a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 25092a21e37eSTheodore Ts'o * the stack trace. 25102a21e37eSTheodore Ts'o */ 25114ab2f15bSTheodore Ts'o if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) 25122a21e37eSTheodore Ts'o return -EROFS; 25132a21e37eSTheodore Ts'o 251422208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 251522208dedSAneesh Kumar K.V range_whole = 1; 251661628a3fSMingming Cao 25172acf2c26SAneesh Kumar K.V range_cyclic = wbc->range_cyclic; 25182acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 251922208dedSAneesh Kumar K.V index = mapping->writeback_index; 25202acf2c26SAneesh Kumar K.V if (index) 25212acf2c26SAneesh Kumar K.V cycled = 0; 25222acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 25232acf2c26SAneesh Kumar K.V wbc->range_end = LLONG_MAX; 25242acf2c26SAneesh Kumar K.V wbc->range_cyclic = 0; 25255b41d924SEric Sandeen end = -1; 25265b41d924SEric Sandeen } else { 252722208dedSAneesh Kumar K.V index = wbc->range_start >> PAGE_CACHE_SHIFT; 25285b41d924SEric Sandeen end = wbc->range_end >> PAGE_CACHE_SHIFT; 25295b41d924SEric Sandeen } 2530a1d6cc56SAneesh Kumar K.V 253155138e0bSTheodore Ts'o /* 253255138e0bSTheodore Ts'o * This works around two forms of stupidity. The first is in 253355138e0bSTheodore Ts'o * the writeback code, which caps the maximum number of pages 253455138e0bSTheodore Ts'o * written to be 1024 pages. This is wrong on multiple 253555138e0bSTheodore Ts'o * levels; different architectues have a different page size, 253655138e0bSTheodore Ts'o * which changes the maximum amount of data which gets 253755138e0bSTheodore Ts'o * written. Secondly, 4 megabytes is way too small. XFS 253855138e0bSTheodore Ts'o * forces this value to be 16 megabytes by multiplying 253955138e0bSTheodore Ts'o * nr_to_write parameter by four, and then relies on its 254055138e0bSTheodore Ts'o * allocator to allocate larger extents to make them 254155138e0bSTheodore Ts'o * contiguous. Unfortunately this brings us to the second 254255138e0bSTheodore Ts'o * stupidity, which is that ext4's mballoc code only allocates 254355138e0bSTheodore Ts'o * at most 2048 blocks. So we force contiguous writes up to 254455138e0bSTheodore Ts'o * the number of dirty blocks in the inode, or 254555138e0bSTheodore Ts'o * sbi->max_writeback_mb_bump whichever is smaller. 254655138e0bSTheodore Ts'o */ 254755138e0bSTheodore Ts'o max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT); 2548b443e733SEric Sandeen if (!range_cyclic && range_whole) { 2549b443e733SEric Sandeen if (wbc->nr_to_write == LONG_MAX) 2550b443e733SEric Sandeen desired_nr_to_write = wbc->nr_to_write; 255155138e0bSTheodore Ts'o else 2552b443e733SEric Sandeen desired_nr_to_write = wbc->nr_to_write * 8; 2553b443e733SEric Sandeen } else 255455138e0bSTheodore Ts'o desired_nr_to_write = ext4_num_dirty_pages(inode, index, 255555138e0bSTheodore Ts'o max_pages); 255655138e0bSTheodore Ts'o if (desired_nr_to_write > max_pages) 255755138e0bSTheodore Ts'o desired_nr_to_write = max_pages; 255855138e0bSTheodore Ts'o 255955138e0bSTheodore Ts'o if (wbc->nr_to_write < desired_nr_to_write) { 256055138e0bSTheodore Ts'o nr_to_writebump = desired_nr_to_write - wbc->nr_to_write; 256155138e0bSTheodore Ts'o wbc->nr_to_write = desired_nr_to_write; 256255138e0bSTheodore Ts'o } 256355138e0bSTheodore Ts'o 25642acf2c26SAneesh Kumar K.V retry: 25656e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 25665b41d924SEric Sandeen tag_pages_for_writeback(mapping, index, end); 25675b41d924SEric Sandeen 25681bce63d1SShaohua Li blk_start_plug(&plug); 256922208dedSAneesh Kumar K.V while (!ret && wbc->nr_to_write > 0) { 2570a1d6cc56SAneesh Kumar K.V 2571a1d6cc56SAneesh Kumar K.V /* 2572a1d6cc56SAneesh Kumar K.V * we insert one extent at a time. So we need 2573a1d6cc56SAneesh Kumar K.V * credit needed for single extent allocation. 2574a1d6cc56SAneesh Kumar K.V * journalled mode is currently not supported 2575a1d6cc56SAneesh Kumar K.V * by delalloc 2576a1d6cc56SAneesh Kumar K.V */ 2577a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 2578525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 2579a1d6cc56SAneesh Kumar K.V 258061628a3fSMingming Cao /* start a new transaction*/ 25819924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 25829924a92aSTheodore Ts'o needed_blocks); 258361628a3fSMingming Cao if (IS_ERR(handle)) { 258461628a3fSMingming Cao ret = PTR_ERR(handle); 25851693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2586fbe845ddSCurt Wohlgemuth "%ld pages, ino %lu; err %d", __func__, 2587a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 25883c1fcb2cSNamjae Jeon blk_finish_plug(&plug); 258961628a3fSMingming Cao goto out_writepages; 259061628a3fSMingming Cao } 2591f63e6005STheodore Ts'o 2592f63e6005STheodore Ts'o /* 25938eb9e5ceSTheodore Ts'o * Now call write_cache_pages_da() to find the next 2594f63e6005STheodore Ts'o * contiguous region of logical blocks that need 25958eb9e5ceSTheodore Ts'o * blocks to be allocated by ext4 and submit them. 2596f63e6005STheodore Ts'o */ 25979c3569b5STao Ma ret = write_cache_pages_da(handle, mapping, 25989c3569b5STao Ma wbc, &mpd, &done_index); 2599f63e6005STheodore Ts'o /* 2600af901ca1SAndré Goddard Rosa * If we have a contiguous extent of pages and we 2601f63e6005STheodore Ts'o * haven't done the I/O yet, map the blocks and submit 2602f63e6005STheodore Ts'o * them for I/O. 2603f63e6005STheodore Ts'o */ 2604f63e6005STheodore Ts'o if (!mpd.io_done && mpd.next_page != mpd.first_page) { 26055a87b7a5STheodore Ts'o mpage_da_map_and_submit(&mpd); 2606f63e6005STheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 2607f63e6005STheodore Ts'o } 2608b3a3ca8cSTheodore Ts'o trace_ext4_da_write_pages(inode, &mpd); 2609f63e6005STheodore Ts'o wbc->nr_to_write -= mpd.pages_written; 2610df22291fSAneesh Kumar K.V 261161628a3fSMingming Cao ext4_journal_stop(handle); 2612df22291fSAneesh Kumar K.V 26138f64b32eSEric Sandeen if ((mpd.retval == -ENOSPC) && sbi->s_journal) { 261422208dedSAneesh Kumar K.V /* commit the transaction which would 261522208dedSAneesh Kumar K.V * free blocks released in the transaction 261622208dedSAneesh Kumar K.V * and try again 261722208dedSAneesh Kumar K.V */ 2618df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 261922208dedSAneesh Kumar K.V ret = 0; 262022208dedSAneesh Kumar K.V } else if (ret == MPAGE_DA_EXTENT_TAIL) { 2621a1d6cc56SAneesh Kumar K.V /* 26228de49e67SKazuya Mio * Got one extent now try with rest of the pages. 26238de49e67SKazuya Mio * If mpd.retval is set -EIO, journal is aborted. 26248de49e67SKazuya Mio * So we don't need to write any more. 2625a1d6cc56SAneesh Kumar K.V */ 262622208dedSAneesh Kumar K.V pages_written += mpd.pages_written; 26278de49e67SKazuya Mio ret = mpd.retval; 26282acf2c26SAneesh Kumar K.V io_done = 1; 262922208dedSAneesh Kumar K.V } else if (wbc->nr_to_write) 263061628a3fSMingming Cao /* 263161628a3fSMingming Cao * There is no more writeout needed 263261628a3fSMingming Cao * or we requested for a noblocking writeout 263361628a3fSMingming Cao * and we found the device congested 263461628a3fSMingming Cao */ 263561628a3fSMingming Cao break; 263661628a3fSMingming Cao } 26371bce63d1SShaohua Li blk_finish_plug(&plug); 26382acf2c26SAneesh Kumar K.V if (!io_done && !cycled) { 26392acf2c26SAneesh Kumar K.V cycled = 1; 26402acf2c26SAneesh Kumar K.V index = 0; 26412acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 26422acf2c26SAneesh Kumar K.V wbc->range_end = mapping->writeback_index - 1; 26432acf2c26SAneesh Kumar K.V goto retry; 26442acf2c26SAneesh Kumar K.V } 264561628a3fSMingming Cao 264622208dedSAneesh Kumar K.V /* Update index */ 26472acf2c26SAneesh Kumar K.V wbc->range_cyclic = range_cyclic; 264822208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 264922208dedSAneesh Kumar K.V /* 265022208dedSAneesh Kumar K.V * set the writeback_index so that range_cyclic 265122208dedSAneesh Kumar K.V * mode will write it back later 265222208dedSAneesh Kumar K.V */ 265372f84e65SEric Sandeen mapping->writeback_index = done_index; 2654a1d6cc56SAneesh Kumar K.V 265561628a3fSMingming Cao out_writepages: 265622208dedSAneesh Kumar K.V wbc->nr_to_write -= nr_to_writebump; 2657de89de6eSTheodore Ts'o wbc->range_start = range_start; 26589bffad1eSTheodore Ts'o trace_ext4_da_writepages_result(inode, wbc, ret, pages_written); 265961628a3fSMingming Cao return ret; 266064769240SAlex Tomas } 266164769240SAlex Tomas 266279f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 266379f0be8dSAneesh Kumar K.V { 266479f0be8dSAneesh Kumar K.V s64 free_blocks, dirty_blocks; 266579f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 266679f0be8dSAneesh Kumar K.V 266779f0be8dSAneesh Kumar K.V /* 266879f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 266979f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 2670179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 267179f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 267279f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 267379f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 267479f0be8dSAneesh Kumar K.V */ 267557042651STheodore Ts'o free_blocks = EXT4_C2B(sbi, 267657042651STheodore Ts'o percpu_counter_read_positive(&sbi->s_freeclusters_counter)); 267757042651STheodore Ts'o dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyclusters_counter); 267800d4e736STheodore Ts'o /* 267900d4e736STheodore Ts'o * Start pushing delalloc when 1/2 of free blocks are dirty. 268000d4e736STheodore Ts'o */ 268100d4e736STheodore Ts'o if (dirty_blocks && (free_blocks < 2 * dirty_blocks) && 268200d4e736STheodore Ts'o !writeback_in_progress(sb->s_bdi) && 268300d4e736STheodore Ts'o down_read_trylock(&sb->s_umount)) { 268400d4e736STheodore Ts'o writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE); 268500d4e736STheodore Ts'o up_read(&sb->s_umount); 268600d4e736STheodore Ts'o } 268700d4e736STheodore Ts'o 268879f0be8dSAneesh Kumar K.V if (2 * free_blocks < 3 * dirty_blocks || 2689df55c99dSTheodore Ts'o free_blocks < (dirty_blocks + EXT4_FREECLUSTERS_WATERMARK)) { 269079f0be8dSAneesh Kumar K.V /* 2691c8afb446SEric Sandeen * free block count is less than 150% of dirty blocks 2692c8afb446SEric Sandeen * or free blocks is less than watermark 269379f0be8dSAneesh Kumar K.V */ 269479f0be8dSAneesh Kumar K.V return 1; 269579f0be8dSAneesh Kumar K.V } 269679f0be8dSAneesh Kumar K.V return 0; 269779f0be8dSAneesh Kumar K.V } 269879f0be8dSAneesh Kumar K.V 269964769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 270064769240SAlex Tomas loff_t pos, unsigned len, unsigned flags, 270164769240SAlex Tomas struct page **pagep, void **fsdata) 270264769240SAlex Tomas { 270372b8ab9dSEric Sandeen int ret, retries = 0; 270464769240SAlex Tomas struct page *page; 270564769240SAlex Tomas pgoff_t index; 270664769240SAlex Tomas struct inode *inode = mapping->host; 270764769240SAlex Tomas handle_t *handle; 270864769240SAlex Tomas 270964769240SAlex Tomas index = pos >> PAGE_CACHE_SHIFT; 271079f0be8dSAneesh Kumar K.V 271179f0be8dSAneesh Kumar K.V if (ext4_nonda_switch(inode->i_sb)) { 271279f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 271379f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 271479f0be8dSAneesh Kumar K.V len, flags, pagep, fsdata); 271579f0be8dSAneesh Kumar K.V } 271679f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 27179bffad1eSTheodore Ts'o trace_ext4_da_write_begin(inode, pos, len, flags); 27189c3569b5STao Ma 27199c3569b5STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 27209c3569b5STao Ma ret = ext4_da_write_inline_data_begin(mapping, inode, 27219c3569b5STao Ma pos, len, flags, 27229c3569b5STao Ma pagep, fsdata); 27239c3569b5STao Ma if (ret < 0) 272447564bfbSTheodore Ts'o return ret; 272547564bfbSTheodore Ts'o if (ret == 1) 272647564bfbSTheodore Ts'o return 0; 27279c3569b5STao Ma } 27289c3569b5STao Ma 272947564bfbSTheodore Ts'o /* 273047564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 273147564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 273247564bfbSTheodore Ts'o * is being written back. So grab it first before we start 273347564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 273447564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 273547564bfbSTheodore Ts'o */ 273647564bfbSTheodore Ts'o retry_grab: 273747564bfbSTheodore Ts'o page = grab_cache_page_write_begin(mapping, index, flags); 273847564bfbSTheodore Ts'o if (!page) 273947564bfbSTheodore Ts'o return -ENOMEM; 274047564bfbSTheodore Ts'o unlock_page(page); 274147564bfbSTheodore Ts'o 274264769240SAlex Tomas /* 274364769240SAlex Tomas * With delayed allocation, we don't log the i_disksize update 274464769240SAlex Tomas * if there is delayed block allocation. But we still need 274564769240SAlex Tomas * to journalling the i_disksize update if writes to the end 274664769240SAlex Tomas * of file which has an already mapped buffer. 274764769240SAlex Tomas */ 274847564bfbSTheodore Ts'o retry_journal: 27499924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 1); 275064769240SAlex Tomas if (IS_ERR(handle)) { 275147564bfbSTheodore Ts'o page_cache_release(page); 275247564bfbSTheodore Ts'o return PTR_ERR(handle); 275364769240SAlex Tomas } 275464769240SAlex Tomas 275547564bfbSTheodore Ts'o lock_page(page); 275647564bfbSTheodore Ts'o if (page->mapping != mapping) { 275747564bfbSTheodore Ts'o /* The page got truncated from under us */ 275847564bfbSTheodore Ts'o unlock_page(page); 275947564bfbSTheodore Ts'o page_cache_release(page); 2760d5a0d4f7SEric Sandeen ext4_journal_stop(handle); 276147564bfbSTheodore Ts'o goto retry_grab; 2762d5a0d4f7SEric Sandeen } 276347564bfbSTheodore Ts'o /* In case writeback began while the page was unlocked */ 276447564bfbSTheodore Ts'o wait_on_page_writeback(page); 276564769240SAlex Tomas 27666e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep); 276764769240SAlex Tomas if (ret < 0) { 276864769240SAlex Tomas unlock_page(page); 276964769240SAlex Tomas ext4_journal_stop(handle); 2770ae4d5372SAneesh Kumar K.V /* 2771ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 2772ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 2773ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 2774ae4d5372SAneesh Kumar K.V */ 2775ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 2776b9a4207dSJan Kara ext4_truncate_failed_write(inode); 277747564bfbSTheodore Ts'o 277847564bfbSTheodore Ts'o if (ret == -ENOSPC && 277947564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 278047564bfbSTheodore Ts'o goto retry_journal; 278147564bfbSTheodore Ts'o 278247564bfbSTheodore Ts'o page_cache_release(page); 278347564bfbSTheodore Ts'o return ret; 278464769240SAlex Tomas } 278564769240SAlex Tomas 278647564bfbSTheodore Ts'o *pagep = page; 278764769240SAlex Tomas return ret; 278864769240SAlex Tomas } 278964769240SAlex Tomas 2790632eaeabSMingming Cao /* 2791632eaeabSMingming Cao * Check if we should update i_disksize 2792632eaeabSMingming Cao * when write to the end of file but not require block allocation 2793632eaeabSMingming Cao */ 2794632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page, 2795632eaeabSMingming Cao unsigned long offset) 2796632eaeabSMingming Cao { 2797632eaeabSMingming Cao struct buffer_head *bh; 2798632eaeabSMingming Cao struct inode *inode = page->mapping->host; 2799632eaeabSMingming Cao unsigned int idx; 2800632eaeabSMingming Cao int i; 2801632eaeabSMingming Cao 2802632eaeabSMingming Cao bh = page_buffers(page); 2803632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 2804632eaeabSMingming Cao 2805632eaeabSMingming Cao for (i = 0; i < idx; i++) 2806632eaeabSMingming Cao bh = bh->b_this_page; 2807632eaeabSMingming Cao 280829fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 2809632eaeabSMingming Cao return 0; 2810632eaeabSMingming Cao return 1; 2811632eaeabSMingming Cao } 2812632eaeabSMingming Cao 281364769240SAlex Tomas static int ext4_da_write_end(struct file *file, 281464769240SAlex Tomas struct address_space *mapping, 281564769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 281664769240SAlex Tomas struct page *page, void *fsdata) 281764769240SAlex Tomas { 281864769240SAlex Tomas struct inode *inode = mapping->host; 281964769240SAlex Tomas int ret = 0, ret2; 282064769240SAlex Tomas handle_t *handle = ext4_journal_current_handle(); 282164769240SAlex Tomas loff_t new_i_size; 2822632eaeabSMingming Cao unsigned long start, end; 282379f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 282479f0be8dSAneesh Kumar K.V 282579f0be8dSAneesh Kumar K.V if (write_mode == FALL_BACK_TO_NONDELALLOC) { 28263d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 28273d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 282879f0be8dSAneesh Kumar K.V return ext4_ordered_write_end(file, mapping, pos, 282979f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 28303d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 283179f0be8dSAneesh Kumar K.V return ext4_writeback_write_end(file, mapping, pos, 283279f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 28333d2b1582SLukas Czerner default: 283479f0be8dSAneesh Kumar K.V BUG(); 283579f0be8dSAneesh Kumar K.V } 283679f0be8dSAneesh Kumar K.V } 2837632eaeabSMingming Cao 28389bffad1eSTheodore Ts'o trace_ext4_da_write_end(inode, pos, len, copied); 2839632eaeabSMingming Cao start = pos & (PAGE_CACHE_SIZE - 1); 2840632eaeabSMingming Cao end = start + copied - 1; 284164769240SAlex Tomas 284264769240SAlex Tomas /* 284364769240SAlex Tomas * generic_write_end() will run mark_inode_dirty() if i_size 284464769240SAlex Tomas * changes. So let's piggyback the i_disksize mark_inode_dirty 284564769240SAlex Tomas * into that. 284664769240SAlex Tomas */ 284764769240SAlex Tomas new_i_size = pos + copied; 2848ea51d132SAndrea Arcangeli if (copied && new_i_size > EXT4_I(inode)->i_disksize) { 28499c3569b5STao Ma if (ext4_has_inline_data(inode) || 28509c3569b5STao Ma ext4_da_should_update_i_disksize(page, end)) { 2851632eaeabSMingming Cao down_write(&EXT4_I(inode)->i_data_sem); 2852f3b59291STheodore Ts'o if (new_i_size > EXT4_I(inode)->i_disksize) 285364769240SAlex Tomas EXT4_I(inode)->i_disksize = new_i_size; 2854632eaeabSMingming Cao up_write(&EXT4_I(inode)->i_data_sem); 2855cf17fea6SAneesh Kumar K.V /* We need to mark inode dirty even if 2856cf17fea6SAneesh Kumar K.V * new_i_size is less that inode->i_size 2857cf17fea6SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 2858cf17fea6SAneesh Kumar K.V */ 2859cf17fea6SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 2860632eaeabSMingming Cao } 2861632eaeabSMingming Cao } 28629c3569b5STao Ma 28639c3569b5STao Ma if (write_mode != CONVERT_INLINE_DATA && 28649c3569b5STao Ma ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) && 28659c3569b5STao Ma ext4_has_inline_data(inode)) 28669c3569b5STao Ma ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied, 28679c3569b5STao Ma page); 28689c3569b5STao Ma else 286964769240SAlex Tomas ret2 = generic_write_end(file, mapping, pos, len, copied, 287064769240SAlex Tomas page, fsdata); 28719c3569b5STao Ma 287264769240SAlex Tomas copied = ret2; 287364769240SAlex Tomas if (ret2 < 0) 287464769240SAlex Tomas ret = ret2; 287564769240SAlex Tomas ret2 = ext4_journal_stop(handle); 287664769240SAlex Tomas if (!ret) 287764769240SAlex Tomas ret = ret2; 287864769240SAlex Tomas 287964769240SAlex Tomas return ret ? ret : copied; 288064769240SAlex Tomas } 288164769240SAlex Tomas 288264769240SAlex Tomas static void ext4_da_invalidatepage(struct page *page, unsigned long offset) 288364769240SAlex Tomas { 288464769240SAlex Tomas /* 288564769240SAlex Tomas * Drop reserved blocks 288664769240SAlex Tomas */ 288764769240SAlex Tomas BUG_ON(!PageLocked(page)); 288864769240SAlex Tomas if (!page_has_buffers(page)) 288964769240SAlex Tomas goto out; 289064769240SAlex Tomas 2891d2a17637SMingming Cao ext4_da_page_release_reservation(page, offset); 289264769240SAlex Tomas 289364769240SAlex Tomas out: 289464769240SAlex Tomas ext4_invalidatepage(page, offset); 289564769240SAlex Tomas 289664769240SAlex Tomas return; 289764769240SAlex Tomas } 289864769240SAlex Tomas 2899ccd2506bSTheodore Ts'o /* 2900ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 2901ccd2506bSTheodore Ts'o */ 2902ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 2903ccd2506bSTheodore Ts'o { 2904fb40ba0dSTheodore Ts'o trace_ext4_alloc_da_blocks(inode); 2905fb40ba0dSTheodore Ts'o 2906ccd2506bSTheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks && 2907ccd2506bSTheodore Ts'o !EXT4_I(inode)->i_reserved_meta_blocks) 2908ccd2506bSTheodore Ts'o return 0; 2909ccd2506bSTheodore Ts'o 2910ccd2506bSTheodore Ts'o /* 2911ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 2912ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 2913ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 2914ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 2915ccd2506bSTheodore Ts'o * would require replicating code paths in: 2916ccd2506bSTheodore Ts'o * 2917ccd2506bSTheodore Ts'o * ext4_da_writepages() -> 2918ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 2919ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 2920ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 2921ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 2922ccd2506bSTheodore Ts'o * 2923ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 2924ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 2925ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 2926ccd2506bSTheodore Ts'o * doing I/O at all. 2927ccd2506bSTheodore Ts'o * 2928ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 2929380cf090SWu Fengguang * the pages by calling redirty_page_for_writepage() but that 2930ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 2931ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 293225985edcSLucas De Marchi * simplifying them because we wouldn't actually intend to 2933ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 2934ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 2935ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 2936ccd2506bSTheodore Ts'o * 2937ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 2938ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 2939ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 2940ccd2506bSTheodore Ts'o */ 2941ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 2942ccd2506bSTheodore Ts'o } 294364769240SAlex Tomas 294464769240SAlex Tomas /* 2945ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 2946ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 2947ac27a0ecSDave Kleikamp * 2948ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 2949617ba13bSMingming Cao * journal. If somebody makes a swapfile on an ext4 data-journaling 2950ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 2951ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 2952ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 2953ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 2954ac27a0ecSDave Kleikamp * 2955ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 2956ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 2957ac27a0ecSDave Kleikamp */ 2958617ba13bSMingming Cao static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 2959ac27a0ecSDave Kleikamp { 2960ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 2961ac27a0ecSDave Kleikamp journal_t *journal; 2962ac27a0ecSDave Kleikamp int err; 2963ac27a0ecSDave Kleikamp 296446c7f254STao Ma /* 296546c7f254STao Ma * We can get here for an inline file via the FIBMAP ioctl 296646c7f254STao Ma */ 296746c7f254STao Ma if (ext4_has_inline_data(inode)) 296846c7f254STao Ma return 0; 296946c7f254STao Ma 297064769240SAlex Tomas if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 297164769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) { 297264769240SAlex Tomas /* 297364769240SAlex Tomas * With delalloc we want to sync the file 297464769240SAlex Tomas * so that we can make sure we allocate 297564769240SAlex Tomas * blocks for file 297664769240SAlex Tomas */ 297764769240SAlex Tomas filemap_write_and_wait(mapping); 297864769240SAlex Tomas } 297964769240SAlex Tomas 298019f5fb7aSTheodore Ts'o if (EXT4_JOURNAL(inode) && 298119f5fb7aSTheodore Ts'o ext4_test_inode_state(inode, EXT4_STATE_JDATA)) { 2982ac27a0ecSDave Kleikamp /* 2983ac27a0ecSDave Kleikamp * This is a REALLY heavyweight approach, but the use of 2984ac27a0ecSDave Kleikamp * bmap on dirty files is expected to be extremely rare: 2985ac27a0ecSDave Kleikamp * only if we run lilo or swapon on a freshly made file 2986ac27a0ecSDave Kleikamp * do we expect this to happen. 2987ac27a0ecSDave Kleikamp * 2988ac27a0ecSDave Kleikamp * (bmap requires CAP_SYS_RAWIO so this does not 2989ac27a0ecSDave Kleikamp * represent an unprivileged user DOS attack --- we'd be 2990ac27a0ecSDave Kleikamp * in trouble if mortal users could trigger this path at 2991ac27a0ecSDave Kleikamp * will.) 2992ac27a0ecSDave Kleikamp * 2993617ba13bSMingming Cao * NB. EXT4_STATE_JDATA is not set on files other than 2994ac27a0ecSDave Kleikamp * regular files. If somebody wants to bmap a directory 2995ac27a0ecSDave Kleikamp * or symlink and gets confused because the buffer 2996ac27a0ecSDave Kleikamp * hasn't yet been flushed to disk, they deserve 2997ac27a0ecSDave Kleikamp * everything they get. 2998ac27a0ecSDave Kleikamp */ 2999ac27a0ecSDave Kleikamp 300019f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_JDATA); 3001617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 3002dab291afSMingming Cao jbd2_journal_lock_updates(journal); 3003dab291afSMingming Cao err = jbd2_journal_flush(journal); 3004dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 3005ac27a0ecSDave Kleikamp 3006ac27a0ecSDave Kleikamp if (err) 3007ac27a0ecSDave Kleikamp return 0; 3008ac27a0ecSDave Kleikamp } 3009ac27a0ecSDave Kleikamp 3010617ba13bSMingming Cao return generic_block_bmap(mapping, block, ext4_get_block); 3011ac27a0ecSDave Kleikamp } 3012ac27a0ecSDave Kleikamp 3013617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page) 3014ac27a0ecSDave Kleikamp { 301546c7f254STao Ma int ret = -EAGAIN; 301646c7f254STao Ma struct inode *inode = page->mapping->host; 301746c7f254STao Ma 30180562e0baSJiaying Zhang trace_ext4_readpage(page); 301946c7f254STao Ma 302046c7f254STao Ma if (ext4_has_inline_data(inode)) 302146c7f254STao Ma ret = ext4_readpage_inline(inode, page); 302246c7f254STao Ma 302346c7f254STao Ma if (ret == -EAGAIN) 3024617ba13bSMingming Cao return mpage_readpage(page, ext4_get_block); 302546c7f254STao Ma 302646c7f254STao Ma return ret; 3027ac27a0ecSDave Kleikamp } 3028ac27a0ecSDave Kleikamp 3029ac27a0ecSDave Kleikamp static int 3030617ba13bSMingming Cao ext4_readpages(struct file *file, struct address_space *mapping, 3031ac27a0ecSDave Kleikamp struct list_head *pages, unsigned nr_pages) 3032ac27a0ecSDave Kleikamp { 303346c7f254STao Ma struct inode *inode = mapping->host; 303446c7f254STao Ma 303546c7f254STao Ma /* If the file has inline data, no need to do readpages. */ 303646c7f254STao Ma if (ext4_has_inline_data(inode)) 303746c7f254STao Ma return 0; 303846c7f254STao Ma 3039617ba13bSMingming Cao return mpage_readpages(mapping, pages, nr_pages, ext4_get_block); 3040ac27a0ecSDave Kleikamp } 3041ac27a0ecSDave Kleikamp 3042617ba13bSMingming Cao static void ext4_invalidatepage(struct page *page, unsigned long offset) 3043ac27a0ecSDave Kleikamp { 30440562e0baSJiaying Zhang trace_ext4_invalidatepage(page, offset); 30450562e0baSJiaying Zhang 30464520fb3cSJan Kara /* No journalling happens on data buffers when this function is used */ 30474520fb3cSJan Kara WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page))); 30484520fb3cSJan Kara 30494520fb3cSJan Kara block_invalidatepage(page, offset); 30504520fb3cSJan Kara } 30514520fb3cSJan Kara 305253e87268SJan Kara static int __ext4_journalled_invalidatepage(struct page *page, 30534520fb3cSJan Kara unsigned long offset) 30544520fb3cSJan Kara { 30554520fb3cSJan Kara journal_t *journal = EXT4_JOURNAL(page->mapping->host); 30564520fb3cSJan Kara 30574520fb3cSJan Kara trace_ext4_journalled_invalidatepage(page, offset); 30584520fb3cSJan Kara 3059744692dcSJiaying Zhang /* 3060ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 3061ac27a0ecSDave Kleikamp */ 3062ac27a0ecSDave Kleikamp if (offset == 0) 3063ac27a0ecSDave Kleikamp ClearPageChecked(page); 3064ac27a0ecSDave Kleikamp 306553e87268SJan Kara return jbd2_journal_invalidatepage(journal, page, offset); 306653e87268SJan Kara } 306753e87268SJan Kara 306853e87268SJan Kara /* Wrapper for aops... */ 306953e87268SJan Kara static void ext4_journalled_invalidatepage(struct page *page, 307053e87268SJan Kara unsigned long offset) 307153e87268SJan Kara { 307253e87268SJan Kara WARN_ON(__ext4_journalled_invalidatepage(page, offset) < 0); 3073ac27a0ecSDave Kleikamp } 3074ac27a0ecSDave Kleikamp 3075617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait) 3076ac27a0ecSDave Kleikamp { 3077617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 3078ac27a0ecSDave Kleikamp 30790562e0baSJiaying Zhang trace_ext4_releasepage(page); 30800562e0baSJiaying Zhang 3081e1c36595SJan Kara /* Page has dirty journalled data -> cannot release */ 3082e1c36595SJan Kara if (PageChecked(page)) 3083ac27a0ecSDave Kleikamp return 0; 30840390131bSFrank Mayhar if (journal) 3085dab291afSMingming Cao return jbd2_journal_try_to_free_buffers(journal, page, wait); 30860390131bSFrank Mayhar else 30870390131bSFrank Mayhar return try_to_free_buffers(page); 3088ac27a0ecSDave Kleikamp } 3089ac27a0ecSDave Kleikamp 3090ac27a0ecSDave Kleikamp /* 30912ed88685STheodore Ts'o * ext4_get_block used when preparing for a DIO write or buffer write. 30922ed88685STheodore Ts'o * We allocate an uinitialized extent if blocks haven't been allocated. 30932ed88685STheodore Ts'o * The extent will be converted to initialized after the IO is complete. 30942ed88685STheodore Ts'o */ 3095f19d5870STao Ma int ext4_get_block_write(struct inode *inode, sector_t iblock, 30964c0425ffSMingming Cao struct buffer_head *bh_result, int create) 30974c0425ffSMingming Cao { 3098c7064ef1SJiaying Zhang ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n", 30998d5d02e6SMingming Cao inode->i_ino, create); 31002ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh_result, 31012ed88685STheodore Ts'o EXT4_GET_BLOCKS_IO_CREATE_EXT); 31024c0425ffSMingming Cao } 31034c0425ffSMingming Cao 3104729f52c6SZheng Liu static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock, 31058b0f165fSAnatol Pomozov struct buffer_head *bh_result, int create) 3106729f52c6SZheng Liu { 31078b0f165fSAnatol Pomozov ext4_debug("ext4_get_block_write_nolock: inode %lu, create flag %d\n", 31088b0f165fSAnatol Pomozov inode->i_ino, create); 31098b0f165fSAnatol Pomozov return _ext4_get_block(inode, iblock, bh_result, 31108b0f165fSAnatol Pomozov EXT4_GET_BLOCKS_NO_LOCK); 3111729f52c6SZheng Liu } 3112729f52c6SZheng Liu 31134c0425ffSMingming Cao static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset, 3114552ef802SChristoph Hellwig ssize_t size, void *private, int ret, 3115552ef802SChristoph Hellwig bool is_async) 31164c0425ffSMingming Cao { 311772c5052dSChristoph Hellwig struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode; 31184c0425ffSMingming Cao ext4_io_end_t *io_end = iocb->private; 31194c0425ffSMingming Cao 31204b70df18SMingming /* if not async direct IO or dio with 0 bytes write, just return */ 31214b70df18SMingming if (!io_end || !size) 3122552ef802SChristoph Hellwig goto out; 31234b70df18SMingming 31248d5d02e6SMingming Cao ext_debug("ext4_end_io_dio(): io_end 0x%p " 3125ace36ad4SJoe Perches "for inode %lu, iocb 0x%p, offset %llu, size %zd\n", 31268d5d02e6SMingming Cao iocb->private, io_end->inode->i_ino, iocb, offset, 31278d5d02e6SMingming Cao size); 31288d5d02e6SMingming Cao 3129b5a7e970STheodore Ts'o iocb->private = NULL; 3130b5a7e970STheodore Ts'o 31318d5d02e6SMingming Cao /* if not aio dio with unwritten extents, just free io and return */ 3132bd2d0210STheodore Ts'o if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) { 31338d5d02e6SMingming Cao ext4_free_io_end(io_end); 31345b3ff237Sjiayingz@google.com (Jiaying Zhang) out: 3135091e26dfSJan Kara inode_dio_done(inode); 31365b3ff237Sjiayingz@google.com (Jiaying Zhang) if (is_async) 31375b3ff237Sjiayingz@google.com (Jiaying Zhang) aio_complete(iocb, ret, 0); 31385b3ff237Sjiayingz@google.com (Jiaying Zhang) return; 31398d5d02e6SMingming Cao } 31408d5d02e6SMingming Cao 31414c0425ffSMingming Cao io_end->offset = offset; 31424c0425ffSMingming Cao io_end->size = size; 31435b3ff237Sjiayingz@google.com (Jiaying Zhang) if (is_async) { 31445b3ff237Sjiayingz@google.com (Jiaying Zhang) io_end->iocb = iocb; 31455b3ff237Sjiayingz@google.com (Jiaying Zhang) io_end->result = ret; 31465b3ff237Sjiayingz@google.com (Jiaying Zhang) } 31474c0425ffSMingming Cao 314828a535f9SDmitry Monakhov ext4_add_complete_io(io_end); 31494c0425ffSMingming Cao } 3150c7064ef1SJiaying Zhang 31514c0425ffSMingming Cao /* 31524c0425ffSMingming Cao * For ext4 extent files, ext4 will do direct-io write to holes, 31534c0425ffSMingming Cao * preallocated extents, and those write extend the file, no need to 31544c0425ffSMingming Cao * fall back to buffered IO. 31554c0425ffSMingming Cao * 3156b595076aSUwe Kleine-König * For holes, we fallocate those blocks, mark them as uninitialized 315769c499d1STheodore Ts'o * If those blocks were preallocated, we mark sure they are split, but 3158b595076aSUwe Kleine-König * still keep the range to write as uninitialized. 31594c0425ffSMingming Cao * 316069c499d1STheodore Ts'o * The unwritten extents will be converted to written when DIO is completed. 31618d5d02e6SMingming Cao * For async direct IO, since the IO may still pending when return, we 316225985edcSLucas De Marchi * set up an end_io call back function, which will do the conversion 31638d5d02e6SMingming Cao * when async direct IO completed. 31644c0425ffSMingming Cao * 31654c0425ffSMingming Cao * If the O_DIRECT write will extend the file then add this inode to the 31664c0425ffSMingming Cao * orphan list. So recovery will truncate it back to the original size 31674c0425ffSMingming Cao * if the machine crashes during the write. 31684c0425ffSMingming Cao * 31694c0425ffSMingming Cao */ 31704c0425ffSMingming Cao static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb, 31714c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 31724c0425ffSMingming Cao unsigned long nr_segs) 31734c0425ffSMingming Cao { 31744c0425ffSMingming Cao struct file *file = iocb->ki_filp; 31754c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 31764c0425ffSMingming Cao ssize_t ret; 31774c0425ffSMingming Cao size_t count = iov_length(iov, nr_segs); 3178729f52c6SZheng Liu int overwrite = 0; 31798b0f165fSAnatol Pomozov get_block_t *get_block_func = NULL; 31808b0f165fSAnatol Pomozov int dio_flags = 0; 318169c499d1STheodore Ts'o loff_t final_size = offset + count; 318269c499d1STheodore Ts'o 318369c499d1STheodore Ts'o /* Use the old path for reads and writes beyond i_size. */ 318469c499d1STheodore Ts'o if (rw != WRITE || final_size > inode->i_size) 318569c499d1STheodore Ts'o return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 3186729f52c6SZheng Liu 31874bd809dbSZheng Liu BUG_ON(iocb->private == NULL); 31884bd809dbSZheng Liu 31894bd809dbSZheng Liu /* If we do a overwrite dio, i_mutex locking can be released */ 31904bd809dbSZheng Liu overwrite = *((int *)iocb->private); 31914bd809dbSZheng Liu 31924bd809dbSZheng Liu if (overwrite) { 31931f555cfaSDmitry Monakhov atomic_inc(&inode->i_dio_count); 31944bd809dbSZheng Liu down_read(&EXT4_I(inode)->i_data_sem); 31954bd809dbSZheng Liu mutex_unlock(&inode->i_mutex); 31964bd809dbSZheng Liu } 31974bd809dbSZheng Liu 31984c0425ffSMingming Cao /* 31998d5d02e6SMingming Cao * We could direct write to holes and fallocate. 32008d5d02e6SMingming Cao * 320169c499d1STheodore Ts'o * Allocated blocks to fill the hole are marked as 320269c499d1STheodore Ts'o * uninitialized to prevent parallel buffered read to expose 320369c499d1STheodore Ts'o * the stale data before DIO complete the data IO. 32048d5d02e6SMingming Cao * 320569c499d1STheodore Ts'o * As to previously fallocated extents, ext4 get_block will 320669c499d1STheodore Ts'o * just simply mark the buffer mapped but still keep the 320769c499d1STheodore Ts'o * extents uninitialized. 32084c0425ffSMingming Cao * 320969c499d1STheodore Ts'o * For non AIO case, we will convert those unwritten extents 32108d5d02e6SMingming Cao * to written after return back from blockdev_direct_IO. 32114c0425ffSMingming Cao * 321269c499d1STheodore Ts'o * For async DIO, the conversion needs to be deferred when the 321369c499d1STheodore Ts'o * IO is completed. The ext4 end_io callback function will be 321469c499d1STheodore Ts'o * called to take care of the conversion work. Here for async 321569c499d1STheodore Ts'o * case, we allocate an io_end structure to hook to the iocb. 32164c0425ffSMingming Cao */ 32178d5d02e6SMingming Cao iocb->private = NULL; 3218f45ee3a1SDmitry Monakhov ext4_inode_aio_set(inode, NULL); 32198d5d02e6SMingming Cao if (!is_sync_kiocb(iocb)) { 322069c499d1STheodore Ts'o ext4_io_end_t *io_end = ext4_init_io_end(inode, GFP_NOFS); 32214bd809dbSZheng Liu if (!io_end) { 32224bd809dbSZheng Liu ret = -ENOMEM; 32234bd809dbSZheng Liu goto retake_lock; 32244bd809dbSZheng Liu } 3225266991b1SJeff Moyer io_end->flag |= EXT4_IO_END_DIRECT; 3226266991b1SJeff Moyer iocb->private = io_end; 32278d5d02e6SMingming Cao /* 322869c499d1STheodore Ts'o * we save the io structure for current async direct 322969c499d1STheodore Ts'o * IO, so that later ext4_map_blocks() could flag the 323069c499d1STheodore Ts'o * io structure whether there is a unwritten extents 323169c499d1STheodore Ts'o * needs to be converted when IO is completed. 32328d5d02e6SMingming Cao */ 3233f45ee3a1SDmitry Monakhov ext4_inode_aio_set(inode, io_end); 32348d5d02e6SMingming Cao } 32358d5d02e6SMingming Cao 32368b0f165fSAnatol Pomozov if (overwrite) { 32378b0f165fSAnatol Pomozov get_block_func = ext4_get_block_write_nolock; 32388b0f165fSAnatol Pomozov } else { 32398b0f165fSAnatol Pomozov get_block_func = ext4_get_block_write; 32408b0f165fSAnatol Pomozov dio_flags = DIO_LOCKING; 32418b0f165fSAnatol Pomozov } 3242729f52c6SZheng Liu ret = __blockdev_direct_IO(rw, iocb, inode, 3243729f52c6SZheng Liu inode->i_sb->s_bdev, iov, 3244729f52c6SZheng Liu offset, nr_segs, 32458b0f165fSAnatol Pomozov get_block_func, 3246729f52c6SZheng Liu ext4_end_io_dio, 3247729f52c6SZheng Liu NULL, 32488b0f165fSAnatol Pomozov dio_flags); 32498b0f165fSAnatol Pomozov 32508d5d02e6SMingming Cao if (iocb->private) 3251f45ee3a1SDmitry Monakhov ext4_inode_aio_set(inode, NULL); 32528d5d02e6SMingming Cao /* 325369c499d1STheodore Ts'o * The io_end structure takes a reference to the inode, that 325469c499d1STheodore Ts'o * structure needs to be destroyed and the reference to the 325569c499d1STheodore Ts'o * inode need to be dropped, when IO is complete, even with 0 325669c499d1STheodore Ts'o * byte write, or failed. 32578d5d02e6SMingming Cao * 325869c499d1STheodore Ts'o * In the successful AIO DIO case, the io_end structure will 325969c499d1STheodore Ts'o * be destroyed and the reference to the inode will be dropped 32608d5d02e6SMingming Cao * after the end_io call back function is called. 32618d5d02e6SMingming Cao * 326269c499d1STheodore Ts'o * In the case there is 0 byte write, or error case, since VFS 326369c499d1STheodore Ts'o * direct IO won't invoke the end_io call back function, we 326469c499d1STheodore Ts'o * need to free the end_io structure here. 32658d5d02e6SMingming Cao */ 32668d5d02e6SMingming Cao if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) { 32678d5d02e6SMingming Cao ext4_free_io_end(iocb->private); 32688d5d02e6SMingming Cao iocb->private = NULL; 3269729f52c6SZheng Liu } else if (ret > 0 && !overwrite && ext4_test_inode_state(inode, 32705f524950SMingming EXT4_STATE_DIO_UNWRITTEN)) { 3271109f5565SMingming int err; 32728d5d02e6SMingming Cao /* 32738d5d02e6SMingming Cao * for non AIO case, since the IO is already 327425985edcSLucas De Marchi * completed, we could do the conversion right here 32758d5d02e6SMingming Cao */ 3276109f5565SMingming err = ext4_convert_unwritten_extents(inode, 32778d5d02e6SMingming Cao offset, ret); 3278109f5565SMingming if (err < 0) 3279109f5565SMingming ret = err; 328019f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN); 3281109f5565SMingming } 32824bd809dbSZheng Liu 32834bd809dbSZheng Liu retake_lock: 32844bd809dbSZheng Liu /* take i_mutex locking again if we do a ovewrite dio */ 32854bd809dbSZheng Liu if (overwrite) { 32861f555cfaSDmitry Monakhov inode_dio_done(inode); 32874bd809dbSZheng Liu up_read(&EXT4_I(inode)->i_data_sem); 32884bd809dbSZheng Liu mutex_lock(&inode->i_mutex); 32894bd809dbSZheng Liu } 32904bd809dbSZheng Liu 32914c0425ffSMingming Cao return ret; 32924c0425ffSMingming Cao } 32938d5d02e6SMingming Cao 32944c0425ffSMingming Cao static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb, 32954c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 32964c0425ffSMingming Cao unsigned long nr_segs) 32974c0425ffSMingming Cao { 32984c0425ffSMingming Cao struct file *file = iocb->ki_filp; 32994c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 33000562e0baSJiaying Zhang ssize_t ret; 33014c0425ffSMingming Cao 330284ebd795STheodore Ts'o /* 330384ebd795STheodore Ts'o * If we are doing data journalling we don't support O_DIRECT 330484ebd795STheodore Ts'o */ 330584ebd795STheodore Ts'o if (ext4_should_journal_data(inode)) 330684ebd795STheodore Ts'o return 0; 330784ebd795STheodore Ts'o 330846c7f254STao Ma /* Let buffer I/O handle the inline data case. */ 330946c7f254STao Ma if (ext4_has_inline_data(inode)) 331046c7f254STao Ma return 0; 331146c7f254STao Ma 33120562e0baSJiaying Zhang trace_ext4_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw); 331312e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 33140562e0baSJiaying Zhang ret = ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs); 33150562e0baSJiaying Zhang else 33160562e0baSJiaying Zhang ret = ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 33170562e0baSJiaying Zhang trace_ext4_direct_IO_exit(inode, offset, 33180562e0baSJiaying Zhang iov_length(iov, nr_segs), rw, ret); 33190562e0baSJiaying Zhang return ret; 33204c0425ffSMingming Cao } 33214c0425ffSMingming Cao 3322ac27a0ecSDave Kleikamp /* 3323617ba13bSMingming Cao * Pages can be marked dirty completely asynchronously from ext4's journalling 3324ac27a0ecSDave Kleikamp * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do 3325ac27a0ecSDave Kleikamp * much here because ->set_page_dirty is called under VFS locks. The page is 3326ac27a0ecSDave Kleikamp * not necessarily locked. 3327ac27a0ecSDave Kleikamp * 3328ac27a0ecSDave Kleikamp * We cannot just dirty the page and leave attached buffers clean, because the 3329ac27a0ecSDave Kleikamp * buffers' dirty state is "definitive". We cannot just set the buffers dirty 3330ac27a0ecSDave Kleikamp * or jbddirty because all the journalling code will explode. 3331ac27a0ecSDave Kleikamp * 3332ac27a0ecSDave Kleikamp * So what we do is to mark the page "pending dirty" and next time writepage 3333ac27a0ecSDave Kleikamp * is called, propagate that into the buffers appropriately. 3334ac27a0ecSDave Kleikamp */ 3335617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page) 3336ac27a0ecSDave Kleikamp { 3337ac27a0ecSDave Kleikamp SetPageChecked(page); 3338ac27a0ecSDave Kleikamp return __set_page_dirty_nobuffers(page); 3339ac27a0ecSDave Kleikamp } 3340ac27a0ecSDave Kleikamp 3341617ba13bSMingming Cao static const struct address_space_operations ext4_ordered_aops = { 3342617ba13bSMingming Cao .readpage = ext4_readpage, 3343617ba13bSMingming Cao .readpages = ext4_readpages, 334443ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3345bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3346bfc1af65SNick Piggin .write_end = ext4_ordered_write_end, 3347617ba13bSMingming Cao .bmap = ext4_bmap, 3348617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3349617ba13bSMingming Cao .releasepage = ext4_releasepage, 3350617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 3351ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 33528ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3353aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3354ac27a0ecSDave Kleikamp }; 3355ac27a0ecSDave Kleikamp 3356617ba13bSMingming Cao static const struct address_space_operations ext4_writeback_aops = { 3357617ba13bSMingming Cao .readpage = ext4_readpage, 3358617ba13bSMingming Cao .readpages = ext4_readpages, 335943ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3360bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3361bfc1af65SNick Piggin .write_end = ext4_writeback_write_end, 3362617ba13bSMingming Cao .bmap = ext4_bmap, 3363617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3364617ba13bSMingming Cao .releasepage = ext4_releasepage, 3365617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 3366ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 33678ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3368aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3369ac27a0ecSDave Kleikamp }; 3370ac27a0ecSDave Kleikamp 3371617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 3372617ba13bSMingming Cao .readpage = ext4_readpage, 3373617ba13bSMingming Cao .readpages = ext4_readpages, 337443ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3375bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3376bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 3377617ba13bSMingming Cao .set_page_dirty = ext4_journalled_set_page_dirty, 3378617ba13bSMingming Cao .bmap = ext4_bmap, 33794520fb3cSJan Kara .invalidatepage = ext4_journalled_invalidatepage, 3380617ba13bSMingming Cao .releasepage = ext4_releasepage, 338184ebd795STheodore Ts'o .direct_IO = ext4_direct_IO, 33828ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3383aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3384ac27a0ecSDave Kleikamp }; 3385ac27a0ecSDave Kleikamp 338664769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 338764769240SAlex Tomas .readpage = ext4_readpage, 338864769240SAlex Tomas .readpages = ext4_readpages, 338943ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 339064769240SAlex Tomas .writepages = ext4_da_writepages, 339164769240SAlex Tomas .write_begin = ext4_da_write_begin, 339264769240SAlex Tomas .write_end = ext4_da_write_end, 339364769240SAlex Tomas .bmap = ext4_bmap, 339464769240SAlex Tomas .invalidatepage = ext4_da_invalidatepage, 339564769240SAlex Tomas .releasepage = ext4_releasepage, 339664769240SAlex Tomas .direct_IO = ext4_direct_IO, 339764769240SAlex Tomas .migratepage = buffer_migrate_page, 33988ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3399aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 340064769240SAlex Tomas }; 340164769240SAlex Tomas 3402617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 3403ac27a0ecSDave Kleikamp { 34043d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 34053d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 34063d2b1582SLukas Czerner if (test_opt(inode->i_sb, DELALLOC)) 3407cd1aac32SAneesh Kumar K.V inode->i_mapping->a_ops = &ext4_da_aops; 3408ac27a0ecSDave Kleikamp else 34093d2b1582SLukas Czerner inode->i_mapping->a_ops = &ext4_ordered_aops; 34103d2b1582SLukas Czerner break; 34113d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 34123d2b1582SLukas Czerner if (test_opt(inode->i_sb, DELALLOC)) 34133d2b1582SLukas Czerner inode->i_mapping->a_ops = &ext4_da_aops; 34143d2b1582SLukas Czerner else 34153d2b1582SLukas Czerner inode->i_mapping->a_ops = &ext4_writeback_aops; 34163d2b1582SLukas Czerner break; 34173d2b1582SLukas Czerner case EXT4_INODE_JOURNAL_DATA_MODE: 3418617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 34193d2b1582SLukas Czerner break; 34203d2b1582SLukas Czerner default: 34213d2b1582SLukas Czerner BUG(); 34223d2b1582SLukas Czerner } 3423ac27a0ecSDave Kleikamp } 3424ac27a0ecSDave Kleikamp 34254e96b2dbSAllison Henderson 34264e96b2dbSAllison Henderson /* 34274e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers() 34284e96b2dbSAllison Henderson * Wrapper function for ext4_discard_partial_page_buffers_no_lock. 34294e96b2dbSAllison Henderson * This function finds and locks the page containing the offset 34304e96b2dbSAllison Henderson * "from" and passes it to ext4_discard_partial_page_buffers_no_lock. 34314e96b2dbSAllison Henderson * Calling functions that already have the page locked should call 34324e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers_no_lock directly. 34334e96b2dbSAllison Henderson */ 34344e96b2dbSAllison Henderson int ext4_discard_partial_page_buffers(handle_t *handle, 34354e96b2dbSAllison Henderson struct address_space *mapping, loff_t from, 34364e96b2dbSAllison Henderson loff_t length, int flags) 34374e96b2dbSAllison Henderson { 34384e96b2dbSAllison Henderson struct inode *inode = mapping->host; 34394e96b2dbSAllison Henderson struct page *page; 34404e96b2dbSAllison Henderson int err = 0; 34414e96b2dbSAllison Henderson 34424e96b2dbSAllison Henderson page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT, 34434e96b2dbSAllison Henderson mapping_gfp_mask(mapping) & ~__GFP_FS); 34444e96b2dbSAllison Henderson if (!page) 34455129d05fSYongqiang Yang return -ENOMEM; 34464e96b2dbSAllison Henderson 34474e96b2dbSAllison Henderson err = ext4_discard_partial_page_buffers_no_lock(handle, inode, page, 34484e96b2dbSAllison Henderson from, length, flags); 34494e96b2dbSAllison Henderson 34504e96b2dbSAllison Henderson unlock_page(page); 34514e96b2dbSAllison Henderson page_cache_release(page); 34524e96b2dbSAllison Henderson return err; 34534e96b2dbSAllison Henderson } 34544e96b2dbSAllison Henderson 34554e96b2dbSAllison Henderson /* 34564e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers_no_lock() 34574e96b2dbSAllison Henderson * Zeros a page range of length 'length' starting from offset 'from'. 34584e96b2dbSAllison Henderson * Buffer heads that correspond to the block aligned regions of the 34594e96b2dbSAllison Henderson * zeroed range will be unmapped. Unblock aligned regions 34604e96b2dbSAllison Henderson * will have the corresponding buffer head mapped if needed so that 34614e96b2dbSAllison Henderson * that region of the page can be updated with the partial zero out. 34624e96b2dbSAllison Henderson * 34634e96b2dbSAllison Henderson * This function assumes that the page has already been locked. The 34644e96b2dbSAllison Henderson * The range to be discarded must be contained with in the given page. 34654e96b2dbSAllison Henderson * If the specified range exceeds the end of the page it will be shortened 34664e96b2dbSAllison Henderson * to the end of the page that corresponds to 'from'. This function is 34674e96b2dbSAllison Henderson * appropriate for updating a page and it buffer heads to be unmapped and 34684e96b2dbSAllison Henderson * zeroed for blocks that have been either released, or are going to be 34694e96b2dbSAllison Henderson * released. 34704e96b2dbSAllison Henderson * 34714e96b2dbSAllison Henderson * handle: The journal handle 34724e96b2dbSAllison Henderson * inode: The files inode 34734e96b2dbSAllison Henderson * page: A locked page that contains the offset "from" 34744907cb7bSAnatol Pomozov * from: The starting byte offset (from the beginning of the file) 34754e96b2dbSAllison Henderson * to begin discarding 34764e96b2dbSAllison Henderson * len: The length of bytes to discard 34774e96b2dbSAllison Henderson * flags: Optional flags that may be used: 34784e96b2dbSAllison Henderson * 34794e96b2dbSAllison Henderson * EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED 34804e96b2dbSAllison Henderson * Only zero the regions of the page whose buffer heads 34814e96b2dbSAllison Henderson * have already been unmapped. This flag is appropriate 34824907cb7bSAnatol Pomozov * for updating the contents of a page whose blocks may 34834e96b2dbSAllison Henderson * have already been released, and we only want to zero 34844e96b2dbSAllison Henderson * out the regions that correspond to those released blocks. 34854e96b2dbSAllison Henderson * 34864907cb7bSAnatol Pomozov * Returns zero on success or negative on failure. 34874e96b2dbSAllison Henderson */ 34885f163cc7SEric Sandeen static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle, 34894e96b2dbSAllison Henderson struct inode *inode, struct page *page, loff_t from, 34904e96b2dbSAllison Henderson loff_t length, int flags) 34914e96b2dbSAllison Henderson { 34924e96b2dbSAllison Henderson ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; 34934e96b2dbSAllison Henderson unsigned int offset = from & (PAGE_CACHE_SIZE-1); 34944e96b2dbSAllison Henderson unsigned int blocksize, max, pos; 34954e96b2dbSAllison Henderson ext4_lblk_t iblock; 34964e96b2dbSAllison Henderson struct buffer_head *bh; 34974e96b2dbSAllison Henderson int err = 0; 34984e96b2dbSAllison Henderson 34994e96b2dbSAllison Henderson blocksize = inode->i_sb->s_blocksize; 35004e96b2dbSAllison Henderson max = PAGE_CACHE_SIZE - offset; 35014e96b2dbSAllison Henderson 35024e96b2dbSAllison Henderson if (index != page->index) 35034e96b2dbSAllison Henderson return -EINVAL; 35044e96b2dbSAllison Henderson 35054e96b2dbSAllison Henderson /* 35064e96b2dbSAllison Henderson * correct length if it does not fall between 35074e96b2dbSAllison Henderson * 'from' and the end of the page 35084e96b2dbSAllison Henderson */ 35094e96b2dbSAllison Henderson if (length > max || length < 0) 35104e96b2dbSAllison Henderson length = max; 35114e96b2dbSAllison Henderson 35124e96b2dbSAllison Henderson iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); 35134e96b2dbSAllison Henderson 3514093e6e36SYongqiang Yang if (!page_has_buffers(page)) 35154e96b2dbSAllison Henderson create_empty_buffers(page, blocksize, 0); 35164e96b2dbSAllison Henderson 35174e96b2dbSAllison Henderson /* Find the buffer that contains "offset" */ 35184e96b2dbSAllison Henderson bh = page_buffers(page); 35194e96b2dbSAllison Henderson pos = blocksize; 35204e96b2dbSAllison Henderson while (offset >= pos) { 35214e96b2dbSAllison Henderson bh = bh->b_this_page; 35224e96b2dbSAllison Henderson iblock++; 35234e96b2dbSAllison Henderson pos += blocksize; 35244e96b2dbSAllison Henderson } 35254e96b2dbSAllison Henderson 35264e96b2dbSAllison Henderson pos = offset; 35274e96b2dbSAllison Henderson while (pos < offset + length) { 3528e260daf2SYongqiang Yang unsigned int end_of_block, range_to_discard; 3529e260daf2SYongqiang Yang 35304e96b2dbSAllison Henderson err = 0; 35314e96b2dbSAllison Henderson 35324e96b2dbSAllison Henderson /* The length of space left to zero and unmap */ 35334e96b2dbSAllison Henderson range_to_discard = offset + length - pos; 35344e96b2dbSAllison Henderson 35354e96b2dbSAllison Henderson /* The length of space until the end of the block */ 35364e96b2dbSAllison Henderson end_of_block = blocksize - (pos & (blocksize-1)); 35374e96b2dbSAllison Henderson 35384e96b2dbSAllison Henderson /* 35394e96b2dbSAllison Henderson * Do not unmap or zero past end of block 35404e96b2dbSAllison Henderson * for this buffer head 35414e96b2dbSAllison Henderson */ 35424e96b2dbSAllison Henderson if (range_to_discard > end_of_block) 35434e96b2dbSAllison Henderson range_to_discard = end_of_block; 35444e96b2dbSAllison Henderson 35454e96b2dbSAllison Henderson 35464e96b2dbSAllison Henderson /* 35474e96b2dbSAllison Henderson * Skip this buffer head if we are only zeroing unampped 35484e96b2dbSAllison Henderson * regions of the page 35494e96b2dbSAllison Henderson */ 35504e96b2dbSAllison Henderson if (flags & EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED && 35514e96b2dbSAllison Henderson buffer_mapped(bh)) 35524e96b2dbSAllison Henderson goto next; 35534e96b2dbSAllison Henderson 35544e96b2dbSAllison Henderson /* If the range is block aligned, unmap */ 35554e96b2dbSAllison Henderson if (range_to_discard == blocksize) { 35564e96b2dbSAllison Henderson clear_buffer_dirty(bh); 35574e96b2dbSAllison Henderson bh->b_bdev = NULL; 35584e96b2dbSAllison Henderson clear_buffer_mapped(bh); 35594e96b2dbSAllison Henderson clear_buffer_req(bh); 35604e96b2dbSAllison Henderson clear_buffer_new(bh); 35614e96b2dbSAllison Henderson clear_buffer_delay(bh); 35624e96b2dbSAllison Henderson clear_buffer_unwritten(bh); 35634e96b2dbSAllison Henderson clear_buffer_uptodate(bh); 35644e96b2dbSAllison Henderson zero_user(page, pos, range_to_discard); 35654e96b2dbSAllison Henderson BUFFER_TRACE(bh, "Buffer discarded"); 35664e96b2dbSAllison Henderson goto next; 35674e96b2dbSAllison Henderson } 35684e96b2dbSAllison Henderson 35694e96b2dbSAllison Henderson /* 35704e96b2dbSAllison Henderson * If this block is not completely contained in the range 35714e96b2dbSAllison Henderson * to be discarded, then it is not going to be released. Because 35724e96b2dbSAllison Henderson * we need to keep this block, we need to make sure this part 35734e96b2dbSAllison Henderson * of the page is uptodate before we modify it by writeing 35744e96b2dbSAllison Henderson * partial zeros on it. 35754e96b2dbSAllison Henderson */ 35764e96b2dbSAllison Henderson if (!buffer_mapped(bh)) { 35774e96b2dbSAllison Henderson /* 35784e96b2dbSAllison Henderson * Buffer head must be mapped before we can read 35794e96b2dbSAllison Henderson * from the block 35804e96b2dbSAllison Henderson */ 35814e96b2dbSAllison Henderson BUFFER_TRACE(bh, "unmapped"); 35824e96b2dbSAllison Henderson ext4_get_block(inode, iblock, bh, 0); 35834e96b2dbSAllison Henderson /* unmapped? It's a hole - nothing to do */ 35844e96b2dbSAllison Henderson if (!buffer_mapped(bh)) { 35854e96b2dbSAllison Henderson BUFFER_TRACE(bh, "still unmapped"); 35864e96b2dbSAllison Henderson goto next; 35874e96b2dbSAllison Henderson } 35884e96b2dbSAllison Henderson } 35894e96b2dbSAllison Henderson 35904e96b2dbSAllison Henderson /* Ok, it's mapped. Make sure it's up-to-date */ 35914e96b2dbSAllison Henderson if (PageUptodate(page)) 35924e96b2dbSAllison Henderson set_buffer_uptodate(bh); 35934e96b2dbSAllison Henderson 35944e96b2dbSAllison Henderson if (!buffer_uptodate(bh)) { 35954e96b2dbSAllison Henderson err = -EIO; 35964e96b2dbSAllison Henderson ll_rw_block(READ, 1, &bh); 35974e96b2dbSAllison Henderson wait_on_buffer(bh); 35984e96b2dbSAllison Henderson /* Uhhuh. Read error. Complain and punt.*/ 35994e96b2dbSAllison Henderson if (!buffer_uptodate(bh)) 36004e96b2dbSAllison Henderson goto next; 36014e96b2dbSAllison Henderson } 36024e96b2dbSAllison Henderson 36034e96b2dbSAllison Henderson if (ext4_should_journal_data(inode)) { 36044e96b2dbSAllison Henderson BUFFER_TRACE(bh, "get write access"); 36054e96b2dbSAllison Henderson err = ext4_journal_get_write_access(handle, bh); 36064e96b2dbSAllison Henderson if (err) 36074e96b2dbSAllison Henderson goto next; 36084e96b2dbSAllison Henderson } 36094e96b2dbSAllison Henderson 36104e96b2dbSAllison Henderson zero_user(page, pos, range_to_discard); 36114e96b2dbSAllison Henderson 36124e96b2dbSAllison Henderson err = 0; 36134e96b2dbSAllison Henderson if (ext4_should_journal_data(inode)) { 36144e96b2dbSAllison Henderson err = ext4_handle_dirty_metadata(handle, inode, bh); 3615decbd919STheodore Ts'o } else 36164e96b2dbSAllison Henderson mark_buffer_dirty(bh); 36174e96b2dbSAllison Henderson 36184e96b2dbSAllison Henderson BUFFER_TRACE(bh, "Partial buffer zeroed"); 36194e96b2dbSAllison Henderson next: 36204e96b2dbSAllison Henderson bh = bh->b_this_page; 36214e96b2dbSAllison Henderson iblock++; 36224e96b2dbSAllison Henderson pos += range_to_discard; 36234e96b2dbSAllison Henderson } 36244e96b2dbSAllison Henderson 36254e96b2dbSAllison Henderson return err; 36264e96b2dbSAllison Henderson } 36274e96b2dbSAllison Henderson 362891ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 362991ef4cafSDuane Griffin { 363091ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 363191ef4cafSDuane Griffin return 1; 363291ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 363391ef4cafSDuane Griffin return 1; 363491ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 363591ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 363691ef4cafSDuane Griffin return 0; 363791ef4cafSDuane Griffin } 363891ef4cafSDuane Griffin 3639ac27a0ecSDave Kleikamp /* 3640a4bb6b64SAllison Henderson * ext4_punch_hole: punches a hole in a file by releaseing the blocks 3641a4bb6b64SAllison Henderson * associated with the given offset and length 3642a4bb6b64SAllison Henderson * 3643a4bb6b64SAllison Henderson * @inode: File inode 3644a4bb6b64SAllison Henderson * @offset: The offset where the hole will begin 3645a4bb6b64SAllison Henderson * @len: The length of the hole 3646a4bb6b64SAllison Henderson * 36474907cb7bSAnatol Pomozov * Returns: 0 on success or negative on failure 3648a4bb6b64SAllison Henderson */ 3649a4bb6b64SAllison Henderson 3650a4bb6b64SAllison Henderson int ext4_punch_hole(struct file *file, loff_t offset, loff_t length) 3651a4bb6b64SAllison Henderson { 3652a4bb6b64SAllison Henderson struct inode *inode = file->f_path.dentry->d_inode; 3653a4bb6b64SAllison Henderson if (!S_ISREG(inode->i_mode)) 365473355192SAllison Henderson return -EOPNOTSUPP; 3655a4bb6b64SAllison Henderson 36568bad6fc8SZheng Liu if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 36578bad6fc8SZheng Liu return ext4_ind_punch_hole(file, offset, length); 3658a4bb6b64SAllison Henderson 3659bab08ab9STheodore Ts'o if (EXT4_SB(inode->i_sb)->s_cluster_ratio > 1) { 3660bab08ab9STheodore Ts'o /* TODO: Add support for bigalloc file systems */ 366173355192SAllison Henderson return -EOPNOTSUPP; 3662bab08ab9STheodore Ts'o } 3663bab08ab9STheodore Ts'o 3664aaddea81SZheng Liu trace_ext4_punch_hole(inode, offset, length); 3665aaddea81SZheng Liu 3666a4bb6b64SAllison Henderson return ext4_ext_punch_hole(file, offset, length); 3667a4bb6b64SAllison Henderson } 3668a4bb6b64SAllison Henderson 3669a4bb6b64SAllison Henderson /* 3670617ba13bSMingming Cao * ext4_truncate() 3671ac27a0ecSDave Kleikamp * 3672617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 3673617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 3674ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 3675ac27a0ecSDave Kleikamp * 367642b2aa86SJustin P. Mattock * As we work through the truncate and commit bits of it to the journal there 3677ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 3678ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 3679ac27a0ecSDave Kleikamp * 3680ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 3681ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 3682ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 3683ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 3684ac27a0ecSDave Kleikamp * left-to-right works OK too). 3685ac27a0ecSDave Kleikamp * 3686ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 3687ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 3688ac27a0ecSDave Kleikamp * 3689ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 3690617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 3691ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 3692617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 3693617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 3694ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 3695617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 3696ac27a0ecSDave Kleikamp */ 3697617ba13bSMingming Cao void ext4_truncate(struct inode *inode) 3698ac27a0ecSDave Kleikamp { 36990562e0baSJiaying Zhang trace_ext4_truncate_enter(inode); 37000562e0baSJiaying Zhang 370191ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 3702ac27a0ecSDave Kleikamp return; 3703ac27a0ecSDave Kleikamp 370412e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS); 3705c8d46e41SJiaying Zhang 37065534fb5bSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 370719f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE); 37087d8f9f7dSTheodore Ts'o 3709aef1c851STao Ma if (ext4_has_inline_data(inode)) { 3710aef1c851STao Ma int has_inline = 1; 3711aef1c851STao Ma 3712aef1c851STao Ma ext4_inline_data_truncate(inode, &has_inline); 3713aef1c851STao Ma if (has_inline) 3714aef1c851STao Ma return; 3715aef1c851STao Ma } 3716aef1c851STao Ma 3717ff9893dcSAmir Goldstein if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3718cf108bcaSJan Kara ext4_ext_truncate(inode); 3719ff9893dcSAmir Goldstein else 3720ff9893dcSAmir Goldstein ext4_ind_truncate(inode); 3721a86c6181SAlex Tomas 37220562e0baSJiaying Zhang trace_ext4_truncate_exit(inode); 3723ac27a0ecSDave Kleikamp } 3724ac27a0ecSDave Kleikamp 3725ac27a0ecSDave Kleikamp /* 3726617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 3727ac27a0ecSDave Kleikamp * underlying buffer_head on success. If 'in_mem' is true, we have all 3728ac27a0ecSDave Kleikamp * data in memory that is needed to recreate the on-disk version of this 3729ac27a0ecSDave Kleikamp * inode. 3730ac27a0ecSDave Kleikamp */ 3731617ba13bSMingming Cao static int __ext4_get_inode_loc(struct inode *inode, 3732617ba13bSMingming Cao struct ext4_iloc *iloc, int in_mem) 3733ac27a0ecSDave Kleikamp { 3734240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 3735ac27a0ecSDave Kleikamp struct buffer_head *bh; 3736240799cdSTheodore Ts'o struct super_block *sb = inode->i_sb; 3737240799cdSTheodore Ts'o ext4_fsblk_t block; 3738240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 3739ac27a0ecSDave Kleikamp 37403a06d778SAneesh Kumar K.V iloc->bh = NULL; 3741240799cdSTheodore Ts'o if (!ext4_valid_inum(sb, inode->i_ino)) 3742ac27a0ecSDave Kleikamp return -EIO; 3743ac27a0ecSDave Kleikamp 3744240799cdSTheodore Ts'o iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb); 3745240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 3746240799cdSTheodore Ts'o if (!gdp) 3747240799cdSTheodore Ts'o return -EIO; 3748240799cdSTheodore Ts'o 3749240799cdSTheodore Ts'o /* 3750240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 3751240799cdSTheodore Ts'o */ 375200d09882STao Ma inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 3753240799cdSTheodore Ts'o inode_offset = ((inode->i_ino - 1) % 3754240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 3755240799cdSTheodore Ts'o block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); 3756240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 3757240799cdSTheodore Ts'o 3758240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 3759aebf0243SWang Shilong if (unlikely(!bh)) 3760860d21e2STheodore Ts'o return -ENOMEM; 3761ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 3762ac27a0ecSDave Kleikamp lock_buffer(bh); 37639c83a923SHidehiro Kawai 37649c83a923SHidehiro Kawai /* 37659c83a923SHidehiro Kawai * If the buffer has the write error flag, we have failed 37669c83a923SHidehiro Kawai * to write out another inode in the same block. In this 37679c83a923SHidehiro Kawai * case, we don't have to read the block because we may 37689c83a923SHidehiro Kawai * read the old inode data successfully. 37699c83a923SHidehiro Kawai */ 37709c83a923SHidehiro Kawai if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) 37719c83a923SHidehiro Kawai set_buffer_uptodate(bh); 37729c83a923SHidehiro Kawai 3773ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) { 3774ac27a0ecSDave Kleikamp /* someone brought it uptodate while we waited */ 3775ac27a0ecSDave Kleikamp unlock_buffer(bh); 3776ac27a0ecSDave Kleikamp goto has_buffer; 3777ac27a0ecSDave Kleikamp } 3778ac27a0ecSDave Kleikamp 3779ac27a0ecSDave Kleikamp /* 3780ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 3781ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 3782ac27a0ecSDave Kleikamp * block. 3783ac27a0ecSDave Kleikamp */ 3784ac27a0ecSDave Kleikamp if (in_mem) { 3785ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 3786240799cdSTheodore Ts'o int i, start; 3787ac27a0ecSDave Kleikamp 3788240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 3789ac27a0ecSDave Kleikamp 3790ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 3791240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 3792aebf0243SWang Shilong if (unlikely(!bitmap_bh)) 3793ac27a0ecSDave Kleikamp goto make_io; 3794ac27a0ecSDave Kleikamp 3795ac27a0ecSDave Kleikamp /* 3796ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 3797ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 3798ac27a0ecSDave Kleikamp * of one, so skip it. 3799ac27a0ecSDave Kleikamp */ 3800ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 3801ac27a0ecSDave Kleikamp brelse(bitmap_bh); 3802ac27a0ecSDave Kleikamp goto make_io; 3803ac27a0ecSDave Kleikamp } 3804240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 3805ac27a0ecSDave Kleikamp if (i == inode_offset) 3806ac27a0ecSDave Kleikamp continue; 3807617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 3808ac27a0ecSDave Kleikamp break; 3809ac27a0ecSDave Kleikamp } 3810ac27a0ecSDave Kleikamp brelse(bitmap_bh); 3811240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 3812ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 3813ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 3814ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 3815ac27a0ecSDave Kleikamp unlock_buffer(bh); 3816ac27a0ecSDave Kleikamp goto has_buffer; 3817ac27a0ecSDave Kleikamp } 3818ac27a0ecSDave Kleikamp } 3819ac27a0ecSDave Kleikamp 3820ac27a0ecSDave Kleikamp make_io: 3821ac27a0ecSDave Kleikamp /* 3822240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 3823240799cdSTheodore Ts'o * blocks from the inode table. 3824240799cdSTheodore Ts'o */ 3825240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 3826240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 3827240799cdSTheodore Ts'o unsigned num; 3828240799cdSTheodore Ts'o 3829240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 3830b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 3831240799cdSTheodore Ts'o b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1); 3832240799cdSTheodore Ts'o if (table > b) 3833240799cdSTheodore Ts'o b = table; 3834240799cdSTheodore Ts'o end = b + EXT4_SB(sb)->s_inode_readahead_blks; 3835240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 3836feb0ab32SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) 3837560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 3838240799cdSTheodore Ts'o table += num / inodes_per_block; 3839240799cdSTheodore Ts'o if (end > table) 3840240799cdSTheodore Ts'o end = table; 3841240799cdSTheodore Ts'o while (b <= end) 3842240799cdSTheodore Ts'o sb_breadahead(sb, b++); 3843240799cdSTheodore Ts'o } 3844240799cdSTheodore Ts'o 3845240799cdSTheodore Ts'o /* 3846ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 3847ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 3848ac27a0ecSDave Kleikamp * Read the block from disk. 3849ac27a0ecSDave Kleikamp */ 38500562e0baSJiaying Zhang trace_ext4_load_inode(inode); 3851ac27a0ecSDave Kleikamp get_bh(bh); 3852ac27a0ecSDave Kleikamp bh->b_end_io = end_buffer_read_sync; 385365299a3bSChristoph Hellwig submit_bh(READ | REQ_META | REQ_PRIO, bh); 3854ac27a0ecSDave Kleikamp wait_on_buffer(bh); 3855ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 3856c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, block, 3857c398eda0STheodore Ts'o "unable to read itable block"); 3858ac27a0ecSDave Kleikamp brelse(bh); 3859ac27a0ecSDave Kleikamp return -EIO; 3860ac27a0ecSDave Kleikamp } 3861ac27a0ecSDave Kleikamp } 3862ac27a0ecSDave Kleikamp has_buffer: 3863ac27a0ecSDave Kleikamp iloc->bh = bh; 3864ac27a0ecSDave Kleikamp return 0; 3865ac27a0ecSDave Kleikamp } 3866ac27a0ecSDave Kleikamp 3867617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 3868ac27a0ecSDave Kleikamp { 3869ac27a0ecSDave Kleikamp /* We have all inode data except xattrs in memory here. */ 3870617ba13bSMingming Cao return __ext4_get_inode_loc(inode, iloc, 387119f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_XATTR)); 3872ac27a0ecSDave Kleikamp } 3873ac27a0ecSDave Kleikamp 3874617ba13bSMingming Cao void ext4_set_inode_flags(struct inode *inode) 3875ac27a0ecSDave Kleikamp { 3876617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 3877ac27a0ecSDave Kleikamp 3878ac27a0ecSDave Kleikamp inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); 3879617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 3880ac27a0ecSDave Kleikamp inode->i_flags |= S_SYNC; 3881617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 3882ac27a0ecSDave Kleikamp inode->i_flags |= S_APPEND; 3883617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 3884ac27a0ecSDave Kleikamp inode->i_flags |= S_IMMUTABLE; 3885617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 3886ac27a0ecSDave Kleikamp inode->i_flags |= S_NOATIME; 3887617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 3888ac27a0ecSDave Kleikamp inode->i_flags |= S_DIRSYNC; 3889ac27a0ecSDave Kleikamp } 3890ac27a0ecSDave Kleikamp 3891ff9ddf7eSJan Kara /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */ 3892ff9ddf7eSJan Kara void ext4_get_inode_flags(struct ext4_inode_info *ei) 3893ff9ddf7eSJan Kara { 389484a8dce2SDmitry Monakhov unsigned int vfs_fl; 389584a8dce2SDmitry Monakhov unsigned long old_fl, new_fl; 3896ff9ddf7eSJan Kara 389784a8dce2SDmitry Monakhov do { 389884a8dce2SDmitry Monakhov vfs_fl = ei->vfs_inode.i_flags; 389984a8dce2SDmitry Monakhov old_fl = ei->i_flags; 390084a8dce2SDmitry Monakhov new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL| 390184a8dce2SDmitry Monakhov EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL| 390284a8dce2SDmitry Monakhov EXT4_DIRSYNC_FL); 390384a8dce2SDmitry Monakhov if (vfs_fl & S_SYNC) 390484a8dce2SDmitry Monakhov new_fl |= EXT4_SYNC_FL; 390584a8dce2SDmitry Monakhov if (vfs_fl & S_APPEND) 390684a8dce2SDmitry Monakhov new_fl |= EXT4_APPEND_FL; 390784a8dce2SDmitry Monakhov if (vfs_fl & S_IMMUTABLE) 390884a8dce2SDmitry Monakhov new_fl |= EXT4_IMMUTABLE_FL; 390984a8dce2SDmitry Monakhov if (vfs_fl & S_NOATIME) 391084a8dce2SDmitry Monakhov new_fl |= EXT4_NOATIME_FL; 391184a8dce2SDmitry Monakhov if (vfs_fl & S_DIRSYNC) 391284a8dce2SDmitry Monakhov new_fl |= EXT4_DIRSYNC_FL; 391384a8dce2SDmitry Monakhov } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl); 3914ff9ddf7eSJan Kara } 3915de9a55b8STheodore Ts'o 39160fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 39170fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 39180fc1b451SAneesh Kumar K.V { 39190fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 39208180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 39218180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 39220fc1b451SAneesh Kumar K.V 39230fc1b451SAneesh Kumar K.V if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 39240fc1b451SAneesh Kumar K.V EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) { 39250fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 39260fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 39270fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 392807a03824STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) { 39298180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 39308180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 39318180a562SAneesh Kumar K.V } else { 39320fc1b451SAneesh Kumar K.V return i_blocks; 39338180a562SAneesh Kumar K.V } 39340fc1b451SAneesh Kumar K.V } else { 39350fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 39360fc1b451SAneesh Kumar K.V } 39370fc1b451SAneesh Kumar K.V } 3938ff9ddf7eSJan Kara 3939152a7b0aSTao Ma static inline void ext4_iget_extra_inode(struct inode *inode, 3940152a7b0aSTao Ma struct ext4_inode *raw_inode, 3941152a7b0aSTao Ma struct ext4_inode_info *ei) 3942152a7b0aSTao Ma { 3943152a7b0aSTao Ma __le32 *magic = (void *)raw_inode + 3944152a7b0aSTao Ma EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize; 394567cf5b09STao Ma if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) { 3946152a7b0aSTao Ma ext4_set_inode_state(inode, EXT4_STATE_XATTR); 394767cf5b09STao Ma ext4_find_inline_data_nolock(inode); 3948f19d5870STao Ma } else 3949f19d5870STao Ma EXT4_I(inode)->i_inline_off = 0; 3950152a7b0aSTao Ma } 3951152a7b0aSTao Ma 39521d1fe1eeSDavid Howells struct inode *ext4_iget(struct super_block *sb, unsigned long ino) 3953ac27a0ecSDave Kleikamp { 3954617ba13bSMingming Cao struct ext4_iloc iloc; 3955617ba13bSMingming Cao struct ext4_inode *raw_inode; 39561d1fe1eeSDavid Howells struct ext4_inode_info *ei; 39571d1fe1eeSDavid Howells struct inode *inode; 3958b436b9beSJan Kara journal_t *journal = EXT4_SB(sb)->s_journal; 39591d1fe1eeSDavid Howells long ret; 3960ac27a0ecSDave Kleikamp int block; 396108cefc7aSEric W. Biederman uid_t i_uid; 396208cefc7aSEric W. Biederman gid_t i_gid; 3963ac27a0ecSDave Kleikamp 39641d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 39651d1fe1eeSDavid Howells if (!inode) 39661d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 39671d1fe1eeSDavid Howells if (!(inode->i_state & I_NEW)) 39681d1fe1eeSDavid Howells return inode; 39691d1fe1eeSDavid Howells 39701d1fe1eeSDavid Howells ei = EXT4_I(inode); 39717dc57615SPeter Huewe iloc.bh = NULL; 3972ac27a0ecSDave Kleikamp 39731d1fe1eeSDavid Howells ret = __ext4_get_inode_loc(inode, &iloc, 0); 39741d1fe1eeSDavid Howells if (ret < 0) 3975ac27a0ecSDave Kleikamp goto bad_inode; 3976617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 3977814525f4SDarrick J. Wong 3978814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 3979814525f4SDarrick J. Wong ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 3980814525f4SDarrick J. Wong if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 3981814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)) { 3982814525f4SDarrick J. Wong EXT4_ERROR_INODE(inode, "bad extra_isize (%u != %u)", 3983814525f4SDarrick J. Wong EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize, 3984814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)); 3985814525f4SDarrick J. Wong ret = -EIO; 3986814525f4SDarrick J. Wong goto bad_inode; 3987814525f4SDarrick J. Wong } 3988814525f4SDarrick J. Wong } else 3989814525f4SDarrick J. Wong ei->i_extra_isize = 0; 3990814525f4SDarrick J. Wong 3991814525f4SDarrick J. Wong /* Precompute checksum seed for inode metadata */ 3992814525f4SDarrick J. Wong if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 3993814525f4SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) { 3994814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3995814525f4SDarrick J. Wong __u32 csum; 3996814525f4SDarrick J. Wong __le32 inum = cpu_to_le32(inode->i_ino); 3997814525f4SDarrick J. Wong __le32 gen = raw_inode->i_generation; 3998814525f4SDarrick J. Wong csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, 3999814525f4SDarrick J. Wong sizeof(inum)); 4000814525f4SDarrick J. Wong ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, 4001814525f4SDarrick J. Wong sizeof(gen)); 4002814525f4SDarrick J. Wong } 4003814525f4SDarrick J. Wong 4004814525f4SDarrick J. Wong if (!ext4_inode_csum_verify(inode, raw_inode, ei)) { 4005814525f4SDarrick J. Wong EXT4_ERROR_INODE(inode, "checksum invalid"); 4006814525f4SDarrick J. Wong ret = -EIO; 4007814525f4SDarrick J. Wong goto bad_inode; 4008814525f4SDarrick J. Wong } 4009814525f4SDarrick J. Wong 4010ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 401108cefc7aSEric W. Biederman i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 401208cefc7aSEric W. Biederman i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 4013ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 401408cefc7aSEric W. Biederman i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 401508cefc7aSEric W. Biederman i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 4016ac27a0ecSDave Kleikamp } 401708cefc7aSEric W. Biederman i_uid_write(inode, i_uid); 401808cefc7aSEric W. Biederman i_gid_write(inode, i_gid); 4019bfe86848SMiklos Szeredi set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); 4020ac27a0ecSDave Kleikamp 4021353eb83cSTheodore Ts'o ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 402267cf5b09STao Ma ei->i_inline_off = 0; 4023ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 4024ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 4025ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 4026ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 4027ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 4028ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 4029ac27a0ecSDave Kleikamp */ 4030ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 4031ac27a0ecSDave Kleikamp if (inode->i_mode == 0 || 4032617ba13bSMingming Cao !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) { 4033ac27a0ecSDave Kleikamp /* this inode is deleted */ 40341d1fe1eeSDavid Howells ret = -ESTALE; 4035ac27a0ecSDave Kleikamp goto bad_inode; 4036ac27a0ecSDave Kleikamp } 4037ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 4038ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 4039ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 4040ac27a0ecSDave Kleikamp * the process of deleting those. */ 4041ac27a0ecSDave Kleikamp } 4042ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 40430fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 40447973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 4045a9e81742STheodore Ts'o if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) 4046a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 4047a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 4048a48380f7SAneesh Kumar K.V inode->i_size = ext4_isize(raw_inode); 4049ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 4050a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 4051a9e7f447SDmitry Monakhov ei->i_reserved_quota = 0; 4052a9e7f447SDmitry Monakhov #endif 4053ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 4054ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 4055a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 4056ac27a0ecSDave Kleikamp /* 4057ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 4058ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 4059ac27a0ecSDave Kleikamp */ 4060617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 4061ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 4062ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 4063ac27a0ecSDave Kleikamp 4064b436b9beSJan Kara /* 4065b436b9beSJan Kara * Set transaction id's of transactions that have to be committed 4066b436b9beSJan Kara * to finish f[data]sync. We set them to currently running transaction 4067b436b9beSJan Kara * as we cannot be sure that the inode or some of its metadata isn't 4068b436b9beSJan Kara * part of the transaction - the inode could have been reclaimed and 4069b436b9beSJan Kara * now it is reread from disk. 4070b436b9beSJan Kara */ 4071b436b9beSJan Kara if (journal) { 4072b436b9beSJan Kara transaction_t *transaction; 4073b436b9beSJan Kara tid_t tid; 4074b436b9beSJan Kara 4075a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 4076b436b9beSJan Kara if (journal->j_running_transaction) 4077b436b9beSJan Kara transaction = journal->j_running_transaction; 4078b436b9beSJan Kara else 4079b436b9beSJan Kara transaction = journal->j_committing_transaction; 4080b436b9beSJan Kara if (transaction) 4081b436b9beSJan Kara tid = transaction->t_tid; 4082b436b9beSJan Kara else 4083b436b9beSJan Kara tid = journal->j_commit_sequence; 4084a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 4085b436b9beSJan Kara ei->i_sync_tid = tid; 4086b436b9beSJan Kara ei->i_datasync_tid = tid; 4087b436b9beSJan Kara } 4088b436b9beSJan Kara 40890040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4090ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 4091ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 4092617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 4093617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 4094ac27a0ecSDave Kleikamp } else { 4095152a7b0aSTao Ma ext4_iget_extra_inode(inode, raw_inode, ei); 4096ac27a0ecSDave Kleikamp } 4097814525f4SDarrick J. Wong } 4098ac27a0ecSDave Kleikamp 4099ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 4100ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 4101ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 4102ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 4103ef7f3835SKalpak Shah 410425ec56b5SJean Noel Cordenner inode->i_version = le32_to_cpu(raw_inode->i_disk_version); 410525ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 410625ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 410725ec56b5SJean Noel Cordenner inode->i_version |= 410825ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 410925ec56b5SJean Noel Cordenner } 411025ec56b5SJean Noel Cordenner 4111c4b5a614STheodore Ts'o ret = 0; 4112485c26ecSTheodore Ts'o if (ei->i_file_acl && 41131032988cSTheodore Ts'o !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) { 411424676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "bad extended attribute block %llu", 411524676da4STheodore Ts'o ei->i_file_acl); 4116485c26ecSTheodore Ts'o ret = -EIO; 4117485c26ecSTheodore Ts'o goto bad_inode; 4118f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 4119f19d5870STao Ma if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 4120f19d5870STao Ma if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 4121c4b5a614STheodore Ts'o (S_ISLNK(inode->i_mode) && 4122f19d5870STao Ma !ext4_inode_is_fast_symlink(inode)))) 41237a262f7cSAneesh Kumar K.V /* Validate extent which is part of inode */ 41247a262f7cSAneesh Kumar K.V ret = ext4_ext_check_inode(inode); 4125fe2c8191SThiemo Nagel } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 4126fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 4127fe2c8191SThiemo Nagel !ext4_inode_is_fast_symlink(inode))) { 4128fe2c8191SThiemo Nagel /* Validate block references which are part of inode */ 41291f7d1e77STheodore Ts'o ret = ext4_ind_check_inode(inode); 4130fe2c8191SThiemo Nagel } 4131f19d5870STao Ma } 4132567f3e9aSTheodore Ts'o if (ret) 41337a262f7cSAneesh Kumar K.V goto bad_inode; 41347a262f7cSAneesh Kumar K.V 4135ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 4136617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 4137617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 4138617ba13bSMingming Cao ext4_set_aops(inode); 4139ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 4140617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 4141617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 4142ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 4143e83c1397SDuane Griffin if (ext4_inode_is_fast_symlink(inode)) { 4144617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 4145e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 4146e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 4147e83c1397SDuane Griffin } else { 4148617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 4149617ba13bSMingming Cao ext4_set_aops(inode); 4150ac27a0ecSDave Kleikamp } 4151563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 4152563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 4153617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 4154ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 4155ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4156ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 4157ac27a0ecSDave Kleikamp else 4158ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4159ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 4160563bdd61STheodore Ts'o } else { 4161563bdd61STheodore Ts'o ret = -EIO; 416224676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode); 4163563bdd61STheodore Ts'o goto bad_inode; 4164ac27a0ecSDave Kleikamp } 4165ac27a0ecSDave Kleikamp brelse(iloc.bh); 4166617ba13bSMingming Cao ext4_set_inode_flags(inode); 41671d1fe1eeSDavid Howells unlock_new_inode(inode); 41681d1fe1eeSDavid Howells return inode; 4169ac27a0ecSDave Kleikamp 4170ac27a0ecSDave Kleikamp bad_inode: 4171567f3e9aSTheodore Ts'o brelse(iloc.bh); 41721d1fe1eeSDavid Howells iget_failed(inode); 41731d1fe1eeSDavid Howells return ERR_PTR(ret); 4174ac27a0ecSDave Kleikamp } 4175ac27a0ecSDave Kleikamp 41760fc1b451SAneesh Kumar K.V static int ext4_inode_blocks_set(handle_t *handle, 41770fc1b451SAneesh Kumar K.V struct ext4_inode *raw_inode, 41780fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 41790fc1b451SAneesh Kumar K.V { 41800fc1b451SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 41810fc1b451SAneesh Kumar K.V u64 i_blocks = inode->i_blocks; 41820fc1b451SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 41830fc1b451SAneesh Kumar K.V 41840fc1b451SAneesh Kumar K.V if (i_blocks <= ~0U) { 41850fc1b451SAneesh Kumar K.V /* 41864907cb7bSAnatol Pomozov * i_blocks can be represented in a 32 bit variable 41870fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 41880fc1b451SAneesh Kumar K.V */ 41898180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 41900fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = 0; 419184a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 4192f287a1a5STheodore Ts'o return 0; 4193f287a1a5STheodore Ts'o } 4194f287a1a5STheodore Ts'o if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) 4195f287a1a5STheodore Ts'o return -EFBIG; 4196f287a1a5STheodore Ts'o 4197f287a1a5STheodore Ts'o if (i_blocks <= 0xffffffffffffULL) { 41980fc1b451SAneesh Kumar K.V /* 41990fc1b451SAneesh Kumar K.V * i_blocks can be represented in a 48 bit variable 42000fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 42010fc1b451SAneesh Kumar K.V */ 42028180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 42030fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 420484a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 42050fc1b451SAneesh Kumar K.V } else { 420684a8dce2SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE); 42078180a562SAneesh Kumar K.V /* i_block is stored in file system block size */ 42088180a562SAneesh Kumar K.V i_blocks = i_blocks >> (inode->i_blkbits - 9); 42098180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 42108180a562SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 42110fc1b451SAneesh Kumar K.V } 4212f287a1a5STheodore Ts'o return 0; 42130fc1b451SAneesh Kumar K.V } 42140fc1b451SAneesh Kumar K.V 4215ac27a0ecSDave Kleikamp /* 4216ac27a0ecSDave Kleikamp * Post the struct inode info into an on-disk inode location in the 4217ac27a0ecSDave Kleikamp * buffer-cache. This gobbles the caller's reference to the 4218ac27a0ecSDave Kleikamp * buffer_head in the inode location struct. 4219ac27a0ecSDave Kleikamp * 4220ac27a0ecSDave Kleikamp * The caller must have write access to iloc->bh. 4221ac27a0ecSDave Kleikamp */ 4222617ba13bSMingming Cao static int ext4_do_update_inode(handle_t *handle, 4223ac27a0ecSDave Kleikamp struct inode *inode, 4224830156c7SFrank Mayhar struct ext4_iloc *iloc) 4225ac27a0ecSDave Kleikamp { 4226617ba13bSMingming Cao struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 4227617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 4228ac27a0ecSDave Kleikamp struct buffer_head *bh = iloc->bh; 4229ac27a0ecSDave Kleikamp int err = 0, rc, block; 4230b71fc079SJan Kara int need_datasync = 0; 423108cefc7aSEric W. Biederman uid_t i_uid; 423208cefc7aSEric W. Biederman gid_t i_gid; 4233ac27a0ecSDave Kleikamp 4234ac27a0ecSDave Kleikamp /* For fields not not tracking in the in-memory inode, 4235ac27a0ecSDave Kleikamp * initialise them to zero for new inodes. */ 423619f5fb7aSTheodore Ts'o if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) 4237617ba13bSMingming Cao memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 4238ac27a0ecSDave Kleikamp 4239ff9ddf7eSJan Kara ext4_get_inode_flags(ei); 4240ac27a0ecSDave Kleikamp raw_inode->i_mode = cpu_to_le16(inode->i_mode); 424108cefc7aSEric W. Biederman i_uid = i_uid_read(inode); 424208cefc7aSEric W. Biederman i_gid = i_gid_read(inode); 4243ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 424408cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid)); 424508cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid)); 4246ac27a0ecSDave Kleikamp /* 4247ac27a0ecSDave Kleikamp * Fix up interoperability with old kernels. Otherwise, old inodes get 4248ac27a0ecSDave Kleikamp * re-used with the upper 16 bits of the uid/gid intact 4249ac27a0ecSDave Kleikamp */ 4250ac27a0ecSDave Kleikamp if (!ei->i_dtime) { 4251ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 425208cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_uid)); 4253ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 425408cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_gid)); 4255ac27a0ecSDave Kleikamp } else { 4256ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4257ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4258ac27a0ecSDave Kleikamp } 4259ac27a0ecSDave Kleikamp } else { 426008cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid)); 426108cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid)); 4262ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4263ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4264ac27a0ecSDave Kleikamp } 4265ac27a0ecSDave Kleikamp raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 4266ef7f3835SKalpak Shah 4267ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 4268ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 4269ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 4270ef7f3835SKalpak Shah EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 4271ef7f3835SKalpak Shah 42720fc1b451SAneesh Kumar K.V if (ext4_inode_blocks_set(handle, raw_inode, ei)) 42730fc1b451SAneesh Kumar K.V goto out_brelse; 4274ac27a0ecSDave Kleikamp raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 4275353eb83cSTheodore Ts'o raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF); 42769b8f1f01SMingming Cao if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 42779b8f1f01SMingming Cao cpu_to_le32(EXT4_OS_HURD)) 4278a1ddeb7eSBadari Pulavarty raw_inode->i_file_acl_high = 4279a1ddeb7eSBadari Pulavarty cpu_to_le16(ei->i_file_acl >> 32); 42807973c0c1SAneesh Kumar K.V raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 4281b71fc079SJan Kara if (ei->i_disksize != ext4_isize(raw_inode)) { 4282a48380f7SAneesh Kumar K.V ext4_isize_set(raw_inode, ei->i_disksize); 4283b71fc079SJan Kara need_datasync = 1; 4284b71fc079SJan Kara } 4285ac27a0ecSDave Kleikamp if (ei->i_disksize > 0x7fffffffULL) { 4286ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 4287617ba13bSMingming Cao if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, 4288617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE) || 4289617ba13bSMingming Cao EXT4_SB(sb)->s_es->s_rev_level == 4290617ba13bSMingming Cao cpu_to_le32(EXT4_GOOD_OLD_REV)) { 4291ac27a0ecSDave Kleikamp /* If this is the first large file 4292ac27a0ecSDave Kleikamp * created, add a flag to the superblock. 4293ac27a0ecSDave Kleikamp */ 4294617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, 4295617ba13bSMingming Cao EXT4_SB(sb)->s_sbh); 4296ac27a0ecSDave Kleikamp if (err) 4297ac27a0ecSDave Kleikamp goto out_brelse; 4298617ba13bSMingming Cao ext4_update_dynamic_rev(sb); 4299617ba13bSMingming Cao EXT4_SET_RO_COMPAT_FEATURE(sb, 4300617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE); 43010390131bSFrank Mayhar ext4_handle_sync(handle); 4302b50924c2SArtem Bityutskiy err = ext4_handle_dirty_super(handle, sb); 4303ac27a0ecSDave Kleikamp } 4304ac27a0ecSDave Kleikamp } 4305ac27a0ecSDave Kleikamp raw_inode->i_generation = cpu_to_le32(inode->i_generation); 4306ac27a0ecSDave Kleikamp if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 4307ac27a0ecSDave Kleikamp if (old_valid_dev(inode->i_rdev)) { 4308ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 4309ac27a0ecSDave Kleikamp cpu_to_le32(old_encode_dev(inode->i_rdev)); 4310ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 0; 4311ac27a0ecSDave Kleikamp } else { 4312ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 0; 4313ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 4314ac27a0ecSDave Kleikamp cpu_to_le32(new_encode_dev(inode->i_rdev)); 4315ac27a0ecSDave Kleikamp raw_inode->i_block[2] = 0; 4316ac27a0ecSDave Kleikamp } 4317f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 4318de9a55b8STheodore Ts'o for (block = 0; block < EXT4_N_BLOCKS; block++) 4319ac27a0ecSDave Kleikamp raw_inode->i_block[block] = ei->i_data[block]; 4320f19d5870STao Ma } 4321ac27a0ecSDave Kleikamp 432225ec56b5SJean Noel Cordenner raw_inode->i_disk_version = cpu_to_le32(inode->i_version); 432325ec56b5SJean Noel Cordenner if (ei->i_extra_isize) { 432425ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 432525ec56b5SJean Noel Cordenner raw_inode->i_version_hi = 432625ec56b5SJean Noel Cordenner cpu_to_le32(inode->i_version >> 32); 4327ac27a0ecSDave Kleikamp raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize); 432825ec56b5SJean Noel Cordenner } 432925ec56b5SJean Noel Cordenner 4330814525f4SDarrick J. Wong ext4_inode_csum_set(inode, raw_inode, ei); 4331814525f4SDarrick J. Wong 43320390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 433373b50c1cSCurt Wohlgemuth rc = ext4_handle_dirty_metadata(handle, NULL, bh); 4334ac27a0ecSDave Kleikamp if (!err) 4335ac27a0ecSDave Kleikamp err = rc; 433619f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_NEW); 4337ac27a0ecSDave Kleikamp 4338b71fc079SJan Kara ext4_update_inode_fsync_trans(handle, inode, need_datasync); 4339ac27a0ecSDave Kleikamp out_brelse: 4340ac27a0ecSDave Kleikamp brelse(bh); 4341617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4342ac27a0ecSDave Kleikamp return err; 4343ac27a0ecSDave Kleikamp } 4344ac27a0ecSDave Kleikamp 4345ac27a0ecSDave Kleikamp /* 4346617ba13bSMingming Cao * ext4_write_inode() 4347ac27a0ecSDave Kleikamp * 4348ac27a0ecSDave Kleikamp * We are called from a few places: 4349ac27a0ecSDave Kleikamp * 4350ac27a0ecSDave Kleikamp * - Within generic_file_write() for O_SYNC files. 4351ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 43524907cb7bSAnatol Pomozov * transaction to commit. 4353ac27a0ecSDave Kleikamp * 4354ac27a0ecSDave Kleikamp * - Within sys_sync(), kupdate and such. 4355ac27a0ecSDave Kleikamp * We wait on commit, if tol to. 4356ac27a0ecSDave Kleikamp * 4357ac27a0ecSDave Kleikamp * - Within prune_icache() (PF_MEMALLOC == true) 4358ac27a0ecSDave Kleikamp * Here we simply return. We can't afford to block kswapd on the 4359ac27a0ecSDave Kleikamp * journal commit. 4360ac27a0ecSDave Kleikamp * 4361ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 4362ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 4363617ba13bSMingming Cao * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for 4364ac27a0ecSDave Kleikamp * knfsd. 4365ac27a0ecSDave Kleikamp * 4366ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 4367ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 4368ac27a0ecSDave Kleikamp * which we are interested. 4369ac27a0ecSDave Kleikamp * 4370ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 4371ac27a0ecSDave Kleikamp * 4372ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 4373ac27a0ecSDave Kleikamp * stuff(); 4374ac27a0ecSDave Kleikamp * inode->i_size = expr; 4375ac27a0ecSDave Kleikamp * 4376ac27a0ecSDave Kleikamp * is in error because a kswapd-driven write_inode() could occur while 4377ac27a0ecSDave Kleikamp * `stuff()' is running, and the new i_size will be lost. Plus the inode 4378ac27a0ecSDave Kleikamp * will no longer be on the superblock's dirty inode list. 4379ac27a0ecSDave Kleikamp */ 4380a9185b41SChristoph Hellwig int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) 4381ac27a0ecSDave Kleikamp { 438291ac6f43SFrank Mayhar int err; 438391ac6f43SFrank Mayhar 4384ac27a0ecSDave Kleikamp if (current->flags & PF_MEMALLOC) 4385ac27a0ecSDave Kleikamp return 0; 4386ac27a0ecSDave Kleikamp 438791ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal) { 4388617ba13bSMingming Cao if (ext4_journal_current_handle()) { 4389b38bd33aSMingming Cao jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); 4390ac27a0ecSDave Kleikamp dump_stack(); 4391ac27a0ecSDave Kleikamp return -EIO; 4392ac27a0ecSDave Kleikamp } 4393ac27a0ecSDave Kleikamp 4394a9185b41SChristoph Hellwig if (wbc->sync_mode != WB_SYNC_ALL) 4395ac27a0ecSDave Kleikamp return 0; 4396ac27a0ecSDave Kleikamp 439791ac6f43SFrank Mayhar err = ext4_force_commit(inode->i_sb); 439891ac6f43SFrank Mayhar } else { 439991ac6f43SFrank Mayhar struct ext4_iloc iloc; 440091ac6f43SFrank Mayhar 44018b472d73SCurt Wohlgemuth err = __ext4_get_inode_loc(inode, &iloc, 0); 440291ac6f43SFrank Mayhar if (err) 440391ac6f43SFrank Mayhar return err; 4404a9185b41SChristoph Hellwig if (wbc->sync_mode == WB_SYNC_ALL) 4405830156c7SFrank Mayhar sync_dirty_buffer(iloc.bh); 4406830156c7SFrank Mayhar if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { 4407c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr, 4408c398eda0STheodore Ts'o "IO error syncing inode"); 4409830156c7SFrank Mayhar err = -EIO; 4410830156c7SFrank Mayhar } 4411fd2dd9fbSCurt Wohlgemuth brelse(iloc.bh); 441291ac6f43SFrank Mayhar } 441391ac6f43SFrank Mayhar return err; 4414ac27a0ecSDave Kleikamp } 4415ac27a0ecSDave Kleikamp 4416ac27a0ecSDave Kleikamp /* 441753e87268SJan Kara * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate 441853e87268SJan Kara * buffers that are attached to a page stradding i_size and are undergoing 441953e87268SJan Kara * commit. In that case we have to wait for commit to finish and try again. 442053e87268SJan Kara */ 442153e87268SJan Kara static void ext4_wait_for_tail_page_commit(struct inode *inode) 442253e87268SJan Kara { 442353e87268SJan Kara struct page *page; 442453e87268SJan Kara unsigned offset; 442553e87268SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 442653e87268SJan Kara tid_t commit_tid = 0; 442753e87268SJan Kara int ret; 442853e87268SJan Kara 442953e87268SJan Kara offset = inode->i_size & (PAGE_CACHE_SIZE - 1); 443053e87268SJan Kara /* 443153e87268SJan Kara * All buffers in the last page remain valid? Then there's nothing to 443253e87268SJan Kara * do. We do the check mainly to optimize the common PAGE_CACHE_SIZE == 443353e87268SJan Kara * blocksize case 443453e87268SJan Kara */ 443553e87268SJan Kara if (offset > PAGE_CACHE_SIZE - (1 << inode->i_blkbits)) 443653e87268SJan Kara return; 443753e87268SJan Kara while (1) { 443853e87268SJan Kara page = find_lock_page(inode->i_mapping, 443953e87268SJan Kara inode->i_size >> PAGE_CACHE_SHIFT); 444053e87268SJan Kara if (!page) 444153e87268SJan Kara return; 444253e87268SJan Kara ret = __ext4_journalled_invalidatepage(page, offset); 444353e87268SJan Kara unlock_page(page); 444453e87268SJan Kara page_cache_release(page); 444553e87268SJan Kara if (ret != -EBUSY) 444653e87268SJan Kara return; 444753e87268SJan Kara commit_tid = 0; 444853e87268SJan Kara read_lock(&journal->j_state_lock); 444953e87268SJan Kara if (journal->j_committing_transaction) 445053e87268SJan Kara commit_tid = journal->j_committing_transaction->t_tid; 445153e87268SJan Kara read_unlock(&journal->j_state_lock); 445253e87268SJan Kara if (commit_tid) 445353e87268SJan Kara jbd2_log_wait_commit(journal, commit_tid); 445453e87268SJan Kara } 445553e87268SJan Kara } 445653e87268SJan Kara 445753e87268SJan Kara /* 4458617ba13bSMingming Cao * ext4_setattr() 4459ac27a0ecSDave Kleikamp * 4460ac27a0ecSDave Kleikamp * Called from notify_change. 4461ac27a0ecSDave Kleikamp * 4462ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 4463ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 4464ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 4465ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 4466ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 4467ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 4468ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 4469ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 4470ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 4471ac27a0ecSDave Kleikamp * 4472678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 4473678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 4474678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 4475678aaf48SJan Kara * This way we are sure that all the data written in the previous 4476678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 4477678aaf48SJan Kara * writeback). 4478678aaf48SJan Kara * 4479678aaf48SJan Kara * Called with inode->i_mutex down. 4480ac27a0ecSDave Kleikamp */ 4481617ba13bSMingming Cao int ext4_setattr(struct dentry *dentry, struct iattr *attr) 4482ac27a0ecSDave Kleikamp { 4483ac27a0ecSDave Kleikamp struct inode *inode = dentry->d_inode; 4484ac27a0ecSDave Kleikamp int error, rc = 0; 44853d287de3SDmitry Monakhov int orphan = 0; 4486ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 4487ac27a0ecSDave Kleikamp 4488ac27a0ecSDave Kleikamp error = inode_change_ok(inode, attr); 4489ac27a0ecSDave Kleikamp if (error) 4490ac27a0ecSDave Kleikamp return error; 4491ac27a0ecSDave Kleikamp 449212755627SDmitry Monakhov if (is_quota_modification(inode, attr)) 4493871a2931SChristoph Hellwig dquot_initialize(inode); 449408cefc7aSEric W. Biederman if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) || 449508cefc7aSEric W. Biederman (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) { 4496ac27a0ecSDave Kleikamp handle_t *handle; 4497ac27a0ecSDave Kleikamp 4498ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 4499ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 45009924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 45019924a92aSTheodore Ts'o (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) + 4502194074acSDmitry Monakhov EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3); 4503ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 4504ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 4505ac27a0ecSDave Kleikamp goto err_out; 4506ac27a0ecSDave Kleikamp } 4507b43fa828SChristoph Hellwig error = dquot_transfer(inode, attr); 4508ac27a0ecSDave Kleikamp if (error) { 4509617ba13bSMingming Cao ext4_journal_stop(handle); 4510ac27a0ecSDave Kleikamp return error; 4511ac27a0ecSDave Kleikamp } 4512ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 4513ac27a0ecSDave Kleikamp * one transaction */ 4514ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_UID) 4515ac27a0ecSDave Kleikamp inode->i_uid = attr->ia_uid; 4516ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_GID) 4517ac27a0ecSDave Kleikamp inode->i_gid = attr->ia_gid; 4518617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 4519617ba13bSMingming Cao ext4_journal_stop(handle); 4520ac27a0ecSDave Kleikamp } 4521ac27a0ecSDave Kleikamp 4522e2b46574SEric Sandeen if (attr->ia_valid & ATTR_SIZE) { 4523562c72aaSChristoph Hellwig 452412e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 4525e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4526e2b46574SEric Sandeen 45270c095c7fSTheodore Ts'o if (attr->ia_size > sbi->s_bitmap_maxbytes) 45280c095c7fSTheodore Ts'o return -EFBIG; 4529e2b46574SEric Sandeen } 4530e2b46574SEric Sandeen } 4531e2b46574SEric Sandeen 4532ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode) && 4533c8d46e41SJiaying Zhang attr->ia_valid & ATTR_SIZE && 4534072bd7eaSTheodore Ts'o (attr->ia_size < inode->i_size)) { 4535ac27a0ecSDave Kleikamp handle_t *handle; 4536ac27a0ecSDave Kleikamp 45379924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 3); 4538ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 4539ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 4540ac27a0ecSDave Kleikamp goto err_out; 4541ac27a0ecSDave Kleikamp } 45423d287de3SDmitry Monakhov if (ext4_handle_valid(handle)) { 4543617ba13bSMingming Cao error = ext4_orphan_add(handle, inode); 45443d287de3SDmitry Monakhov orphan = 1; 45453d287de3SDmitry Monakhov } 4546617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 4547617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 4548ac27a0ecSDave Kleikamp if (!error) 4549ac27a0ecSDave Kleikamp error = rc; 4550617ba13bSMingming Cao ext4_journal_stop(handle); 4551678aaf48SJan Kara 4552678aaf48SJan Kara if (ext4_should_order_data(inode)) { 4553678aaf48SJan Kara error = ext4_begin_ordered_truncate(inode, 4554678aaf48SJan Kara attr->ia_size); 4555678aaf48SJan Kara if (error) { 4556678aaf48SJan Kara /* Do as much error cleanup as possible */ 45579924a92aSTheodore Ts'o handle = ext4_journal_start(inode, 45589924a92aSTheodore Ts'o EXT4_HT_INODE, 3); 4559678aaf48SJan Kara if (IS_ERR(handle)) { 4560678aaf48SJan Kara ext4_orphan_del(NULL, inode); 4561678aaf48SJan Kara goto err_out; 4562678aaf48SJan Kara } 4563678aaf48SJan Kara ext4_orphan_del(handle, inode); 45643d287de3SDmitry Monakhov orphan = 0; 4565678aaf48SJan Kara ext4_journal_stop(handle); 4566678aaf48SJan Kara goto err_out; 4567678aaf48SJan Kara } 4568678aaf48SJan Kara } 4569ac27a0ecSDave Kleikamp } 4570ac27a0ecSDave Kleikamp 4571072bd7eaSTheodore Ts'o if (attr->ia_valid & ATTR_SIZE) { 457253e87268SJan Kara if (attr->ia_size != inode->i_size) { 457353e87268SJan Kara loff_t oldsize = inode->i_size; 457453e87268SJan Kara 457553e87268SJan Kara i_size_write(inode, attr->ia_size); 457653e87268SJan Kara /* 457753e87268SJan Kara * Blocks are going to be removed from the inode. Wait 457853e87268SJan Kara * for dio in flight. Temporarily disable 457953e87268SJan Kara * dioread_nolock to prevent livelock. 458053e87268SJan Kara */ 45811b65007eSDmitry Monakhov if (orphan) { 458253e87268SJan Kara if (!ext4_should_journal_data(inode)) { 45831b65007eSDmitry Monakhov ext4_inode_block_unlocked_dio(inode); 45841c9114f9SDmitry Monakhov inode_dio_wait(inode); 45851b65007eSDmitry Monakhov ext4_inode_resume_unlocked_dio(inode); 458653e87268SJan Kara } else 458753e87268SJan Kara ext4_wait_for_tail_page_commit(inode); 45881b65007eSDmitry Monakhov } 458953e87268SJan Kara /* 459053e87268SJan Kara * Truncate pagecache after we've waited for commit 459153e87268SJan Kara * in data=journal mode to make pages freeable. 459253e87268SJan Kara */ 459353e87268SJan Kara truncate_pagecache(inode, oldsize, inode->i_size); 45941c9114f9SDmitry Monakhov } 4595072bd7eaSTheodore Ts'o ext4_truncate(inode); 4596072bd7eaSTheodore Ts'o } 4597ac27a0ecSDave Kleikamp 45981025774cSChristoph Hellwig if (!rc) { 45991025774cSChristoph Hellwig setattr_copy(inode, attr); 46001025774cSChristoph Hellwig mark_inode_dirty(inode); 46011025774cSChristoph Hellwig } 46021025774cSChristoph Hellwig 46031025774cSChristoph Hellwig /* 46041025774cSChristoph Hellwig * If the call to ext4_truncate failed to get a transaction handle at 46051025774cSChristoph Hellwig * all, we need to clean up the in-core orphan list manually. 46061025774cSChristoph Hellwig */ 46073d287de3SDmitry Monakhov if (orphan && inode->i_nlink) 4608617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 4609ac27a0ecSDave Kleikamp 4610ac27a0ecSDave Kleikamp if (!rc && (ia_valid & ATTR_MODE)) 4611617ba13bSMingming Cao rc = ext4_acl_chmod(inode); 4612ac27a0ecSDave Kleikamp 4613ac27a0ecSDave Kleikamp err_out: 4614617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 4615ac27a0ecSDave Kleikamp if (!error) 4616ac27a0ecSDave Kleikamp error = rc; 4617ac27a0ecSDave Kleikamp return error; 4618ac27a0ecSDave Kleikamp } 4619ac27a0ecSDave Kleikamp 46203e3398a0SMingming Cao int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry, 46213e3398a0SMingming Cao struct kstat *stat) 46223e3398a0SMingming Cao { 46233e3398a0SMingming Cao struct inode *inode; 46243e3398a0SMingming Cao unsigned long delalloc_blocks; 46253e3398a0SMingming Cao 46263e3398a0SMingming Cao inode = dentry->d_inode; 46273e3398a0SMingming Cao generic_fillattr(inode, stat); 46283e3398a0SMingming Cao 46293e3398a0SMingming Cao /* 46303e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 46313e3398a0SMingming Cao * otherwise in the case of system crash before the real block 46323e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 46333e3398a0SMingming Cao * on-disk file blocks. 46343e3398a0SMingming Cao * We always keep i_blocks updated together with real 46353e3398a0SMingming Cao * allocation. But to not confuse with user, stat 46363e3398a0SMingming Cao * will return the blocks that include the delayed allocation 46373e3398a0SMingming Cao * blocks for this file. 46383e3398a0SMingming Cao */ 463996607551STao Ma delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb), 464096607551STao Ma EXT4_I(inode)->i_reserved_data_blocks); 46413e3398a0SMingming Cao 46423e3398a0SMingming Cao stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9; 46433e3398a0SMingming Cao return 0; 46443e3398a0SMingming Cao } 4645ac27a0ecSDave Kleikamp 4646a02908f1SMingming Cao static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4647a02908f1SMingming Cao { 464812e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 46498bb2b247SAmir Goldstein return ext4_ind_trans_blocks(inode, nrblocks, chunk); 4650ac51d837STheodore Ts'o return ext4_ext_index_trans_blocks(inode, nrblocks, chunk); 4651a02908f1SMingming Cao } 4652ac51d837STheodore Ts'o 4653a02908f1SMingming Cao /* 4654a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 4655a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 4656a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 4657a02908f1SMingming Cao * 4658a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 46594907cb7bSAnatol Pomozov * different block groups too. If they are contiguous, with flexbg, 4660a02908f1SMingming Cao * they could still across block group boundary. 4661a02908f1SMingming Cao * 4662a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 4663a02908f1SMingming Cao */ 46641f109d5aSTheodore Ts'o static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4665a02908f1SMingming Cao { 46668df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 46678df9675fSTheodore Ts'o int gdpblocks; 4668a02908f1SMingming Cao int idxblocks; 4669a02908f1SMingming Cao int ret = 0; 4670a02908f1SMingming Cao 4671a02908f1SMingming Cao /* 4672a02908f1SMingming Cao * How many index blocks need to touch to modify nrblocks? 4673a02908f1SMingming Cao * The "Chunk" flag indicating whether the nrblocks is 4674a02908f1SMingming Cao * physically contiguous on disk 4675a02908f1SMingming Cao * 4676a02908f1SMingming Cao * For Direct IO and fallocate, they calls get_block to allocate 4677a02908f1SMingming Cao * one single extent at a time, so they could set the "Chunk" flag 4678a02908f1SMingming Cao */ 4679a02908f1SMingming Cao idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk); 4680a02908f1SMingming Cao 4681a02908f1SMingming Cao ret = idxblocks; 4682a02908f1SMingming Cao 4683a02908f1SMingming Cao /* 4684a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 4685a02908f1SMingming Cao * to account 4686a02908f1SMingming Cao */ 4687a02908f1SMingming Cao groups = idxblocks; 4688a02908f1SMingming Cao if (chunk) 4689a02908f1SMingming Cao groups += 1; 4690ac27a0ecSDave Kleikamp else 4691a02908f1SMingming Cao groups += nrblocks; 4692ac27a0ecSDave Kleikamp 4693a02908f1SMingming Cao gdpblocks = groups; 46948df9675fSTheodore Ts'o if (groups > ngroups) 46958df9675fSTheodore Ts'o groups = ngroups; 4696a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 4697a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 4698a02908f1SMingming Cao 4699a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 4700a02908f1SMingming Cao ret += groups + gdpblocks; 4701a02908f1SMingming Cao 4702a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 4703a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 4704ac27a0ecSDave Kleikamp 4705ac27a0ecSDave Kleikamp return ret; 4706ac27a0ecSDave Kleikamp } 4707ac27a0ecSDave Kleikamp 4708ac27a0ecSDave Kleikamp /* 470925985edcSLucas De Marchi * Calculate the total number of credits to reserve to fit 4710f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 4711f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 4712a02908f1SMingming Cao * 4713525f4ed8SMingming Cao * This could be called via ext4_write_begin() 4714a02908f1SMingming Cao * 4715525f4ed8SMingming Cao * We need to consider the worse case, when 4716a02908f1SMingming Cao * one new block per extent. 4717a02908f1SMingming Cao */ 4718a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 4719a02908f1SMingming Cao { 4720a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 4721a02908f1SMingming Cao int ret; 4722a02908f1SMingming Cao 4723a02908f1SMingming Cao ret = ext4_meta_trans_blocks(inode, bpp, 0); 4724a02908f1SMingming Cao 4725a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 4726a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 4727a02908f1SMingming Cao ret += bpp; 4728a02908f1SMingming Cao return ret; 4729a02908f1SMingming Cao } 4730f3bd1f3fSMingming Cao 4731f3bd1f3fSMingming Cao /* 4732f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 4733f3bd1f3fSMingming Cao * 4734f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 473579e83036SEric Sandeen * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks. 4736f3bd1f3fSMingming Cao * 4737f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 4738f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 4739f3bd1f3fSMingming Cao */ 4740f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 4741f3bd1f3fSMingming Cao { 4742f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 4743f3bd1f3fSMingming Cao } 4744f3bd1f3fSMingming Cao 4745a02908f1SMingming Cao /* 4746617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 4747ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 4748ac27a0ecSDave Kleikamp */ 4749617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 4750617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 4751ac27a0ecSDave Kleikamp { 4752ac27a0ecSDave Kleikamp int err = 0; 4753ac27a0ecSDave Kleikamp 4754c64db50eSTheodore Ts'o if (IS_I_VERSION(inode)) 475525ec56b5SJean Noel Cordenner inode_inc_iversion(inode); 475625ec56b5SJean Noel Cordenner 4757ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 4758ac27a0ecSDave Kleikamp get_bh(iloc->bh); 4759ac27a0ecSDave Kleikamp 4760dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 4761830156c7SFrank Mayhar err = ext4_do_update_inode(handle, inode, iloc); 4762ac27a0ecSDave Kleikamp put_bh(iloc->bh); 4763ac27a0ecSDave Kleikamp return err; 4764ac27a0ecSDave Kleikamp } 4765ac27a0ecSDave Kleikamp 4766ac27a0ecSDave Kleikamp /* 4767ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 4768ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 4769ac27a0ecSDave Kleikamp */ 4770ac27a0ecSDave Kleikamp 4771ac27a0ecSDave Kleikamp int 4772617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 4773617ba13bSMingming Cao struct ext4_iloc *iloc) 4774ac27a0ecSDave Kleikamp { 47750390131bSFrank Mayhar int err; 47760390131bSFrank Mayhar 4777617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 4778ac27a0ecSDave Kleikamp if (!err) { 4779ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 4780617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, iloc->bh); 4781ac27a0ecSDave Kleikamp if (err) { 4782ac27a0ecSDave Kleikamp brelse(iloc->bh); 4783ac27a0ecSDave Kleikamp iloc->bh = NULL; 4784ac27a0ecSDave Kleikamp } 4785ac27a0ecSDave Kleikamp } 4786617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4787ac27a0ecSDave Kleikamp return err; 4788ac27a0ecSDave Kleikamp } 4789ac27a0ecSDave Kleikamp 4790ac27a0ecSDave Kleikamp /* 47916dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 47926dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 47936dd4ee7cSKalpak Shah */ 47941d03ec98SAneesh Kumar K.V static int ext4_expand_extra_isize(struct inode *inode, 47951d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 47961d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 47971d03ec98SAneesh Kumar K.V handle_t *handle) 47986dd4ee7cSKalpak Shah { 47996dd4ee7cSKalpak Shah struct ext4_inode *raw_inode; 48006dd4ee7cSKalpak Shah struct ext4_xattr_ibody_header *header; 48016dd4ee7cSKalpak Shah 48026dd4ee7cSKalpak Shah if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) 48036dd4ee7cSKalpak Shah return 0; 48046dd4ee7cSKalpak Shah 48056dd4ee7cSKalpak Shah raw_inode = ext4_raw_inode(&iloc); 48066dd4ee7cSKalpak Shah 48076dd4ee7cSKalpak Shah header = IHDR(inode, raw_inode); 48086dd4ee7cSKalpak Shah 48096dd4ee7cSKalpak Shah /* No extended attributes present */ 481019f5fb7aSTheodore Ts'o if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) || 48116dd4ee7cSKalpak Shah header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 48126dd4ee7cSKalpak Shah memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0, 48136dd4ee7cSKalpak Shah new_extra_isize); 48146dd4ee7cSKalpak Shah EXT4_I(inode)->i_extra_isize = new_extra_isize; 48156dd4ee7cSKalpak Shah return 0; 48166dd4ee7cSKalpak Shah } 48176dd4ee7cSKalpak Shah 48186dd4ee7cSKalpak Shah /* try to expand with EAs present */ 48196dd4ee7cSKalpak Shah return ext4_expand_extra_isize_ea(inode, new_extra_isize, 48206dd4ee7cSKalpak Shah raw_inode, handle); 48216dd4ee7cSKalpak Shah } 48226dd4ee7cSKalpak Shah 48236dd4ee7cSKalpak Shah /* 4824ac27a0ecSDave Kleikamp * What we do here is to mark the in-core inode as clean with respect to inode 4825ac27a0ecSDave Kleikamp * dirtiness (it may still be data-dirty). 4826ac27a0ecSDave Kleikamp * This means that the in-core inode may be reaped by prune_icache 4827ac27a0ecSDave Kleikamp * without having to perform any I/O. This is a very good thing, 4828ac27a0ecSDave Kleikamp * because *any* task may call prune_icache - even ones which 4829ac27a0ecSDave Kleikamp * have a transaction open against a different journal. 4830ac27a0ecSDave Kleikamp * 4831ac27a0ecSDave Kleikamp * Is this cheating? Not really. Sure, we haven't written the 4832ac27a0ecSDave Kleikamp * inode out, but prune_icache isn't a user-visible syncing function. 4833ac27a0ecSDave Kleikamp * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 4834ac27a0ecSDave Kleikamp * we start and wait on commits. 4835ac27a0ecSDave Kleikamp */ 4836617ba13bSMingming Cao int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode) 4837ac27a0ecSDave Kleikamp { 4838617ba13bSMingming Cao struct ext4_iloc iloc; 48396dd4ee7cSKalpak Shah struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 48406dd4ee7cSKalpak Shah static unsigned int mnt_count; 48416dd4ee7cSKalpak Shah int err, ret; 4842ac27a0ecSDave Kleikamp 4843ac27a0ecSDave Kleikamp might_sleep(); 48447ff9c073STheodore Ts'o trace_ext4_mark_inode_dirty(inode, _RET_IP_); 4845617ba13bSMingming Cao err = ext4_reserve_inode_write(handle, inode, &iloc); 48460390131bSFrank Mayhar if (ext4_handle_valid(handle) && 48470390131bSFrank Mayhar EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize && 484819f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) { 48496dd4ee7cSKalpak Shah /* 48506dd4ee7cSKalpak Shah * We need extra buffer credits since we may write into EA block 48516dd4ee7cSKalpak Shah * with this same handle. If journal_extend fails, then it will 48526dd4ee7cSKalpak Shah * only result in a minor loss of functionality for that inode. 48536dd4ee7cSKalpak Shah * If this is felt to be critical, then e2fsck should be run to 48546dd4ee7cSKalpak Shah * force a large enough s_min_extra_isize. 48556dd4ee7cSKalpak Shah */ 48566dd4ee7cSKalpak Shah if ((jbd2_journal_extend(handle, 48576dd4ee7cSKalpak Shah EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) { 48586dd4ee7cSKalpak Shah ret = ext4_expand_extra_isize(inode, 48596dd4ee7cSKalpak Shah sbi->s_want_extra_isize, 48606dd4ee7cSKalpak Shah iloc, handle); 48616dd4ee7cSKalpak Shah if (ret) { 486219f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, 486319f5fb7aSTheodore Ts'o EXT4_STATE_NO_EXPAND); 4864c1bddad9SAneesh Kumar K.V if (mnt_count != 4865c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count)) { 486612062dddSEric Sandeen ext4_warning(inode->i_sb, 48676dd4ee7cSKalpak Shah "Unable to expand inode %lu. Delete" 48686dd4ee7cSKalpak Shah " some EAs or run e2fsck.", 48696dd4ee7cSKalpak Shah inode->i_ino); 4870c1bddad9SAneesh Kumar K.V mnt_count = 4871c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count); 48726dd4ee7cSKalpak Shah } 48736dd4ee7cSKalpak Shah } 48746dd4ee7cSKalpak Shah } 48756dd4ee7cSKalpak Shah } 4876ac27a0ecSDave Kleikamp if (!err) 4877617ba13bSMingming Cao err = ext4_mark_iloc_dirty(handle, inode, &iloc); 4878ac27a0ecSDave Kleikamp return err; 4879ac27a0ecSDave Kleikamp } 4880ac27a0ecSDave Kleikamp 4881ac27a0ecSDave Kleikamp /* 4882617ba13bSMingming Cao * ext4_dirty_inode() is called from __mark_inode_dirty() 4883ac27a0ecSDave Kleikamp * 4884ac27a0ecSDave Kleikamp * We're really interested in the case where a file is being extended. 4885ac27a0ecSDave Kleikamp * i_size has been changed by generic_commit_write() and we thus need 4886ac27a0ecSDave Kleikamp * to include the updated inode in the current transaction. 4887ac27a0ecSDave Kleikamp * 48885dd4056dSChristoph Hellwig * Also, dquot_alloc_block() will always dirty the inode when blocks 4889ac27a0ecSDave Kleikamp * are allocated to the file. 4890ac27a0ecSDave Kleikamp * 4891ac27a0ecSDave Kleikamp * If the inode is marked synchronous, we don't honour that here - doing 4892ac27a0ecSDave Kleikamp * so would cause a commit on atime updates, which we don't bother doing. 4893ac27a0ecSDave Kleikamp * We handle synchronous inodes at the highest possible level. 4894ac27a0ecSDave Kleikamp */ 4895aa385729SChristoph Hellwig void ext4_dirty_inode(struct inode *inode, int flags) 4896ac27a0ecSDave Kleikamp { 4897ac27a0ecSDave Kleikamp handle_t *handle; 4898ac27a0ecSDave Kleikamp 48999924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 2); 4900ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 4901ac27a0ecSDave Kleikamp goto out; 4902f3dc272fSCurt Wohlgemuth 4903617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 4904f3dc272fSCurt Wohlgemuth 4905617ba13bSMingming Cao ext4_journal_stop(handle); 4906ac27a0ecSDave Kleikamp out: 4907ac27a0ecSDave Kleikamp return; 4908ac27a0ecSDave Kleikamp } 4909ac27a0ecSDave Kleikamp 4910ac27a0ecSDave Kleikamp #if 0 4911ac27a0ecSDave Kleikamp /* 4912ac27a0ecSDave Kleikamp * Bind an inode's backing buffer_head into this transaction, to prevent 4913ac27a0ecSDave Kleikamp * it from being flushed to disk early. Unlike 4914617ba13bSMingming Cao * ext4_reserve_inode_write, this leaves behind no bh reference and 4915ac27a0ecSDave Kleikamp * returns no iloc structure, so the caller needs to repeat the iloc 4916ac27a0ecSDave Kleikamp * lookup to mark the inode dirty later. 4917ac27a0ecSDave Kleikamp */ 4918617ba13bSMingming Cao static int ext4_pin_inode(handle_t *handle, struct inode *inode) 4919ac27a0ecSDave Kleikamp { 4920617ba13bSMingming Cao struct ext4_iloc iloc; 4921ac27a0ecSDave Kleikamp 4922ac27a0ecSDave Kleikamp int err = 0; 4923ac27a0ecSDave Kleikamp if (handle) { 4924617ba13bSMingming Cao err = ext4_get_inode_loc(inode, &iloc); 4925ac27a0ecSDave Kleikamp if (!err) { 4926ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc.bh, "get_write_access"); 4927dab291afSMingming Cao err = jbd2_journal_get_write_access(handle, iloc.bh); 4928ac27a0ecSDave Kleikamp if (!err) 49290390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, 493073b50c1cSCurt Wohlgemuth NULL, 4931ac27a0ecSDave Kleikamp iloc.bh); 4932ac27a0ecSDave Kleikamp brelse(iloc.bh); 4933ac27a0ecSDave Kleikamp } 4934ac27a0ecSDave Kleikamp } 4935617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4936ac27a0ecSDave Kleikamp return err; 4937ac27a0ecSDave Kleikamp } 4938ac27a0ecSDave Kleikamp #endif 4939ac27a0ecSDave Kleikamp 4940617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val) 4941ac27a0ecSDave Kleikamp { 4942ac27a0ecSDave Kleikamp journal_t *journal; 4943ac27a0ecSDave Kleikamp handle_t *handle; 4944ac27a0ecSDave Kleikamp int err; 4945ac27a0ecSDave Kleikamp 4946ac27a0ecSDave Kleikamp /* 4947ac27a0ecSDave Kleikamp * We have to be very careful here: changing a data block's 4948ac27a0ecSDave Kleikamp * journaling status dynamically is dangerous. If we write a 4949ac27a0ecSDave Kleikamp * data block to the journal, change the status and then delete 4950ac27a0ecSDave Kleikamp * that block, we risk forgetting to revoke the old log record 4951ac27a0ecSDave Kleikamp * from the journal and so a subsequent replay can corrupt data. 4952ac27a0ecSDave Kleikamp * So, first we make sure that the journal is empty and that 4953ac27a0ecSDave Kleikamp * nobody is changing anything. 4954ac27a0ecSDave Kleikamp */ 4955ac27a0ecSDave Kleikamp 4956617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 49570390131bSFrank Mayhar if (!journal) 49580390131bSFrank Mayhar return 0; 4959d699594dSDave Hansen if (is_journal_aborted(journal)) 4960ac27a0ecSDave Kleikamp return -EROFS; 49612aff57b0SYongqiang Yang /* We have to allocate physical blocks for delalloc blocks 49622aff57b0SYongqiang Yang * before flushing journal. otherwise delalloc blocks can not 49632aff57b0SYongqiang Yang * be allocated any more. even more truncate on delalloc blocks 49642aff57b0SYongqiang Yang * could trigger BUG by flushing delalloc blocks in journal. 49652aff57b0SYongqiang Yang * There is no delalloc block in non-journal data mode. 49662aff57b0SYongqiang Yang */ 49672aff57b0SYongqiang Yang if (val && test_opt(inode->i_sb, DELALLOC)) { 49682aff57b0SYongqiang Yang err = ext4_alloc_da_blocks(inode); 49692aff57b0SYongqiang Yang if (err < 0) 49702aff57b0SYongqiang Yang return err; 49712aff57b0SYongqiang Yang } 4972ac27a0ecSDave Kleikamp 497317335dccSDmitry Monakhov /* Wait for all existing dio workers */ 497417335dccSDmitry Monakhov ext4_inode_block_unlocked_dio(inode); 497517335dccSDmitry Monakhov inode_dio_wait(inode); 497617335dccSDmitry Monakhov 4977dab291afSMingming Cao jbd2_journal_lock_updates(journal); 4978ac27a0ecSDave Kleikamp 4979ac27a0ecSDave Kleikamp /* 4980ac27a0ecSDave Kleikamp * OK, there are no updates running now, and all cached data is 4981ac27a0ecSDave Kleikamp * synced to disk. We are now in a completely consistent state 4982ac27a0ecSDave Kleikamp * which doesn't have anything in the journal, and we know that 4983ac27a0ecSDave Kleikamp * no filesystem updates are running, so it is safe to modify 4984ac27a0ecSDave Kleikamp * the inode's in-core data-journaling state flag now. 4985ac27a0ecSDave Kleikamp */ 4986ac27a0ecSDave Kleikamp 4987ac27a0ecSDave Kleikamp if (val) 498812e9b892SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 49895872ddaaSYongqiang Yang else { 49905872ddaaSYongqiang Yang jbd2_journal_flush(journal); 499112e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 49925872ddaaSYongqiang Yang } 4993617ba13bSMingming Cao ext4_set_aops(inode); 4994ac27a0ecSDave Kleikamp 4995dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 499617335dccSDmitry Monakhov ext4_inode_resume_unlocked_dio(inode); 4997ac27a0ecSDave Kleikamp 4998ac27a0ecSDave Kleikamp /* Finally we can mark the inode as dirty. */ 4999ac27a0ecSDave Kleikamp 50009924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 5001ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5002ac27a0ecSDave Kleikamp return PTR_ERR(handle); 5003ac27a0ecSDave Kleikamp 5004617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 50050390131bSFrank Mayhar ext4_handle_sync(handle); 5006617ba13bSMingming Cao ext4_journal_stop(handle); 5007617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5008ac27a0ecSDave Kleikamp 5009ac27a0ecSDave Kleikamp return err; 5010ac27a0ecSDave Kleikamp } 50112e9ee850SAneesh Kumar K.V 50122e9ee850SAneesh Kumar K.V static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) 50132e9ee850SAneesh Kumar K.V { 50142e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 50152e9ee850SAneesh Kumar K.V } 50162e9ee850SAneesh Kumar K.V 5017c2ec175cSNick Piggin int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) 50182e9ee850SAneesh Kumar K.V { 5019c2ec175cSNick Piggin struct page *page = vmf->page; 50202e9ee850SAneesh Kumar K.V loff_t size; 50212e9ee850SAneesh Kumar K.V unsigned long len; 50229ea7df53SJan Kara int ret; 50232e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 50242e9ee850SAneesh Kumar K.V struct inode *inode = file->f_path.dentry->d_inode; 50252e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 50269ea7df53SJan Kara handle_t *handle; 50279ea7df53SJan Kara get_block_t *get_block; 50289ea7df53SJan Kara int retries = 0; 50292e9ee850SAneesh Kumar K.V 50308e8ad8a5SJan Kara sb_start_pagefault(inode->i_sb); 5031041bbb6dSTheodore Ts'o file_update_time(vma->vm_file); 50329ea7df53SJan Kara /* Delalloc case is easy... */ 50339ea7df53SJan Kara if (test_opt(inode->i_sb, DELALLOC) && 50349ea7df53SJan Kara !ext4_should_journal_data(inode) && 50359ea7df53SJan Kara !ext4_nonda_switch(inode->i_sb)) { 50369ea7df53SJan Kara do { 50379ea7df53SJan Kara ret = __block_page_mkwrite(vma, vmf, 50389ea7df53SJan Kara ext4_da_get_block_prep); 50399ea7df53SJan Kara } while (ret == -ENOSPC && 50409ea7df53SJan Kara ext4_should_retry_alloc(inode->i_sb, &retries)); 50419ea7df53SJan Kara goto out_ret; 50422e9ee850SAneesh Kumar K.V } 50430e499890SDarrick J. Wong 50440e499890SDarrick J. Wong lock_page(page); 50459ea7df53SJan Kara size = i_size_read(inode); 50469ea7df53SJan Kara /* Page got truncated from under us? */ 50479ea7df53SJan Kara if (page->mapping != mapping || page_offset(page) > size) { 50489ea7df53SJan Kara unlock_page(page); 50499ea7df53SJan Kara ret = VM_FAULT_NOPAGE; 50509ea7df53SJan Kara goto out; 50510e499890SDarrick J. Wong } 50522e9ee850SAneesh Kumar K.V 50532e9ee850SAneesh Kumar K.V if (page->index == size >> PAGE_CACHE_SHIFT) 50542e9ee850SAneesh Kumar K.V len = size & ~PAGE_CACHE_MASK; 50552e9ee850SAneesh Kumar K.V else 50562e9ee850SAneesh Kumar K.V len = PAGE_CACHE_SIZE; 5057a827eaffSAneesh Kumar K.V /* 50589ea7df53SJan Kara * Return if we have all the buffers mapped. This avoids the need to do 50599ea7df53SJan Kara * journal_start/journal_stop which can block and take a long time 5060a827eaffSAneesh Kumar K.V */ 50612e9ee850SAneesh Kumar K.V if (page_has_buffers(page)) { 5062f19d5870STao Ma if (!ext4_walk_page_buffers(NULL, page_buffers(page), 5063f19d5870STao Ma 0, len, NULL, 5064a827eaffSAneesh Kumar K.V ext4_bh_unmapped)) { 50659ea7df53SJan Kara /* Wait so that we don't change page under IO */ 50669ea7df53SJan Kara wait_on_page_writeback(page); 50679ea7df53SJan Kara ret = VM_FAULT_LOCKED; 50689ea7df53SJan Kara goto out; 50692e9ee850SAneesh Kumar K.V } 5070a827eaffSAneesh Kumar K.V } 5071a827eaffSAneesh Kumar K.V unlock_page(page); 50729ea7df53SJan Kara /* OK, we need to fill the hole... */ 50739ea7df53SJan Kara if (ext4_should_dioread_nolock(inode)) 50749ea7df53SJan Kara get_block = ext4_get_block_write; 50759ea7df53SJan Kara else 50769ea7df53SJan Kara get_block = ext4_get_block; 50779ea7df53SJan Kara retry_alloc: 50789924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 50799924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 50809ea7df53SJan Kara if (IS_ERR(handle)) { 5081c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 50829ea7df53SJan Kara goto out; 50839ea7df53SJan Kara } 50849ea7df53SJan Kara ret = __block_page_mkwrite(vma, vmf, get_block); 50859ea7df53SJan Kara if (!ret && ext4_should_journal_data(inode)) { 5086f19d5870STao Ma if (ext4_walk_page_buffers(handle, page_buffers(page), 0, 50879ea7df53SJan Kara PAGE_CACHE_SIZE, NULL, do_journal_get_write_access)) { 50889ea7df53SJan Kara unlock_page(page); 50899ea7df53SJan Kara ret = VM_FAULT_SIGBUS; 5090fcbb5515SYongqiang Yang ext4_journal_stop(handle); 50919ea7df53SJan Kara goto out; 50929ea7df53SJan Kara } 50939ea7df53SJan Kara ext4_set_inode_state(inode, EXT4_STATE_JDATA); 50949ea7df53SJan Kara } 50959ea7df53SJan Kara ext4_journal_stop(handle); 50969ea7df53SJan Kara if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 50979ea7df53SJan Kara goto retry_alloc; 50989ea7df53SJan Kara out_ret: 50999ea7df53SJan Kara ret = block_page_mkwrite_return(ret); 51009ea7df53SJan Kara out: 51018e8ad8a5SJan Kara sb_end_pagefault(inode->i_sb); 51022e9ee850SAneesh Kumar K.V return ret; 51032e9ee850SAneesh Kumar K.V } 5104