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> 40a27bb332SKent Overstreet #include <linux/aio.h> 419bffad1eSTheodore Ts'o 423dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 43ac27a0ecSDave Kleikamp #include "xattr.h" 44ac27a0ecSDave Kleikamp #include "acl.h" 459f125d64STheodore Ts'o #include "truncate.h" 46ac27a0ecSDave Kleikamp 479bffad1eSTheodore Ts'o #include <trace/events/ext4.h> 489bffad1eSTheodore Ts'o 49a1d6cc56SAneesh Kumar K.V #define MPAGE_DA_EXTENT_TAIL 0x01 50a1d6cc56SAneesh Kumar K.V 51814525f4SDarrick J. Wong static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw, 52814525f4SDarrick J. Wong struct ext4_inode_info *ei) 53814525f4SDarrick J. Wong { 54814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 55814525f4SDarrick J. Wong __u16 csum_lo; 56814525f4SDarrick J. Wong __u16 csum_hi = 0; 57814525f4SDarrick J. Wong __u32 csum; 58814525f4SDarrick J. Wong 59171a7f21SDmitry Monakhov csum_lo = le16_to_cpu(raw->i_checksum_lo); 60814525f4SDarrick J. Wong raw->i_checksum_lo = 0; 61814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 62814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) { 63171a7f21SDmitry Monakhov csum_hi = le16_to_cpu(raw->i_checksum_hi); 64814525f4SDarrick J. Wong raw->i_checksum_hi = 0; 65814525f4SDarrick J. Wong } 66814525f4SDarrick J. Wong 67814525f4SDarrick J. Wong csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw, 68814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)); 69814525f4SDarrick J. Wong 70171a7f21SDmitry Monakhov raw->i_checksum_lo = cpu_to_le16(csum_lo); 71814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 72814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 73171a7f21SDmitry Monakhov raw->i_checksum_hi = cpu_to_le16(csum_hi); 74814525f4SDarrick J. Wong 75814525f4SDarrick J. Wong return csum; 76814525f4SDarrick J. Wong } 77814525f4SDarrick J. Wong 78814525f4SDarrick J. Wong static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw, 79814525f4SDarrick J. Wong struct ext4_inode_info *ei) 80814525f4SDarrick J. Wong { 81814525f4SDarrick J. Wong __u32 provided, calculated; 82814525f4SDarrick J. Wong 83814525f4SDarrick J. Wong if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 84814525f4SDarrick J. Wong cpu_to_le32(EXT4_OS_LINUX) || 85814525f4SDarrick J. Wong !EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, 86814525f4SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) 87814525f4SDarrick J. Wong return 1; 88814525f4SDarrick J. Wong 89814525f4SDarrick J. Wong provided = le16_to_cpu(raw->i_checksum_lo); 90814525f4SDarrick J. Wong calculated = ext4_inode_csum(inode, raw, ei); 91814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 92814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 93814525f4SDarrick J. Wong provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16; 94814525f4SDarrick J. Wong else 95814525f4SDarrick J. Wong calculated &= 0xFFFF; 96814525f4SDarrick J. Wong 97814525f4SDarrick J. Wong return provided == calculated; 98814525f4SDarrick J. Wong } 99814525f4SDarrick J. Wong 100814525f4SDarrick J. Wong static void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw, 101814525f4SDarrick J. Wong struct ext4_inode_info *ei) 102814525f4SDarrick J. Wong { 103814525f4SDarrick J. Wong __u32 csum; 104814525f4SDarrick J. Wong 105814525f4SDarrick J. Wong if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 106814525f4SDarrick J. Wong cpu_to_le32(EXT4_OS_LINUX) || 107814525f4SDarrick J. Wong !EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, 108814525f4SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) 109814525f4SDarrick J. Wong return; 110814525f4SDarrick J. Wong 111814525f4SDarrick J. Wong csum = ext4_inode_csum(inode, raw, ei); 112814525f4SDarrick J. Wong raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF); 113814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 114814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 115814525f4SDarrick J. Wong raw->i_checksum_hi = cpu_to_le16(csum >> 16); 116814525f4SDarrick J. Wong } 117814525f4SDarrick J. Wong 118678aaf48SJan Kara static inline int ext4_begin_ordered_truncate(struct inode *inode, 119678aaf48SJan Kara loff_t new_size) 120678aaf48SJan Kara { 1217ff9c073STheodore Ts'o trace_ext4_begin_ordered_truncate(inode, new_size); 1228aefcd55STheodore Ts'o /* 1238aefcd55STheodore Ts'o * If jinode is zero, then we never opened the file for 1248aefcd55STheodore Ts'o * writing, so there's no need to call 1258aefcd55STheodore Ts'o * jbd2_journal_begin_ordered_truncate() since there's no 1268aefcd55STheodore Ts'o * outstanding writes we need to flush. 1278aefcd55STheodore Ts'o */ 1288aefcd55STheodore Ts'o if (!EXT4_I(inode)->jinode) 1298aefcd55STheodore Ts'o return 0; 1308aefcd55STheodore Ts'o return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode), 1318aefcd55STheodore Ts'o EXT4_I(inode)->jinode, 132678aaf48SJan Kara new_size); 133678aaf48SJan Kara } 134678aaf48SJan Kara 135d47992f8SLukas Czerner static void ext4_invalidatepage(struct page *page, unsigned int offset, 136d47992f8SLukas Czerner unsigned int length); 137cb20d518STheodore Ts'o static int __ext4_journalled_writepage(struct page *page, unsigned int len); 138cb20d518STheodore Ts'o static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh); 1395f163cc7SEric Sandeen static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle, 1405f163cc7SEric Sandeen struct inode *inode, struct page *page, loff_t from, 1415f163cc7SEric Sandeen loff_t length, int flags); 14264769240SAlex Tomas 143ac27a0ecSDave Kleikamp /* 144ac27a0ecSDave Kleikamp * Test whether an inode is a fast symlink. 145ac27a0ecSDave Kleikamp */ 146617ba13bSMingming Cao static int ext4_inode_is_fast_symlink(struct inode *inode) 147ac27a0ecSDave Kleikamp { 148617ba13bSMingming Cao int ea_blocks = EXT4_I(inode)->i_file_acl ? 149ac27a0ecSDave Kleikamp (inode->i_sb->s_blocksize >> 9) : 0; 150ac27a0ecSDave Kleikamp 151ac27a0ecSDave Kleikamp return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0); 152ac27a0ecSDave Kleikamp } 153ac27a0ecSDave Kleikamp 154ac27a0ecSDave Kleikamp /* 155ac27a0ecSDave Kleikamp * Restart the transaction associated with *handle. This does a commit, 156ac27a0ecSDave Kleikamp * so before we call here everything must be consistently dirtied against 157ac27a0ecSDave Kleikamp * this transaction. 158ac27a0ecSDave Kleikamp */ 159487caeefSJan Kara int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode, 160487caeefSJan Kara int nblocks) 161ac27a0ecSDave Kleikamp { 162487caeefSJan Kara int ret; 163487caeefSJan Kara 164487caeefSJan Kara /* 165e35fd660STheodore Ts'o * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this 166487caeefSJan Kara * moment, get_block can be called only for blocks inside i_size since 167487caeefSJan Kara * page cache has been already dropped and writes are blocked by 168487caeefSJan Kara * i_mutex. So we can safely drop the i_data_sem here. 169487caeefSJan Kara */ 1700390131bSFrank Mayhar BUG_ON(EXT4_JOURNAL(inode) == NULL); 171ac27a0ecSDave Kleikamp jbd_debug(2, "restarting handle %p\n", handle); 172487caeefSJan Kara up_write(&EXT4_I(inode)->i_data_sem); 1738e8eaabeSAmir Goldstein ret = ext4_journal_restart(handle, nblocks); 174487caeefSJan Kara down_write(&EXT4_I(inode)->i_data_sem); 175fa5d1113SAneesh Kumar K.V ext4_discard_preallocations(inode); 176487caeefSJan Kara 177487caeefSJan Kara return ret; 178ac27a0ecSDave Kleikamp } 179ac27a0ecSDave Kleikamp 180ac27a0ecSDave Kleikamp /* 181ac27a0ecSDave Kleikamp * Called at the last iput() if i_nlink is zero. 182ac27a0ecSDave Kleikamp */ 1830930fcc1SAl Viro void ext4_evict_inode(struct inode *inode) 184ac27a0ecSDave Kleikamp { 185ac27a0ecSDave Kleikamp handle_t *handle; 186bc965ab3STheodore Ts'o int err; 187ac27a0ecSDave Kleikamp 1887ff9c073STheodore Ts'o trace_ext4_evict_inode(inode); 1892581fdc8SJiaying Zhang 1900930fcc1SAl Viro if (inode->i_nlink) { 1912d859db3SJan Kara /* 1922d859db3SJan Kara * When journalling data dirty buffers are tracked only in the 1932d859db3SJan Kara * journal. So although mm thinks everything is clean and 1942d859db3SJan Kara * ready for reaping the inode might still have some pages to 1952d859db3SJan Kara * write in the running transaction or waiting to be 1962d859db3SJan Kara * checkpointed. Thus calling jbd2_journal_invalidatepage() 1972d859db3SJan Kara * (via truncate_inode_pages()) to discard these buffers can 1982d859db3SJan Kara * cause data loss. Also even if we did not discard these 1992d859db3SJan Kara * buffers, we would have no way to find them after the inode 2002d859db3SJan Kara * is reaped and thus user could see stale data if he tries to 2012d859db3SJan Kara * read them before the transaction is checkpointed. So be 2022d859db3SJan Kara * careful and force everything to disk here... We use 2032d859db3SJan Kara * ei->i_datasync_tid to store the newest transaction 2042d859db3SJan Kara * containing inode's data. 2052d859db3SJan Kara * 2062d859db3SJan Kara * Note that directories do not have this problem because they 2072d859db3SJan Kara * don't use page cache. 2082d859db3SJan Kara */ 2092d859db3SJan Kara if (ext4_should_journal_data(inode) && 2102b405bfaSTheodore Ts'o (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode)) && 2112b405bfaSTheodore Ts'o inode->i_ino != EXT4_JOURNAL_INO) { 2122d859db3SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 2132d859db3SJan Kara tid_t commit_tid = EXT4_I(inode)->i_datasync_tid; 2142d859db3SJan Kara 215d76a3a77STheodore Ts'o jbd2_complete_transaction(journal, commit_tid); 2162d859db3SJan Kara filemap_write_and_wait(&inode->i_data); 2172d859db3SJan Kara } 2180930fcc1SAl Viro truncate_inode_pages(&inode->i_data, 0); 2191ada47d9STheodore Ts'o ext4_ioend_shutdown(inode); 2200930fcc1SAl Viro goto no_delete; 2210930fcc1SAl Viro } 2220930fcc1SAl Viro 223907f4554SChristoph Hellwig if (!is_bad_inode(inode)) 224871a2931SChristoph Hellwig dquot_initialize(inode); 225907f4554SChristoph Hellwig 226678aaf48SJan Kara if (ext4_should_order_data(inode)) 227678aaf48SJan Kara ext4_begin_ordered_truncate(inode, 0); 228ac27a0ecSDave Kleikamp truncate_inode_pages(&inode->i_data, 0); 2291ada47d9STheodore Ts'o ext4_ioend_shutdown(inode); 230ac27a0ecSDave Kleikamp 231ac27a0ecSDave Kleikamp if (is_bad_inode(inode)) 232ac27a0ecSDave Kleikamp goto no_delete; 233ac27a0ecSDave Kleikamp 2348e8ad8a5SJan Kara /* 2358e8ad8a5SJan Kara * Protect us against freezing - iput() caller didn't have to have any 2368e8ad8a5SJan Kara * protection against it 2378e8ad8a5SJan Kara */ 2388e8ad8a5SJan Kara sb_start_intwrite(inode->i_sb); 2399924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, 2409924a92aSTheodore Ts'o ext4_blocks_for_truncate(inode)+3); 241ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 242bc965ab3STheodore Ts'o ext4_std_error(inode->i_sb, PTR_ERR(handle)); 243ac27a0ecSDave Kleikamp /* 244ac27a0ecSDave Kleikamp * If we're going to skip the normal cleanup, we still need to 245ac27a0ecSDave Kleikamp * make sure that the in-core orphan linked list is properly 246ac27a0ecSDave Kleikamp * cleaned up. 247ac27a0ecSDave Kleikamp */ 248617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 2498e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 250ac27a0ecSDave Kleikamp goto no_delete; 251ac27a0ecSDave Kleikamp } 252ac27a0ecSDave Kleikamp 253ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 2540390131bSFrank Mayhar ext4_handle_sync(handle); 255ac27a0ecSDave Kleikamp inode->i_size = 0; 256bc965ab3STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 257bc965ab3STheodore Ts'o if (err) { 25812062dddSEric Sandeen ext4_warning(inode->i_sb, 259bc965ab3STheodore Ts'o "couldn't mark inode dirty (err %d)", err); 260bc965ab3STheodore Ts'o goto stop_handle; 261bc965ab3STheodore Ts'o } 262ac27a0ecSDave Kleikamp if (inode->i_blocks) 263617ba13bSMingming Cao ext4_truncate(inode); 264bc965ab3STheodore Ts'o 265bc965ab3STheodore Ts'o /* 266bc965ab3STheodore Ts'o * ext4_ext_truncate() doesn't reserve any slop when it 267bc965ab3STheodore Ts'o * restarts journal transactions; therefore there may not be 268bc965ab3STheodore Ts'o * enough credits left in the handle to remove the inode from 269bc965ab3STheodore Ts'o * the orphan list and set the dtime field. 270bc965ab3STheodore Ts'o */ 2710390131bSFrank Mayhar if (!ext4_handle_has_enough_credits(handle, 3)) { 272bc965ab3STheodore Ts'o err = ext4_journal_extend(handle, 3); 273bc965ab3STheodore Ts'o if (err > 0) 274bc965ab3STheodore Ts'o err = ext4_journal_restart(handle, 3); 275bc965ab3STheodore Ts'o if (err != 0) { 27612062dddSEric Sandeen ext4_warning(inode->i_sb, 277bc965ab3STheodore Ts'o "couldn't extend journal (err %d)", err); 278bc965ab3STheodore Ts'o stop_handle: 279bc965ab3STheodore Ts'o ext4_journal_stop(handle); 28045388219STheodore Ts'o ext4_orphan_del(NULL, inode); 2818e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 282bc965ab3STheodore Ts'o goto no_delete; 283bc965ab3STheodore Ts'o } 284bc965ab3STheodore Ts'o } 285bc965ab3STheodore Ts'o 286ac27a0ecSDave Kleikamp /* 287617ba13bSMingming Cao * Kill off the orphan record which ext4_truncate created. 288ac27a0ecSDave Kleikamp * AKPM: I think this can be inside the above `if'. 289617ba13bSMingming Cao * Note that ext4_orphan_del() has to be able to cope with the 290ac27a0ecSDave Kleikamp * deletion of a non-existent orphan - this is because we don't 291617ba13bSMingming Cao * know if ext4_truncate() actually created an orphan record. 292ac27a0ecSDave Kleikamp * (Well, we could do this if we need to, but heck - it works) 293ac27a0ecSDave Kleikamp */ 294617ba13bSMingming Cao ext4_orphan_del(handle, inode); 295617ba13bSMingming Cao EXT4_I(inode)->i_dtime = get_seconds(); 296ac27a0ecSDave Kleikamp 297ac27a0ecSDave Kleikamp /* 298ac27a0ecSDave Kleikamp * One subtle ordering requirement: if anything has gone wrong 299ac27a0ecSDave Kleikamp * (transaction abort, IO errors, whatever), then we can still 300ac27a0ecSDave Kleikamp * do these next steps (the fs will already have been marked as 301ac27a0ecSDave Kleikamp * having errors), but we can't free the inode if the mark_dirty 302ac27a0ecSDave Kleikamp * fails. 303ac27a0ecSDave Kleikamp */ 304617ba13bSMingming Cao if (ext4_mark_inode_dirty(handle, inode)) 305ac27a0ecSDave Kleikamp /* If that failed, just do the required in-core inode clear. */ 3060930fcc1SAl Viro ext4_clear_inode(inode); 307ac27a0ecSDave Kleikamp else 308617ba13bSMingming Cao ext4_free_inode(handle, inode); 309617ba13bSMingming Cao ext4_journal_stop(handle); 3108e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 311ac27a0ecSDave Kleikamp return; 312ac27a0ecSDave Kleikamp no_delete: 3130930fcc1SAl Viro ext4_clear_inode(inode); /* We must guarantee clearing of inode... */ 314ac27a0ecSDave Kleikamp } 315ac27a0ecSDave Kleikamp 316a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 317a9e7f447SDmitry Monakhov qsize_t *ext4_get_reserved_space(struct inode *inode) 31860e58e0fSMingming Cao { 319a9e7f447SDmitry Monakhov return &EXT4_I(inode)->i_reserved_quota; 32060e58e0fSMingming Cao } 321a9e7f447SDmitry Monakhov #endif 3229d0be502STheodore Ts'o 32312219aeaSAneesh Kumar K.V /* 32412219aeaSAneesh Kumar K.V * Calculate the number of metadata blocks need to reserve 3259d0be502STheodore Ts'o * to allocate a block located at @lblock 32612219aeaSAneesh Kumar K.V */ 32701f49d0bSTheodore Ts'o static int ext4_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock) 32812219aeaSAneesh Kumar K.V { 32912e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3309d0be502STheodore Ts'o return ext4_ext_calc_metadata_amount(inode, lblock); 33112219aeaSAneesh Kumar K.V 3328bb2b247SAmir Goldstein return ext4_ind_calc_metadata_amount(inode, lblock); 33312219aeaSAneesh Kumar K.V } 33412219aeaSAneesh Kumar K.V 3350637c6f4STheodore Ts'o /* 3360637c6f4STheodore Ts'o * Called with i_data_sem down, which is important since we can call 3370637c6f4STheodore Ts'o * ext4_discard_preallocations() from here. 3380637c6f4STheodore Ts'o */ 3395f634d06SAneesh Kumar K.V void ext4_da_update_reserve_space(struct inode *inode, 3405f634d06SAneesh Kumar K.V int used, int quota_claim) 34112219aeaSAneesh Kumar K.V { 34212219aeaSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3430637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 34412219aeaSAneesh Kumar K.V 3450637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 346d8990240SAditya Kali trace_ext4_da_update_reserve_space(inode, used, quota_claim); 3470637c6f4STheodore Ts'o if (unlikely(used > ei->i_reserved_data_blocks)) { 3488de5c325STheodore Ts'o ext4_warning(inode->i_sb, "%s: ino %lu, used %d " 3491084f252STheodore Ts'o "with only %d reserved data blocks", 3500637c6f4STheodore Ts'o __func__, inode->i_ino, used, 3510637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 3520637c6f4STheodore Ts'o WARN_ON(1); 3530637c6f4STheodore Ts'o used = ei->i_reserved_data_blocks; 3546bc6e63fSAneesh Kumar K.V } 35512219aeaSAneesh Kumar K.V 35697795d2aSBrian Foster if (unlikely(ei->i_allocated_meta_blocks > ei->i_reserved_meta_blocks)) { 35701a523ebSTheodore Ts'o ext4_warning(inode->i_sb, "ino %lu, allocated %d " 35801a523ebSTheodore Ts'o "with only %d reserved metadata blocks " 35901a523ebSTheodore Ts'o "(releasing %d blocks with reserved %d data blocks)", 36097795d2aSBrian Foster inode->i_ino, ei->i_allocated_meta_blocks, 36101a523ebSTheodore Ts'o ei->i_reserved_meta_blocks, used, 36201a523ebSTheodore Ts'o ei->i_reserved_data_blocks); 36397795d2aSBrian Foster WARN_ON(1); 36497795d2aSBrian Foster ei->i_allocated_meta_blocks = ei->i_reserved_meta_blocks; 36597795d2aSBrian Foster } 36697795d2aSBrian Foster 3670637c6f4STheodore Ts'o /* Update per-inode reservations */ 3680637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= used; 3690637c6f4STheodore Ts'o ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks; 37057042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 37172b8ab9dSEric Sandeen used + ei->i_allocated_meta_blocks); 3720637c6f4STheodore Ts'o ei->i_allocated_meta_blocks = 0; 3730637c6f4STheodore Ts'o 3740637c6f4STheodore Ts'o if (ei->i_reserved_data_blocks == 0) { 3750637c6f4STheodore Ts'o /* 3760637c6f4STheodore Ts'o * We can release all of the reserved metadata blocks 3770637c6f4STheodore Ts'o * only when we have written all of the delayed 3780637c6f4STheodore Ts'o * allocation blocks. 3790637c6f4STheodore Ts'o */ 38057042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 38172b8ab9dSEric Sandeen ei->i_reserved_meta_blocks); 382ee5f4d9cSTheodore Ts'o ei->i_reserved_meta_blocks = 0; 3839d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 3840637c6f4STheodore Ts'o } 38512219aeaSAneesh Kumar K.V spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 38660e58e0fSMingming Cao 38772b8ab9dSEric Sandeen /* Update quota subsystem for data blocks */ 38872b8ab9dSEric Sandeen if (quota_claim) 3897b415bf6SAditya Kali dquot_claim_block(inode, EXT4_C2B(sbi, used)); 39072b8ab9dSEric Sandeen else { 3915f634d06SAneesh Kumar K.V /* 3925f634d06SAneesh Kumar K.V * We did fallocate with an offset that is already delayed 3935f634d06SAneesh Kumar K.V * allocated. So on delayed allocated writeback we should 39472b8ab9dSEric Sandeen * not re-claim the quota for fallocated blocks. 3955f634d06SAneesh Kumar K.V */ 3967b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, used)); 3975f634d06SAneesh Kumar K.V } 398d6014301SAneesh Kumar K.V 399d6014301SAneesh Kumar K.V /* 400d6014301SAneesh Kumar K.V * If we have done all the pending block allocations and if 401d6014301SAneesh Kumar K.V * there aren't any writers on the inode, we can discard the 402d6014301SAneesh Kumar K.V * inode's preallocations. 403d6014301SAneesh Kumar K.V */ 4040637c6f4STheodore Ts'o if ((ei->i_reserved_data_blocks == 0) && 4050637c6f4STheodore Ts'o (atomic_read(&inode->i_writecount) == 0)) 406d6014301SAneesh Kumar K.V ext4_discard_preallocations(inode); 40712219aeaSAneesh Kumar K.V } 40812219aeaSAneesh Kumar K.V 409e29136f8STheodore Ts'o static int __check_block_validity(struct inode *inode, const char *func, 410c398eda0STheodore Ts'o unsigned int line, 41124676da4STheodore Ts'o struct ext4_map_blocks *map) 4126fd058f7STheodore Ts'o { 41324676da4STheodore Ts'o if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk, 41424676da4STheodore Ts'o map->m_len)) { 415c398eda0STheodore Ts'o ext4_error_inode(inode, func, line, map->m_pblk, 416c398eda0STheodore Ts'o "lblock %lu mapped to illegal pblock " 41724676da4STheodore Ts'o "(length %d)", (unsigned long) map->m_lblk, 418c398eda0STheodore Ts'o map->m_len); 4196fd058f7STheodore Ts'o return -EIO; 4206fd058f7STheodore Ts'o } 4216fd058f7STheodore Ts'o return 0; 4226fd058f7STheodore Ts'o } 4236fd058f7STheodore Ts'o 424e29136f8STheodore Ts'o #define check_block_validity(inode, map) \ 425c398eda0STheodore Ts'o __check_block_validity((inode), __func__, __LINE__, (map)) 426e29136f8STheodore Ts'o 427f5ab0d1fSMingming Cao /* 4281f94533dSTheodore Ts'o * Return the number of contiguous dirty pages in a given inode 4291f94533dSTheodore Ts'o * starting at page frame idx. 43055138e0bSTheodore Ts'o */ 43155138e0bSTheodore Ts'o static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx, 43255138e0bSTheodore Ts'o unsigned int max_pages) 43355138e0bSTheodore Ts'o { 43455138e0bSTheodore Ts'o struct address_space *mapping = inode->i_mapping; 43555138e0bSTheodore Ts'o pgoff_t index; 43655138e0bSTheodore Ts'o struct pagevec pvec; 43755138e0bSTheodore Ts'o pgoff_t num = 0; 43855138e0bSTheodore Ts'o int i, nr_pages, done = 0; 43955138e0bSTheodore Ts'o 44055138e0bSTheodore Ts'o if (max_pages == 0) 44155138e0bSTheodore Ts'o return 0; 44255138e0bSTheodore Ts'o pagevec_init(&pvec, 0); 44355138e0bSTheodore Ts'o while (!done) { 44455138e0bSTheodore Ts'o index = idx; 44555138e0bSTheodore Ts'o nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 44655138e0bSTheodore Ts'o PAGECACHE_TAG_DIRTY, 44755138e0bSTheodore Ts'o (pgoff_t)PAGEVEC_SIZE); 44855138e0bSTheodore Ts'o if (nr_pages == 0) 44955138e0bSTheodore Ts'o break; 45055138e0bSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 45155138e0bSTheodore Ts'o struct page *page = pvec.pages[i]; 45255138e0bSTheodore Ts'o struct buffer_head *bh, *head; 45355138e0bSTheodore Ts'o 45455138e0bSTheodore Ts'o lock_page(page); 45555138e0bSTheodore Ts'o if (unlikely(page->mapping != mapping) || 45655138e0bSTheodore Ts'o !PageDirty(page) || 45755138e0bSTheodore Ts'o PageWriteback(page) || 45855138e0bSTheodore Ts'o page->index != idx) { 45955138e0bSTheodore Ts'o done = 1; 46055138e0bSTheodore Ts'o unlock_page(page); 46155138e0bSTheodore Ts'o break; 46255138e0bSTheodore Ts'o } 4631f94533dSTheodore Ts'o if (page_has_buffers(page)) { 4641f94533dSTheodore Ts'o bh = head = page_buffers(page); 46555138e0bSTheodore Ts'o do { 46655138e0bSTheodore Ts'o if (!buffer_delay(bh) && 4671f94533dSTheodore Ts'o !buffer_unwritten(bh)) 46855138e0bSTheodore Ts'o done = 1; 4691f94533dSTheodore Ts'o bh = bh->b_this_page; 4701f94533dSTheodore Ts'o } while (!done && (bh != head)); 47155138e0bSTheodore Ts'o } 47255138e0bSTheodore Ts'o unlock_page(page); 47355138e0bSTheodore Ts'o if (done) 47455138e0bSTheodore Ts'o break; 47555138e0bSTheodore Ts'o idx++; 47655138e0bSTheodore Ts'o num++; 477659c6009SEric Sandeen if (num >= max_pages) { 478659c6009SEric Sandeen done = 1; 47955138e0bSTheodore Ts'o break; 48055138e0bSTheodore Ts'o } 481659c6009SEric Sandeen } 48255138e0bSTheodore Ts'o pagevec_release(&pvec); 48355138e0bSTheodore Ts'o } 48455138e0bSTheodore Ts'o return num; 48555138e0bSTheodore Ts'o } 48655138e0bSTheodore Ts'o 487921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 488921f266bSDmitry Monakhov static void ext4_map_blocks_es_recheck(handle_t *handle, 489921f266bSDmitry Monakhov struct inode *inode, 490921f266bSDmitry Monakhov struct ext4_map_blocks *es_map, 491921f266bSDmitry Monakhov struct ext4_map_blocks *map, 492921f266bSDmitry Monakhov int flags) 493921f266bSDmitry Monakhov { 494921f266bSDmitry Monakhov int retval; 495921f266bSDmitry Monakhov 496921f266bSDmitry Monakhov map->m_flags = 0; 497921f266bSDmitry Monakhov /* 498921f266bSDmitry Monakhov * There is a race window that the result is not the same. 499921f266bSDmitry Monakhov * e.g. xfstests #223 when dioread_nolock enables. The reason 500921f266bSDmitry Monakhov * is that we lookup a block mapping in extent status tree with 501921f266bSDmitry Monakhov * out taking i_data_sem. So at the time the unwritten extent 502921f266bSDmitry Monakhov * could be converted. 503921f266bSDmitry Monakhov */ 504921f266bSDmitry Monakhov if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 505921f266bSDmitry Monakhov down_read((&EXT4_I(inode)->i_data_sem)); 506921f266bSDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 507921f266bSDmitry Monakhov retval = ext4_ext_map_blocks(handle, inode, map, flags & 508921f266bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 509921f266bSDmitry Monakhov } else { 510921f266bSDmitry Monakhov retval = ext4_ind_map_blocks(handle, inode, map, flags & 511921f266bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 512921f266bSDmitry Monakhov } 513921f266bSDmitry Monakhov if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 514921f266bSDmitry Monakhov up_read((&EXT4_I(inode)->i_data_sem)); 515921f266bSDmitry Monakhov /* 516921f266bSDmitry Monakhov * Clear EXT4_MAP_FROM_CLUSTER and EXT4_MAP_BOUNDARY flag 517921f266bSDmitry Monakhov * because it shouldn't be marked in es_map->m_flags. 518921f266bSDmitry Monakhov */ 519921f266bSDmitry Monakhov map->m_flags &= ~(EXT4_MAP_FROM_CLUSTER | EXT4_MAP_BOUNDARY); 520921f266bSDmitry Monakhov 521921f266bSDmitry Monakhov /* 522921f266bSDmitry Monakhov * We don't check m_len because extent will be collpased in status 523921f266bSDmitry Monakhov * tree. So the m_len might not equal. 524921f266bSDmitry Monakhov */ 525921f266bSDmitry Monakhov if (es_map->m_lblk != map->m_lblk || 526921f266bSDmitry Monakhov es_map->m_flags != map->m_flags || 527921f266bSDmitry Monakhov es_map->m_pblk != map->m_pblk) { 528921f266bSDmitry Monakhov printk("ES cache assertation failed for inode: %lu " 529921f266bSDmitry Monakhov "es_cached ex [%d/%d/%llu/%x] != " 530921f266bSDmitry Monakhov "found ex [%d/%d/%llu/%x] retval %d flags %x\n", 531921f266bSDmitry Monakhov inode->i_ino, es_map->m_lblk, es_map->m_len, 532921f266bSDmitry Monakhov es_map->m_pblk, es_map->m_flags, map->m_lblk, 533921f266bSDmitry Monakhov map->m_len, map->m_pblk, map->m_flags, 534921f266bSDmitry Monakhov retval, flags); 535921f266bSDmitry Monakhov } 536921f266bSDmitry Monakhov } 537921f266bSDmitry Monakhov #endif /* ES_AGGRESSIVE_TEST */ 538921f266bSDmitry Monakhov 53955138e0bSTheodore Ts'o /* 540e35fd660STheodore Ts'o * The ext4_map_blocks() function tries to look up the requested blocks, 5412b2d6d01STheodore Ts'o * and returns if the blocks are already mapped. 542f5ab0d1fSMingming Cao * 543f5ab0d1fSMingming Cao * Otherwise it takes the write lock of the i_data_sem and allocate blocks 544f5ab0d1fSMingming Cao * and store the allocated blocks in the result buffer head and mark it 545f5ab0d1fSMingming Cao * mapped. 546f5ab0d1fSMingming Cao * 547e35fd660STheodore Ts'o * If file type is extents based, it will call ext4_ext_map_blocks(), 548e35fd660STheodore Ts'o * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping 549f5ab0d1fSMingming Cao * based files 550f5ab0d1fSMingming Cao * 551f5ab0d1fSMingming Cao * On success, it returns the number of blocks being mapped or allocate. 552f5ab0d1fSMingming Cao * if create==0 and the blocks are pre-allocated and uninitialized block, 553f5ab0d1fSMingming Cao * the result buffer head is unmapped. If the create ==1, it will make sure 554f5ab0d1fSMingming Cao * the buffer head is mapped. 555f5ab0d1fSMingming Cao * 556f5ab0d1fSMingming Cao * It returns 0 if plain look up failed (blocks have not been allocated), in 557df3ab170STao Ma * that case, buffer head is unmapped 558f5ab0d1fSMingming Cao * 559f5ab0d1fSMingming Cao * It returns the error in case of allocation failure. 560f5ab0d1fSMingming Cao */ 561e35fd660STheodore Ts'o int ext4_map_blocks(handle_t *handle, struct inode *inode, 562e35fd660STheodore Ts'o struct ext4_map_blocks *map, int flags) 5630e855ac8SAneesh Kumar K.V { 564d100eef2SZheng Liu struct extent_status es; 5650e855ac8SAneesh Kumar K.V int retval; 566921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 567921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 568921f266bSDmitry Monakhov 569921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 570921f266bSDmitry Monakhov #endif 571f5ab0d1fSMingming Cao 572e35fd660STheodore Ts'o map->m_flags = 0; 573e35fd660STheodore Ts'o ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u," 574e35fd660STheodore Ts'o "logical block %lu\n", inode->i_ino, flags, map->m_len, 575e35fd660STheodore Ts'o (unsigned long) map->m_lblk); 576d100eef2SZheng Liu 577d100eef2SZheng Liu /* Lookup extent status tree firstly */ 578d100eef2SZheng Liu if (ext4_es_lookup_extent(inode, map->m_lblk, &es)) { 579d100eef2SZheng Liu if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) { 580d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + 581d100eef2SZheng Liu map->m_lblk - es.es_lblk; 582d100eef2SZheng Liu map->m_flags |= ext4_es_is_written(&es) ? 583d100eef2SZheng Liu EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN; 584d100eef2SZheng Liu retval = es.es_len - (map->m_lblk - es.es_lblk); 585d100eef2SZheng Liu if (retval > map->m_len) 586d100eef2SZheng Liu retval = map->m_len; 587d100eef2SZheng Liu map->m_len = retval; 588d100eef2SZheng Liu } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) { 589d100eef2SZheng Liu retval = 0; 590d100eef2SZheng Liu } else { 591d100eef2SZheng Liu BUG_ON(1); 592d100eef2SZheng Liu } 593921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 594921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(handle, inode, map, 595921f266bSDmitry Monakhov &orig_map, flags); 596921f266bSDmitry Monakhov #endif 597d100eef2SZheng Liu goto found; 598d100eef2SZheng Liu } 599d100eef2SZheng Liu 6004df3d265SAneesh Kumar K.V /* 601b920c755STheodore Ts'o * Try to see if we can get the block without requesting a new 602b920c755STheodore Ts'o * file system block. 6034df3d265SAneesh Kumar K.V */ 604729f52c6SZheng Liu if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 6050e855ac8SAneesh Kumar K.V down_read((&EXT4_I(inode)->i_data_sem)); 60612e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 607a4e5d88bSDmitry Monakhov retval = ext4_ext_map_blocks(handle, inode, map, flags & 608a4e5d88bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 6094df3d265SAneesh Kumar K.V } else { 610a4e5d88bSDmitry Monakhov retval = ext4_ind_map_blocks(handle, inode, map, flags & 611a4e5d88bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 6120e855ac8SAneesh Kumar K.V } 613f7fec032SZheng Liu if (retval > 0) { 614f7fec032SZheng Liu int ret; 615f7fec032SZheng Liu unsigned long long status; 616f7fec032SZheng Liu 617921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 618921f266bSDmitry Monakhov if (retval != map->m_len) { 619921f266bSDmitry Monakhov printk("ES len assertation failed for inode: %lu " 620921f266bSDmitry Monakhov "retval %d != map->m_len %d " 621921f266bSDmitry Monakhov "in %s (lookup)\n", inode->i_ino, retval, 622921f266bSDmitry Monakhov map->m_len, __func__); 623921f266bSDmitry Monakhov } 624921f266bSDmitry Monakhov #endif 625921f266bSDmitry Monakhov 626f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 627f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 628f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 629f7fec032SZheng Liu ext4_find_delalloc_range(inode, map->m_lblk, 630f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 631f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 632f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, 633f7fec032SZheng Liu map->m_len, map->m_pblk, status); 634f7fec032SZheng Liu if (ret < 0) 635f7fec032SZheng Liu retval = ret; 636f7fec032SZheng Liu } 637729f52c6SZheng Liu if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 6384df3d265SAneesh Kumar K.V up_read((&EXT4_I(inode)->i_data_sem)); 639f5ab0d1fSMingming Cao 640d100eef2SZheng Liu found: 641e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 642f7fec032SZheng Liu int ret = check_block_validity(inode, map); 6436fd058f7STheodore Ts'o if (ret != 0) 6446fd058f7STheodore Ts'o return ret; 6456fd058f7STheodore Ts'o } 6466fd058f7STheodore Ts'o 647f5ab0d1fSMingming Cao /* If it is only a block(s) look up */ 648c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 6494df3d265SAneesh Kumar K.V return retval; 6504df3d265SAneesh Kumar K.V 6514df3d265SAneesh Kumar K.V /* 652f5ab0d1fSMingming Cao * Returns if the blocks have already allocated 653f5ab0d1fSMingming Cao * 654f5ab0d1fSMingming Cao * Note that if blocks have been preallocated 655df3ab170STao Ma * ext4_ext_get_block() returns the create = 0 656f5ab0d1fSMingming Cao * with buffer head unmapped. 657f5ab0d1fSMingming Cao */ 658e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) 659f5ab0d1fSMingming Cao return retval; 660f5ab0d1fSMingming Cao 661f5ab0d1fSMingming Cao /* 662a25a4e1aSZheng Liu * Here we clear m_flags because after allocating an new extent, 663a25a4e1aSZheng Liu * it will be set again. 6642a8964d6SAneesh Kumar K.V */ 665a25a4e1aSZheng Liu map->m_flags &= ~EXT4_MAP_FLAGS; 6662a8964d6SAneesh Kumar K.V 6672a8964d6SAneesh Kumar K.V /* 668f5ab0d1fSMingming Cao * New blocks allocate and/or writing to uninitialized extent 669f5ab0d1fSMingming Cao * will possibly result in updating i_data, so we take 670f5ab0d1fSMingming Cao * the write lock of i_data_sem, and call get_blocks() 671f5ab0d1fSMingming Cao * with create == 1 flag. 6724df3d265SAneesh Kumar K.V */ 6734df3d265SAneesh Kumar K.V down_write((&EXT4_I(inode)->i_data_sem)); 674d2a17637SMingming Cao 675d2a17637SMingming Cao /* 676d2a17637SMingming Cao * if the caller is from delayed allocation writeout path 677d2a17637SMingming Cao * we have already reserved fs blocks for allocation 678d2a17637SMingming Cao * let the underlying get_block() function know to 679d2a17637SMingming Cao * avoid double accounting 680d2a17637SMingming Cao */ 681c2177057STheodore Ts'o if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 682f2321097STheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED); 6834df3d265SAneesh Kumar K.V /* 6844df3d265SAneesh Kumar K.V * We need to check for EXT4 here because migrate 6854df3d265SAneesh Kumar K.V * could have changed the inode type in between 6864df3d265SAneesh Kumar K.V */ 68712e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 688e35fd660STheodore Ts'o retval = ext4_ext_map_blocks(handle, inode, map, flags); 6890e855ac8SAneesh Kumar K.V } else { 690e35fd660STheodore Ts'o retval = ext4_ind_map_blocks(handle, inode, map, flags); 691267e4db9SAneesh Kumar K.V 692e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_NEW) { 693267e4db9SAneesh Kumar K.V /* 694267e4db9SAneesh Kumar K.V * We allocated new blocks which will result in 695267e4db9SAneesh Kumar K.V * i_data's format changing. Force the migrate 696267e4db9SAneesh Kumar K.V * to fail by clearing migrate flags 697267e4db9SAneesh Kumar K.V */ 69819f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE); 699267e4db9SAneesh Kumar K.V } 7002ac3b6e0STheodore Ts'o 701d2a17637SMingming Cao /* 7022ac3b6e0STheodore Ts'o * Update reserved blocks/metadata blocks after successful 7035f634d06SAneesh Kumar K.V * block allocation which had been deferred till now. We don't 7045f634d06SAneesh Kumar K.V * support fallocate for non extent files. So we can update 7055f634d06SAneesh Kumar K.V * reserve space here. 706d2a17637SMingming Cao */ 7075f634d06SAneesh Kumar K.V if ((retval > 0) && 7081296cc85SAneesh Kumar K.V (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)) 7095f634d06SAneesh Kumar K.V ext4_da_update_reserve_space(inode, retval, 1); 7105f634d06SAneesh Kumar K.V } 711f7fec032SZheng Liu if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 712f2321097STheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED); 713d2a17637SMingming Cao 714f7fec032SZheng Liu if (retval > 0) { 71551865fdaSZheng Liu int ret; 716f7fec032SZheng Liu unsigned long long status; 717f7fec032SZheng Liu 718921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 719921f266bSDmitry Monakhov if (retval != map->m_len) { 720921f266bSDmitry Monakhov printk("ES len assertation failed for inode: %lu " 721921f266bSDmitry Monakhov "retval %d != map->m_len %d " 722921f266bSDmitry Monakhov "in %s (allocation)\n", inode->i_ino, retval, 723921f266bSDmitry Monakhov map->m_len, __func__); 724921f266bSDmitry Monakhov } 725921f266bSDmitry Monakhov #endif 726921f266bSDmitry Monakhov 727adb23551SZheng Liu /* 728adb23551SZheng Liu * If the extent has been zeroed out, we don't need to update 729adb23551SZheng Liu * extent status tree. 730adb23551SZheng Liu */ 731adb23551SZheng Liu if ((flags & EXT4_GET_BLOCKS_PRE_IO) && 732adb23551SZheng Liu ext4_es_lookup_extent(inode, map->m_lblk, &es)) { 733adb23551SZheng Liu if (ext4_es_is_written(&es)) 734adb23551SZheng Liu goto has_zeroout; 735adb23551SZheng Liu } 736f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 737f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 738f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 739f7fec032SZheng Liu ext4_find_delalloc_range(inode, map->m_lblk, 740f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 741f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 742f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 743f7fec032SZheng Liu map->m_pblk, status); 74451865fdaSZheng Liu if (ret < 0) 74551865fdaSZheng Liu retval = ret; 74651865fdaSZheng Liu } 7475356f261SAditya Kali 748adb23551SZheng Liu has_zeroout: 7490e855ac8SAneesh Kumar K.V up_write((&EXT4_I(inode)->i_data_sem)); 750e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 751e29136f8STheodore Ts'o int ret = check_block_validity(inode, map); 7526fd058f7STheodore Ts'o if (ret != 0) 7536fd058f7STheodore Ts'o return ret; 7546fd058f7STheodore Ts'o } 7550e855ac8SAneesh Kumar K.V return retval; 7560e855ac8SAneesh Kumar K.V } 7570e855ac8SAneesh Kumar K.V 758f3bd1f3fSMingming Cao /* Maximum number of blocks we map for direct IO at once. */ 759f3bd1f3fSMingming Cao #define DIO_MAX_BLOCKS 4096 760f3bd1f3fSMingming Cao 7612ed88685STheodore Ts'o static int _ext4_get_block(struct inode *inode, sector_t iblock, 7622ed88685STheodore Ts'o struct buffer_head *bh, int flags) 763ac27a0ecSDave Kleikamp { 7643e4fdaf8SDmitriy Monakhov handle_t *handle = ext4_journal_current_handle(); 7652ed88685STheodore Ts'o struct ext4_map_blocks map; 7667fb5409dSJan Kara int ret = 0, started = 0; 767f3bd1f3fSMingming Cao int dio_credits; 768ac27a0ecSDave Kleikamp 76946c7f254STao Ma if (ext4_has_inline_data(inode)) 77046c7f254STao Ma return -ERANGE; 77146c7f254STao Ma 7722ed88685STheodore Ts'o map.m_lblk = iblock; 7732ed88685STheodore Ts'o map.m_len = bh->b_size >> inode->i_blkbits; 7742ed88685STheodore Ts'o 7758b0f165fSAnatol Pomozov if (flags && !(flags & EXT4_GET_BLOCKS_NO_LOCK) && !handle) { 7767fb5409dSJan Kara /* Direct IO write... */ 7772ed88685STheodore Ts'o if (map.m_len > DIO_MAX_BLOCKS) 7782ed88685STheodore Ts'o map.m_len = DIO_MAX_BLOCKS; 7792ed88685STheodore Ts'o dio_credits = ext4_chunk_trans_blocks(inode, map.m_len); 7809924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, 7819924a92aSTheodore Ts'o dio_credits); 7827fb5409dSJan Kara if (IS_ERR(handle)) { 783ac27a0ecSDave Kleikamp ret = PTR_ERR(handle); 7842ed88685STheodore Ts'o return ret; 7857fb5409dSJan Kara } 7867fb5409dSJan Kara started = 1; 787ac27a0ecSDave Kleikamp } 788ac27a0ecSDave Kleikamp 7892ed88685STheodore Ts'o ret = ext4_map_blocks(handle, inode, &map, flags); 790ac27a0ecSDave Kleikamp if (ret > 0) { 7912ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 7922ed88685STheodore Ts'o bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; 7932ed88685STheodore Ts'o bh->b_size = inode->i_sb->s_blocksize * map.m_len; 794ac27a0ecSDave Kleikamp ret = 0; 795ac27a0ecSDave Kleikamp } 7967fb5409dSJan Kara if (started) 7977fb5409dSJan Kara ext4_journal_stop(handle); 798ac27a0ecSDave Kleikamp return ret; 799ac27a0ecSDave Kleikamp } 800ac27a0ecSDave Kleikamp 8012ed88685STheodore Ts'o int ext4_get_block(struct inode *inode, sector_t iblock, 8022ed88685STheodore Ts'o struct buffer_head *bh, int create) 8032ed88685STheodore Ts'o { 8042ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh, 8052ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 8062ed88685STheodore Ts'o } 8072ed88685STheodore Ts'o 808ac27a0ecSDave Kleikamp /* 809ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 810ac27a0ecSDave Kleikamp */ 811617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 812725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *errp) 813ac27a0ecSDave Kleikamp { 8142ed88685STheodore Ts'o struct ext4_map_blocks map; 8152ed88685STheodore Ts'o struct buffer_head *bh; 816ac27a0ecSDave Kleikamp int fatal = 0, err; 817ac27a0ecSDave Kleikamp 818ac27a0ecSDave Kleikamp J_ASSERT(handle != NULL || create == 0); 819ac27a0ecSDave Kleikamp 8202ed88685STheodore Ts'o map.m_lblk = block; 8212ed88685STheodore Ts'o map.m_len = 1; 8222ed88685STheodore Ts'o err = ext4_map_blocks(handle, inode, &map, 8232ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 8242ed88685STheodore Ts'o 82590b0a973SCarlos Maiolino /* ensure we send some value back into *errp */ 82690b0a973SCarlos Maiolino *errp = 0; 82790b0a973SCarlos Maiolino 8280f70b406STheodore Ts'o if (create && err == 0) 8290f70b406STheodore Ts'o err = -ENOSPC; /* should never happen */ 8302ed88685STheodore Ts'o if (err < 0) 831ac27a0ecSDave Kleikamp *errp = err; 8322ed88685STheodore Ts'o if (err <= 0) 8332ed88685STheodore Ts'o return NULL; 8342ed88685STheodore Ts'o 8352ed88685STheodore Ts'o bh = sb_getblk(inode->i_sb, map.m_pblk); 836aebf0243SWang Shilong if (unlikely(!bh)) { 837860d21e2STheodore Ts'o *errp = -ENOMEM; 8382ed88685STheodore Ts'o return NULL; 839ac27a0ecSDave Kleikamp } 8402ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 841ac27a0ecSDave Kleikamp J_ASSERT(create != 0); 842ac39849dSAneesh Kumar K.V J_ASSERT(handle != NULL); 843ac27a0ecSDave Kleikamp 844ac27a0ecSDave Kleikamp /* 845ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 846ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 847ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 848617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 849ac27a0ecSDave Kleikamp * problem. 850ac27a0ecSDave Kleikamp */ 851ac27a0ecSDave Kleikamp lock_buffer(bh); 852ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 853617ba13bSMingming Cao fatal = ext4_journal_get_create_access(handle, bh); 854ac27a0ecSDave Kleikamp if (!fatal && !buffer_uptodate(bh)) { 855ac27a0ecSDave Kleikamp memset(bh->b_data, 0, inode->i_sb->s_blocksize); 856ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 857ac27a0ecSDave Kleikamp } 858ac27a0ecSDave Kleikamp unlock_buffer(bh); 8590390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 8600390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 861ac27a0ecSDave Kleikamp if (!fatal) 862ac27a0ecSDave Kleikamp fatal = err; 863ac27a0ecSDave Kleikamp } else { 864ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "not a new buffer"); 865ac27a0ecSDave Kleikamp } 866ac27a0ecSDave Kleikamp if (fatal) { 867ac27a0ecSDave Kleikamp *errp = fatal; 868ac27a0ecSDave Kleikamp brelse(bh); 869ac27a0ecSDave Kleikamp bh = NULL; 870ac27a0ecSDave Kleikamp } 871ac27a0ecSDave Kleikamp return bh; 872ac27a0ecSDave Kleikamp } 873ac27a0ecSDave Kleikamp 874617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 875725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *err) 876ac27a0ecSDave Kleikamp { 877ac27a0ecSDave Kleikamp struct buffer_head *bh; 878ac27a0ecSDave Kleikamp 879617ba13bSMingming Cao bh = ext4_getblk(handle, inode, block, create, err); 880ac27a0ecSDave Kleikamp if (!bh) 881ac27a0ecSDave Kleikamp return bh; 882ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 883ac27a0ecSDave Kleikamp return bh; 88465299a3bSChristoph Hellwig ll_rw_block(READ | REQ_META | REQ_PRIO, 1, &bh); 885ac27a0ecSDave Kleikamp wait_on_buffer(bh); 886ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 887ac27a0ecSDave Kleikamp return bh; 888ac27a0ecSDave Kleikamp put_bh(bh); 889ac27a0ecSDave Kleikamp *err = -EIO; 890ac27a0ecSDave Kleikamp return NULL; 891ac27a0ecSDave Kleikamp } 892ac27a0ecSDave Kleikamp 893f19d5870STao Ma int ext4_walk_page_buffers(handle_t *handle, 894ac27a0ecSDave Kleikamp struct buffer_head *head, 895ac27a0ecSDave Kleikamp unsigned from, 896ac27a0ecSDave Kleikamp unsigned to, 897ac27a0ecSDave Kleikamp int *partial, 898ac27a0ecSDave Kleikamp int (*fn)(handle_t *handle, 899ac27a0ecSDave Kleikamp struct buffer_head *bh)) 900ac27a0ecSDave Kleikamp { 901ac27a0ecSDave Kleikamp struct buffer_head *bh; 902ac27a0ecSDave Kleikamp unsigned block_start, block_end; 903ac27a0ecSDave Kleikamp unsigned blocksize = head->b_size; 904ac27a0ecSDave Kleikamp int err, ret = 0; 905ac27a0ecSDave Kleikamp struct buffer_head *next; 906ac27a0ecSDave Kleikamp 907ac27a0ecSDave Kleikamp for (bh = head, block_start = 0; 908ac27a0ecSDave Kleikamp ret == 0 && (bh != head || !block_start); 909de9a55b8STheodore Ts'o block_start = block_end, bh = next) { 910ac27a0ecSDave Kleikamp next = bh->b_this_page; 911ac27a0ecSDave Kleikamp block_end = block_start + blocksize; 912ac27a0ecSDave Kleikamp if (block_end <= from || block_start >= to) { 913ac27a0ecSDave Kleikamp if (partial && !buffer_uptodate(bh)) 914ac27a0ecSDave Kleikamp *partial = 1; 915ac27a0ecSDave Kleikamp continue; 916ac27a0ecSDave Kleikamp } 917ac27a0ecSDave Kleikamp err = (*fn)(handle, bh); 918ac27a0ecSDave Kleikamp if (!ret) 919ac27a0ecSDave Kleikamp ret = err; 920ac27a0ecSDave Kleikamp } 921ac27a0ecSDave Kleikamp return ret; 922ac27a0ecSDave Kleikamp } 923ac27a0ecSDave Kleikamp 924ac27a0ecSDave Kleikamp /* 925ac27a0ecSDave Kleikamp * To preserve ordering, it is essential that the hole instantiation and 926ac27a0ecSDave Kleikamp * the data write be encapsulated in a single transaction. We cannot 927617ba13bSMingming Cao * close off a transaction and start a new one between the ext4_get_block() 928dab291afSMingming Cao * and the commit_write(). So doing the jbd2_journal_start at the start of 929ac27a0ecSDave Kleikamp * prepare_write() is the right place. 930ac27a0ecSDave Kleikamp * 93136ade451SJan Kara * Also, this function can nest inside ext4_writepage(). In that case, we 93236ade451SJan Kara * *know* that ext4_writepage() has generated enough buffer credits to do the 93336ade451SJan Kara * whole page. So we won't block on the journal in that case, which is good, 93436ade451SJan Kara * because the caller may be PF_MEMALLOC. 935ac27a0ecSDave Kleikamp * 936617ba13bSMingming Cao * By accident, ext4 can be reentered when a transaction is open via 937ac27a0ecSDave Kleikamp * quota file writes. If we were to commit the transaction while thus 938ac27a0ecSDave Kleikamp * reentered, there can be a deadlock - we would be holding a quota 939ac27a0ecSDave Kleikamp * lock, and the commit would never complete if another thread had a 940ac27a0ecSDave Kleikamp * transaction open and was blocking on the quota lock - a ranking 941ac27a0ecSDave Kleikamp * violation. 942ac27a0ecSDave Kleikamp * 943dab291afSMingming Cao * So what we do is to rely on the fact that jbd2_journal_stop/journal_start 944ac27a0ecSDave Kleikamp * will _not_ run commit under these circumstances because handle->h_ref 945ac27a0ecSDave Kleikamp * is elevated. We'll still have enough credits for the tiny quotafile 946ac27a0ecSDave Kleikamp * write. 947ac27a0ecSDave Kleikamp */ 948f19d5870STao Ma int do_journal_get_write_access(handle_t *handle, 949ac27a0ecSDave Kleikamp struct buffer_head *bh) 950ac27a0ecSDave Kleikamp { 95156d35a4cSJan Kara int dirty = buffer_dirty(bh); 95256d35a4cSJan Kara int ret; 95356d35a4cSJan Kara 954ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 955ac27a0ecSDave Kleikamp return 0; 95656d35a4cSJan Kara /* 957ebdec241SChristoph Hellwig * __block_write_begin() could have dirtied some buffers. Clean 95856d35a4cSJan Kara * the dirty bit as jbd2_journal_get_write_access() could complain 95956d35a4cSJan Kara * otherwise about fs integrity issues. Setting of the dirty bit 960ebdec241SChristoph Hellwig * by __block_write_begin() isn't a real problem here as we clear 96156d35a4cSJan Kara * the bit before releasing a page lock and thus writeback cannot 96256d35a4cSJan Kara * ever write the buffer. 96356d35a4cSJan Kara */ 96456d35a4cSJan Kara if (dirty) 96556d35a4cSJan Kara clear_buffer_dirty(bh); 96656d35a4cSJan Kara ret = ext4_journal_get_write_access(handle, bh); 96756d35a4cSJan Kara if (!ret && dirty) 96856d35a4cSJan Kara ret = ext4_handle_dirty_metadata(handle, NULL, bh); 96956d35a4cSJan Kara return ret; 970ac27a0ecSDave Kleikamp } 971ac27a0ecSDave Kleikamp 9728b0f165fSAnatol Pomozov static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock, 9738b0f165fSAnatol Pomozov struct buffer_head *bh_result, int create); 974bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 975bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned flags, 976bfc1af65SNick Piggin struct page **pagep, void **fsdata) 977ac27a0ecSDave Kleikamp { 978bfc1af65SNick Piggin struct inode *inode = mapping->host; 9791938a150SAneesh Kumar K.V int ret, needed_blocks; 980ac27a0ecSDave Kleikamp handle_t *handle; 981ac27a0ecSDave Kleikamp int retries = 0; 982bfc1af65SNick Piggin struct page *page; 983bfc1af65SNick Piggin pgoff_t index; 984bfc1af65SNick Piggin unsigned from, to; 985bfc1af65SNick Piggin 9869bffad1eSTheodore Ts'o trace_ext4_write_begin(inode, pos, len, flags); 9871938a150SAneesh Kumar K.V /* 9881938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 9891938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 9901938a150SAneesh Kumar K.V */ 9911938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 992bfc1af65SNick Piggin index = pos >> PAGE_CACHE_SHIFT; 993bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 994bfc1af65SNick Piggin to = from + len; 995ac27a0ecSDave Kleikamp 996f19d5870STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 997f19d5870STao Ma ret = ext4_try_to_write_inline_data(mapping, inode, pos, len, 998f19d5870STao Ma flags, pagep); 999f19d5870STao Ma if (ret < 0) 100047564bfbSTheodore Ts'o return ret; 100147564bfbSTheodore Ts'o if (ret == 1) 100247564bfbSTheodore Ts'o return 0; 1003f19d5870STao Ma } 1004f19d5870STao Ma 100547564bfbSTheodore Ts'o /* 100647564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 100747564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 100847564bfbSTheodore Ts'o * is being written back. So grab it first before we start 100947564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 101047564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 101147564bfbSTheodore Ts'o */ 101247564bfbSTheodore Ts'o retry_grab: 101354566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 101447564bfbSTheodore Ts'o if (!page) 101547564bfbSTheodore Ts'o return -ENOMEM; 101647564bfbSTheodore Ts'o unlock_page(page); 101747564bfbSTheodore Ts'o 101847564bfbSTheodore Ts'o retry_journal: 10199924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks); 1020ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 102147564bfbSTheodore Ts'o page_cache_release(page); 102247564bfbSTheodore Ts'o return PTR_ERR(handle); 1023cf108bcaSJan Kara } 1024f19d5870STao Ma 102547564bfbSTheodore Ts'o lock_page(page); 102647564bfbSTheodore Ts'o if (page->mapping != mapping) { 102747564bfbSTheodore Ts'o /* The page got truncated from under us */ 102847564bfbSTheodore Ts'o unlock_page(page); 102947564bfbSTheodore Ts'o page_cache_release(page); 1030cf108bcaSJan Kara ext4_journal_stop(handle); 103147564bfbSTheodore Ts'o goto retry_grab; 1032cf108bcaSJan Kara } 103347564bfbSTheodore Ts'o wait_on_page_writeback(page); 1034cf108bcaSJan Kara 1035744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 10366e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block_write); 1037744692dcSJiaying Zhang else 10386e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block); 1039bfc1af65SNick Piggin 1040bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 1041f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_buffers(page), 1042f19d5870STao Ma from, to, NULL, 1043f19d5870STao Ma do_journal_get_write_access); 1044b46be050SAndrey Savochkin } 1045bfc1af65SNick Piggin 1046bfc1af65SNick Piggin if (ret) { 1047bfc1af65SNick Piggin unlock_page(page); 1048ae4d5372SAneesh Kumar K.V /* 10496e1db88dSChristoph Hellwig * __block_write_begin may have instantiated a few blocks 1050ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 1051ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 10521938a150SAneesh Kumar K.V * 10531938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 10541938a150SAneesh Kumar K.V * truncate finishes 1055ae4d5372SAneesh Kumar K.V */ 1056ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 10571938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 10581938a150SAneesh Kumar K.V 10591938a150SAneesh Kumar K.V ext4_journal_stop(handle); 10601938a150SAneesh Kumar K.V if (pos + len > inode->i_size) { 1061b9a4207dSJan Kara ext4_truncate_failed_write(inode); 10621938a150SAneesh Kumar K.V /* 1063ffacfa7aSJan Kara * If truncate failed early the inode might 10641938a150SAneesh Kumar K.V * still be on the orphan list; we need to 10651938a150SAneesh Kumar K.V * make sure the inode is removed from the 10661938a150SAneesh Kumar K.V * orphan list in that case. 10671938a150SAneesh Kumar K.V */ 10681938a150SAneesh Kumar K.V if (inode->i_nlink) 10691938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 10701938a150SAneesh Kumar K.V } 1071bfc1af65SNick Piggin 107247564bfbSTheodore Ts'o if (ret == -ENOSPC && 107347564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 107447564bfbSTheodore Ts'o goto retry_journal; 107547564bfbSTheodore Ts'o page_cache_release(page); 107647564bfbSTheodore Ts'o return ret; 107747564bfbSTheodore Ts'o } 107847564bfbSTheodore Ts'o *pagep = page; 1079ac27a0ecSDave Kleikamp return ret; 1080ac27a0ecSDave Kleikamp } 1081ac27a0ecSDave Kleikamp 1082bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 1083bfc1af65SNick Piggin static int write_end_fn(handle_t *handle, struct buffer_head *bh) 1084ac27a0ecSDave Kleikamp { 108513fca323STheodore Ts'o int ret; 1086ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1087ac27a0ecSDave Kleikamp return 0; 1088ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 108913fca323STheodore Ts'o ret = ext4_handle_dirty_metadata(handle, NULL, bh); 109013fca323STheodore Ts'o clear_buffer_meta(bh); 109113fca323STheodore Ts'o clear_buffer_prio(bh); 109213fca323STheodore Ts'o return ret; 1093ac27a0ecSDave Kleikamp } 1094ac27a0ecSDave Kleikamp 1095eed4333fSZheng Liu /* 1096eed4333fSZheng Liu * We need to pick up the new inode size which generic_commit_write gave us 1097eed4333fSZheng Liu * `file' can be NULL - eg, when called from page_symlink(). 1098eed4333fSZheng Liu * 1099eed4333fSZheng Liu * ext4 never places buffers on inode->i_mapping->private_list. metadata 1100eed4333fSZheng Liu * buffers are managed internally. 1101eed4333fSZheng Liu */ 1102eed4333fSZheng Liu static int ext4_write_end(struct file *file, 1103f8514083SAneesh Kumar K.V struct address_space *mapping, 1104f8514083SAneesh Kumar K.V loff_t pos, unsigned len, unsigned copied, 1105f8514083SAneesh Kumar K.V struct page *page, void *fsdata) 1106f8514083SAneesh Kumar K.V { 1107f8514083SAneesh Kumar K.V handle_t *handle = ext4_journal_current_handle(); 1108eed4333fSZheng Liu struct inode *inode = mapping->host; 1109eed4333fSZheng Liu int ret = 0, ret2; 1110eed4333fSZheng Liu int i_size_changed = 0; 1111eed4333fSZheng Liu 1112eed4333fSZheng Liu trace_ext4_write_end(inode, pos, len, copied); 1113eed4333fSZheng Liu if (ext4_test_inode_state(inode, EXT4_STATE_ORDERED_MODE)) { 1114eed4333fSZheng Liu ret = ext4_jbd2_file_inode(handle, inode); 1115eed4333fSZheng Liu if (ret) { 1116eed4333fSZheng Liu unlock_page(page); 1117eed4333fSZheng Liu page_cache_release(page); 1118eed4333fSZheng Liu goto errout; 1119eed4333fSZheng Liu } 1120eed4333fSZheng Liu } 1121f8514083SAneesh Kumar K.V 1122f19d5870STao Ma if (ext4_has_inline_data(inode)) 1123f19d5870STao Ma copied = ext4_write_inline_data_end(inode, pos, len, 1124f19d5870STao Ma copied, page); 1125f19d5870STao Ma else 1126f19d5870STao Ma copied = block_write_end(file, mapping, pos, 1127f19d5870STao Ma len, copied, page, fsdata); 1128f8514083SAneesh Kumar K.V 1129f8514083SAneesh Kumar K.V /* 1130f8514083SAneesh Kumar K.V * No need to use i_size_read() here, the i_size 1131eed4333fSZheng Liu * cannot change under us because we hole i_mutex. 1132f8514083SAneesh Kumar K.V * 1133f8514083SAneesh Kumar K.V * But it's important to update i_size while still holding page lock: 1134f8514083SAneesh Kumar K.V * page writeout could otherwise come in and zero beyond i_size. 1135f8514083SAneesh Kumar K.V */ 1136f8514083SAneesh Kumar K.V if (pos + copied > inode->i_size) { 1137f8514083SAneesh Kumar K.V i_size_write(inode, pos + copied); 1138f8514083SAneesh Kumar K.V i_size_changed = 1; 1139f8514083SAneesh Kumar K.V } 1140f8514083SAneesh Kumar K.V 1141f8514083SAneesh Kumar K.V if (pos + copied > EXT4_I(inode)->i_disksize) { 1142f8514083SAneesh Kumar K.V /* We need to mark inode dirty even if 1143f8514083SAneesh Kumar K.V * new_i_size is less that inode->i_size 1144eed4333fSZheng Liu * but greater than i_disksize. (hint delalloc) 1145f8514083SAneesh Kumar K.V */ 1146f8514083SAneesh Kumar K.V ext4_update_i_disksize(inode, (pos + copied)); 1147f8514083SAneesh Kumar K.V i_size_changed = 1; 1148f8514083SAneesh Kumar K.V } 1149f8514083SAneesh Kumar K.V unlock_page(page); 1150f8514083SAneesh Kumar K.V page_cache_release(page); 1151f8514083SAneesh Kumar K.V 1152f8514083SAneesh Kumar K.V /* 1153f8514083SAneesh Kumar K.V * Don't mark the inode dirty under page lock. First, it unnecessarily 1154f8514083SAneesh Kumar K.V * makes the holding time of page lock longer. Second, it forces lock 1155f8514083SAneesh Kumar K.V * ordering of page lock and transaction start for journaling 1156f8514083SAneesh Kumar K.V * filesystems. 1157f8514083SAneesh Kumar K.V */ 1158f8514083SAneesh Kumar K.V if (i_size_changed) 1159f8514083SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 1160f8514083SAneesh Kumar K.V 116174d553aaSTheodore Ts'o if (copied < 0) 116274d553aaSTheodore Ts'o ret = copied; 1163ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1164f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1165f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1166f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1167f8514083SAneesh Kumar K.V */ 1168f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 116974d553aaSTheodore Ts'o errout: 1170617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1171ac27a0ecSDave Kleikamp if (!ret) 1172ac27a0ecSDave Kleikamp ret = ret2; 1173bfc1af65SNick Piggin 1174f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1175b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1176f8514083SAneesh Kumar K.V /* 1177ffacfa7aSJan Kara * If truncate failed early the inode might still be 1178f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1179f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1180f8514083SAneesh Kumar K.V */ 1181f8514083SAneesh Kumar K.V if (inode->i_nlink) 1182f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1183f8514083SAneesh Kumar K.V } 1184f8514083SAneesh Kumar K.V 1185bfc1af65SNick Piggin return ret ? ret : copied; 1186ac27a0ecSDave Kleikamp } 1187ac27a0ecSDave Kleikamp 1188bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 1189bfc1af65SNick Piggin struct address_space *mapping, 1190bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1191bfc1af65SNick Piggin struct page *page, void *fsdata) 1192ac27a0ecSDave Kleikamp { 1193617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1194bfc1af65SNick Piggin struct inode *inode = mapping->host; 1195ac27a0ecSDave Kleikamp int ret = 0, ret2; 1196ac27a0ecSDave Kleikamp int partial = 0; 1197bfc1af65SNick Piggin unsigned from, to; 1198cf17fea6SAneesh Kumar K.V loff_t new_i_size; 1199ac27a0ecSDave Kleikamp 12009bffad1eSTheodore Ts'o trace_ext4_journalled_write_end(inode, pos, len, copied); 1201bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 1202bfc1af65SNick Piggin to = from + len; 1203bfc1af65SNick Piggin 1204441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1205441c8508SCurt Wohlgemuth 12063fdcfb66STao Ma if (ext4_has_inline_data(inode)) 12073fdcfb66STao Ma copied = ext4_write_inline_data_end(inode, pos, len, 12083fdcfb66STao Ma copied, page); 12093fdcfb66STao Ma else { 1210bfc1af65SNick Piggin if (copied < len) { 1211bfc1af65SNick Piggin if (!PageUptodate(page)) 1212bfc1af65SNick Piggin copied = 0; 1213bfc1af65SNick Piggin page_zero_new_buffers(page, from+copied, to); 1214bfc1af65SNick Piggin } 1215ac27a0ecSDave Kleikamp 1216f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_buffers(page), from, 1217bfc1af65SNick Piggin to, &partial, write_end_fn); 1218ac27a0ecSDave Kleikamp if (!partial) 1219ac27a0ecSDave Kleikamp SetPageUptodate(page); 12203fdcfb66STao Ma } 1221cf17fea6SAneesh Kumar K.V new_i_size = pos + copied; 1222cf17fea6SAneesh Kumar K.V if (new_i_size > inode->i_size) 1223bfc1af65SNick Piggin i_size_write(inode, pos+copied); 122419f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 12252d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 1226cf17fea6SAneesh Kumar K.V if (new_i_size > EXT4_I(inode)->i_disksize) { 1227cf17fea6SAneesh Kumar K.V ext4_update_i_disksize(inode, new_i_size); 1228617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1229ac27a0ecSDave Kleikamp if (!ret) 1230ac27a0ecSDave Kleikamp ret = ret2; 1231ac27a0ecSDave Kleikamp } 1232bfc1af65SNick Piggin 1233cf108bcaSJan Kara unlock_page(page); 1234f8514083SAneesh Kumar K.V page_cache_release(page); 1235ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1236f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1237f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1238f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1239f8514083SAneesh Kumar K.V */ 1240f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1241f8514083SAneesh Kumar K.V 1242617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1243ac27a0ecSDave Kleikamp if (!ret) 1244ac27a0ecSDave Kleikamp ret = ret2; 1245f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1246b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1247f8514083SAneesh Kumar K.V /* 1248ffacfa7aSJan Kara * If truncate failed early the inode might still be 1249f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1250f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1251f8514083SAneesh Kumar K.V */ 1252f8514083SAneesh Kumar K.V if (inode->i_nlink) 1253f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1254f8514083SAneesh Kumar K.V } 1255bfc1af65SNick Piggin 1256bfc1af65SNick Piggin return ret ? ret : copied; 1257ac27a0ecSDave Kleikamp } 1258d2a17637SMingming Cao 12599d0be502STheodore Ts'o /* 1260386ad67cSLukas Czerner * Reserve a metadata for a single block located at lblock 1261386ad67cSLukas Czerner */ 1262386ad67cSLukas Czerner static int ext4_da_reserve_metadata(struct inode *inode, ext4_lblk_t lblock) 1263386ad67cSLukas Czerner { 1264386ad67cSLukas Czerner int retries = 0; 1265386ad67cSLukas Czerner struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 1266386ad67cSLukas Czerner struct ext4_inode_info *ei = EXT4_I(inode); 1267386ad67cSLukas Czerner unsigned int md_needed; 1268386ad67cSLukas Czerner ext4_lblk_t save_last_lblock; 1269386ad67cSLukas Czerner int save_len; 1270386ad67cSLukas Czerner 1271386ad67cSLukas Czerner /* 1272386ad67cSLukas Czerner * recalculate the amount of metadata blocks to reserve 1273386ad67cSLukas Czerner * in order to allocate nrblocks 1274386ad67cSLukas Czerner * worse case is one extent per block 1275386ad67cSLukas Czerner */ 1276386ad67cSLukas Czerner repeat: 1277386ad67cSLukas Czerner spin_lock(&ei->i_block_reservation_lock); 1278386ad67cSLukas Czerner /* 1279386ad67cSLukas Czerner * ext4_calc_metadata_amount() has side effects, which we have 1280386ad67cSLukas Czerner * to be prepared undo if we fail to claim space. 1281386ad67cSLukas Czerner */ 1282386ad67cSLukas Czerner save_len = ei->i_da_metadata_calc_len; 1283386ad67cSLukas Czerner save_last_lblock = ei->i_da_metadata_calc_last_lblock; 1284386ad67cSLukas Czerner md_needed = EXT4_NUM_B2C(sbi, 1285386ad67cSLukas Czerner ext4_calc_metadata_amount(inode, lblock)); 1286386ad67cSLukas Czerner trace_ext4_da_reserve_space(inode, md_needed); 1287386ad67cSLukas Czerner 1288386ad67cSLukas Czerner /* 1289386ad67cSLukas Czerner * We do still charge estimated metadata to the sb though; 1290386ad67cSLukas Czerner * we cannot afford to run out of free blocks. 1291386ad67cSLukas Czerner */ 1292386ad67cSLukas Czerner if (ext4_claim_free_clusters(sbi, md_needed, 0)) { 1293386ad67cSLukas Czerner ei->i_da_metadata_calc_len = save_len; 1294386ad67cSLukas Czerner ei->i_da_metadata_calc_last_lblock = save_last_lblock; 1295386ad67cSLukas Czerner spin_unlock(&ei->i_block_reservation_lock); 1296386ad67cSLukas Czerner if (ext4_should_retry_alloc(inode->i_sb, &retries)) { 1297386ad67cSLukas Czerner cond_resched(); 1298386ad67cSLukas Czerner goto repeat; 1299386ad67cSLukas Czerner } 1300386ad67cSLukas Czerner return -ENOSPC; 1301386ad67cSLukas Czerner } 1302386ad67cSLukas Czerner ei->i_reserved_meta_blocks += md_needed; 1303386ad67cSLukas Czerner spin_unlock(&ei->i_block_reservation_lock); 1304386ad67cSLukas Czerner 1305386ad67cSLukas Czerner return 0; /* success */ 1306386ad67cSLukas Czerner } 1307386ad67cSLukas Czerner 1308386ad67cSLukas Czerner /* 13097b415bf6SAditya Kali * Reserve a single cluster located at lblock 13109d0be502STheodore Ts'o */ 131101f49d0bSTheodore Ts'o static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock) 1312d2a17637SMingming Cao { 1313030ba6bcSAneesh Kumar K.V int retries = 0; 1314d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 13150637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 13167b415bf6SAditya Kali unsigned int md_needed; 13175dd4056dSChristoph Hellwig int ret; 131803179fe9STheodore Ts'o ext4_lblk_t save_last_lblock; 131903179fe9STheodore Ts'o int save_len; 1320d2a17637SMingming Cao 132160e58e0fSMingming Cao /* 132272b8ab9dSEric Sandeen * We will charge metadata quota at writeout time; this saves 132372b8ab9dSEric Sandeen * us from metadata over-estimation, though we may go over by 132472b8ab9dSEric Sandeen * a small amount in the end. Here we just reserve for data. 132560e58e0fSMingming Cao */ 13267b415bf6SAditya Kali ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1)); 13275dd4056dSChristoph Hellwig if (ret) 13285dd4056dSChristoph Hellwig return ret; 132903179fe9STheodore Ts'o 133003179fe9STheodore Ts'o /* 133103179fe9STheodore Ts'o * recalculate the amount of metadata blocks to reserve 133203179fe9STheodore Ts'o * in order to allocate nrblocks 133303179fe9STheodore Ts'o * worse case is one extent per block 133403179fe9STheodore Ts'o */ 133503179fe9STheodore Ts'o repeat: 133603179fe9STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 133703179fe9STheodore Ts'o /* 133803179fe9STheodore Ts'o * ext4_calc_metadata_amount() has side effects, which we have 133903179fe9STheodore Ts'o * to be prepared undo if we fail to claim space. 134003179fe9STheodore Ts'o */ 134103179fe9STheodore Ts'o save_len = ei->i_da_metadata_calc_len; 134203179fe9STheodore Ts'o save_last_lblock = ei->i_da_metadata_calc_last_lblock; 134303179fe9STheodore Ts'o md_needed = EXT4_NUM_B2C(sbi, 134403179fe9STheodore Ts'o ext4_calc_metadata_amount(inode, lblock)); 134503179fe9STheodore Ts'o trace_ext4_da_reserve_space(inode, md_needed); 134603179fe9STheodore Ts'o 134772b8ab9dSEric Sandeen /* 134872b8ab9dSEric Sandeen * We do still charge estimated metadata to the sb though; 134972b8ab9dSEric Sandeen * we cannot afford to run out of free blocks. 135072b8ab9dSEric Sandeen */ 1351e7d5f315STheodore Ts'o if (ext4_claim_free_clusters(sbi, md_needed + 1, 0)) { 135203179fe9STheodore Ts'o ei->i_da_metadata_calc_len = save_len; 135303179fe9STheodore Ts'o ei->i_da_metadata_calc_last_lblock = save_last_lblock; 135403179fe9STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 1355030ba6bcSAneesh Kumar K.V if (ext4_should_retry_alloc(inode->i_sb, &retries)) { 1356bb8b20edSLukas Czerner cond_resched(); 1357030ba6bcSAneesh Kumar K.V goto repeat; 1358030ba6bcSAneesh Kumar K.V } 135903179fe9STheodore Ts'o dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1)); 1360d2a17637SMingming Cao return -ENOSPC; 1361d2a17637SMingming Cao } 13629d0be502STheodore Ts'o ei->i_reserved_data_blocks++; 13630637c6f4STheodore Ts'o ei->i_reserved_meta_blocks += md_needed; 13640637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 136539bc680aSDmitry Monakhov 1366d2a17637SMingming Cao return 0; /* success */ 1367d2a17637SMingming Cao } 1368d2a17637SMingming Cao 136912219aeaSAneesh Kumar K.V static void ext4_da_release_space(struct inode *inode, int to_free) 1370d2a17637SMingming Cao { 1371d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 13720637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 1373d2a17637SMingming Cao 1374cd213226SMingming Cao if (!to_free) 1375cd213226SMingming Cao return; /* Nothing to release, exit */ 1376cd213226SMingming Cao 1377d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1378cd213226SMingming Cao 13795a58ec87SLi Zefan trace_ext4_da_release_space(inode, to_free); 13800637c6f4STheodore Ts'o if (unlikely(to_free > ei->i_reserved_data_blocks)) { 1381cd213226SMingming Cao /* 13820637c6f4STheodore Ts'o * if there aren't enough reserved blocks, then the 13830637c6f4STheodore Ts'o * counter is messed up somewhere. Since this 13840637c6f4STheodore Ts'o * function is called from invalidate page, it's 13850637c6f4STheodore Ts'o * harmless to return without any action. 1386cd213226SMingming Cao */ 13878de5c325STheodore Ts'o ext4_warning(inode->i_sb, "ext4_da_release_space: " 13880637c6f4STheodore Ts'o "ino %lu, to_free %d with only %d reserved " 13891084f252STheodore Ts'o "data blocks", inode->i_ino, to_free, 13900637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 13910637c6f4STheodore Ts'o WARN_ON(1); 13920637c6f4STheodore Ts'o to_free = ei->i_reserved_data_blocks; 13930637c6f4STheodore Ts'o } 13940637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= to_free; 13950637c6f4STheodore Ts'o 13960637c6f4STheodore Ts'o if (ei->i_reserved_data_blocks == 0) { 13970637c6f4STheodore Ts'o /* 13980637c6f4STheodore Ts'o * We can release all of the reserved metadata blocks 13990637c6f4STheodore Ts'o * only when we have written all of the delayed 14000637c6f4STheodore Ts'o * allocation blocks. 14017b415bf6SAditya Kali * Note that in case of bigalloc, i_reserved_meta_blocks, 14027b415bf6SAditya Kali * i_reserved_data_blocks, etc. refer to number of clusters. 14030637c6f4STheodore Ts'o */ 140457042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 140572b8ab9dSEric Sandeen ei->i_reserved_meta_blocks); 1406ee5f4d9cSTheodore Ts'o ei->i_reserved_meta_blocks = 0; 14079d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 1408cd213226SMingming Cao } 1409cd213226SMingming Cao 141072b8ab9dSEric Sandeen /* update fs dirty data blocks counter */ 141157042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free); 1412d2a17637SMingming Cao 1413d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 141460e58e0fSMingming Cao 14157b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free)); 1416d2a17637SMingming Cao } 1417d2a17637SMingming Cao 1418d2a17637SMingming Cao static void ext4_da_page_release_reservation(struct page *page, 1419ca99fdd2SLukas Czerner unsigned int offset, 1420ca99fdd2SLukas Czerner unsigned int length) 1421d2a17637SMingming Cao { 1422d2a17637SMingming Cao int to_release = 0; 1423d2a17637SMingming Cao struct buffer_head *head, *bh; 1424d2a17637SMingming Cao unsigned int curr_off = 0; 14257b415bf6SAditya Kali struct inode *inode = page->mapping->host; 14267b415bf6SAditya Kali struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 1427ca99fdd2SLukas Czerner unsigned int stop = offset + length; 14287b415bf6SAditya Kali int num_clusters; 142951865fdaSZheng Liu ext4_fsblk_t lblk; 1430d2a17637SMingming Cao 1431ca99fdd2SLukas Czerner BUG_ON(stop > PAGE_CACHE_SIZE || stop < length); 1432ca99fdd2SLukas Czerner 1433d2a17637SMingming Cao head = page_buffers(page); 1434d2a17637SMingming Cao bh = head; 1435d2a17637SMingming Cao do { 1436d2a17637SMingming Cao unsigned int next_off = curr_off + bh->b_size; 1437d2a17637SMingming Cao 1438ca99fdd2SLukas Czerner if (next_off > stop) 1439ca99fdd2SLukas Czerner break; 1440ca99fdd2SLukas Czerner 1441d2a17637SMingming Cao if ((offset <= curr_off) && (buffer_delay(bh))) { 1442d2a17637SMingming Cao to_release++; 1443d2a17637SMingming Cao clear_buffer_delay(bh); 1444d2a17637SMingming Cao } 1445d2a17637SMingming Cao curr_off = next_off; 1446d2a17637SMingming Cao } while ((bh = bh->b_this_page) != head); 14477b415bf6SAditya Kali 144851865fdaSZheng Liu if (to_release) { 144951865fdaSZheng Liu lblk = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits); 145051865fdaSZheng Liu ext4_es_remove_extent(inode, lblk, to_release); 145151865fdaSZheng Liu } 145251865fdaSZheng Liu 14537b415bf6SAditya Kali /* If we have released all the blocks belonging to a cluster, then we 14547b415bf6SAditya Kali * need to release the reserved space for that cluster. */ 14557b415bf6SAditya Kali num_clusters = EXT4_NUM_B2C(sbi, to_release); 14567b415bf6SAditya Kali while (num_clusters > 0) { 14577b415bf6SAditya Kali lblk = (page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits)) + 14587b415bf6SAditya Kali ((num_clusters - 1) << sbi->s_cluster_bits); 14597b415bf6SAditya Kali if (sbi->s_cluster_ratio == 1 || 14607d1b1fbcSZheng Liu !ext4_find_delalloc_cluster(inode, lblk)) 14617b415bf6SAditya Kali ext4_da_release_space(inode, 1); 14627b415bf6SAditya Kali 14637b415bf6SAditya Kali num_clusters--; 14647b415bf6SAditya Kali } 1465d2a17637SMingming Cao } 1466ac27a0ecSDave Kleikamp 1467ac27a0ecSDave Kleikamp /* 146864769240SAlex Tomas * Delayed allocation stuff 146964769240SAlex Tomas */ 147064769240SAlex Tomas 147164769240SAlex Tomas /* 147264769240SAlex Tomas * mpage_da_submit_io - walks through extent of pages and try to write 1473a1d6cc56SAneesh Kumar K.V * them with writepage() call back 147464769240SAlex Tomas * 147564769240SAlex Tomas * @mpd->inode: inode 147664769240SAlex Tomas * @mpd->first_page: first page of the extent 147764769240SAlex Tomas * @mpd->next_page: page after the last page of the extent 147864769240SAlex Tomas * 147964769240SAlex Tomas * By the time mpage_da_submit_io() is called we expect all blocks 148064769240SAlex Tomas * to be allocated. this may be wrong if allocation failed. 148164769240SAlex Tomas * 148264769240SAlex Tomas * As pages are already locked by write_cache_pages(), we can't use it 148364769240SAlex Tomas */ 14841de3e3dfSTheodore Ts'o static int mpage_da_submit_io(struct mpage_da_data *mpd, 14851de3e3dfSTheodore Ts'o struct ext4_map_blocks *map) 148664769240SAlex Tomas { 1487791b7f08SAneesh Kumar K.V struct pagevec pvec; 1488791b7f08SAneesh Kumar K.V unsigned long index, end; 1489791b7f08SAneesh Kumar K.V int ret = 0, err, nr_pages, i; 1490791b7f08SAneesh Kumar K.V struct inode *inode = mpd->inode; 1491791b7f08SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 1492cb20d518STheodore Ts'o loff_t size = i_size_read(inode); 14933ecdb3a1STheodore Ts'o unsigned int len, block_start; 14943ecdb3a1STheodore Ts'o struct buffer_head *bh, *page_bufs = NULL; 14951de3e3dfSTheodore Ts'o sector_t pblock = 0, cur_logical = 0; 1496bd2d0210STheodore Ts'o struct ext4_io_submit io_submit; 149764769240SAlex Tomas 149864769240SAlex Tomas BUG_ON(mpd->next_page <= mpd->first_page); 1499a549984bSTheodore Ts'o memset(&io_submit, 0, sizeof(io_submit)); 1500791b7f08SAneesh Kumar K.V /* 1501791b7f08SAneesh Kumar K.V * We need to start from the first_page to the next_page - 1 1502791b7f08SAneesh Kumar K.V * to make sure we also write the mapped dirty buffer_heads. 15038dc207c0STheodore Ts'o * If we look at mpd->b_blocknr we would only be looking 1504791b7f08SAneesh Kumar K.V * at the currently mapped buffer_heads. 1505791b7f08SAneesh Kumar K.V */ 150664769240SAlex Tomas index = mpd->first_page; 150764769240SAlex Tomas end = mpd->next_page - 1; 150864769240SAlex Tomas 1509791b7f08SAneesh Kumar K.V pagevec_init(&pvec, 0); 151064769240SAlex Tomas while (index <= end) { 1511791b7f08SAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 151264769240SAlex Tomas if (nr_pages == 0) 151364769240SAlex Tomas break; 151464769240SAlex Tomas for (i = 0; i < nr_pages; i++) { 1515f8bec370SJan Kara int skip_page = 0; 151664769240SAlex Tomas struct page *page = pvec.pages[i]; 151764769240SAlex Tomas 1518791b7f08SAneesh Kumar K.V index = page->index; 1519791b7f08SAneesh Kumar K.V if (index > end) 1520791b7f08SAneesh Kumar K.V break; 1521cb20d518STheodore Ts'o 1522cb20d518STheodore Ts'o if (index == size >> PAGE_CACHE_SHIFT) 1523cb20d518STheodore Ts'o len = size & ~PAGE_CACHE_MASK; 1524cb20d518STheodore Ts'o else 1525cb20d518STheodore Ts'o len = PAGE_CACHE_SIZE; 15261de3e3dfSTheodore Ts'o if (map) { 15271de3e3dfSTheodore Ts'o cur_logical = index << (PAGE_CACHE_SHIFT - 15281de3e3dfSTheodore Ts'o inode->i_blkbits); 15291de3e3dfSTheodore Ts'o pblock = map->m_pblk + (cur_logical - 15301de3e3dfSTheodore Ts'o map->m_lblk); 15311de3e3dfSTheodore Ts'o } 1532791b7f08SAneesh Kumar K.V index++; 1533791b7f08SAneesh Kumar K.V 1534791b7f08SAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1535791b7f08SAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1536791b7f08SAneesh Kumar K.V 15373ecdb3a1STheodore Ts'o bh = page_bufs = page_buffers(page); 15383ecdb3a1STheodore Ts'o block_start = 0; 15393ecdb3a1STheodore Ts'o do { 15401de3e3dfSTheodore Ts'o if (map && (cur_logical >= map->m_lblk) && 15411de3e3dfSTheodore Ts'o (cur_logical <= (map->m_lblk + 15421de3e3dfSTheodore Ts'o (map->m_len - 1)))) { 15431de3e3dfSTheodore Ts'o if (buffer_delay(bh)) { 15441de3e3dfSTheodore Ts'o clear_buffer_delay(bh); 15451de3e3dfSTheodore Ts'o bh->b_blocknr = pblock; 15461de3e3dfSTheodore Ts'o } 15471de3e3dfSTheodore Ts'o if (buffer_unwritten(bh) || 15481de3e3dfSTheodore Ts'o buffer_mapped(bh)) 15491de3e3dfSTheodore Ts'o BUG_ON(bh->b_blocknr != pblock); 15501de3e3dfSTheodore Ts'o if (map->m_flags & EXT4_MAP_UNINIT) 15511de3e3dfSTheodore Ts'o set_buffer_uninit(bh); 15521de3e3dfSTheodore Ts'o clear_buffer_unwritten(bh); 15531de3e3dfSTheodore Ts'o } 15541de3e3dfSTheodore Ts'o 155513a79a47SYongqiang Yang /* 155613a79a47SYongqiang Yang * skip page if block allocation undone and 155713a79a47SYongqiang Yang * block is dirty 155813a79a47SYongqiang Yang */ 155913a79a47SYongqiang Yang if (ext4_bh_delay_or_unwritten(NULL, bh)) 156097498956STheodore Ts'o skip_page = 1; 15613ecdb3a1STheodore Ts'o bh = bh->b_this_page; 15623ecdb3a1STheodore Ts'o block_start += bh->b_size; 15631de3e3dfSTheodore Ts'o cur_logical++; 15641de3e3dfSTheodore Ts'o pblock++; 15651de3e3dfSTheodore Ts'o } while (bh != page_bufs); 15661de3e3dfSTheodore Ts'o 1567f8bec370SJan Kara if (skip_page) { 1568f8bec370SJan Kara unlock_page(page); 1569f8bec370SJan Kara continue; 1570f8bec370SJan Kara } 1571cb20d518STheodore Ts'o 157297498956STheodore Ts'o clear_page_dirty_for_io(page); 1573fe089c77SJan Kara err = ext4_bio_write_page(&io_submit, page, len, 1574fe089c77SJan Kara mpd->wbc); 1575cb20d518STheodore Ts'o if (!err) 1576a1d6cc56SAneesh Kumar K.V mpd->pages_written++; 157764769240SAlex Tomas /* 157864769240SAlex Tomas * In error case, we have to continue because 157964769240SAlex Tomas * remaining pages are still locked 158064769240SAlex Tomas */ 158164769240SAlex Tomas if (ret == 0) 158264769240SAlex Tomas ret = err; 158364769240SAlex Tomas } 158464769240SAlex Tomas pagevec_release(&pvec); 158564769240SAlex Tomas } 1586bd2d0210STheodore Ts'o ext4_io_submit(&io_submit); 158764769240SAlex Tomas return ret; 158864769240SAlex Tomas } 158964769240SAlex Tomas 1590c7f5938aSCurt Wohlgemuth static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd) 1591c4a0c46eSAneesh Kumar K.V { 1592c4a0c46eSAneesh Kumar K.V int nr_pages, i; 1593c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 1594c4a0c46eSAneesh Kumar K.V struct pagevec pvec; 1595c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 1596c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 159751865fdaSZheng Liu ext4_lblk_t start, last; 1598c4a0c46eSAneesh Kumar K.V 1599c7f5938aSCurt Wohlgemuth index = mpd->first_page; 1600c7f5938aSCurt Wohlgemuth end = mpd->next_page - 1; 160151865fdaSZheng Liu 160251865fdaSZheng Liu start = index << (PAGE_CACHE_SHIFT - inode->i_blkbits); 160351865fdaSZheng Liu last = end << (PAGE_CACHE_SHIFT - inode->i_blkbits); 160451865fdaSZheng Liu ext4_es_remove_extent(inode, start, last - start + 1); 160551865fdaSZheng Liu 160666bea92cSEric Sandeen pagevec_init(&pvec, 0); 1607c4a0c46eSAneesh Kumar K.V while (index <= end) { 1608c4a0c46eSAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 1609c4a0c46eSAneesh Kumar K.V if (nr_pages == 0) 1610c4a0c46eSAneesh Kumar K.V break; 1611c4a0c46eSAneesh Kumar K.V for (i = 0; i < nr_pages; i++) { 1612c4a0c46eSAneesh Kumar K.V struct page *page = pvec.pages[i]; 16139b1d0998SJan Kara if (page->index > end) 1614c4a0c46eSAneesh Kumar K.V break; 1615c4a0c46eSAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1616c4a0c46eSAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1617d47992f8SLukas Czerner block_invalidatepage(page, 0, PAGE_CACHE_SIZE); 1618c4a0c46eSAneesh Kumar K.V ClearPageUptodate(page); 1619c4a0c46eSAneesh Kumar K.V unlock_page(page); 1620c4a0c46eSAneesh Kumar K.V } 16219b1d0998SJan Kara index = pvec.pages[nr_pages - 1]->index + 1; 16229b1d0998SJan Kara pagevec_release(&pvec); 1623c4a0c46eSAneesh Kumar K.V } 1624c4a0c46eSAneesh Kumar K.V return; 1625c4a0c46eSAneesh Kumar K.V } 1626c4a0c46eSAneesh Kumar K.V 1627df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 1628df22291fSAneesh Kumar K.V { 1629df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 163092b97816STheodore Ts'o struct super_block *sb = inode->i_sb; 1631f78ee70dSLukas Czerner struct ext4_inode_info *ei = EXT4_I(inode); 163292b97816STheodore Ts'o 163392b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld", 16345dee5437STheodore Ts'o EXT4_C2B(EXT4_SB(inode->i_sb), 1635f78ee70dSLukas Czerner ext4_count_free_clusters(sb))); 163692b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Free/Dirty block details"); 163792b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "free_blocks=%lld", 1638f78ee70dSLukas Czerner (long long) EXT4_C2B(EXT4_SB(sb), 163957042651STheodore Ts'o percpu_counter_sum(&sbi->s_freeclusters_counter))); 164092b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld", 1641f78ee70dSLukas Czerner (long long) EXT4_C2B(EXT4_SB(sb), 16427b415bf6SAditya Kali percpu_counter_sum(&sbi->s_dirtyclusters_counter))); 164392b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Block reservation details"); 164492b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u", 1645f78ee70dSLukas Czerner ei->i_reserved_data_blocks); 164692b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_meta_blocks=%u", 1647f78ee70dSLukas Czerner ei->i_reserved_meta_blocks); 1648f78ee70dSLukas Czerner ext4_msg(sb, KERN_CRIT, "i_allocated_meta_blocks=%u", 1649f78ee70dSLukas Czerner ei->i_allocated_meta_blocks); 1650df22291fSAneesh Kumar K.V return; 1651df22291fSAneesh Kumar K.V } 1652df22291fSAneesh Kumar K.V 1653b920c755STheodore Ts'o /* 16545a87b7a5STheodore Ts'o * mpage_da_map_and_submit - go through given space, map them 16555a87b7a5STheodore Ts'o * if necessary, and then submit them for I/O 165664769240SAlex Tomas * 16578dc207c0STheodore Ts'o * @mpd - bh describing space 165864769240SAlex Tomas * 165964769240SAlex Tomas * The function skips space we know is already mapped to disk blocks. 166064769240SAlex Tomas * 166164769240SAlex Tomas */ 16625a87b7a5STheodore Ts'o static void mpage_da_map_and_submit(struct mpage_da_data *mpd) 166364769240SAlex Tomas { 16642ac3b6e0STheodore Ts'o int err, blks, get_blocks_flags; 16651de3e3dfSTheodore Ts'o struct ext4_map_blocks map, *mapp = NULL; 16662fa3cdfbSTheodore Ts'o sector_t next = mpd->b_blocknr; 16672fa3cdfbSTheodore Ts'o unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits; 16682fa3cdfbSTheodore Ts'o loff_t disksize = EXT4_I(mpd->inode)->i_disksize; 16692fa3cdfbSTheodore Ts'o handle_t *handle = NULL; 167064769240SAlex Tomas 167164769240SAlex Tomas /* 16725a87b7a5STheodore Ts'o * If the blocks are mapped already, or we couldn't accumulate 16735a87b7a5STheodore Ts'o * any blocks, then proceed immediately to the submission stage. 167464769240SAlex Tomas */ 16755a87b7a5STheodore Ts'o if ((mpd->b_size == 0) || 16765a87b7a5STheodore Ts'o ((mpd->b_state & (1 << BH_Mapped)) && 167729fa89d0SAneesh Kumar K.V !(mpd->b_state & (1 << BH_Delay)) && 16785a87b7a5STheodore Ts'o !(mpd->b_state & (1 << BH_Unwritten)))) 16795a87b7a5STheodore Ts'o goto submit_io; 16802fa3cdfbSTheodore Ts'o 16812fa3cdfbSTheodore Ts'o handle = ext4_journal_current_handle(); 16822fa3cdfbSTheodore Ts'o BUG_ON(!handle); 16832fa3cdfbSTheodore Ts'o 168479ffab34SAneesh Kumar K.V /* 168579e83036SEric Sandeen * Call ext4_map_blocks() to allocate any delayed allocation 16862ac3b6e0STheodore Ts'o * blocks, or to convert an uninitialized extent to be 16872ac3b6e0STheodore Ts'o * initialized (in the case where we have written into 16882ac3b6e0STheodore Ts'o * one or more preallocated blocks). 16892ac3b6e0STheodore Ts'o * 16902ac3b6e0STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to 16912ac3b6e0STheodore Ts'o * indicate that we are on the delayed allocation path. This 16922ac3b6e0STheodore Ts'o * affects functions in many different parts of the allocation 16932ac3b6e0STheodore Ts'o * call path. This flag exists primarily because we don't 169479e83036SEric Sandeen * want to change *many* call functions, so ext4_map_blocks() 1695f2321097STheodore Ts'o * will set the EXT4_STATE_DELALLOC_RESERVED flag once the 16962ac3b6e0STheodore Ts'o * inode's allocation semaphore is taken. 16972ac3b6e0STheodore Ts'o * 16982ac3b6e0STheodore Ts'o * If the blocks in questions were delalloc blocks, set 16992ac3b6e0STheodore Ts'o * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting 17002ac3b6e0STheodore Ts'o * variables are updated after the blocks have been allocated. 170179ffab34SAneesh Kumar K.V */ 17022ed88685STheodore Ts'o map.m_lblk = next; 17032ed88685STheodore Ts'o map.m_len = max_blocks; 170427dd4385SLukas Czerner /* 170527dd4385SLukas Czerner * We're in delalloc path and it is possible that we're going to 170627dd4385SLukas Czerner * need more metadata blocks than previously reserved. However 170727dd4385SLukas Czerner * we must not fail because we're in writeback and there is 170827dd4385SLukas Czerner * nothing we can do about it so it might result in data loss. 170927dd4385SLukas Czerner * So use reserved blocks to allocate metadata if possible. 171027dd4385SLukas Czerner */ 171127dd4385SLukas Czerner get_blocks_flags = EXT4_GET_BLOCKS_CREATE | 171227dd4385SLukas Czerner EXT4_GET_BLOCKS_METADATA_NOFAIL; 1713744692dcSJiaying Zhang if (ext4_should_dioread_nolock(mpd->inode)) 1714744692dcSJiaying Zhang get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; 17152ac3b6e0STheodore Ts'o if (mpd->b_state & (1 << BH_Delay)) 17161296cc85SAneesh Kumar K.V get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; 17171296cc85SAneesh Kumar K.V 171827dd4385SLukas Czerner 17192ed88685STheodore Ts'o blks = ext4_map_blocks(handle, mpd->inode, &map, get_blocks_flags); 17202fa3cdfbSTheodore Ts'o if (blks < 0) { 1721e3570639SEric Sandeen struct super_block *sb = mpd->inode->i_sb; 1722e3570639SEric Sandeen 17232fa3cdfbSTheodore Ts'o err = blks; 1724ed5bde0bSTheodore Ts'o /* 17255a87b7a5STheodore Ts'o * If get block returns EAGAIN or ENOSPC and there 172697498956STheodore Ts'o * appears to be free blocks we will just let 172797498956STheodore Ts'o * mpage_da_submit_io() unlock all of the pages. 1728c4a0c46eSAneesh Kumar K.V */ 1729c4a0c46eSAneesh Kumar K.V if (err == -EAGAIN) 17305a87b7a5STheodore Ts'o goto submit_io; 1731df22291fSAneesh Kumar K.V 17325dee5437STheodore Ts'o if (err == -ENOSPC && ext4_count_free_clusters(sb)) { 1733df22291fSAneesh Kumar K.V mpd->retval = err; 17345a87b7a5STheodore Ts'o goto submit_io; 1735df22291fSAneesh Kumar K.V } 1736df22291fSAneesh Kumar K.V 1737c4a0c46eSAneesh Kumar K.V /* 1738ed5bde0bSTheodore Ts'o * get block failure will cause us to loop in 1739ed5bde0bSTheodore Ts'o * writepages, because a_ops->writepage won't be able 1740ed5bde0bSTheodore Ts'o * to make progress. The page will be redirtied by 1741ed5bde0bSTheodore Ts'o * writepage and writepages will again try to write 1742ed5bde0bSTheodore Ts'o * the same. 1743c4a0c46eSAneesh Kumar K.V */ 1744e3570639SEric Sandeen if (!(EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) { 1745e3570639SEric Sandeen ext4_msg(sb, KERN_CRIT, 1746e3570639SEric Sandeen "delayed block allocation failed for inode %lu " 1747e3570639SEric Sandeen "at logical offset %llu with max blocks %zd " 1748e3570639SEric Sandeen "with error %d", mpd->inode->i_ino, 1749c4a0c46eSAneesh Kumar K.V (unsigned long long) next, 17508dc207c0STheodore Ts'o mpd->b_size >> mpd->inode->i_blkbits, err); 1751e3570639SEric Sandeen ext4_msg(sb, KERN_CRIT, 175201a523ebSTheodore Ts'o "This should not happen!! Data will be lost"); 1753e3570639SEric Sandeen if (err == -ENOSPC) 1754df22291fSAneesh Kumar K.V ext4_print_free_blocks(mpd->inode); 1755030ba6bcSAneesh Kumar K.V } 17562fa3cdfbSTheodore Ts'o /* invalidate all the pages */ 1757c7f5938aSCurt Wohlgemuth ext4_da_block_invalidatepages(mpd); 1758e0fd9b90SCurt Wohlgemuth 1759e0fd9b90SCurt Wohlgemuth /* Mark this page range as having been completed */ 1760e0fd9b90SCurt Wohlgemuth mpd->io_done = 1; 17615a87b7a5STheodore Ts'o return; 1762c4a0c46eSAneesh Kumar K.V } 17632fa3cdfbSTheodore Ts'o BUG_ON(blks == 0); 17642fa3cdfbSTheodore Ts'o 17651de3e3dfSTheodore Ts'o mapp = ↦ 17662ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 17672ed88685STheodore Ts'o struct block_device *bdev = mpd->inode->i_sb->s_bdev; 17682ed88685STheodore Ts'o int i; 176964769240SAlex Tomas 17702ed88685STheodore Ts'o for (i = 0; i < map.m_len; i++) 17712ed88685STheodore Ts'o unmap_underlying_metadata(bdev, map.m_pblk + i); 17722fa3cdfbSTheodore Ts'o } 17732fa3cdfbSTheodore Ts'o 17742fa3cdfbSTheodore Ts'o /* 177503f5d8bcSJan Kara * Update on-disk size along with block allocation. 17762fa3cdfbSTheodore Ts'o */ 17772fa3cdfbSTheodore Ts'o disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits; 17782fa3cdfbSTheodore Ts'o if (disksize > i_size_read(mpd->inode)) 17792fa3cdfbSTheodore Ts'o disksize = i_size_read(mpd->inode); 17802fa3cdfbSTheodore Ts'o if (disksize > EXT4_I(mpd->inode)->i_disksize) { 17812fa3cdfbSTheodore Ts'o ext4_update_i_disksize(mpd->inode, disksize); 17825a87b7a5STheodore Ts'o err = ext4_mark_inode_dirty(handle, mpd->inode); 17835a87b7a5STheodore Ts'o if (err) 17845a87b7a5STheodore Ts'o ext4_error(mpd->inode->i_sb, 17855a87b7a5STheodore Ts'o "Failed to mark inode %lu dirty", 17865a87b7a5STheodore Ts'o mpd->inode->i_ino); 17872fa3cdfbSTheodore Ts'o } 17882fa3cdfbSTheodore Ts'o 17895a87b7a5STheodore Ts'o submit_io: 17901de3e3dfSTheodore Ts'o mpage_da_submit_io(mpd, mapp); 17915a87b7a5STheodore Ts'o mpd->io_done = 1; 179264769240SAlex Tomas } 179364769240SAlex Tomas 1794bf068ee2SAneesh Kumar K.V #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \ 1795bf068ee2SAneesh Kumar K.V (1 << BH_Delay) | (1 << BH_Unwritten)) 179664769240SAlex Tomas 179764769240SAlex Tomas /* 179864769240SAlex Tomas * mpage_add_bh_to_extent - try to add one more block to extent of blocks 179964769240SAlex Tomas * 180064769240SAlex Tomas * @mpd->lbh - extent of blocks 180164769240SAlex Tomas * @logical - logical number of the block in the file 1802b6a8e62fSJan Kara * @b_state - b_state of the buffer head added 180364769240SAlex Tomas * 180464769240SAlex Tomas * the function is used to collect contig. blocks in same state 180564769240SAlex Tomas */ 1806b6a8e62fSJan Kara static void mpage_add_bh_to_extent(struct mpage_da_data *mpd, sector_t logical, 18078dc207c0STheodore Ts'o unsigned long b_state) 180864769240SAlex Tomas { 180964769240SAlex Tomas sector_t next; 1810b6a8e62fSJan Kara int blkbits = mpd->inode->i_blkbits; 1811b6a8e62fSJan Kara int nrblocks = mpd->b_size >> blkbits; 181264769240SAlex Tomas 1813c445e3e0SEric Sandeen /* 1814c445e3e0SEric Sandeen * XXX Don't go larger than mballoc is willing to allocate 1815c445e3e0SEric Sandeen * This is a stopgap solution. We eventually need to fold 1816c445e3e0SEric Sandeen * mpage_da_submit_io() into this function and then call 181779e83036SEric Sandeen * ext4_map_blocks() multiple times in a loop 1818c445e3e0SEric Sandeen */ 1819b6a8e62fSJan Kara if (nrblocks >= (8*1024*1024 >> blkbits)) 1820c445e3e0SEric Sandeen goto flush_it; 1821c445e3e0SEric Sandeen 1822525f4ed8SMingming Cao /* check if the reserved journal credits might overflow */ 1823b6a8e62fSJan Kara if (!ext4_test_inode_flag(mpd->inode, EXT4_INODE_EXTENTS)) { 1824525f4ed8SMingming Cao if (nrblocks >= EXT4_MAX_TRANS_DATA) { 1825525f4ed8SMingming Cao /* 1826525f4ed8SMingming Cao * With non-extent format we are limited by the journal 1827525f4ed8SMingming Cao * credit available. Total credit needed to insert 1828525f4ed8SMingming Cao * nrblocks contiguous blocks is dependent on the 1829525f4ed8SMingming Cao * nrblocks. So limit nrblocks. 1830525f4ed8SMingming Cao */ 1831525f4ed8SMingming Cao goto flush_it; 1832525f4ed8SMingming Cao } 1833525f4ed8SMingming Cao } 183464769240SAlex Tomas /* 183564769240SAlex Tomas * First block in the extent 183664769240SAlex Tomas */ 18378dc207c0STheodore Ts'o if (mpd->b_size == 0) { 18388dc207c0STheodore Ts'o mpd->b_blocknr = logical; 1839b6a8e62fSJan Kara mpd->b_size = 1 << blkbits; 18408dc207c0STheodore Ts'o mpd->b_state = b_state & BH_FLAGS; 184164769240SAlex Tomas return; 184264769240SAlex Tomas } 184364769240SAlex Tomas 18448dc207c0STheodore Ts'o next = mpd->b_blocknr + nrblocks; 184564769240SAlex Tomas /* 184664769240SAlex Tomas * Can we merge the block to our big extent? 184764769240SAlex Tomas */ 18488dc207c0STheodore Ts'o if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) { 1849b6a8e62fSJan Kara mpd->b_size += 1 << blkbits; 185064769240SAlex Tomas return; 185164769240SAlex Tomas } 185264769240SAlex Tomas 1853525f4ed8SMingming Cao flush_it: 185464769240SAlex Tomas /* 185564769240SAlex Tomas * We couldn't merge the block to our extent, so we 185664769240SAlex Tomas * need to flush current extent and start new one 185764769240SAlex Tomas */ 18585a87b7a5STheodore Ts'o mpage_da_map_and_submit(mpd); 1859a1d6cc56SAneesh Kumar K.V return; 186064769240SAlex Tomas } 186164769240SAlex Tomas 1862c364b22cSAneesh Kumar K.V static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh) 186329fa89d0SAneesh Kumar K.V { 1864c364b22cSAneesh Kumar K.V return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 186529fa89d0SAneesh Kumar K.V } 186629fa89d0SAneesh Kumar K.V 186764769240SAlex Tomas /* 18685356f261SAditya Kali * This function is grabs code from the very beginning of 18695356f261SAditya Kali * ext4_map_blocks, but assumes that the caller is from delayed write 18705356f261SAditya Kali * time. This function looks up the requested blocks and sets the 18715356f261SAditya Kali * buffer delay bit under the protection of i_data_sem. 18725356f261SAditya Kali */ 18735356f261SAditya Kali static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, 18745356f261SAditya Kali struct ext4_map_blocks *map, 18755356f261SAditya Kali struct buffer_head *bh) 18765356f261SAditya Kali { 1877d100eef2SZheng Liu struct extent_status es; 18785356f261SAditya Kali int retval; 18795356f261SAditya Kali sector_t invalid_block = ~((sector_t) 0xffff); 1880921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1881921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 1882921f266bSDmitry Monakhov 1883921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 1884921f266bSDmitry Monakhov #endif 18855356f261SAditya Kali 18865356f261SAditya Kali if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 18875356f261SAditya Kali invalid_block = ~0; 18885356f261SAditya Kali 18895356f261SAditya Kali map->m_flags = 0; 18905356f261SAditya Kali ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u," 18915356f261SAditya Kali "logical block %lu\n", inode->i_ino, map->m_len, 18925356f261SAditya Kali (unsigned long) map->m_lblk); 1893d100eef2SZheng Liu 1894d100eef2SZheng Liu /* Lookup extent status tree firstly */ 1895d100eef2SZheng Liu if (ext4_es_lookup_extent(inode, iblock, &es)) { 1896d100eef2SZheng Liu 1897d100eef2SZheng Liu if (ext4_es_is_hole(&es)) { 1898d100eef2SZheng Liu retval = 0; 1899d100eef2SZheng Liu down_read((&EXT4_I(inode)->i_data_sem)); 1900d100eef2SZheng Liu goto add_delayed; 1901d100eef2SZheng Liu } 1902d100eef2SZheng Liu 1903d100eef2SZheng Liu /* 1904d100eef2SZheng Liu * Delayed extent could be allocated by fallocate. 1905d100eef2SZheng Liu * So we need to check it. 1906d100eef2SZheng Liu */ 1907d100eef2SZheng Liu if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) { 1908d100eef2SZheng Liu map_bh(bh, inode->i_sb, invalid_block); 1909d100eef2SZheng Liu set_buffer_new(bh); 1910d100eef2SZheng Liu set_buffer_delay(bh); 1911d100eef2SZheng Liu return 0; 1912d100eef2SZheng Liu } 1913d100eef2SZheng Liu 1914d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk; 1915d100eef2SZheng Liu retval = es.es_len - (iblock - es.es_lblk); 1916d100eef2SZheng Liu if (retval > map->m_len) 1917d100eef2SZheng Liu retval = map->m_len; 1918d100eef2SZheng Liu map->m_len = retval; 1919d100eef2SZheng Liu if (ext4_es_is_written(&es)) 1920d100eef2SZheng Liu map->m_flags |= EXT4_MAP_MAPPED; 1921d100eef2SZheng Liu else if (ext4_es_is_unwritten(&es)) 1922d100eef2SZheng Liu map->m_flags |= EXT4_MAP_UNWRITTEN; 1923d100eef2SZheng Liu else 1924d100eef2SZheng Liu BUG_ON(1); 1925d100eef2SZheng Liu 1926921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1927921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0); 1928921f266bSDmitry Monakhov #endif 1929d100eef2SZheng Liu return retval; 1930d100eef2SZheng Liu } 1931d100eef2SZheng Liu 19325356f261SAditya Kali /* 19335356f261SAditya Kali * Try to see if we can get the block without requesting a new 19345356f261SAditya Kali * file system block. 19355356f261SAditya Kali */ 19365356f261SAditya Kali down_read((&EXT4_I(inode)->i_data_sem)); 19379c3569b5STao Ma if (ext4_has_inline_data(inode)) { 19389c3569b5STao Ma /* 19399c3569b5STao Ma * We will soon create blocks for this page, and let 19409c3569b5STao Ma * us pretend as if the blocks aren't allocated yet. 19419c3569b5STao Ma * In case of clusters, we have to handle the work 19429c3569b5STao Ma * of mapping from cluster so that the reserved space 19439c3569b5STao Ma * is calculated properly. 19449c3569b5STao Ma */ 19459c3569b5STao Ma if ((EXT4_SB(inode->i_sb)->s_cluster_ratio > 1) && 19469c3569b5STao Ma ext4_find_delalloc_cluster(inode, map->m_lblk)) 19479c3569b5STao Ma map->m_flags |= EXT4_MAP_FROM_CLUSTER; 19489c3569b5STao Ma retval = 0; 19499c3569b5STao Ma } else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 1950d100eef2SZheng Liu retval = ext4_ext_map_blocks(NULL, inode, map, 1951d100eef2SZheng Liu EXT4_GET_BLOCKS_NO_PUT_HOLE); 19525356f261SAditya Kali else 1953d100eef2SZheng Liu retval = ext4_ind_map_blocks(NULL, inode, map, 1954d100eef2SZheng Liu EXT4_GET_BLOCKS_NO_PUT_HOLE); 19555356f261SAditya Kali 1956d100eef2SZheng Liu add_delayed: 19575356f261SAditya Kali if (retval == 0) { 1958f7fec032SZheng Liu int ret; 19595356f261SAditya Kali /* 19605356f261SAditya Kali * XXX: __block_prepare_write() unmaps passed block, 19615356f261SAditya Kali * is it OK? 19625356f261SAditya Kali */ 1963386ad67cSLukas Czerner /* 1964386ad67cSLukas Czerner * If the block was allocated from previously allocated cluster, 1965386ad67cSLukas Czerner * then we don't need to reserve it again. However we still need 1966386ad67cSLukas Czerner * to reserve metadata for every block we're going to write. 1967386ad67cSLukas Czerner */ 19685356f261SAditya Kali if (!(map->m_flags & EXT4_MAP_FROM_CLUSTER)) { 1969f7fec032SZheng Liu ret = ext4_da_reserve_space(inode, iblock); 1970f7fec032SZheng Liu if (ret) { 19715356f261SAditya Kali /* not enough space to reserve */ 1972f7fec032SZheng Liu retval = ret; 19735356f261SAditya Kali goto out_unlock; 19745356f261SAditya Kali } 1975386ad67cSLukas Czerner } else { 1976386ad67cSLukas Czerner ret = ext4_da_reserve_metadata(inode, iblock); 1977386ad67cSLukas Czerner if (ret) { 1978386ad67cSLukas Czerner /* not enough space to reserve */ 1979386ad67cSLukas Czerner retval = ret; 1980386ad67cSLukas Czerner goto out_unlock; 1981386ad67cSLukas Czerner } 1982f7fec032SZheng Liu } 19835356f261SAditya Kali 1984f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 1985fdc0212eSZheng Liu ~0, EXTENT_STATUS_DELAYED); 1986f7fec032SZheng Liu if (ret) { 1987f7fec032SZheng Liu retval = ret; 198851865fdaSZheng Liu goto out_unlock; 1989f7fec032SZheng Liu } 199051865fdaSZheng Liu 19915356f261SAditya Kali /* Clear EXT4_MAP_FROM_CLUSTER flag since its purpose is served 19925356f261SAditya Kali * and it should not appear on the bh->b_state. 19935356f261SAditya Kali */ 19945356f261SAditya Kali map->m_flags &= ~EXT4_MAP_FROM_CLUSTER; 19955356f261SAditya Kali 19965356f261SAditya Kali map_bh(bh, inode->i_sb, invalid_block); 19975356f261SAditya Kali set_buffer_new(bh); 19985356f261SAditya Kali set_buffer_delay(bh); 1999f7fec032SZheng Liu } else if (retval > 0) { 2000f7fec032SZheng Liu int ret; 2001f7fec032SZheng Liu unsigned long long status; 2002f7fec032SZheng Liu 2003921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 2004921f266bSDmitry Monakhov if (retval != map->m_len) { 2005921f266bSDmitry Monakhov printk("ES len assertation failed for inode: %lu " 2006921f266bSDmitry Monakhov "retval %d != map->m_len %d " 2007921f266bSDmitry Monakhov "in %s (lookup)\n", inode->i_ino, retval, 2008921f266bSDmitry Monakhov map->m_len, __func__); 2009921f266bSDmitry Monakhov } 2010921f266bSDmitry Monakhov #endif 2011921f266bSDmitry Monakhov 2012f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 2013f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 2014f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 2015f7fec032SZheng Liu map->m_pblk, status); 2016f7fec032SZheng Liu if (ret != 0) 2017f7fec032SZheng Liu retval = ret; 20185356f261SAditya Kali } 20195356f261SAditya Kali 20205356f261SAditya Kali out_unlock: 20215356f261SAditya Kali up_read((&EXT4_I(inode)->i_data_sem)); 20225356f261SAditya Kali 20235356f261SAditya Kali return retval; 20245356f261SAditya Kali } 20255356f261SAditya Kali 20265356f261SAditya Kali /* 2027b920c755STheodore Ts'o * This is a special get_blocks_t callback which is used by 2028b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 2029b920c755STheodore Ts'o * reserve space for a single block. 203029fa89d0SAneesh Kumar K.V * 203129fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 203229fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 203329fa89d0SAneesh Kumar K.V * 203429fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 203529fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 203629fa89d0SAneesh Kumar K.V * initialized properly. 203764769240SAlex Tomas */ 20389c3569b5STao Ma int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 20392ed88685STheodore Ts'o struct buffer_head *bh, int create) 204064769240SAlex Tomas { 20412ed88685STheodore Ts'o struct ext4_map_blocks map; 204264769240SAlex Tomas int ret = 0; 204364769240SAlex Tomas 204464769240SAlex Tomas BUG_ON(create == 0); 20452ed88685STheodore Ts'o BUG_ON(bh->b_size != inode->i_sb->s_blocksize); 20462ed88685STheodore Ts'o 20472ed88685STheodore Ts'o map.m_lblk = iblock; 20482ed88685STheodore Ts'o map.m_len = 1; 204964769240SAlex Tomas 205064769240SAlex Tomas /* 205164769240SAlex Tomas * first, we need to know whether the block is allocated already 205264769240SAlex Tomas * preallocated blocks are unmapped but should treated 205364769240SAlex Tomas * the same as allocated blocks. 205464769240SAlex Tomas */ 20555356f261SAditya Kali ret = ext4_da_map_blocks(inode, iblock, &map, bh); 20565356f261SAditya Kali if (ret <= 0) 20572ed88685STheodore Ts'o return ret; 205864769240SAlex Tomas 20592ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 20602ed88685STheodore Ts'o bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; 20612ed88685STheodore Ts'o 20622ed88685STheodore Ts'o if (buffer_unwritten(bh)) { 20632ed88685STheodore Ts'o /* A delayed write to unwritten bh should be marked 20642ed88685STheodore Ts'o * new and mapped. Mapped ensures that we don't do 20652ed88685STheodore Ts'o * get_block multiple times when we write to the same 20662ed88685STheodore Ts'o * offset and new ensures that we do proper zero out 20672ed88685STheodore Ts'o * for partial write. 20682ed88685STheodore Ts'o */ 20692ed88685STheodore Ts'o set_buffer_new(bh); 2070c8205636STheodore Ts'o set_buffer_mapped(bh); 20712ed88685STheodore Ts'o } 20722ed88685STheodore Ts'o return 0; 207364769240SAlex Tomas } 207461628a3fSMingming Cao 207562e086beSAneesh Kumar K.V static int bget_one(handle_t *handle, struct buffer_head *bh) 207662e086beSAneesh Kumar K.V { 207762e086beSAneesh Kumar K.V get_bh(bh); 207862e086beSAneesh Kumar K.V return 0; 207962e086beSAneesh Kumar K.V } 208062e086beSAneesh Kumar K.V 208162e086beSAneesh Kumar K.V static int bput_one(handle_t *handle, struct buffer_head *bh) 208262e086beSAneesh Kumar K.V { 208362e086beSAneesh Kumar K.V put_bh(bh); 208462e086beSAneesh Kumar K.V return 0; 208562e086beSAneesh Kumar K.V } 208662e086beSAneesh Kumar K.V 208762e086beSAneesh Kumar K.V static int __ext4_journalled_writepage(struct page *page, 208862e086beSAneesh Kumar K.V unsigned int len) 208962e086beSAneesh Kumar K.V { 209062e086beSAneesh Kumar K.V struct address_space *mapping = page->mapping; 209162e086beSAneesh Kumar K.V struct inode *inode = mapping->host; 20923fdcfb66STao Ma struct buffer_head *page_bufs = NULL; 209362e086beSAneesh Kumar K.V handle_t *handle = NULL; 20943fdcfb66STao Ma int ret = 0, err = 0; 20953fdcfb66STao Ma int inline_data = ext4_has_inline_data(inode); 20963fdcfb66STao Ma struct buffer_head *inode_bh = NULL; 209762e086beSAneesh Kumar K.V 2098cb20d518STheodore Ts'o ClearPageChecked(page); 20993fdcfb66STao Ma 21003fdcfb66STao Ma if (inline_data) { 21013fdcfb66STao Ma BUG_ON(page->index != 0); 21023fdcfb66STao Ma BUG_ON(len > ext4_get_max_inline_size(inode)); 21033fdcfb66STao Ma inode_bh = ext4_journalled_write_inline_data(inode, len, page); 21043fdcfb66STao Ma if (inode_bh == NULL) 21053fdcfb66STao Ma goto out; 21063fdcfb66STao Ma } else { 210762e086beSAneesh Kumar K.V page_bufs = page_buffers(page); 21083fdcfb66STao Ma if (!page_bufs) { 21093fdcfb66STao Ma BUG(); 21103fdcfb66STao Ma goto out; 21113fdcfb66STao Ma } 21123fdcfb66STao Ma ext4_walk_page_buffers(handle, page_bufs, 0, len, 21133fdcfb66STao Ma NULL, bget_one); 21143fdcfb66STao Ma } 211562e086beSAneesh Kumar K.V /* As soon as we unlock the page, it can go away, but we have 211662e086beSAneesh Kumar K.V * references to buffers so we are safe */ 211762e086beSAneesh Kumar K.V unlock_page(page); 211862e086beSAneesh Kumar K.V 21199924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 21209924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 212162e086beSAneesh Kumar K.V if (IS_ERR(handle)) { 212262e086beSAneesh Kumar K.V ret = PTR_ERR(handle); 212362e086beSAneesh Kumar K.V goto out; 212462e086beSAneesh Kumar K.V } 212562e086beSAneesh Kumar K.V 2126441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 2127441c8508SCurt Wohlgemuth 21283fdcfb66STao Ma if (inline_data) { 21293fdcfb66STao Ma ret = ext4_journal_get_write_access(handle, inode_bh); 21303fdcfb66STao Ma 21313fdcfb66STao Ma err = ext4_handle_dirty_metadata(handle, inode, inode_bh); 21323fdcfb66STao Ma 21333fdcfb66STao Ma } else { 2134f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 213562e086beSAneesh Kumar K.V do_journal_get_write_access); 213662e086beSAneesh Kumar K.V 2137f19d5870STao Ma err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 213862e086beSAneesh Kumar K.V write_end_fn); 21393fdcfb66STao Ma } 214062e086beSAneesh Kumar K.V if (ret == 0) 214162e086beSAneesh Kumar K.V ret = err; 21422d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 214362e086beSAneesh Kumar K.V err = ext4_journal_stop(handle); 214462e086beSAneesh Kumar K.V if (!ret) 214562e086beSAneesh Kumar K.V ret = err; 214662e086beSAneesh Kumar K.V 21473fdcfb66STao Ma if (!ext4_has_inline_data(inode)) 21483fdcfb66STao Ma ext4_walk_page_buffers(handle, page_bufs, 0, len, 21493fdcfb66STao Ma NULL, bput_one); 215019f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 215162e086beSAneesh Kumar K.V out: 21523fdcfb66STao Ma brelse(inode_bh); 215362e086beSAneesh Kumar K.V return ret; 215462e086beSAneesh Kumar K.V } 215562e086beSAneesh Kumar K.V 215661628a3fSMingming Cao /* 215743ce1d23SAneesh Kumar K.V * Note that we don't need to start a transaction unless we're journaling data 215843ce1d23SAneesh Kumar K.V * because we should have holes filled from ext4_page_mkwrite(). We even don't 215943ce1d23SAneesh Kumar K.V * need to file the inode to the transaction's list in ordered mode because if 216043ce1d23SAneesh Kumar K.V * we are writing back data added by write(), the inode is already there and if 216143ce1d23SAneesh Kumar K.V * we are writing back data modified via mmap(), no one guarantees in which 216243ce1d23SAneesh Kumar K.V * transaction the data will hit the disk. In case we are journaling data, we 216343ce1d23SAneesh Kumar K.V * cannot start transaction directly because transaction start ranks above page 216443ce1d23SAneesh Kumar K.V * lock so we have to do some magic. 216543ce1d23SAneesh Kumar K.V * 2166b920c755STheodore Ts'o * This function can get called via... 2167b920c755STheodore Ts'o * - ext4_da_writepages after taking page lock (have journal handle) 2168b920c755STheodore Ts'o * - journal_submit_inode_data_buffers (no journal handle) 2169f6463b0dSArtem Bityutskiy * - shrink_page_list via the kswapd/direct reclaim (no journal handle) 2170b920c755STheodore Ts'o * - grab_page_cache when doing write_begin (have journal handle) 217143ce1d23SAneesh Kumar K.V * 217243ce1d23SAneesh Kumar K.V * We don't do any block allocation in this function. If we have page with 217343ce1d23SAneesh Kumar K.V * multiple blocks we need to write those buffer_heads that are mapped. This 217443ce1d23SAneesh Kumar K.V * is important for mmaped based write. So if we do with blocksize 1K 217543ce1d23SAneesh Kumar K.V * truncate(f, 1024); 217643ce1d23SAneesh Kumar K.V * a = mmap(f, 0, 4096); 217743ce1d23SAneesh Kumar K.V * a[0] = 'a'; 217843ce1d23SAneesh Kumar K.V * truncate(f, 4096); 217943ce1d23SAneesh Kumar K.V * we have in the page first buffer_head mapped via page_mkwrite call back 218090802ed9SPaul Bolle * but other buffer_heads would be unmapped but dirty (dirty done via the 218143ce1d23SAneesh Kumar K.V * do_wp_page). So writepage should write the first block. If we modify 218243ce1d23SAneesh Kumar K.V * the mmap area beyond 1024 we will again get a page_fault and the 218343ce1d23SAneesh Kumar K.V * page_mkwrite callback will do the block allocation and mark the 218443ce1d23SAneesh Kumar K.V * buffer_heads mapped. 218543ce1d23SAneesh Kumar K.V * 218643ce1d23SAneesh Kumar K.V * We redirty the page if we have any buffer_heads that is either delay or 218743ce1d23SAneesh Kumar K.V * unwritten in the page. 218843ce1d23SAneesh Kumar K.V * 218943ce1d23SAneesh Kumar K.V * We can get recursively called as show below. 219043ce1d23SAneesh Kumar K.V * 219143ce1d23SAneesh Kumar K.V * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> 219243ce1d23SAneesh Kumar K.V * ext4_writepage() 219343ce1d23SAneesh Kumar K.V * 219443ce1d23SAneesh Kumar K.V * But since we don't do any block allocation we should not deadlock. 219543ce1d23SAneesh Kumar K.V * Page also have the dirty flag cleared so we don't get recurive page_lock. 219661628a3fSMingming Cao */ 219743ce1d23SAneesh Kumar K.V static int ext4_writepage(struct page *page, 219864769240SAlex Tomas struct writeback_control *wbc) 219964769240SAlex Tomas { 2200f8bec370SJan Kara int ret = 0; 220161628a3fSMingming Cao loff_t size; 2202498e5f24STheodore Ts'o unsigned int len; 2203744692dcSJiaying Zhang struct buffer_head *page_bufs = NULL; 220461628a3fSMingming Cao struct inode *inode = page->mapping->host; 220536ade451SJan Kara struct ext4_io_submit io_submit; 220664769240SAlex Tomas 2207a9c667f8SLukas Czerner trace_ext4_writepage(page); 220861628a3fSMingming Cao size = i_size_read(inode); 220961628a3fSMingming Cao if (page->index == size >> PAGE_CACHE_SHIFT) 221061628a3fSMingming Cao len = size & ~PAGE_CACHE_MASK; 221161628a3fSMingming Cao else 221261628a3fSMingming Cao len = PAGE_CACHE_SIZE; 221361628a3fSMingming Cao 2214f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 221564769240SAlex Tomas /* 2216fe386132SJan Kara * We cannot do block allocation or other extent handling in this 2217fe386132SJan Kara * function. If there are buffers needing that, we have to redirty 2218fe386132SJan Kara * the page. But we may reach here when we do a journal commit via 2219fe386132SJan Kara * journal_submit_inode_data_buffers() and in that case we must write 2220fe386132SJan Kara * allocated buffers to achieve data=ordered mode guarantees. 222164769240SAlex Tomas */ 2222f19d5870STao Ma if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2223c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 222461628a3fSMingming Cao redirty_page_for_writepage(wbc, page); 2225fe386132SJan Kara if (current->flags & PF_MEMALLOC) { 2226fe386132SJan Kara /* 2227fe386132SJan Kara * For memory cleaning there's no point in writing only 2228fe386132SJan Kara * some buffers. So just bail out. Warn if we came here 2229fe386132SJan Kara * from direct reclaim. 2230fe386132SJan Kara */ 2231fe386132SJan Kara WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) 2232fe386132SJan Kara == PF_MEMALLOC); 223361628a3fSMingming Cao unlock_page(page); 223461628a3fSMingming Cao return 0; 223561628a3fSMingming Cao } 2236f0e6c985SAneesh Kumar K.V } 223764769240SAlex Tomas 2238cb20d518STheodore Ts'o if (PageChecked(page) && ext4_should_journal_data(inode)) 223943ce1d23SAneesh Kumar K.V /* 224043ce1d23SAneesh Kumar K.V * It's mmapped pagecache. Add buffers and journal it. There 224143ce1d23SAneesh Kumar K.V * doesn't seem much point in redirtying the page here. 224243ce1d23SAneesh Kumar K.V */ 22433f0ca309SWu Fengguang return __ext4_journalled_writepage(page, len); 224443ce1d23SAneesh Kumar K.V 2245a549984bSTheodore Ts'o memset(&io_submit, 0, sizeof(io_submit)); 224636ade451SJan Kara ret = ext4_bio_write_page(&io_submit, page, len, wbc); 224736ade451SJan Kara ext4_io_submit(&io_submit); 224864769240SAlex Tomas return ret; 224964769240SAlex Tomas } 225064769240SAlex Tomas 225161628a3fSMingming Cao /* 2252525f4ed8SMingming Cao * This is called via ext4_da_writepages() to 225325985edcSLucas De Marchi * calculate the total number of credits to reserve to fit 2254525f4ed8SMingming Cao * a single extent allocation into a single transaction, 2255525f4ed8SMingming Cao * ext4_da_writpeages() will loop calling this before 2256525f4ed8SMingming Cao * the block allocation. 225761628a3fSMingming Cao */ 2258525f4ed8SMingming Cao 2259525f4ed8SMingming Cao static int ext4_da_writepages_trans_blocks(struct inode *inode) 2260525f4ed8SMingming Cao { 2261525f4ed8SMingming Cao int max_blocks = EXT4_I(inode)->i_reserved_data_blocks; 2262525f4ed8SMingming Cao 2263525f4ed8SMingming Cao /* 2264525f4ed8SMingming Cao * With non-extent format the journal credit needed to 2265525f4ed8SMingming Cao * insert nrblocks contiguous block is dependent on 2266525f4ed8SMingming Cao * number of contiguous block. So we will limit 2267525f4ed8SMingming Cao * number of contiguous block to a sane value 2268525f4ed8SMingming Cao */ 226912e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) && 2270525f4ed8SMingming Cao (max_blocks > EXT4_MAX_TRANS_DATA)) 2271525f4ed8SMingming Cao max_blocks = EXT4_MAX_TRANS_DATA; 2272525f4ed8SMingming Cao 2273525f4ed8SMingming Cao return ext4_chunk_trans_blocks(inode, max_blocks); 2274525f4ed8SMingming Cao } 227561628a3fSMingming Cao 22768e48dcfbSTheodore Ts'o /* 22778e48dcfbSTheodore Ts'o * write_cache_pages_da - walk the list of dirty pages of the given 22788eb9e5ceSTheodore Ts'o * address space and accumulate pages that need writing, and call 2279168fc022STheodore Ts'o * mpage_da_map_and_submit to map a single contiguous memory region 2280168fc022STheodore Ts'o * and then write them. 22818e48dcfbSTheodore Ts'o */ 22829c3569b5STao Ma static int write_cache_pages_da(handle_t *handle, 22839c3569b5STao Ma struct address_space *mapping, 22848e48dcfbSTheodore Ts'o struct writeback_control *wbc, 228572f84e65SEric Sandeen struct mpage_da_data *mpd, 228672f84e65SEric Sandeen pgoff_t *done_index) 22878e48dcfbSTheodore Ts'o { 22888eb9e5ceSTheodore Ts'o struct buffer_head *bh, *head; 2289168fc022STheodore Ts'o struct inode *inode = mapping->host; 22908e48dcfbSTheodore Ts'o struct pagevec pvec; 22914f01b02cSTheodore Ts'o unsigned int nr_pages; 22924f01b02cSTheodore Ts'o sector_t logical; 22934f01b02cSTheodore Ts'o pgoff_t index, end; 22948e48dcfbSTheodore Ts'o long nr_to_write = wbc->nr_to_write; 22954f01b02cSTheodore Ts'o int i, tag, ret = 0; 22968e48dcfbSTheodore Ts'o 2297168fc022STheodore Ts'o memset(mpd, 0, sizeof(struct mpage_da_data)); 2298168fc022STheodore Ts'o mpd->wbc = wbc; 2299168fc022STheodore Ts'o mpd->inode = inode; 23008e48dcfbSTheodore Ts'o pagevec_init(&pvec, 0); 23018e48dcfbSTheodore Ts'o index = wbc->range_start >> PAGE_CACHE_SHIFT; 23028e48dcfbSTheodore Ts'o end = wbc->range_end >> PAGE_CACHE_SHIFT; 23038e48dcfbSTheodore Ts'o 23046e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 23055b41d924SEric Sandeen tag = PAGECACHE_TAG_TOWRITE; 23065b41d924SEric Sandeen else 23075b41d924SEric Sandeen tag = PAGECACHE_TAG_DIRTY; 23085b41d924SEric Sandeen 230972f84e65SEric Sandeen *done_index = index; 23104f01b02cSTheodore Ts'o while (index <= end) { 23115b41d924SEric Sandeen nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, 23128e48dcfbSTheodore Ts'o min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); 23138e48dcfbSTheodore Ts'o if (nr_pages == 0) 23144f01b02cSTheodore Ts'o return 0; 23158e48dcfbSTheodore Ts'o 23168e48dcfbSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 23178e48dcfbSTheodore Ts'o struct page *page = pvec.pages[i]; 23188e48dcfbSTheodore Ts'o 23198e48dcfbSTheodore Ts'o /* 23208e48dcfbSTheodore Ts'o * At this point, the page may be truncated or 23218e48dcfbSTheodore Ts'o * invalidated (changing page->mapping to NULL), or 23228e48dcfbSTheodore Ts'o * even swizzled back from swapper_space to tmpfs file 23238e48dcfbSTheodore Ts'o * mapping. However, page->index will not change 23248e48dcfbSTheodore Ts'o * because we have a reference on the page. 23258e48dcfbSTheodore Ts'o */ 23264f01b02cSTheodore Ts'o if (page->index > end) 23274f01b02cSTheodore Ts'o goto out; 23288e48dcfbSTheodore Ts'o 232972f84e65SEric Sandeen *done_index = page->index + 1; 233072f84e65SEric Sandeen 233178aaced3STheodore Ts'o /* 233278aaced3STheodore Ts'o * If we can't merge this page, and we have 233378aaced3STheodore Ts'o * accumulated an contiguous region, write it 233478aaced3STheodore Ts'o */ 233578aaced3STheodore Ts'o if ((mpd->next_page != page->index) && 233678aaced3STheodore Ts'o (mpd->next_page != mpd->first_page)) { 233778aaced3STheodore Ts'o mpage_da_map_and_submit(mpd); 233878aaced3STheodore Ts'o goto ret_extent_tail; 233978aaced3STheodore Ts'o } 234078aaced3STheodore Ts'o 23418e48dcfbSTheodore Ts'o lock_page(page); 23428e48dcfbSTheodore Ts'o 23438e48dcfbSTheodore Ts'o /* 23444f01b02cSTheodore Ts'o * If the page is no longer dirty, or its 23454f01b02cSTheodore Ts'o * mapping no longer corresponds to inode we 23464f01b02cSTheodore Ts'o * are writing (which means it has been 23474f01b02cSTheodore Ts'o * truncated or invalidated), or the page is 23484f01b02cSTheodore Ts'o * already under writeback and we are not 23494f01b02cSTheodore Ts'o * doing a data integrity writeback, skip the page 23508e48dcfbSTheodore Ts'o */ 23514f01b02cSTheodore Ts'o if (!PageDirty(page) || 23524f01b02cSTheodore Ts'o (PageWriteback(page) && 23534f01b02cSTheodore Ts'o (wbc->sync_mode == WB_SYNC_NONE)) || 23544f01b02cSTheodore Ts'o unlikely(page->mapping != mapping)) { 23558e48dcfbSTheodore Ts'o unlock_page(page); 23568e48dcfbSTheodore Ts'o continue; 23578e48dcfbSTheodore Ts'o } 23588e48dcfbSTheodore Ts'o 23598e48dcfbSTheodore Ts'o wait_on_page_writeback(page); 23608e48dcfbSTheodore Ts'o BUG_ON(PageWriteback(page)); 23618e48dcfbSTheodore Ts'o 23629c3569b5STao Ma /* 23639c3569b5STao Ma * If we have inline data and arrive here, it means that 23649c3569b5STao Ma * we will soon create the block for the 1st page, so 23659c3569b5STao Ma * we'd better clear the inline data here. 23669c3569b5STao Ma */ 23679c3569b5STao Ma if (ext4_has_inline_data(inode)) { 23689c3569b5STao Ma BUG_ON(ext4_test_inode_state(inode, 23699c3569b5STao Ma EXT4_STATE_MAY_INLINE_DATA)); 23709c3569b5STao Ma ext4_destroy_inline_data(handle, inode); 23719c3569b5STao Ma } 23729c3569b5STao Ma 2373168fc022STheodore Ts'o if (mpd->next_page != page->index) 23748eb9e5ceSTheodore Ts'o mpd->first_page = page->index; 23758eb9e5ceSTheodore Ts'o mpd->next_page = page->index + 1; 23768eb9e5ceSTheodore Ts'o logical = (sector_t) page->index << 23778eb9e5ceSTheodore Ts'o (PAGE_CACHE_SHIFT - inode->i_blkbits); 23788eb9e5ceSTheodore Ts'o 2379f8bec370SJan Kara /* Add all dirty buffers to mpd */ 23808eb9e5ceSTheodore Ts'o head = page_buffers(page); 23818eb9e5ceSTheodore Ts'o bh = head; 23828eb9e5ceSTheodore Ts'o do { 23838eb9e5ceSTheodore Ts'o BUG_ON(buffer_locked(bh)); 23848eb9e5ceSTheodore Ts'o /* 2385f8bec370SJan Kara * We need to try to allocate unmapped blocks 2386f8bec370SJan Kara * in the same page. Otherwise we won't make 2387f8bec370SJan Kara * progress with the page in ext4_writepage 23888eb9e5ceSTheodore Ts'o */ 23898eb9e5ceSTheodore Ts'o if (ext4_bh_delay_or_unwritten(NULL, bh)) { 23908eb9e5ceSTheodore Ts'o mpage_add_bh_to_extent(mpd, logical, 23918eb9e5ceSTheodore Ts'o bh->b_state); 23924f01b02cSTheodore Ts'o if (mpd->io_done) 23934f01b02cSTheodore Ts'o goto ret_extent_tail; 2394f8bec370SJan Kara } else if (buffer_dirty(bh) && 2395f8bec370SJan Kara buffer_mapped(bh)) { 23968eb9e5ceSTheodore Ts'o /* 2397f8bec370SJan Kara * mapped dirty buffer. We need to 2398f8bec370SJan Kara * update the b_state because we look 2399f8bec370SJan Kara * at b_state in mpage_da_map_blocks. 2400f8bec370SJan Kara * We don't update b_size because if we 2401f8bec370SJan Kara * find an unmapped buffer_head later 2402f8bec370SJan Kara * we need to use the b_state flag of 2403f8bec370SJan Kara * that buffer_head. 24048eb9e5ceSTheodore Ts'o */ 24058eb9e5ceSTheodore Ts'o if (mpd->b_size == 0) 2406f8bec370SJan Kara mpd->b_state = 2407f8bec370SJan Kara bh->b_state & BH_FLAGS; 24088e48dcfbSTheodore Ts'o } 24098eb9e5ceSTheodore Ts'o logical++; 24108eb9e5ceSTheodore Ts'o } while ((bh = bh->b_this_page) != head); 24118e48dcfbSTheodore Ts'o 24128e48dcfbSTheodore Ts'o if (nr_to_write > 0) { 24138e48dcfbSTheodore Ts'o nr_to_write--; 24148e48dcfbSTheodore Ts'o if (nr_to_write == 0 && 24154f01b02cSTheodore Ts'o wbc->sync_mode == WB_SYNC_NONE) 24168e48dcfbSTheodore Ts'o /* 24178e48dcfbSTheodore Ts'o * We stop writing back only if we are 24188e48dcfbSTheodore Ts'o * not doing integrity sync. In case of 24198e48dcfbSTheodore Ts'o * integrity sync we have to keep going 24208e48dcfbSTheodore Ts'o * because someone may be concurrently 24218e48dcfbSTheodore Ts'o * dirtying pages, and we might have 24228e48dcfbSTheodore Ts'o * synced a lot of newly appeared dirty 24238e48dcfbSTheodore Ts'o * pages, but have not synced all of the 24248e48dcfbSTheodore Ts'o * old dirty pages. 24258e48dcfbSTheodore Ts'o */ 24264f01b02cSTheodore Ts'o goto out; 24278e48dcfbSTheodore Ts'o } 24288e48dcfbSTheodore Ts'o } 24298e48dcfbSTheodore Ts'o pagevec_release(&pvec); 24308e48dcfbSTheodore Ts'o cond_resched(); 24318e48dcfbSTheodore Ts'o } 24324f01b02cSTheodore Ts'o return 0; 24334f01b02cSTheodore Ts'o ret_extent_tail: 24344f01b02cSTheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 24358eb9e5ceSTheodore Ts'o out: 24368eb9e5ceSTheodore Ts'o pagevec_release(&pvec); 24378eb9e5ceSTheodore Ts'o cond_resched(); 24388e48dcfbSTheodore Ts'o return ret; 24398e48dcfbSTheodore Ts'o } 24408e48dcfbSTheodore Ts'o 24418e48dcfbSTheodore Ts'o 244264769240SAlex Tomas static int ext4_da_writepages(struct address_space *mapping, 244364769240SAlex Tomas struct writeback_control *wbc) 244464769240SAlex Tomas { 244522208dedSAneesh Kumar K.V pgoff_t index; 244622208dedSAneesh Kumar K.V int range_whole = 0; 244761628a3fSMingming Cao handle_t *handle = NULL; 2448df22291fSAneesh Kumar K.V struct mpage_da_data mpd; 24495e745b04SAneesh Kumar K.V struct inode *inode = mapping->host; 2450498e5f24STheodore Ts'o int pages_written = 0; 245155138e0bSTheodore Ts'o unsigned int max_pages; 24522acf2c26SAneesh Kumar K.V int range_cyclic, cycled = 1, io_done = 0; 245355138e0bSTheodore Ts'o int needed_blocks, ret = 0; 245455138e0bSTheodore Ts'o long desired_nr_to_write, nr_to_writebump = 0; 2455de89de6eSTheodore Ts'o loff_t range_start = wbc->range_start; 24565e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 245772f84e65SEric Sandeen pgoff_t done_index = 0; 24585b41d924SEric Sandeen pgoff_t end; 24591bce63d1SShaohua Li struct blk_plug plug; 246061628a3fSMingming Cao 24619bffad1eSTheodore Ts'o trace_ext4_da_writepages(inode, wbc); 2462ba80b101STheodore Ts'o 246361628a3fSMingming Cao /* 246461628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 246561628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 246661628a3fSMingming Cao * because that could violate lock ordering on umount 246761628a3fSMingming Cao */ 2468a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 246961628a3fSMingming Cao return 0; 24702a21e37eSTheodore Ts'o 24712a21e37eSTheodore Ts'o /* 24722a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 24732a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 24742a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 24754ab2f15bSTheodore Ts'o * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because 24762a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 24772a21e37eSTheodore Ts'o * read-only, and in that case, ext4_da_writepages should 24782a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 24792a21e37eSTheodore Ts'o * the stack trace. 24802a21e37eSTheodore Ts'o */ 24814ab2f15bSTheodore Ts'o if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) 24822a21e37eSTheodore Ts'o return -EROFS; 24832a21e37eSTheodore Ts'o 248422208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 248522208dedSAneesh Kumar K.V range_whole = 1; 248661628a3fSMingming Cao 24872acf2c26SAneesh Kumar K.V range_cyclic = wbc->range_cyclic; 24882acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 248922208dedSAneesh Kumar K.V index = mapping->writeback_index; 24902acf2c26SAneesh Kumar K.V if (index) 24912acf2c26SAneesh Kumar K.V cycled = 0; 24922acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 24932acf2c26SAneesh Kumar K.V wbc->range_end = LLONG_MAX; 24942acf2c26SAneesh Kumar K.V wbc->range_cyclic = 0; 24955b41d924SEric Sandeen end = -1; 24965b41d924SEric Sandeen } else { 249722208dedSAneesh Kumar K.V index = wbc->range_start >> PAGE_CACHE_SHIFT; 24985b41d924SEric Sandeen end = wbc->range_end >> PAGE_CACHE_SHIFT; 24995b41d924SEric Sandeen } 2500a1d6cc56SAneesh Kumar K.V 250155138e0bSTheodore Ts'o /* 250255138e0bSTheodore Ts'o * This works around two forms of stupidity. The first is in 250355138e0bSTheodore Ts'o * the writeback code, which caps the maximum number of pages 250455138e0bSTheodore Ts'o * written to be 1024 pages. This is wrong on multiple 250555138e0bSTheodore Ts'o * levels; different architectues have a different page size, 250655138e0bSTheodore Ts'o * which changes the maximum amount of data which gets 250755138e0bSTheodore Ts'o * written. Secondly, 4 megabytes is way too small. XFS 250855138e0bSTheodore Ts'o * forces this value to be 16 megabytes by multiplying 250955138e0bSTheodore Ts'o * nr_to_write parameter by four, and then relies on its 251055138e0bSTheodore Ts'o * allocator to allocate larger extents to make them 251155138e0bSTheodore Ts'o * contiguous. Unfortunately this brings us to the second 251255138e0bSTheodore Ts'o * stupidity, which is that ext4's mballoc code only allocates 251355138e0bSTheodore Ts'o * at most 2048 blocks. So we force contiguous writes up to 251455138e0bSTheodore Ts'o * the number of dirty blocks in the inode, or 251555138e0bSTheodore Ts'o * sbi->max_writeback_mb_bump whichever is smaller. 251655138e0bSTheodore Ts'o */ 251755138e0bSTheodore Ts'o max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT); 2518b443e733SEric Sandeen if (!range_cyclic && range_whole) { 2519b443e733SEric Sandeen if (wbc->nr_to_write == LONG_MAX) 2520b443e733SEric Sandeen desired_nr_to_write = wbc->nr_to_write; 252155138e0bSTheodore Ts'o else 2522b443e733SEric Sandeen desired_nr_to_write = wbc->nr_to_write * 8; 2523b443e733SEric Sandeen } else 252455138e0bSTheodore Ts'o desired_nr_to_write = ext4_num_dirty_pages(inode, index, 252555138e0bSTheodore Ts'o max_pages); 252655138e0bSTheodore Ts'o if (desired_nr_to_write > max_pages) 252755138e0bSTheodore Ts'o desired_nr_to_write = max_pages; 252855138e0bSTheodore Ts'o 252955138e0bSTheodore Ts'o if (wbc->nr_to_write < desired_nr_to_write) { 253055138e0bSTheodore Ts'o nr_to_writebump = desired_nr_to_write - wbc->nr_to_write; 253155138e0bSTheodore Ts'o wbc->nr_to_write = desired_nr_to_write; 253255138e0bSTheodore Ts'o } 253355138e0bSTheodore Ts'o 25342acf2c26SAneesh Kumar K.V retry: 25356e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 25365b41d924SEric Sandeen tag_pages_for_writeback(mapping, index, end); 25375b41d924SEric Sandeen 25381bce63d1SShaohua Li blk_start_plug(&plug); 253922208dedSAneesh Kumar K.V while (!ret && wbc->nr_to_write > 0) { 2540a1d6cc56SAneesh Kumar K.V 2541a1d6cc56SAneesh Kumar K.V /* 2542a1d6cc56SAneesh Kumar K.V * we insert one extent at a time. So we need 2543a1d6cc56SAneesh Kumar K.V * credit needed for single extent allocation. 2544a1d6cc56SAneesh Kumar K.V * journalled mode is currently not supported 2545a1d6cc56SAneesh Kumar K.V * by delalloc 2546a1d6cc56SAneesh Kumar K.V */ 2547a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 2548525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 2549a1d6cc56SAneesh Kumar K.V 255061628a3fSMingming Cao /* start a new transaction*/ 25519924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 25529924a92aSTheodore Ts'o needed_blocks); 255361628a3fSMingming Cao if (IS_ERR(handle)) { 255461628a3fSMingming Cao ret = PTR_ERR(handle); 25551693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2556fbe845ddSCurt Wohlgemuth "%ld pages, ino %lu; err %d", __func__, 2557a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 25583c1fcb2cSNamjae Jeon blk_finish_plug(&plug); 255961628a3fSMingming Cao goto out_writepages; 256061628a3fSMingming Cao } 2561f63e6005STheodore Ts'o 2562f63e6005STheodore Ts'o /* 25638eb9e5ceSTheodore Ts'o * Now call write_cache_pages_da() to find the next 2564f63e6005STheodore Ts'o * contiguous region of logical blocks that need 25658eb9e5ceSTheodore Ts'o * blocks to be allocated by ext4 and submit them. 2566f63e6005STheodore Ts'o */ 25679c3569b5STao Ma ret = write_cache_pages_da(handle, mapping, 25689c3569b5STao Ma wbc, &mpd, &done_index); 2569f63e6005STheodore Ts'o /* 2570af901ca1SAndré Goddard Rosa * If we have a contiguous extent of pages and we 2571f63e6005STheodore Ts'o * haven't done the I/O yet, map the blocks and submit 2572f63e6005STheodore Ts'o * them for I/O. 2573f63e6005STheodore Ts'o */ 2574f63e6005STheodore Ts'o if (!mpd.io_done && mpd.next_page != mpd.first_page) { 25755a87b7a5STheodore Ts'o mpage_da_map_and_submit(&mpd); 2576f63e6005STheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 2577f63e6005STheodore Ts'o } 2578b3a3ca8cSTheodore Ts'o trace_ext4_da_write_pages(inode, &mpd); 2579f63e6005STheodore Ts'o wbc->nr_to_write -= mpd.pages_written; 2580df22291fSAneesh Kumar K.V 258161628a3fSMingming Cao ext4_journal_stop(handle); 2582df22291fSAneesh Kumar K.V 25838f64b32eSEric Sandeen if ((mpd.retval == -ENOSPC) && sbi->s_journal) { 258422208dedSAneesh Kumar K.V /* commit the transaction which would 258522208dedSAneesh Kumar K.V * free blocks released in the transaction 258622208dedSAneesh Kumar K.V * and try again 258722208dedSAneesh Kumar K.V */ 2588df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 258922208dedSAneesh Kumar K.V ret = 0; 259022208dedSAneesh Kumar K.V } else if (ret == MPAGE_DA_EXTENT_TAIL) { 2591a1d6cc56SAneesh Kumar K.V /* 25928de49e67SKazuya Mio * Got one extent now try with rest of the pages. 25938de49e67SKazuya Mio * If mpd.retval is set -EIO, journal is aborted. 25948de49e67SKazuya Mio * So we don't need to write any more. 2595a1d6cc56SAneesh Kumar K.V */ 259622208dedSAneesh Kumar K.V pages_written += mpd.pages_written; 25978de49e67SKazuya Mio ret = mpd.retval; 25982acf2c26SAneesh Kumar K.V io_done = 1; 259922208dedSAneesh Kumar K.V } else if (wbc->nr_to_write) 260061628a3fSMingming Cao /* 260161628a3fSMingming Cao * There is no more writeout needed 260261628a3fSMingming Cao * or we requested for a noblocking writeout 260361628a3fSMingming Cao * and we found the device congested 260461628a3fSMingming Cao */ 260561628a3fSMingming Cao break; 260661628a3fSMingming Cao } 26071bce63d1SShaohua Li blk_finish_plug(&plug); 26082acf2c26SAneesh Kumar K.V if (!io_done && !cycled) { 26092acf2c26SAneesh Kumar K.V cycled = 1; 26102acf2c26SAneesh Kumar K.V index = 0; 26112acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 26122acf2c26SAneesh Kumar K.V wbc->range_end = mapping->writeback_index - 1; 26132acf2c26SAneesh Kumar K.V goto retry; 26142acf2c26SAneesh Kumar K.V } 261561628a3fSMingming Cao 261622208dedSAneesh Kumar K.V /* Update index */ 26172acf2c26SAneesh Kumar K.V wbc->range_cyclic = range_cyclic; 261822208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 261922208dedSAneesh Kumar K.V /* 262022208dedSAneesh Kumar K.V * set the writeback_index so that range_cyclic 262122208dedSAneesh Kumar K.V * mode will write it back later 262222208dedSAneesh Kumar K.V */ 262372f84e65SEric Sandeen mapping->writeback_index = done_index; 2624a1d6cc56SAneesh Kumar K.V 262561628a3fSMingming Cao out_writepages: 262622208dedSAneesh Kumar K.V wbc->nr_to_write -= nr_to_writebump; 2627de89de6eSTheodore Ts'o wbc->range_start = range_start; 26289bffad1eSTheodore Ts'o trace_ext4_da_writepages_result(inode, wbc, ret, pages_written); 262961628a3fSMingming Cao return ret; 263064769240SAlex Tomas } 263164769240SAlex Tomas 263279f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 263379f0be8dSAneesh Kumar K.V { 26345c1ff336SEric Whitney s64 free_clusters, dirty_clusters; 263579f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 263679f0be8dSAneesh Kumar K.V 263779f0be8dSAneesh Kumar K.V /* 263879f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 263979f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 2640179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 264179f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 264279f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 264379f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 264479f0be8dSAneesh Kumar K.V */ 26455c1ff336SEric Whitney free_clusters = 26465c1ff336SEric Whitney percpu_counter_read_positive(&sbi->s_freeclusters_counter); 26475c1ff336SEric Whitney dirty_clusters = 26485c1ff336SEric Whitney percpu_counter_read_positive(&sbi->s_dirtyclusters_counter); 264900d4e736STheodore Ts'o /* 265000d4e736STheodore Ts'o * Start pushing delalloc when 1/2 of free blocks are dirty. 265100d4e736STheodore Ts'o */ 26525c1ff336SEric Whitney if (dirty_clusters && (free_clusters < 2 * dirty_clusters)) 265310ee27a0SMiao Xie try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE); 265400d4e736STheodore Ts'o 26555c1ff336SEric Whitney if (2 * free_clusters < 3 * dirty_clusters || 26565c1ff336SEric Whitney free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) { 265779f0be8dSAneesh Kumar K.V /* 2658c8afb446SEric Sandeen * free block count is less than 150% of dirty blocks 2659c8afb446SEric Sandeen * or free blocks is less than watermark 266079f0be8dSAneesh Kumar K.V */ 266179f0be8dSAneesh Kumar K.V return 1; 266279f0be8dSAneesh Kumar K.V } 266379f0be8dSAneesh Kumar K.V return 0; 266479f0be8dSAneesh Kumar K.V } 266579f0be8dSAneesh Kumar K.V 266664769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 266764769240SAlex Tomas loff_t pos, unsigned len, unsigned flags, 266864769240SAlex Tomas struct page **pagep, void **fsdata) 266964769240SAlex Tomas { 267072b8ab9dSEric Sandeen int ret, retries = 0; 267164769240SAlex Tomas struct page *page; 267264769240SAlex Tomas pgoff_t index; 267364769240SAlex Tomas struct inode *inode = mapping->host; 267464769240SAlex Tomas handle_t *handle; 267564769240SAlex Tomas 267664769240SAlex Tomas index = pos >> PAGE_CACHE_SHIFT; 267779f0be8dSAneesh Kumar K.V 267879f0be8dSAneesh Kumar K.V if (ext4_nonda_switch(inode->i_sb)) { 267979f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 268079f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 268179f0be8dSAneesh Kumar K.V len, flags, pagep, fsdata); 268279f0be8dSAneesh Kumar K.V } 268379f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 26849bffad1eSTheodore Ts'o trace_ext4_da_write_begin(inode, pos, len, flags); 26859c3569b5STao Ma 26869c3569b5STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 26879c3569b5STao Ma ret = ext4_da_write_inline_data_begin(mapping, inode, 26889c3569b5STao Ma pos, len, flags, 26899c3569b5STao Ma pagep, fsdata); 26909c3569b5STao Ma if (ret < 0) 269147564bfbSTheodore Ts'o return ret; 269247564bfbSTheodore Ts'o if (ret == 1) 269347564bfbSTheodore Ts'o return 0; 26949c3569b5STao Ma } 26959c3569b5STao Ma 269647564bfbSTheodore Ts'o /* 269747564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 269847564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 269947564bfbSTheodore Ts'o * is being written back. So grab it first before we start 270047564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 270147564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 270247564bfbSTheodore Ts'o */ 270347564bfbSTheodore Ts'o retry_grab: 270447564bfbSTheodore Ts'o page = grab_cache_page_write_begin(mapping, index, flags); 270547564bfbSTheodore Ts'o if (!page) 270647564bfbSTheodore Ts'o return -ENOMEM; 270747564bfbSTheodore Ts'o unlock_page(page); 270847564bfbSTheodore Ts'o 270964769240SAlex Tomas /* 271064769240SAlex Tomas * With delayed allocation, we don't log the i_disksize update 271164769240SAlex Tomas * if there is delayed block allocation. But we still need 271264769240SAlex Tomas * to journalling the i_disksize update if writes to the end 271364769240SAlex Tomas * of file which has an already mapped buffer. 271464769240SAlex Tomas */ 271547564bfbSTheodore Ts'o retry_journal: 27169924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 1); 271764769240SAlex Tomas if (IS_ERR(handle)) { 271847564bfbSTheodore Ts'o page_cache_release(page); 271947564bfbSTheodore Ts'o return PTR_ERR(handle); 272064769240SAlex Tomas } 272164769240SAlex Tomas 272247564bfbSTheodore Ts'o lock_page(page); 272347564bfbSTheodore Ts'o if (page->mapping != mapping) { 272447564bfbSTheodore Ts'o /* The page got truncated from under us */ 272547564bfbSTheodore Ts'o unlock_page(page); 272647564bfbSTheodore Ts'o page_cache_release(page); 2727d5a0d4f7SEric Sandeen ext4_journal_stop(handle); 272847564bfbSTheodore Ts'o goto retry_grab; 2729d5a0d4f7SEric Sandeen } 273047564bfbSTheodore Ts'o /* In case writeback began while the page was unlocked */ 273147564bfbSTheodore Ts'o wait_on_page_writeback(page); 273264769240SAlex Tomas 27336e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep); 273464769240SAlex Tomas if (ret < 0) { 273564769240SAlex Tomas unlock_page(page); 273664769240SAlex Tomas ext4_journal_stop(handle); 2737ae4d5372SAneesh Kumar K.V /* 2738ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 2739ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 2740ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 2741ae4d5372SAneesh Kumar K.V */ 2742ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 2743b9a4207dSJan Kara ext4_truncate_failed_write(inode); 274447564bfbSTheodore Ts'o 274547564bfbSTheodore Ts'o if (ret == -ENOSPC && 274647564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 274747564bfbSTheodore Ts'o goto retry_journal; 274847564bfbSTheodore Ts'o 274947564bfbSTheodore Ts'o page_cache_release(page); 275047564bfbSTheodore Ts'o return ret; 275164769240SAlex Tomas } 275264769240SAlex Tomas 275347564bfbSTheodore Ts'o *pagep = page; 275464769240SAlex Tomas return ret; 275564769240SAlex Tomas } 275664769240SAlex Tomas 2757632eaeabSMingming Cao /* 2758632eaeabSMingming Cao * Check if we should update i_disksize 2759632eaeabSMingming Cao * when write to the end of file but not require block allocation 2760632eaeabSMingming Cao */ 2761632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page, 2762632eaeabSMingming Cao unsigned long offset) 2763632eaeabSMingming Cao { 2764632eaeabSMingming Cao struct buffer_head *bh; 2765632eaeabSMingming Cao struct inode *inode = page->mapping->host; 2766632eaeabSMingming Cao unsigned int idx; 2767632eaeabSMingming Cao int i; 2768632eaeabSMingming Cao 2769632eaeabSMingming Cao bh = page_buffers(page); 2770632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 2771632eaeabSMingming Cao 2772632eaeabSMingming Cao for (i = 0; i < idx; i++) 2773632eaeabSMingming Cao bh = bh->b_this_page; 2774632eaeabSMingming Cao 277529fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 2776632eaeabSMingming Cao return 0; 2777632eaeabSMingming Cao return 1; 2778632eaeabSMingming Cao } 2779632eaeabSMingming Cao 278064769240SAlex Tomas static int ext4_da_write_end(struct file *file, 278164769240SAlex Tomas struct address_space *mapping, 278264769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 278364769240SAlex Tomas struct page *page, void *fsdata) 278464769240SAlex Tomas { 278564769240SAlex Tomas struct inode *inode = mapping->host; 278664769240SAlex Tomas int ret = 0, ret2; 278764769240SAlex Tomas handle_t *handle = ext4_journal_current_handle(); 278864769240SAlex Tomas loff_t new_i_size; 2789632eaeabSMingming Cao unsigned long start, end; 279079f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 279179f0be8dSAneesh Kumar K.V 279274d553aaSTheodore Ts'o if (write_mode == FALL_BACK_TO_NONDELALLOC) 279374d553aaSTheodore Ts'o return ext4_write_end(file, mapping, pos, 279479f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 2795632eaeabSMingming Cao 27969bffad1eSTheodore Ts'o trace_ext4_da_write_end(inode, pos, len, copied); 2797632eaeabSMingming Cao start = pos & (PAGE_CACHE_SIZE - 1); 2798632eaeabSMingming Cao end = start + copied - 1; 279964769240SAlex Tomas 280064769240SAlex Tomas /* 280164769240SAlex Tomas * generic_write_end() will run mark_inode_dirty() if i_size 280264769240SAlex Tomas * changes. So let's piggyback the i_disksize mark_inode_dirty 280364769240SAlex Tomas * into that. 280464769240SAlex Tomas */ 280564769240SAlex Tomas new_i_size = pos + copied; 2806ea51d132SAndrea Arcangeli if (copied && new_i_size > EXT4_I(inode)->i_disksize) { 28079c3569b5STao Ma if (ext4_has_inline_data(inode) || 28089c3569b5STao Ma ext4_da_should_update_i_disksize(page, end)) { 2809632eaeabSMingming Cao down_write(&EXT4_I(inode)->i_data_sem); 2810f3b59291STheodore Ts'o if (new_i_size > EXT4_I(inode)->i_disksize) 281164769240SAlex Tomas EXT4_I(inode)->i_disksize = new_i_size; 2812632eaeabSMingming Cao up_write(&EXT4_I(inode)->i_data_sem); 2813cf17fea6SAneesh Kumar K.V /* We need to mark inode dirty even if 2814cf17fea6SAneesh Kumar K.V * new_i_size is less that inode->i_size 2815cf17fea6SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 2816cf17fea6SAneesh Kumar K.V */ 2817cf17fea6SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 2818632eaeabSMingming Cao } 2819632eaeabSMingming Cao } 28209c3569b5STao Ma 28219c3569b5STao Ma if (write_mode != CONVERT_INLINE_DATA && 28229c3569b5STao Ma ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) && 28239c3569b5STao Ma ext4_has_inline_data(inode)) 28249c3569b5STao Ma ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied, 28259c3569b5STao Ma page); 28269c3569b5STao Ma else 282764769240SAlex Tomas ret2 = generic_write_end(file, mapping, pos, len, copied, 282864769240SAlex Tomas page, fsdata); 28299c3569b5STao Ma 283064769240SAlex Tomas copied = ret2; 283164769240SAlex Tomas if (ret2 < 0) 283264769240SAlex Tomas ret = ret2; 283364769240SAlex Tomas ret2 = ext4_journal_stop(handle); 283464769240SAlex Tomas if (!ret) 283564769240SAlex Tomas ret = ret2; 283664769240SAlex Tomas 283764769240SAlex Tomas return ret ? ret : copied; 283864769240SAlex Tomas } 283964769240SAlex Tomas 2840d47992f8SLukas Czerner static void ext4_da_invalidatepage(struct page *page, unsigned int offset, 2841d47992f8SLukas Czerner unsigned int length) 284264769240SAlex Tomas { 284364769240SAlex Tomas /* 284464769240SAlex Tomas * Drop reserved blocks 284564769240SAlex Tomas */ 284664769240SAlex Tomas BUG_ON(!PageLocked(page)); 284764769240SAlex Tomas if (!page_has_buffers(page)) 284864769240SAlex Tomas goto out; 284964769240SAlex Tomas 2850ca99fdd2SLukas Czerner ext4_da_page_release_reservation(page, offset, length); 285164769240SAlex Tomas 285264769240SAlex Tomas out: 2853d47992f8SLukas Czerner ext4_invalidatepage(page, offset, length); 285464769240SAlex Tomas 285564769240SAlex Tomas return; 285664769240SAlex Tomas } 285764769240SAlex Tomas 2858ccd2506bSTheodore Ts'o /* 2859ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 2860ccd2506bSTheodore Ts'o */ 2861ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 2862ccd2506bSTheodore Ts'o { 2863fb40ba0dSTheodore Ts'o trace_ext4_alloc_da_blocks(inode); 2864fb40ba0dSTheodore Ts'o 2865ccd2506bSTheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks && 2866ccd2506bSTheodore Ts'o !EXT4_I(inode)->i_reserved_meta_blocks) 2867ccd2506bSTheodore Ts'o return 0; 2868ccd2506bSTheodore Ts'o 2869ccd2506bSTheodore Ts'o /* 2870ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 2871ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 2872ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 2873ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 2874ccd2506bSTheodore Ts'o * would require replicating code paths in: 2875ccd2506bSTheodore Ts'o * 2876ccd2506bSTheodore Ts'o * ext4_da_writepages() -> 2877ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 2878ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 2879ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 2880ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 2881ccd2506bSTheodore Ts'o * 2882ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 2883ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 2884ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 2885ccd2506bSTheodore Ts'o * doing I/O at all. 2886ccd2506bSTheodore Ts'o * 2887ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 2888380cf090SWu Fengguang * the pages by calling redirty_page_for_writepage() but that 2889ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 2890ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 289125985edcSLucas De Marchi * simplifying them because we wouldn't actually intend to 2892ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 2893ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 2894ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 2895ccd2506bSTheodore Ts'o * 2896ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 2897ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 2898ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 2899ccd2506bSTheodore Ts'o */ 2900ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 2901ccd2506bSTheodore Ts'o } 290264769240SAlex Tomas 290364769240SAlex Tomas /* 2904ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 2905ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 2906ac27a0ecSDave Kleikamp * 2907ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 2908617ba13bSMingming Cao * journal. If somebody makes a swapfile on an ext4 data-journaling 2909ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 2910ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 2911ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 2912ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 2913ac27a0ecSDave Kleikamp * 2914ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 2915ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 2916ac27a0ecSDave Kleikamp */ 2917617ba13bSMingming Cao static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 2918ac27a0ecSDave Kleikamp { 2919ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 2920ac27a0ecSDave Kleikamp journal_t *journal; 2921ac27a0ecSDave Kleikamp int err; 2922ac27a0ecSDave Kleikamp 292346c7f254STao Ma /* 292446c7f254STao Ma * We can get here for an inline file via the FIBMAP ioctl 292546c7f254STao Ma */ 292646c7f254STao Ma if (ext4_has_inline_data(inode)) 292746c7f254STao Ma return 0; 292846c7f254STao Ma 292964769240SAlex Tomas if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 293064769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) { 293164769240SAlex Tomas /* 293264769240SAlex Tomas * With delalloc we want to sync the file 293364769240SAlex Tomas * so that we can make sure we allocate 293464769240SAlex Tomas * blocks for file 293564769240SAlex Tomas */ 293664769240SAlex Tomas filemap_write_and_wait(mapping); 293764769240SAlex Tomas } 293864769240SAlex Tomas 293919f5fb7aSTheodore Ts'o if (EXT4_JOURNAL(inode) && 294019f5fb7aSTheodore Ts'o ext4_test_inode_state(inode, EXT4_STATE_JDATA)) { 2941ac27a0ecSDave Kleikamp /* 2942ac27a0ecSDave Kleikamp * This is a REALLY heavyweight approach, but the use of 2943ac27a0ecSDave Kleikamp * bmap on dirty files is expected to be extremely rare: 2944ac27a0ecSDave Kleikamp * only if we run lilo or swapon on a freshly made file 2945ac27a0ecSDave Kleikamp * do we expect this to happen. 2946ac27a0ecSDave Kleikamp * 2947ac27a0ecSDave Kleikamp * (bmap requires CAP_SYS_RAWIO so this does not 2948ac27a0ecSDave Kleikamp * represent an unprivileged user DOS attack --- we'd be 2949ac27a0ecSDave Kleikamp * in trouble if mortal users could trigger this path at 2950ac27a0ecSDave Kleikamp * will.) 2951ac27a0ecSDave Kleikamp * 2952617ba13bSMingming Cao * NB. EXT4_STATE_JDATA is not set on files other than 2953ac27a0ecSDave Kleikamp * regular files. If somebody wants to bmap a directory 2954ac27a0ecSDave Kleikamp * or symlink and gets confused because the buffer 2955ac27a0ecSDave Kleikamp * hasn't yet been flushed to disk, they deserve 2956ac27a0ecSDave Kleikamp * everything they get. 2957ac27a0ecSDave Kleikamp */ 2958ac27a0ecSDave Kleikamp 295919f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_JDATA); 2960617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 2961dab291afSMingming Cao jbd2_journal_lock_updates(journal); 2962dab291afSMingming Cao err = jbd2_journal_flush(journal); 2963dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 2964ac27a0ecSDave Kleikamp 2965ac27a0ecSDave Kleikamp if (err) 2966ac27a0ecSDave Kleikamp return 0; 2967ac27a0ecSDave Kleikamp } 2968ac27a0ecSDave Kleikamp 2969617ba13bSMingming Cao return generic_block_bmap(mapping, block, ext4_get_block); 2970ac27a0ecSDave Kleikamp } 2971ac27a0ecSDave Kleikamp 2972617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page) 2973ac27a0ecSDave Kleikamp { 297446c7f254STao Ma int ret = -EAGAIN; 297546c7f254STao Ma struct inode *inode = page->mapping->host; 297646c7f254STao Ma 29770562e0baSJiaying Zhang trace_ext4_readpage(page); 297846c7f254STao Ma 297946c7f254STao Ma if (ext4_has_inline_data(inode)) 298046c7f254STao Ma ret = ext4_readpage_inline(inode, page); 298146c7f254STao Ma 298246c7f254STao Ma if (ret == -EAGAIN) 2983617ba13bSMingming Cao return mpage_readpage(page, ext4_get_block); 298446c7f254STao Ma 298546c7f254STao Ma return ret; 2986ac27a0ecSDave Kleikamp } 2987ac27a0ecSDave Kleikamp 2988ac27a0ecSDave Kleikamp static int 2989617ba13bSMingming Cao ext4_readpages(struct file *file, struct address_space *mapping, 2990ac27a0ecSDave Kleikamp struct list_head *pages, unsigned nr_pages) 2991ac27a0ecSDave Kleikamp { 299246c7f254STao Ma struct inode *inode = mapping->host; 299346c7f254STao Ma 299446c7f254STao Ma /* If the file has inline data, no need to do readpages. */ 299546c7f254STao Ma if (ext4_has_inline_data(inode)) 299646c7f254STao Ma return 0; 299746c7f254STao Ma 2998617ba13bSMingming Cao return mpage_readpages(mapping, pages, nr_pages, ext4_get_block); 2999ac27a0ecSDave Kleikamp } 3000ac27a0ecSDave Kleikamp 3001d47992f8SLukas Czerner static void ext4_invalidatepage(struct page *page, unsigned int offset, 3002d47992f8SLukas Czerner unsigned int length) 3003ac27a0ecSDave Kleikamp { 3004ca99fdd2SLukas Czerner trace_ext4_invalidatepage(page, offset, length); 30050562e0baSJiaying Zhang 30064520fb3cSJan Kara /* No journalling happens on data buffers when this function is used */ 30074520fb3cSJan Kara WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page))); 30084520fb3cSJan Kara 3009ca99fdd2SLukas Czerner block_invalidatepage(page, offset, length); 30104520fb3cSJan Kara } 30114520fb3cSJan Kara 301253e87268SJan Kara static int __ext4_journalled_invalidatepage(struct page *page, 3013ca99fdd2SLukas Czerner unsigned int offset, 3014ca99fdd2SLukas Czerner unsigned int length) 30154520fb3cSJan Kara { 30164520fb3cSJan Kara journal_t *journal = EXT4_JOURNAL(page->mapping->host); 30174520fb3cSJan Kara 3018ca99fdd2SLukas Czerner trace_ext4_journalled_invalidatepage(page, offset, length); 30194520fb3cSJan Kara 3020744692dcSJiaying Zhang /* 3021ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 3022ac27a0ecSDave Kleikamp */ 3023ca99fdd2SLukas Czerner if (offset == 0 && length == PAGE_CACHE_SIZE) 3024ac27a0ecSDave Kleikamp ClearPageChecked(page); 3025ac27a0ecSDave Kleikamp 3026ca99fdd2SLukas Czerner return jbd2_journal_invalidatepage(journal, page, offset, length); 302753e87268SJan Kara } 302853e87268SJan Kara 302953e87268SJan Kara /* Wrapper for aops... */ 303053e87268SJan Kara static void ext4_journalled_invalidatepage(struct page *page, 3031d47992f8SLukas Czerner unsigned int offset, 3032d47992f8SLukas Czerner unsigned int length) 303353e87268SJan Kara { 3034ca99fdd2SLukas Czerner WARN_ON(__ext4_journalled_invalidatepage(page, offset, length) < 0); 3035ac27a0ecSDave Kleikamp } 3036ac27a0ecSDave Kleikamp 3037617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait) 3038ac27a0ecSDave Kleikamp { 3039617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 3040ac27a0ecSDave Kleikamp 30410562e0baSJiaying Zhang trace_ext4_releasepage(page); 30420562e0baSJiaying Zhang 3043e1c36595SJan Kara /* Page has dirty journalled data -> cannot release */ 3044e1c36595SJan Kara if (PageChecked(page)) 3045ac27a0ecSDave Kleikamp return 0; 30460390131bSFrank Mayhar if (journal) 3047dab291afSMingming Cao return jbd2_journal_try_to_free_buffers(journal, page, wait); 30480390131bSFrank Mayhar else 30490390131bSFrank Mayhar return try_to_free_buffers(page); 3050ac27a0ecSDave Kleikamp } 3051ac27a0ecSDave Kleikamp 3052ac27a0ecSDave Kleikamp /* 30532ed88685STheodore Ts'o * ext4_get_block used when preparing for a DIO write or buffer write. 30542ed88685STheodore Ts'o * We allocate an uinitialized extent if blocks haven't been allocated. 30552ed88685STheodore Ts'o * The extent will be converted to initialized after the IO is complete. 30562ed88685STheodore Ts'o */ 3057f19d5870STao Ma int ext4_get_block_write(struct inode *inode, sector_t iblock, 30584c0425ffSMingming Cao struct buffer_head *bh_result, int create) 30594c0425ffSMingming Cao { 3060c7064ef1SJiaying Zhang ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n", 30618d5d02e6SMingming Cao inode->i_ino, create); 30622ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh_result, 30632ed88685STheodore Ts'o EXT4_GET_BLOCKS_IO_CREATE_EXT); 30644c0425ffSMingming Cao } 30654c0425ffSMingming Cao 3066729f52c6SZheng Liu static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock, 30678b0f165fSAnatol Pomozov struct buffer_head *bh_result, int create) 3068729f52c6SZheng Liu { 30698b0f165fSAnatol Pomozov ext4_debug("ext4_get_block_write_nolock: inode %lu, create flag %d\n", 30708b0f165fSAnatol Pomozov inode->i_ino, create); 30718b0f165fSAnatol Pomozov return _ext4_get_block(inode, iblock, bh_result, 30728b0f165fSAnatol Pomozov EXT4_GET_BLOCKS_NO_LOCK); 3073729f52c6SZheng Liu } 3074729f52c6SZheng Liu 30754c0425ffSMingming Cao static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset, 3076552ef802SChristoph Hellwig ssize_t size, void *private, int ret, 3077552ef802SChristoph Hellwig bool is_async) 30784c0425ffSMingming Cao { 3079496ad9aaSAl Viro struct inode *inode = file_inode(iocb->ki_filp); 30804c0425ffSMingming Cao ext4_io_end_t *io_end = iocb->private; 30814c0425ffSMingming Cao 3082a549984bSTheodore Ts'o /* if not async direct IO or dio with 0 bytes write, just return */ 3083a549984bSTheodore Ts'o if (!io_end || !size) 3084a549984bSTheodore Ts'o goto out; 30854b70df18SMingming 30868d5d02e6SMingming Cao ext_debug("ext4_end_io_dio(): io_end 0x%p " 3087ace36ad4SJoe Perches "for inode %lu, iocb 0x%p, offset %llu, size %zd\n", 30888d5d02e6SMingming Cao iocb->private, io_end->inode->i_ino, iocb, offset, 30898d5d02e6SMingming Cao size); 30908d5d02e6SMingming Cao 3091b5a7e970STheodore Ts'o iocb->private = NULL; 3092a549984bSTheodore Ts'o 3093a549984bSTheodore Ts'o /* if not aio dio with unwritten extents, just free io and return */ 3094a549984bSTheodore Ts'o if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) { 3095a549984bSTheodore Ts'o ext4_free_io_end(io_end); 3096a549984bSTheodore Ts'o out: 3097a549984bSTheodore Ts'o inode_dio_done(inode); 3098a549984bSTheodore Ts'o if (is_async) 3099a549984bSTheodore Ts'o aio_complete(iocb, ret, 0); 3100a549984bSTheodore Ts'o return; 3101a549984bSTheodore Ts'o } 3102a549984bSTheodore Ts'o 31034c0425ffSMingming Cao io_end->offset = offset; 31044c0425ffSMingming Cao io_end->size = size; 31055b3ff237Sjiayingz@google.com (Jiaying Zhang) if (is_async) { 31065b3ff237Sjiayingz@google.com (Jiaying Zhang) io_end->iocb = iocb; 31075b3ff237Sjiayingz@google.com (Jiaying Zhang) io_end->result = ret; 31085b3ff237Sjiayingz@google.com (Jiaying Zhang) } 3109a549984bSTheodore Ts'o 3110a549984bSTheodore Ts'o ext4_add_complete_io(io_end); 31114c0425ffSMingming Cao } 3112c7064ef1SJiaying Zhang 31134c0425ffSMingming Cao /* 31144c0425ffSMingming Cao * For ext4 extent files, ext4 will do direct-io write to holes, 31154c0425ffSMingming Cao * preallocated extents, and those write extend the file, no need to 31164c0425ffSMingming Cao * fall back to buffered IO. 31174c0425ffSMingming Cao * 3118b595076aSUwe Kleine-König * For holes, we fallocate those blocks, mark them as uninitialized 311969c499d1STheodore Ts'o * If those blocks were preallocated, we mark sure they are split, but 3120b595076aSUwe Kleine-König * still keep the range to write as uninitialized. 31214c0425ffSMingming Cao * 312269c499d1STheodore Ts'o * The unwritten extents will be converted to written when DIO is completed. 31238d5d02e6SMingming Cao * For async direct IO, since the IO may still pending when return, we 312425985edcSLucas De Marchi * set up an end_io call back function, which will do the conversion 31258d5d02e6SMingming Cao * when async direct IO completed. 31264c0425ffSMingming Cao * 31274c0425ffSMingming Cao * If the O_DIRECT write will extend the file then add this inode to the 31284c0425ffSMingming Cao * orphan list. So recovery will truncate it back to the original size 31294c0425ffSMingming Cao * if the machine crashes during the write. 31304c0425ffSMingming Cao * 31314c0425ffSMingming Cao */ 31324c0425ffSMingming Cao static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb, 31334c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 31344c0425ffSMingming Cao unsigned long nr_segs) 31354c0425ffSMingming Cao { 31364c0425ffSMingming Cao struct file *file = iocb->ki_filp; 31374c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 31384c0425ffSMingming Cao ssize_t ret; 31394c0425ffSMingming Cao size_t count = iov_length(iov, nr_segs); 3140729f52c6SZheng Liu int overwrite = 0; 31418b0f165fSAnatol Pomozov get_block_t *get_block_func = NULL; 31428b0f165fSAnatol Pomozov int dio_flags = 0; 314369c499d1STheodore Ts'o loff_t final_size = offset + count; 314469c499d1STheodore Ts'o 314569c499d1STheodore Ts'o /* Use the old path for reads and writes beyond i_size. */ 314669c499d1STheodore Ts'o if (rw != WRITE || final_size > inode->i_size) 314769c499d1STheodore Ts'o return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 3148729f52c6SZheng Liu 31494bd809dbSZheng Liu BUG_ON(iocb->private == NULL); 31504bd809dbSZheng Liu 31514bd809dbSZheng Liu /* If we do a overwrite dio, i_mutex locking can be released */ 31524bd809dbSZheng Liu overwrite = *((int *)iocb->private); 31534bd809dbSZheng Liu 31544bd809dbSZheng Liu if (overwrite) { 31551f555cfaSDmitry Monakhov atomic_inc(&inode->i_dio_count); 31564bd809dbSZheng Liu down_read(&EXT4_I(inode)->i_data_sem); 31574bd809dbSZheng Liu mutex_unlock(&inode->i_mutex); 31584bd809dbSZheng Liu } 31594bd809dbSZheng Liu 31604c0425ffSMingming Cao /* 31618d5d02e6SMingming Cao * We could direct write to holes and fallocate. 31628d5d02e6SMingming Cao * 316369c499d1STheodore Ts'o * Allocated blocks to fill the hole are marked as 316469c499d1STheodore Ts'o * uninitialized to prevent parallel buffered read to expose 316569c499d1STheodore Ts'o * the stale data before DIO complete the data IO. 31668d5d02e6SMingming Cao * 316769c499d1STheodore Ts'o * As to previously fallocated extents, ext4 get_block will 316869c499d1STheodore Ts'o * just simply mark the buffer mapped but still keep the 316969c499d1STheodore Ts'o * extents uninitialized. 31704c0425ffSMingming Cao * 317169c499d1STheodore Ts'o * For non AIO case, we will convert those unwritten extents 31728d5d02e6SMingming Cao * to written after return back from blockdev_direct_IO. 31734c0425ffSMingming Cao * 317469c499d1STheodore Ts'o * For async DIO, the conversion needs to be deferred when the 317569c499d1STheodore Ts'o * IO is completed. The ext4 end_io callback function will be 317669c499d1STheodore Ts'o * called to take care of the conversion work. Here for async 317769c499d1STheodore Ts'o * case, we allocate an io_end structure to hook to the iocb. 31784c0425ffSMingming Cao */ 31798d5d02e6SMingming Cao iocb->private = NULL; 3180f45ee3a1SDmitry Monakhov ext4_inode_aio_set(inode, NULL); 31818d5d02e6SMingming Cao if (!is_sync_kiocb(iocb)) { 3182a549984bSTheodore Ts'o ext4_io_end_t *io_end = ext4_init_io_end(inode, GFP_NOFS); 31834bd809dbSZheng Liu if (!io_end) { 31844bd809dbSZheng Liu ret = -ENOMEM; 31854bd809dbSZheng Liu goto retake_lock; 31864bd809dbSZheng Liu } 3187266991b1SJeff Moyer io_end->flag |= EXT4_IO_END_DIRECT; 3188a549984bSTheodore Ts'o iocb->private = io_end; 31898d5d02e6SMingming Cao /* 319069c499d1STheodore Ts'o * we save the io structure for current async direct 319169c499d1STheodore Ts'o * IO, so that later ext4_map_blocks() could flag the 319269c499d1STheodore Ts'o * io structure whether there is a unwritten extents 319369c499d1STheodore Ts'o * needs to be converted when IO is completed. 31948d5d02e6SMingming Cao */ 3195f45ee3a1SDmitry Monakhov ext4_inode_aio_set(inode, io_end); 31968d5d02e6SMingming Cao } 31978d5d02e6SMingming Cao 31988b0f165fSAnatol Pomozov if (overwrite) { 31998b0f165fSAnatol Pomozov get_block_func = ext4_get_block_write_nolock; 32008b0f165fSAnatol Pomozov } else { 32018b0f165fSAnatol Pomozov get_block_func = ext4_get_block_write; 32028b0f165fSAnatol Pomozov dio_flags = DIO_LOCKING; 32038b0f165fSAnatol Pomozov } 3204729f52c6SZheng Liu ret = __blockdev_direct_IO(rw, iocb, inode, 3205729f52c6SZheng Liu inode->i_sb->s_bdev, iov, 3206729f52c6SZheng Liu offset, nr_segs, 32078b0f165fSAnatol Pomozov get_block_func, 3208729f52c6SZheng Liu ext4_end_io_dio, 3209729f52c6SZheng Liu NULL, 32108b0f165fSAnatol Pomozov dio_flags); 32118b0f165fSAnatol Pomozov 3212a549984bSTheodore Ts'o if (iocb->private) 32134eec708dSJan Kara ext4_inode_aio_set(inode, NULL); 32144eec708dSJan Kara /* 3215a549984bSTheodore Ts'o * The io_end structure takes a reference to the inode, that 3216a549984bSTheodore Ts'o * structure needs to be destroyed and the reference to the 3217a549984bSTheodore Ts'o * inode need to be dropped, when IO is complete, even with 0 3218a549984bSTheodore Ts'o * byte write, or failed. 3219a549984bSTheodore Ts'o * 3220a549984bSTheodore Ts'o * In the successful AIO DIO case, the io_end structure will 3221a549984bSTheodore Ts'o * be destroyed and the reference to the inode will be dropped 3222a549984bSTheodore Ts'o * after the end_io call back function is called. 3223a549984bSTheodore Ts'o * 3224a549984bSTheodore Ts'o * In the case there is 0 byte write, or error case, since VFS 3225a549984bSTheodore Ts'o * direct IO won't invoke the end_io call back function, we 3226a549984bSTheodore Ts'o * need to free the end_io structure here. 32274eec708dSJan Kara */ 3228a549984bSTheodore Ts'o if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) { 3229a549984bSTheodore Ts'o ext4_free_io_end(iocb->private); 32308d5d02e6SMingming Cao iocb->private = NULL; 3231a549984bSTheodore Ts'o } else if (ret > 0 && !overwrite && ext4_test_inode_state(inode, 32325f524950SMingming EXT4_STATE_DIO_UNWRITTEN)) { 3233109f5565SMingming int err; 32348d5d02e6SMingming Cao /* 32358d5d02e6SMingming Cao * for non AIO case, since the IO is already 323625985edcSLucas De Marchi * completed, we could do the conversion right here 32378d5d02e6SMingming Cao */ 3238109f5565SMingming err = ext4_convert_unwritten_extents(inode, 32398d5d02e6SMingming Cao offset, ret); 3240109f5565SMingming if (err < 0) 3241109f5565SMingming ret = err; 324219f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN); 3243109f5565SMingming } 32444bd809dbSZheng Liu 32454bd809dbSZheng Liu retake_lock: 32464bd809dbSZheng Liu /* take i_mutex locking again if we do a ovewrite dio */ 32474bd809dbSZheng Liu if (overwrite) { 32481f555cfaSDmitry Monakhov inode_dio_done(inode); 32494bd809dbSZheng Liu up_read(&EXT4_I(inode)->i_data_sem); 32504bd809dbSZheng Liu mutex_lock(&inode->i_mutex); 32514bd809dbSZheng Liu } 32524bd809dbSZheng Liu 32534c0425ffSMingming Cao return ret; 32544c0425ffSMingming Cao } 32558d5d02e6SMingming Cao 32564c0425ffSMingming Cao static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb, 32574c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 32584c0425ffSMingming Cao unsigned long nr_segs) 32594c0425ffSMingming Cao { 32604c0425ffSMingming Cao struct file *file = iocb->ki_filp; 32614c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 32620562e0baSJiaying Zhang ssize_t ret; 32634c0425ffSMingming Cao 326484ebd795STheodore Ts'o /* 326584ebd795STheodore Ts'o * If we are doing data journalling we don't support O_DIRECT 326684ebd795STheodore Ts'o */ 326784ebd795STheodore Ts'o if (ext4_should_journal_data(inode)) 326884ebd795STheodore Ts'o return 0; 326984ebd795STheodore Ts'o 327046c7f254STao Ma /* Let buffer I/O handle the inline data case. */ 327146c7f254STao Ma if (ext4_has_inline_data(inode)) 327246c7f254STao Ma return 0; 327346c7f254STao Ma 32740562e0baSJiaying Zhang trace_ext4_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw); 327512e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 32760562e0baSJiaying Zhang ret = ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs); 32770562e0baSJiaying Zhang else 32780562e0baSJiaying Zhang ret = ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 32790562e0baSJiaying Zhang trace_ext4_direct_IO_exit(inode, offset, 32800562e0baSJiaying Zhang iov_length(iov, nr_segs), rw, ret); 32810562e0baSJiaying Zhang return ret; 32824c0425ffSMingming Cao } 32834c0425ffSMingming Cao 3284ac27a0ecSDave Kleikamp /* 3285617ba13bSMingming Cao * Pages can be marked dirty completely asynchronously from ext4's journalling 3286ac27a0ecSDave Kleikamp * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do 3287ac27a0ecSDave Kleikamp * much here because ->set_page_dirty is called under VFS locks. The page is 3288ac27a0ecSDave Kleikamp * not necessarily locked. 3289ac27a0ecSDave Kleikamp * 3290ac27a0ecSDave Kleikamp * We cannot just dirty the page and leave attached buffers clean, because the 3291ac27a0ecSDave Kleikamp * buffers' dirty state is "definitive". We cannot just set the buffers dirty 3292ac27a0ecSDave Kleikamp * or jbddirty because all the journalling code will explode. 3293ac27a0ecSDave Kleikamp * 3294ac27a0ecSDave Kleikamp * So what we do is to mark the page "pending dirty" and next time writepage 3295ac27a0ecSDave Kleikamp * is called, propagate that into the buffers appropriately. 3296ac27a0ecSDave Kleikamp */ 3297617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page) 3298ac27a0ecSDave Kleikamp { 3299ac27a0ecSDave Kleikamp SetPageChecked(page); 3300ac27a0ecSDave Kleikamp return __set_page_dirty_nobuffers(page); 3301ac27a0ecSDave Kleikamp } 3302ac27a0ecSDave Kleikamp 330374d553aaSTheodore Ts'o static const struct address_space_operations ext4_aops = { 3304617ba13bSMingming Cao .readpage = ext4_readpage, 3305617ba13bSMingming Cao .readpages = ext4_readpages, 330643ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3307bfc1af65SNick Piggin .write_begin = ext4_write_begin, 330874d553aaSTheodore Ts'o .write_end = ext4_write_end, 3309617ba13bSMingming Cao .bmap = ext4_bmap, 3310617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3311617ba13bSMingming Cao .releasepage = ext4_releasepage, 3312617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 3313ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 33148ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3315aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3316ac27a0ecSDave Kleikamp }; 3317ac27a0ecSDave Kleikamp 3318617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 3319617ba13bSMingming Cao .readpage = ext4_readpage, 3320617ba13bSMingming Cao .readpages = ext4_readpages, 332143ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3322bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3323bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 3324617ba13bSMingming Cao .set_page_dirty = ext4_journalled_set_page_dirty, 3325617ba13bSMingming Cao .bmap = ext4_bmap, 33264520fb3cSJan Kara .invalidatepage = ext4_journalled_invalidatepage, 3327617ba13bSMingming Cao .releasepage = ext4_releasepage, 332884ebd795STheodore Ts'o .direct_IO = ext4_direct_IO, 33298ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3330aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3331ac27a0ecSDave Kleikamp }; 3332ac27a0ecSDave Kleikamp 333364769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 333464769240SAlex Tomas .readpage = ext4_readpage, 333564769240SAlex Tomas .readpages = ext4_readpages, 333643ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 333764769240SAlex Tomas .writepages = ext4_da_writepages, 333864769240SAlex Tomas .write_begin = ext4_da_write_begin, 333964769240SAlex Tomas .write_end = ext4_da_write_end, 334064769240SAlex Tomas .bmap = ext4_bmap, 334164769240SAlex Tomas .invalidatepage = ext4_da_invalidatepage, 334264769240SAlex Tomas .releasepage = ext4_releasepage, 334364769240SAlex Tomas .direct_IO = ext4_direct_IO, 334464769240SAlex Tomas .migratepage = buffer_migrate_page, 33458ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3346aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 334764769240SAlex Tomas }; 334864769240SAlex Tomas 3349617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 3350ac27a0ecSDave Kleikamp { 33513d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 33523d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 335374d553aaSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_ORDERED_MODE); 33543d2b1582SLukas Czerner break; 33553d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 335674d553aaSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_ORDERED_MODE); 33573d2b1582SLukas Czerner break; 33583d2b1582SLukas Czerner case EXT4_INODE_JOURNAL_DATA_MODE: 3359617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 336074d553aaSTheodore Ts'o return; 33613d2b1582SLukas Czerner default: 33623d2b1582SLukas Czerner BUG(); 33633d2b1582SLukas Czerner } 336474d553aaSTheodore Ts'o if (test_opt(inode->i_sb, DELALLOC)) 336574d553aaSTheodore Ts'o inode->i_mapping->a_ops = &ext4_da_aops; 336674d553aaSTheodore Ts'o else 336774d553aaSTheodore Ts'o inode->i_mapping->a_ops = &ext4_aops; 3368ac27a0ecSDave Kleikamp } 3369ac27a0ecSDave Kleikamp 33704e96b2dbSAllison Henderson 33714e96b2dbSAllison Henderson /* 33724e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers() 33734e96b2dbSAllison Henderson * Wrapper function for ext4_discard_partial_page_buffers_no_lock. 33744e96b2dbSAllison Henderson * This function finds and locks the page containing the offset 33754e96b2dbSAllison Henderson * "from" and passes it to ext4_discard_partial_page_buffers_no_lock. 33764e96b2dbSAllison Henderson * Calling functions that already have the page locked should call 33774e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers_no_lock directly. 33784e96b2dbSAllison Henderson */ 33794e96b2dbSAllison Henderson int ext4_discard_partial_page_buffers(handle_t *handle, 33804e96b2dbSAllison Henderson struct address_space *mapping, loff_t from, 33814e96b2dbSAllison Henderson loff_t length, int flags) 33824e96b2dbSAllison Henderson { 33834e96b2dbSAllison Henderson struct inode *inode = mapping->host; 33844e96b2dbSAllison Henderson struct page *page; 33854e96b2dbSAllison Henderson int err = 0; 33864e96b2dbSAllison Henderson 33874e96b2dbSAllison Henderson page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT, 33884e96b2dbSAllison Henderson mapping_gfp_mask(mapping) & ~__GFP_FS); 33894e96b2dbSAllison Henderson if (!page) 33905129d05fSYongqiang Yang return -ENOMEM; 33914e96b2dbSAllison Henderson 33924e96b2dbSAllison Henderson err = ext4_discard_partial_page_buffers_no_lock(handle, inode, page, 33934e96b2dbSAllison Henderson from, length, flags); 33944e96b2dbSAllison Henderson 33954e96b2dbSAllison Henderson unlock_page(page); 33964e96b2dbSAllison Henderson page_cache_release(page); 33974e96b2dbSAllison Henderson return err; 33984e96b2dbSAllison Henderson } 33994e96b2dbSAllison Henderson 34004e96b2dbSAllison Henderson /* 34014e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers_no_lock() 34024e96b2dbSAllison Henderson * Zeros a page range of length 'length' starting from offset 'from'. 34034e96b2dbSAllison Henderson * Buffer heads that correspond to the block aligned regions of the 34044e96b2dbSAllison Henderson * zeroed range will be unmapped. Unblock aligned regions 34054e96b2dbSAllison Henderson * will have the corresponding buffer head mapped if needed so that 34064e96b2dbSAllison Henderson * that region of the page can be updated with the partial zero out. 34074e96b2dbSAllison Henderson * 34084e96b2dbSAllison Henderson * This function assumes that the page has already been locked. The 34094e96b2dbSAllison Henderson * The range to be discarded must be contained with in the given page. 34104e96b2dbSAllison Henderson * If the specified range exceeds the end of the page it will be shortened 34114e96b2dbSAllison Henderson * to the end of the page that corresponds to 'from'. This function is 34124e96b2dbSAllison Henderson * appropriate for updating a page and it buffer heads to be unmapped and 34134e96b2dbSAllison Henderson * zeroed for blocks that have been either released, or are going to be 34144e96b2dbSAllison Henderson * released. 34154e96b2dbSAllison Henderson * 34164e96b2dbSAllison Henderson * handle: The journal handle 34174e96b2dbSAllison Henderson * inode: The files inode 34184e96b2dbSAllison Henderson * page: A locked page that contains the offset "from" 34194907cb7bSAnatol Pomozov * from: The starting byte offset (from the beginning of the file) 34204e96b2dbSAllison Henderson * to begin discarding 34214e96b2dbSAllison Henderson * len: The length of bytes to discard 34224e96b2dbSAllison Henderson * flags: Optional flags that may be used: 34234e96b2dbSAllison Henderson * 34244e96b2dbSAllison Henderson * EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED 34254e96b2dbSAllison Henderson * Only zero the regions of the page whose buffer heads 34264e96b2dbSAllison Henderson * have already been unmapped. This flag is appropriate 34274907cb7bSAnatol Pomozov * for updating the contents of a page whose blocks may 34284e96b2dbSAllison Henderson * have already been released, and we only want to zero 34294e96b2dbSAllison Henderson * out the regions that correspond to those released blocks. 34304e96b2dbSAllison Henderson * 34314907cb7bSAnatol Pomozov * Returns zero on success or negative on failure. 34324e96b2dbSAllison Henderson */ 34335f163cc7SEric Sandeen static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle, 34344e96b2dbSAllison Henderson struct inode *inode, struct page *page, loff_t from, 34354e96b2dbSAllison Henderson loff_t length, int flags) 34364e96b2dbSAllison Henderson { 34374e96b2dbSAllison Henderson ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; 34384e96b2dbSAllison Henderson unsigned int offset = from & (PAGE_CACHE_SIZE-1); 34394e96b2dbSAllison Henderson unsigned int blocksize, max, pos; 34404e96b2dbSAllison Henderson ext4_lblk_t iblock; 34414e96b2dbSAllison Henderson struct buffer_head *bh; 34424e96b2dbSAllison Henderson int err = 0; 34434e96b2dbSAllison Henderson 34444e96b2dbSAllison Henderson blocksize = inode->i_sb->s_blocksize; 34454e96b2dbSAllison Henderson max = PAGE_CACHE_SIZE - offset; 34464e96b2dbSAllison Henderson 34474e96b2dbSAllison Henderson if (index != page->index) 34484e96b2dbSAllison Henderson return -EINVAL; 34494e96b2dbSAllison Henderson 34504e96b2dbSAllison Henderson /* 34514e96b2dbSAllison Henderson * correct length if it does not fall between 34524e96b2dbSAllison Henderson * 'from' and the end of the page 34534e96b2dbSAllison Henderson */ 34544e96b2dbSAllison Henderson if (length > max || length < 0) 34554e96b2dbSAllison Henderson length = max; 34564e96b2dbSAllison Henderson 34574e96b2dbSAllison Henderson iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); 34584e96b2dbSAllison Henderson 3459093e6e36SYongqiang Yang if (!page_has_buffers(page)) 34604e96b2dbSAllison Henderson create_empty_buffers(page, blocksize, 0); 34614e96b2dbSAllison Henderson 34624e96b2dbSAllison Henderson /* Find the buffer that contains "offset" */ 34634e96b2dbSAllison Henderson bh = page_buffers(page); 34644e96b2dbSAllison Henderson pos = blocksize; 34654e96b2dbSAllison Henderson while (offset >= pos) { 34664e96b2dbSAllison Henderson bh = bh->b_this_page; 34674e96b2dbSAllison Henderson iblock++; 34684e96b2dbSAllison Henderson pos += blocksize; 34694e96b2dbSAllison Henderson } 34704e96b2dbSAllison Henderson 34714e96b2dbSAllison Henderson pos = offset; 34724e96b2dbSAllison Henderson while (pos < offset + length) { 3473e260daf2SYongqiang Yang unsigned int end_of_block, range_to_discard; 3474e260daf2SYongqiang Yang 34754e96b2dbSAllison Henderson err = 0; 34764e96b2dbSAllison Henderson 34774e96b2dbSAllison Henderson /* The length of space left to zero and unmap */ 34784e96b2dbSAllison Henderson range_to_discard = offset + length - pos; 34794e96b2dbSAllison Henderson 34804e96b2dbSAllison Henderson /* The length of space until the end of the block */ 34814e96b2dbSAllison Henderson end_of_block = blocksize - (pos & (blocksize-1)); 34824e96b2dbSAllison Henderson 34834e96b2dbSAllison Henderson /* 34844e96b2dbSAllison Henderson * Do not unmap or zero past end of block 34854e96b2dbSAllison Henderson * for this buffer head 34864e96b2dbSAllison Henderson */ 34874e96b2dbSAllison Henderson if (range_to_discard > end_of_block) 34884e96b2dbSAllison Henderson range_to_discard = end_of_block; 34894e96b2dbSAllison Henderson 34904e96b2dbSAllison Henderson 34914e96b2dbSAllison Henderson /* 34924e96b2dbSAllison Henderson * Skip this buffer head if we are only zeroing unampped 34934e96b2dbSAllison Henderson * regions of the page 34944e96b2dbSAllison Henderson */ 34954e96b2dbSAllison Henderson if (flags & EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED && 34964e96b2dbSAllison Henderson buffer_mapped(bh)) 34974e96b2dbSAllison Henderson goto next; 34984e96b2dbSAllison Henderson 34994e96b2dbSAllison Henderson /* If the range is block aligned, unmap */ 35004e96b2dbSAllison Henderson if (range_to_discard == blocksize) { 35014e96b2dbSAllison Henderson clear_buffer_dirty(bh); 35024e96b2dbSAllison Henderson bh->b_bdev = NULL; 35034e96b2dbSAllison Henderson clear_buffer_mapped(bh); 35044e96b2dbSAllison Henderson clear_buffer_req(bh); 35054e96b2dbSAllison Henderson clear_buffer_new(bh); 35064e96b2dbSAllison Henderson clear_buffer_delay(bh); 35074e96b2dbSAllison Henderson clear_buffer_unwritten(bh); 35084e96b2dbSAllison Henderson clear_buffer_uptodate(bh); 35094e96b2dbSAllison Henderson zero_user(page, pos, range_to_discard); 35104e96b2dbSAllison Henderson BUFFER_TRACE(bh, "Buffer discarded"); 35114e96b2dbSAllison Henderson goto next; 35124e96b2dbSAllison Henderson } 35134e96b2dbSAllison Henderson 35144e96b2dbSAllison Henderson /* 35154e96b2dbSAllison Henderson * If this block is not completely contained in the range 35164e96b2dbSAllison Henderson * to be discarded, then it is not going to be released. Because 35174e96b2dbSAllison Henderson * we need to keep this block, we need to make sure this part 35184e96b2dbSAllison Henderson * of the page is uptodate before we modify it by writeing 35194e96b2dbSAllison Henderson * partial zeros on it. 35204e96b2dbSAllison Henderson */ 35214e96b2dbSAllison Henderson if (!buffer_mapped(bh)) { 35224e96b2dbSAllison Henderson /* 35234e96b2dbSAllison Henderson * Buffer head must be mapped before we can read 35244e96b2dbSAllison Henderson * from the block 35254e96b2dbSAllison Henderson */ 35264e96b2dbSAllison Henderson BUFFER_TRACE(bh, "unmapped"); 35274e96b2dbSAllison Henderson ext4_get_block(inode, iblock, bh, 0); 35284e96b2dbSAllison Henderson /* unmapped? It's a hole - nothing to do */ 35294e96b2dbSAllison Henderson if (!buffer_mapped(bh)) { 35304e96b2dbSAllison Henderson BUFFER_TRACE(bh, "still unmapped"); 35314e96b2dbSAllison Henderson goto next; 35324e96b2dbSAllison Henderson } 35334e96b2dbSAllison Henderson } 35344e96b2dbSAllison Henderson 35354e96b2dbSAllison Henderson /* Ok, it's mapped. Make sure it's up-to-date */ 35364e96b2dbSAllison Henderson if (PageUptodate(page)) 35374e96b2dbSAllison Henderson set_buffer_uptodate(bh); 35384e96b2dbSAllison Henderson 35394e96b2dbSAllison Henderson if (!buffer_uptodate(bh)) { 35404e96b2dbSAllison Henderson err = -EIO; 35414e96b2dbSAllison Henderson ll_rw_block(READ, 1, &bh); 35424e96b2dbSAllison Henderson wait_on_buffer(bh); 35434e96b2dbSAllison Henderson /* Uhhuh. Read error. Complain and punt.*/ 35444e96b2dbSAllison Henderson if (!buffer_uptodate(bh)) 35454e96b2dbSAllison Henderson goto next; 35464e96b2dbSAllison Henderson } 35474e96b2dbSAllison Henderson 35484e96b2dbSAllison Henderson if (ext4_should_journal_data(inode)) { 35494e96b2dbSAllison Henderson BUFFER_TRACE(bh, "get write access"); 35504e96b2dbSAllison Henderson err = ext4_journal_get_write_access(handle, bh); 35514e96b2dbSAllison Henderson if (err) 35524e96b2dbSAllison Henderson goto next; 35534e96b2dbSAllison Henderson } 35544e96b2dbSAllison Henderson 35554e96b2dbSAllison Henderson zero_user(page, pos, range_to_discard); 35564e96b2dbSAllison Henderson 35574e96b2dbSAllison Henderson err = 0; 35584e96b2dbSAllison Henderson if (ext4_should_journal_data(inode)) { 35594e96b2dbSAllison Henderson err = ext4_handle_dirty_metadata(handle, inode, bh); 3560decbd919STheodore Ts'o } else 35614e96b2dbSAllison Henderson mark_buffer_dirty(bh); 35624e96b2dbSAllison Henderson 35634e96b2dbSAllison Henderson BUFFER_TRACE(bh, "Partial buffer zeroed"); 35644e96b2dbSAllison Henderson next: 35654e96b2dbSAllison Henderson bh = bh->b_this_page; 35664e96b2dbSAllison Henderson iblock++; 35674e96b2dbSAllison Henderson pos += range_to_discard; 35684e96b2dbSAllison Henderson } 35694e96b2dbSAllison Henderson 35704e96b2dbSAllison Henderson return err; 35714e96b2dbSAllison Henderson } 35724e96b2dbSAllison Henderson 3573d863dc36SLukas Czerner /* 3574d863dc36SLukas Czerner * ext4_block_truncate_page() zeroes out a mapping from file offset `from' 3575d863dc36SLukas Czerner * up to the end of the block which corresponds to `from'. 3576d863dc36SLukas Czerner * This required during truncate. We need to physically zero the tail end 3577d863dc36SLukas Czerner * of that block so it doesn't yield old data if the file is later grown. 3578d863dc36SLukas Czerner */ 3579d863dc36SLukas Czerner int ext4_block_truncate_page(handle_t *handle, 3580d863dc36SLukas Czerner struct address_space *mapping, loff_t from) 3581d863dc36SLukas Czerner { 3582d863dc36SLukas Czerner unsigned offset = from & (PAGE_CACHE_SIZE-1); 3583d863dc36SLukas Czerner unsigned length; 3584d863dc36SLukas Czerner unsigned blocksize; 3585d863dc36SLukas Czerner struct inode *inode = mapping->host; 3586d863dc36SLukas Czerner 3587d863dc36SLukas Czerner blocksize = inode->i_sb->s_blocksize; 3588d863dc36SLukas Czerner length = blocksize - (offset & (blocksize - 1)); 3589d863dc36SLukas Czerner 3590d863dc36SLukas Czerner return ext4_block_zero_page_range(handle, mapping, from, length); 3591d863dc36SLukas Czerner } 3592d863dc36SLukas Czerner 3593d863dc36SLukas Czerner /* 3594d863dc36SLukas Czerner * ext4_block_zero_page_range() zeros out a mapping of length 'length' 3595d863dc36SLukas Czerner * starting from file offset 'from'. The range to be zero'd must 3596d863dc36SLukas Czerner * be contained with in one block. If the specified range exceeds 3597d863dc36SLukas Czerner * the end of the block it will be shortened to end of the block 3598d863dc36SLukas Czerner * that cooresponds to 'from' 3599d863dc36SLukas Czerner */ 3600d863dc36SLukas Czerner int ext4_block_zero_page_range(handle_t *handle, 3601d863dc36SLukas Czerner struct address_space *mapping, loff_t from, loff_t length) 3602d863dc36SLukas Czerner { 3603d863dc36SLukas Czerner ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; 3604d863dc36SLukas Czerner unsigned offset = from & (PAGE_CACHE_SIZE-1); 3605d863dc36SLukas Czerner unsigned blocksize, max, pos; 3606d863dc36SLukas Czerner ext4_lblk_t iblock; 3607d863dc36SLukas Czerner struct inode *inode = mapping->host; 3608d863dc36SLukas Czerner struct buffer_head *bh; 3609d863dc36SLukas Czerner struct page *page; 3610d863dc36SLukas Czerner int err = 0; 3611d863dc36SLukas Czerner 3612d863dc36SLukas Czerner page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT, 3613d863dc36SLukas Czerner mapping_gfp_mask(mapping) & ~__GFP_FS); 3614d863dc36SLukas Czerner if (!page) 3615d863dc36SLukas Czerner return -ENOMEM; 3616d863dc36SLukas Czerner 3617d863dc36SLukas Czerner blocksize = inode->i_sb->s_blocksize; 3618d863dc36SLukas Czerner max = blocksize - (offset & (blocksize - 1)); 3619d863dc36SLukas Czerner 3620d863dc36SLukas Czerner /* 3621d863dc36SLukas Czerner * correct length if it does not fall between 3622d863dc36SLukas Czerner * 'from' and the end of the block 3623d863dc36SLukas Czerner */ 3624d863dc36SLukas Czerner if (length > max || length < 0) 3625d863dc36SLukas Czerner length = max; 3626d863dc36SLukas Czerner 3627d863dc36SLukas Czerner iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); 3628d863dc36SLukas Czerner 3629d863dc36SLukas Czerner if (!page_has_buffers(page)) 3630d863dc36SLukas Czerner create_empty_buffers(page, blocksize, 0); 3631d863dc36SLukas Czerner 3632d863dc36SLukas Czerner /* Find the buffer that contains "offset" */ 3633d863dc36SLukas Czerner bh = page_buffers(page); 3634d863dc36SLukas Czerner pos = blocksize; 3635d863dc36SLukas Czerner while (offset >= pos) { 3636d863dc36SLukas Czerner bh = bh->b_this_page; 3637d863dc36SLukas Czerner iblock++; 3638d863dc36SLukas Czerner pos += blocksize; 3639d863dc36SLukas Czerner } 3640d863dc36SLukas Czerner 3641d863dc36SLukas Czerner err = 0; 3642d863dc36SLukas Czerner if (buffer_freed(bh)) { 3643d863dc36SLukas Czerner BUFFER_TRACE(bh, "freed: skip"); 3644d863dc36SLukas Czerner goto unlock; 3645d863dc36SLukas Czerner } 3646d863dc36SLukas Czerner 3647d863dc36SLukas Czerner if (!buffer_mapped(bh)) { 3648d863dc36SLukas Czerner BUFFER_TRACE(bh, "unmapped"); 3649d863dc36SLukas Czerner ext4_get_block(inode, iblock, bh, 0); 3650d863dc36SLukas Czerner /* unmapped? It's a hole - nothing to do */ 3651d863dc36SLukas Czerner if (!buffer_mapped(bh)) { 3652d863dc36SLukas Czerner BUFFER_TRACE(bh, "still unmapped"); 3653d863dc36SLukas Czerner goto unlock; 3654d863dc36SLukas Czerner } 3655d863dc36SLukas Czerner } 3656d863dc36SLukas Czerner 3657d863dc36SLukas Czerner /* Ok, it's mapped. Make sure it's up-to-date */ 3658d863dc36SLukas Czerner if (PageUptodate(page)) 3659d863dc36SLukas Czerner set_buffer_uptodate(bh); 3660d863dc36SLukas Czerner 3661d863dc36SLukas Czerner if (!buffer_uptodate(bh)) { 3662d863dc36SLukas Czerner err = -EIO; 3663d863dc36SLukas Czerner ll_rw_block(READ, 1, &bh); 3664d863dc36SLukas Czerner wait_on_buffer(bh); 3665d863dc36SLukas Czerner /* Uhhuh. Read error. Complain and punt. */ 3666d863dc36SLukas Czerner if (!buffer_uptodate(bh)) 3667d863dc36SLukas Czerner goto unlock; 3668d863dc36SLukas Czerner } 3669d863dc36SLukas Czerner 3670d863dc36SLukas Czerner if (ext4_should_journal_data(inode)) { 3671d863dc36SLukas Czerner BUFFER_TRACE(bh, "get write access"); 3672d863dc36SLukas Czerner err = ext4_journal_get_write_access(handle, bh); 3673d863dc36SLukas Czerner if (err) 3674d863dc36SLukas Czerner goto unlock; 3675d863dc36SLukas Czerner } 3676d863dc36SLukas Czerner 3677d863dc36SLukas Czerner zero_user(page, offset, length); 3678d863dc36SLukas Czerner 3679d863dc36SLukas Czerner BUFFER_TRACE(bh, "zeroed end of block"); 3680d863dc36SLukas Czerner 3681d863dc36SLukas Czerner err = 0; 3682d863dc36SLukas Czerner if (ext4_should_journal_data(inode)) { 3683d863dc36SLukas Czerner err = ext4_handle_dirty_metadata(handle, inode, bh); 3684*0713ed0cSLukas Czerner } else { 3685d863dc36SLukas Czerner mark_buffer_dirty(bh); 3686*0713ed0cSLukas Czerner if (ext4_test_inode_state(inode, EXT4_STATE_ORDERED_MODE)) 3687*0713ed0cSLukas Czerner err = ext4_jbd2_file_inode(handle, inode); 3688*0713ed0cSLukas Czerner } 3689d863dc36SLukas Czerner 3690d863dc36SLukas Czerner unlock: 3691d863dc36SLukas Czerner unlock_page(page); 3692d863dc36SLukas Czerner page_cache_release(page); 3693d863dc36SLukas Czerner return err; 3694d863dc36SLukas Czerner } 3695d863dc36SLukas Czerner 369691ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 369791ef4cafSDuane Griffin { 369891ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 369991ef4cafSDuane Griffin return 1; 370091ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 370191ef4cafSDuane Griffin return 1; 370291ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 370391ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 370491ef4cafSDuane Griffin return 0; 370591ef4cafSDuane Griffin } 370691ef4cafSDuane Griffin 3707ac27a0ecSDave Kleikamp /* 3708a4bb6b64SAllison Henderson * ext4_punch_hole: punches a hole in a file by releaseing the blocks 3709a4bb6b64SAllison Henderson * associated with the given offset and length 3710a4bb6b64SAllison Henderson * 3711a4bb6b64SAllison Henderson * @inode: File inode 3712a4bb6b64SAllison Henderson * @offset: The offset where the hole will begin 3713a4bb6b64SAllison Henderson * @len: The length of the hole 3714a4bb6b64SAllison Henderson * 37154907cb7bSAnatol Pomozov * Returns: 0 on success or negative on failure 3716a4bb6b64SAllison Henderson */ 3717a4bb6b64SAllison Henderson 3718a4bb6b64SAllison Henderson int ext4_punch_hole(struct file *file, loff_t offset, loff_t length) 3719a4bb6b64SAllison Henderson { 3720496ad9aaSAl Viro struct inode *inode = file_inode(file); 372126a4c0c6STheodore Ts'o struct super_block *sb = inode->i_sb; 372226a4c0c6STheodore Ts'o ext4_lblk_t first_block, stop_block; 372326a4c0c6STheodore Ts'o struct address_space *mapping = inode->i_mapping; 372426a4c0c6STheodore Ts'o loff_t first_page, last_page, page_len; 372526a4c0c6STheodore Ts'o loff_t first_page_offset, last_page_offset; 372626a4c0c6STheodore Ts'o handle_t *handle; 372726a4c0c6STheodore Ts'o unsigned int credits; 372826a4c0c6STheodore Ts'o int ret = 0; 372926a4c0c6STheodore Ts'o 3730a4bb6b64SAllison Henderson if (!S_ISREG(inode->i_mode)) 373173355192SAllison Henderson return -EOPNOTSUPP; 3732a4bb6b64SAllison Henderson 373326a4c0c6STheodore Ts'o if (EXT4_SB(sb)->s_cluster_ratio > 1) { 3734bab08ab9STheodore Ts'o /* TODO: Add support for bigalloc file systems */ 373573355192SAllison Henderson return -EOPNOTSUPP; 3736bab08ab9STheodore Ts'o } 3737bab08ab9STheodore Ts'o 3738aaddea81SZheng Liu trace_ext4_punch_hole(inode, offset, length); 3739aaddea81SZheng Liu 374026a4c0c6STheodore Ts'o /* 374126a4c0c6STheodore Ts'o * Write out all dirty pages to avoid race conditions 374226a4c0c6STheodore Ts'o * Then release them. 374326a4c0c6STheodore Ts'o */ 374426a4c0c6STheodore Ts'o if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) { 374526a4c0c6STheodore Ts'o ret = filemap_write_and_wait_range(mapping, offset, 374626a4c0c6STheodore Ts'o offset + length - 1); 374726a4c0c6STheodore Ts'o if (ret) 374826a4c0c6STheodore Ts'o return ret; 374926a4c0c6STheodore Ts'o } 375026a4c0c6STheodore Ts'o 375126a4c0c6STheodore Ts'o mutex_lock(&inode->i_mutex); 375226a4c0c6STheodore Ts'o /* It's not possible punch hole on append only file */ 375326a4c0c6STheodore Ts'o if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) { 375426a4c0c6STheodore Ts'o ret = -EPERM; 375526a4c0c6STheodore Ts'o goto out_mutex; 375626a4c0c6STheodore Ts'o } 375726a4c0c6STheodore Ts'o if (IS_SWAPFILE(inode)) { 375826a4c0c6STheodore Ts'o ret = -ETXTBSY; 375926a4c0c6STheodore Ts'o goto out_mutex; 376026a4c0c6STheodore Ts'o } 376126a4c0c6STheodore Ts'o 376226a4c0c6STheodore Ts'o /* No need to punch hole beyond i_size */ 376326a4c0c6STheodore Ts'o if (offset >= inode->i_size) 376426a4c0c6STheodore Ts'o goto out_mutex; 376526a4c0c6STheodore Ts'o 376626a4c0c6STheodore Ts'o /* 376726a4c0c6STheodore Ts'o * If the hole extends beyond i_size, set the hole 376826a4c0c6STheodore Ts'o * to end after the page that contains i_size 376926a4c0c6STheodore Ts'o */ 377026a4c0c6STheodore Ts'o if (offset + length > inode->i_size) { 377126a4c0c6STheodore Ts'o length = inode->i_size + 377226a4c0c6STheodore Ts'o PAGE_CACHE_SIZE - (inode->i_size & (PAGE_CACHE_SIZE - 1)) - 377326a4c0c6STheodore Ts'o offset; 377426a4c0c6STheodore Ts'o } 377526a4c0c6STheodore Ts'o 377626a4c0c6STheodore Ts'o first_page = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 377726a4c0c6STheodore Ts'o last_page = (offset + length) >> PAGE_CACHE_SHIFT; 377826a4c0c6STheodore Ts'o 377926a4c0c6STheodore Ts'o first_page_offset = first_page << PAGE_CACHE_SHIFT; 378026a4c0c6STheodore Ts'o last_page_offset = last_page << PAGE_CACHE_SHIFT; 378126a4c0c6STheodore Ts'o 378226a4c0c6STheodore Ts'o /* Now release the pages */ 378326a4c0c6STheodore Ts'o if (last_page_offset > first_page_offset) { 378426a4c0c6STheodore Ts'o truncate_pagecache_range(inode, first_page_offset, 378526a4c0c6STheodore Ts'o last_page_offset - 1); 378626a4c0c6STheodore Ts'o } 378726a4c0c6STheodore Ts'o 378826a4c0c6STheodore Ts'o /* Wait all existing dio workers, newcomers will block on i_mutex */ 378926a4c0c6STheodore Ts'o ext4_inode_block_unlocked_dio(inode); 379026a4c0c6STheodore Ts'o ret = ext4_flush_unwritten_io(inode); 379126a4c0c6STheodore Ts'o if (ret) 379226a4c0c6STheodore Ts'o goto out_dio; 379326a4c0c6STheodore Ts'o inode_dio_wait(inode); 379426a4c0c6STheodore Ts'o 379526a4c0c6STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 379626a4c0c6STheodore Ts'o credits = ext4_writepage_trans_blocks(inode); 379726a4c0c6STheodore Ts'o else 379826a4c0c6STheodore Ts'o credits = ext4_blocks_for_truncate(inode); 379926a4c0c6STheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 380026a4c0c6STheodore Ts'o if (IS_ERR(handle)) { 380126a4c0c6STheodore Ts'o ret = PTR_ERR(handle); 380226a4c0c6STheodore Ts'o ext4_std_error(sb, ret); 380326a4c0c6STheodore Ts'o goto out_dio; 380426a4c0c6STheodore Ts'o } 380526a4c0c6STheodore Ts'o 380626a4c0c6STheodore Ts'o /* 380726a4c0c6STheodore Ts'o * Now we need to zero out the non-page-aligned data in the 380826a4c0c6STheodore Ts'o * pages at the start and tail of the hole, and unmap the 380926a4c0c6STheodore Ts'o * buffer heads for the block aligned regions of the page that 381026a4c0c6STheodore Ts'o * were completely zeroed. 381126a4c0c6STheodore Ts'o */ 381226a4c0c6STheodore Ts'o if (first_page > last_page) { 381326a4c0c6STheodore Ts'o /* 381426a4c0c6STheodore Ts'o * If the file space being truncated is contained 381526a4c0c6STheodore Ts'o * within a page just zero out and unmap the middle of 381626a4c0c6STheodore Ts'o * that page 381726a4c0c6STheodore Ts'o */ 381826a4c0c6STheodore Ts'o ret = ext4_discard_partial_page_buffers(handle, 381926a4c0c6STheodore Ts'o mapping, offset, length, 0); 382026a4c0c6STheodore Ts'o 382126a4c0c6STheodore Ts'o if (ret) 382226a4c0c6STheodore Ts'o goto out_stop; 382326a4c0c6STheodore Ts'o } else { 382426a4c0c6STheodore Ts'o /* 382526a4c0c6STheodore Ts'o * zero out and unmap the partial page that contains 382626a4c0c6STheodore Ts'o * the start of the hole 382726a4c0c6STheodore Ts'o */ 382826a4c0c6STheodore Ts'o page_len = first_page_offset - offset; 382926a4c0c6STheodore Ts'o if (page_len > 0) { 383026a4c0c6STheodore Ts'o ret = ext4_discard_partial_page_buffers(handle, mapping, 383126a4c0c6STheodore Ts'o offset, page_len, 0); 383226a4c0c6STheodore Ts'o if (ret) 383326a4c0c6STheodore Ts'o goto out_stop; 383426a4c0c6STheodore Ts'o } 383526a4c0c6STheodore Ts'o 383626a4c0c6STheodore Ts'o /* 383726a4c0c6STheodore Ts'o * zero out and unmap the partial page that contains 383826a4c0c6STheodore Ts'o * the end of the hole 383926a4c0c6STheodore Ts'o */ 384026a4c0c6STheodore Ts'o page_len = offset + length - last_page_offset; 384126a4c0c6STheodore Ts'o if (page_len > 0) { 384226a4c0c6STheodore Ts'o ret = ext4_discard_partial_page_buffers(handle, mapping, 384326a4c0c6STheodore Ts'o last_page_offset, page_len, 0); 384426a4c0c6STheodore Ts'o if (ret) 384526a4c0c6STheodore Ts'o goto out_stop; 384626a4c0c6STheodore Ts'o } 384726a4c0c6STheodore Ts'o } 384826a4c0c6STheodore Ts'o 384926a4c0c6STheodore Ts'o /* 385026a4c0c6STheodore Ts'o * If i_size is contained in the last page, we need to 385126a4c0c6STheodore Ts'o * unmap and zero the partial page after i_size 385226a4c0c6STheodore Ts'o */ 385326a4c0c6STheodore Ts'o if (inode->i_size >> PAGE_CACHE_SHIFT == last_page && 385426a4c0c6STheodore Ts'o inode->i_size % PAGE_CACHE_SIZE != 0) { 385526a4c0c6STheodore Ts'o page_len = PAGE_CACHE_SIZE - 385626a4c0c6STheodore Ts'o (inode->i_size & (PAGE_CACHE_SIZE - 1)); 385726a4c0c6STheodore Ts'o 385826a4c0c6STheodore Ts'o if (page_len > 0) { 385926a4c0c6STheodore Ts'o ret = ext4_discard_partial_page_buffers(handle, 386026a4c0c6STheodore Ts'o mapping, inode->i_size, page_len, 0); 386126a4c0c6STheodore Ts'o 386226a4c0c6STheodore Ts'o if (ret) 386326a4c0c6STheodore Ts'o goto out_stop; 386426a4c0c6STheodore Ts'o } 386526a4c0c6STheodore Ts'o } 386626a4c0c6STheodore Ts'o 386726a4c0c6STheodore Ts'o first_block = (offset + sb->s_blocksize - 1) >> 386826a4c0c6STheodore Ts'o EXT4_BLOCK_SIZE_BITS(sb); 386926a4c0c6STheodore Ts'o stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb); 387026a4c0c6STheodore Ts'o 387126a4c0c6STheodore Ts'o /* If there are no blocks to remove, return now */ 387226a4c0c6STheodore Ts'o if (first_block >= stop_block) 387326a4c0c6STheodore Ts'o goto out_stop; 387426a4c0c6STheodore Ts'o 387526a4c0c6STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 387626a4c0c6STheodore Ts'o ext4_discard_preallocations(inode); 387726a4c0c6STheodore Ts'o 387826a4c0c6STheodore Ts'o ret = ext4_es_remove_extent(inode, first_block, 387926a4c0c6STheodore Ts'o stop_block - first_block); 388026a4c0c6STheodore Ts'o if (ret) { 388126a4c0c6STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 388226a4c0c6STheodore Ts'o goto out_stop; 388326a4c0c6STheodore Ts'o } 388426a4c0c6STheodore Ts'o 388526a4c0c6STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 388626a4c0c6STheodore Ts'o ret = ext4_ext_remove_space(inode, first_block, 388726a4c0c6STheodore Ts'o stop_block - 1); 388826a4c0c6STheodore Ts'o else 388926a4c0c6STheodore Ts'o ret = ext4_free_hole_blocks(handle, inode, first_block, 389026a4c0c6STheodore Ts'o stop_block); 389126a4c0c6STheodore Ts'o 389226a4c0c6STheodore Ts'o ext4_discard_preallocations(inode); 3893819c4920STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 389426a4c0c6STheodore Ts'o if (IS_SYNC(inode)) 389526a4c0c6STheodore Ts'o ext4_handle_sync(handle); 389626a4c0c6STheodore Ts'o inode->i_mtime = inode->i_ctime = ext4_current_time(inode); 389726a4c0c6STheodore Ts'o ext4_mark_inode_dirty(handle, inode); 389826a4c0c6STheodore Ts'o out_stop: 389926a4c0c6STheodore Ts'o ext4_journal_stop(handle); 390026a4c0c6STheodore Ts'o out_dio: 390126a4c0c6STheodore Ts'o ext4_inode_resume_unlocked_dio(inode); 390226a4c0c6STheodore Ts'o out_mutex: 390326a4c0c6STheodore Ts'o mutex_unlock(&inode->i_mutex); 390426a4c0c6STheodore Ts'o return ret; 3905a4bb6b64SAllison Henderson } 3906a4bb6b64SAllison Henderson 3907a4bb6b64SAllison Henderson /* 3908617ba13bSMingming Cao * ext4_truncate() 3909ac27a0ecSDave Kleikamp * 3910617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 3911617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 3912ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 3913ac27a0ecSDave Kleikamp * 391442b2aa86SJustin P. Mattock * As we work through the truncate and commit bits of it to the journal there 3915ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 3916ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 3917ac27a0ecSDave Kleikamp * 3918ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 3919ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 3920ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 3921ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 3922ac27a0ecSDave Kleikamp * left-to-right works OK too). 3923ac27a0ecSDave Kleikamp * 3924ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 3925ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 3926ac27a0ecSDave Kleikamp * 3927ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 3928617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 3929ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 3930617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 3931617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 3932ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 3933617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 3934ac27a0ecSDave Kleikamp */ 3935617ba13bSMingming Cao void ext4_truncate(struct inode *inode) 3936ac27a0ecSDave Kleikamp { 3937819c4920STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 3938819c4920STheodore Ts'o unsigned int credits; 3939819c4920STheodore Ts'o handle_t *handle; 3940819c4920STheodore Ts'o struct address_space *mapping = inode->i_mapping; 3941819c4920STheodore Ts'o loff_t page_len; 3942819c4920STheodore Ts'o 394319b5ef61STheodore Ts'o /* 394419b5ef61STheodore Ts'o * There is a possibility that we're either freeing the inode 394519b5ef61STheodore Ts'o * or it completely new indode. In those cases we might not 394619b5ef61STheodore Ts'o * have i_mutex locked because it's not necessary. 394719b5ef61STheodore Ts'o */ 394819b5ef61STheodore Ts'o if (!(inode->i_state & (I_NEW|I_FREEING))) 394919b5ef61STheodore Ts'o WARN_ON(!mutex_is_locked(&inode->i_mutex)); 39500562e0baSJiaying Zhang trace_ext4_truncate_enter(inode); 39510562e0baSJiaying Zhang 395291ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 3953ac27a0ecSDave Kleikamp return; 3954ac27a0ecSDave Kleikamp 395512e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS); 3956c8d46e41SJiaying Zhang 39575534fb5bSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 395819f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE); 39597d8f9f7dSTheodore Ts'o 3960aef1c851STao Ma if (ext4_has_inline_data(inode)) { 3961aef1c851STao Ma int has_inline = 1; 3962aef1c851STao Ma 3963aef1c851STao Ma ext4_inline_data_truncate(inode, &has_inline); 3964aef1c851STao Ma if (has_inline) 3965aef1c851STao Ma return; 3966aef1c851STao Ma } 3967aef1c851STao Ma 3968819c4920STheodore Ts'o /* 3969819c4920STheodore Ts'o * finish any pending end_io work so we won't run the risk of 3970819c4920STheodore Ts'o * converting any truncated blocks to initialized later 3971819c4920STheodore Ts'o */ 3972819c4920STheodore Ts'o ext4_flush_unwritten_io(inode); 3973819c4920STheodore Ts'o 3974ff9893dcSAmir Goldstein if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3975819c4920STheodore Ts'o credits = ext4_writepage_trans_blocks(inode); 3976ff9893dcSAmir Goldstein else 3977819c4920STheodore Ts'o credits = ext4_blocks_for_truncate(inode); 3978819c4920STheodore Ts'o 3979819c4920STheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 3980819c4920STheodore Ts'o if (IS_ERR(handle)) { 3981819c4920STheodore Ts'o ext4_std_error(inode->i_sb, PTR_ERR(handle)); 3982819c4920STheodore Ts'o return; 3983819c4920STheodore Ts'o } 3984819c4920STheodore Ts'o 3985819c4920STheodore Ts'o if (inode->i_size % PAGE_CACHE_SIZE != 0) { 3986819c4920STheodore Ts'o page_len = PAGE_CACHE_SIZE - 3987819c4920STheodore Ts'o (inode->i_size & (PAGE_CACHE_SIZE - 1)); 3988819c4920STheodore Ts'o 3989819c4920STheodore Ts'o if (ext4_discard_partial_page_buffers(handle, 3990819c4920STheodore Ts'o mapping, inode->i_size, page_len, 0)) 3991819c4920STheodore Ts'o goto out_stop; 3992819c4920STheodore Ts'o } 3993819c4920STheodore Ts'o 3994819c4920STheodore Ts'o /* 3995819c4920STheodore Ts'o * We add the inode to the orphan list, so that if this 3996819c4920STheodore Ts'o * truncate spans multiple transactions, and we crash, we will 3997819c4920STheodore Ts'o * resume the truncate when the filesystem recovers. It also 3998819c4920STheodore Ts'o * marks the inode dirty, to catch the new size. 3999819c4920STheodore Ts'o * 4000819c4920STheodore Ts'o * Implication: the file must always be in a sane, consistent 4001819c4920STheodore Ts'o * truncatable state while each transaction commits. 4002819c4920STheodore Ts'o */ 4003819c4920STheodore Ts'o if (ext4_orphan_add(handle, inode)) 4004819c4920STheodore Ts'o goto out_stop; 4005819c4920STheodore Ts'o 4006819c4920STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 4007819c4920STheodore Ts'o 4008819c4920STheodore Ts'o ext4_discard_preallocations(inode); 4009819c4920STheodore Ts'o 4010819c4920STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4011819c4920STheodore Ts'o ext4_ext_truncate(handle, inode); 4012819c4920STheodore Ts'o else 4013819c4920STheodore Ts'o ext4_ind_truncate(handle, inode); 4014819c4920STheodore Ts'o 4015819c4920STheodore Ts'o up_write(&ei->i_data_sem); 4016819c4920STheodore Ts'o 4017819c4920STheodore Ts'o if (IS_SYNC(inode)) 4018819c4920STheodore Ts'o ext4_handle_sync(handle); 4019819c4920STheodore Ts'o 4020819c4920STheodore Ts'o out_stop: 4021819c4920STheodore Ts'o /* 4022819c4920STheodore Ts'o * If this was a simple ftruncate() and the file will remain alive, 4023819c4920STheodore Ts'o * then we need to clear up the orphan record which we created above. 4024819c4920STheodore Ts'o * However, if this was a real unlink then we were called by 4025819c4920STheodore Ts'o * ext4_delete_inode(), and we allow that function to clean up the 4026819c4920STheodore Ts'o * orphan info for us. 4027819c4920STheodore Ts'o */ 4028819c4920STheodore Ts'o if (inode->i_nlink) 4029819c4920STheodore Ts'o ext4_orphan_del(handle, inode); 4030819c4920STheodore Ts'o 4031819c4920STheodore Ts'o inode->i_mtime = inode->i_ctime = ext4_current_time(inode); 4032819c4920STheodore Ts'o ext4_mark_inode_dirty(handle, inode); 4033819c4920STheodore Ts'o ext4_journal_stop(handle); 4034a86c6181SAlex Tomas 40350562e0baSJiaying Zhang trace_ext4_truncate_exit(inode); 4036ac27a0ecSDave Kleikamp } 4037ac27a0ecSDave Kleikamp 4038ac27a0ecSDave Kleikamp /* 4039617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 4040ac27a0ecSDave Kleikamp * underlying buffer_head on success. If 'in_mem' is true, we have all 4041ac27a0ecSDave Kleikamp * data in memory that is needed to recreate the on-disk version of this 4042ac27a0ecSDave Kleikamp * inode. 4043ac27a0ecSDave Kleikamp */ 4044617ba13bSMingming Cao static int __ext4_get_inode_loc(struct inode *inode, 4045617ba13bSMingming Cao struct ext4_iloc *iloc, int in_mem) 4046ac27a0ecSDave Kleikamp { 4047240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 4048ac27a0ecSDave Kleikamp struct buffer_head *bh; 4049240799cdSTheodore Ts'o struct super_block *sb = inode->i_sb; 4050240799cdSTheodore Ts'o ext4_fsblk_t block; 4051240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 4052ac27a0ecSDave Kleikamp 40533a06d778SAneesh Kumar K.V iloc->bh = NULL; 4054240799cdSTheodore Ts'o if (!ext4_valid_inum(sb, inode->i_ino)) 4055ac27a0ecSDave Kleikamp return -EIO; 4056ac27a0ecSDave Kleikamp 4057240799cdSTheodore Ts'o iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb); 4058240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 4059240799cdSTheodore Ts'o if (!gdp) 4060240799cdSTheodore Ts'o return -EIO; 4061240799cdSTheodore Ts'o 4062240799cdSTheodore Ts'o /* 4063240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 4064240799cdSTheodore Ts'o */ 406500d09882STao Ma inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 4066240799cdSTheodore Ts'o inode_offset = ((inode->i_ino - 1) % 4067240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 4068240799cdSTheodore Ts'o block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); 4069240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 4070240799cdSTheodore Ts'o 4071240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 4072aebf0243SWang Shilong if (unlikely(!bh)) 4073860d21e2STheodore Ts'o return -ENOMEM; 4074ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 4075ac27a0ecSDave Kleikamp lock_buffer(bh); 40769c83a923SHidehiro Kawai 40779c83a923SHidehiro Kawai /* 40789c83a923SHidehiro Kawai * If the buffer has the write error flag, we have failed 40799c83a923SHidehiro Kawai * to write out another inode in the same block. In this 40809c83a923SHidehiro Kawai * case, we don't have to read the block because we may 40819c83a923SHidehiro Kawai * read the old inode data successfully. 40829c83a923SHidehiro Kawai */ 40839c83a923SHidehiro Kawai if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) 40849c83a923SHidehiro Kawai set_buffer_uptodate(bh); 40859c83a923SHidehiro Kawai 4086ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) { 4087ac27a0ecSDave Kleikamp /* someone brought it uptodate while we waited */ 4088ac27a0ecSDave Kleikamp unlock_buffer(bh); 4089ac27a0ecSDave Kleikamp goto has_buffer; 4090ac27a0ecSDave Kleikamp } 4091ac27a0ecSDave Kleikamp 4092ac27a0ecSDave Kleikamp /* 4093ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 4094ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 4095ac27a0ecSDave Kleikamp * block. 4096ac27a0ecSDave Kleikamp */ 4097ac27a0ecSDave Kleikamp if (in_mem) { 4098ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 4099240799cdSTheodore Ts'o int i, start; 4100ac27a0ecSDave Kleikamp 4101240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 4102ac27a0ecSDave Kleikamp 4103ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 4104240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 4105aebf0243SWang Shilong if (unlikely(!bitmap_bh)) 4106ac27a0ecSDave Kleikamp goto make_io; 4107ac27a0ecSDave Kleikamp 4108ac27a0ecSDave Kleikamp /* 4109ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 4110ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 4111ac27a0ecSDave Kleikamp * of one, so skip it. 4112ac27a0ecSDave Kleikamp */ 4113ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 4114ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4115ac27a0ecSDave Kleikamp goto make_io; 4116ac27a0ecSDave Kleikamp } 4117240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 4118ac27a0ecSDave Kleikamp if (i == inode_offset) 4119ac27a0ecSDave Kleikamp continue; 4120617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 4121ac27a0ecSDave Kleikamp break; 4122ac27a0ecSDave Kleikamp } 4123ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4124240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 4125ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 4126ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 4127ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 4128ac27a0ecSDave Kleikamp unlock_buffer(bh); 4129ac27a0ecSDave Kleikamp goto has_buffer; 4130ac27a0ecSDave Kleikamp } 4131ac27a0ecSDave Kleikamp } 4132ac27a0ecSDave Kleikamp 4133ac27a0ecSDave Kleikamp make_io: 4134ac27a0ecSDave Kleikamp /* 4135240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 4136240799cdSTheodore Ts'o * blocks from the inode table. 4137240799cdSTheodore Ts'o */ 4138240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 4139240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 4140240799cdSTheodore Ts'o unsigned num; 41410d606e2cSTheodore Ts'o __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks; 4142240799cdSTheodore Ts'o 4143240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 4144b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 41450d606e2cSTheodore Ts'o b = block & ~((ext4_fsblk_t) ra_blks - 1); 4146240799cdSTheodore Ts'o if (table > b) 4147240799cdSTheodore Ts'o b = table; 41480d606e2cSTheodore Ts'o end = b + ra_blks; 4149240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 4150feb0ab32SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) 4151560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 4152240799cdSTheodore Ts'o table += num / inodes_per_block; 4153240799cdSTheodore Ts'o if (end > table) 4154240799cdSTheodore Ts'o end = table; 4155240799cdSTheodore Ts'o while (b <= end) 4156240799cdSTheodore Ts'o sb_breadahead(sb, b++); 4157240799cdSTheodore Ts'o } 4158240799cdSTheodore Ts'o 4159240799cdSTheodore Ts'o /* 4160ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 4161ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 4162ac27a0ecSDave Kleikamp * Read the block from disk. 4163ac27a0ecSDave Kleikamp */ 41640562e0baSJiaying Zhang trace_ext4_load_inode(inode); 4165ac27a0ecSDave Kleikamp get_bh(bh); 4166ac27a0ecSDave Kleikamp bh->b_end_io = end_buffer_read_sync; 416765299a3bSChristoph Hellwig submit_bh(READ | REQ_META | REQ_PRIO, bh); 4168ac27a0ecSDave Kleikamp wait_on_buffer(bh); 4169ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 4170c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, block, 4171c398eda0STheodore Ts'o "unable to read itable block"); 4172ac27a0ecSDave Kleikamp brelse(bh); 4173ac27a0ecSDave Kleikamp return -EIO; 4174ac27a0ecSDave Kleikamp } 4175ac27a0ecSDave Kleikamp } 4176ac27a0ecSDave Kleikamp has_buffer: 4177ac27a0ecSDave Kleikamp iloc->bh = bh; 4178ac27a0ecSDave Kleikamp return 0; 4179ac27a0ecSDave Kleikamp } 4180ac27a0ecSDave Kleikamp 4181617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 4182ac27a0ecSDave Kleikamp { 4183ac27a0ecSDave Kleikamp /* We have all inode data except xattrs in memory here. */ 4184617ba13bSMingming Cao return __ext4_get_inode_loc(inode, iloc, 418519f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_XATTR)); 4186ac27a0ecSDave Kleikamp } 4187ac27a0ecSDave Kleikamp 4188617ba13bSMingming Cao void ext4_set_inode_flags(struct inode *inode) 4189ac27a0ecSDave Kleikamp { 4190617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 4191ac27a0ecSDave Kleikamp 4192ac27a0ecSDave Kleikamp inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); 4193617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 4194ac27a0ecSDave Kleikamp inode->i_flags |= S_SYNC; 4195617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 4196ac27a0ecSDave Kleikamp inode->i_flags |= S_APPEND; 4197617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 4198ac27a0ecSDave Kleikamp inode->i_flags |= S_IMMUTABLE; 4199617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 4200ac27a0ecSDave Kleikamp inode->i_flags |= S_NOATIME; 4201617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 4202ac27a0ecSDave Kleikamp inode->i_flags |= S_DIRSYNC; 4203ac27a0ecSDave Kleikamp } 4204ac27a0ecSDave Kleikamp 4205ff9ddf7eSJan Kara /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */ 4206ff9ddf7eSJan Kara void ext4_get_inode_flags(struct ext4_inode_info *ei) 4207ff9ddf7eSJan Kara { 420884a8dce2SDmitry Monakhov unsigned int vfs_fl; 420984a8dce2SDmitry Monakhov unsigned long old_fl, new_fl; 4210ff9ddf7eSJan Kara 421184a8dce2SDmitry Monakhov do { 421284a8dce2SDmitry Monakhov vfs_fl = ei->vfs_inode.i_flags; 421384a8dce2SDmitry Monakhov old_fl = ei->i_flags; 421484a8dce2SDmitry Monakhov new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL| 421584a8dce2SDmitry Monakhov EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL| 421684a8dce2SDmitry Monakhov EXT4_DIRSYNC_FL); 421784a8dce2SDmitry Monakhov if (vfs_fl & S_SYNC) 421884a8dce2SDmitry Monakhov new_fl |= EXT4_SYNC_FL; 421984a8dce2SDmitry Monakhov if (vfs_fl & S_APPEND) 422084a8dce2SDmitry Monakhov new_fl |= EXT4_APPEND_FL; 422184a8dce2SDmitry Monakhov if (vfs_fl & S_IMMUTABLE) 422284a8dce2SDmitry Monakhov new_fl |= EXT4_IMMUTABLE_FL; 422384a8dce2SDmitry Monakhov if (vfs_fl & S_NOATIME) 422484a8dce2SDmitry Monakhov new_fl |= EXT4_NOATIME_FL; 422584a8dce2SDmitry Monakhov if (vfs_fl & S_DIRSYNC) 422684a8dce2SDmitry Monakhov new_fl |= EXT4_DIRSYNC_FL; 422784a8dce2SDmitry Monakhov } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl); 4228ff9ddf7eSJan Kara } 4229de9a55b8STheodore Ts'o 42300fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 42310fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 42320fc1b451SAneesh Kumar K.V { 42330fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 42348180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 42358180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 42360fc1b451SAneesh Kumar K.V 42370fc1b451SAneesh Kumar K.V if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 42380fc1b451SAneesh Kumar K.V EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) { 42390fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 42400fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 42410fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 424207a03824STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) { 42438180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 42448180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 42458180a562SAneesh Kumar K.V } else { 42460fc1b451SAneesh Kumar K.V return i_blocks; 42478180a562SAneesh Kumar K.V } 42480fc1b451SAneesh Kumar K.V } else { 42490fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 42500fc1b451SAneesh Kumar K.V } 42510fc1b451SAneesh Kumar K.V } 4252ff9ddf7eSJan Kara 4253152a7b0aSTao Ma static inline void ext4_iget_extra_inode(struct inode *inode, 4254152a7b0aSTao Ma struct ext4_inode *raw_inode, 4255152a7b0aSTao Ma struct ext4_inode_info *ei) 4256152a7b0aSTao Ma { 4257152a7b0aSTao Ma __le32 *magic = (void *)raw_inode + 4258152a7b0aSTao Ma EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize; 425967cf5b09STao Ma if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) { 4260152a7b0aSTao Ma ext4_set_inode_state(inode, EXT4_STATE_XATTR); 426167cf5b09STao Ma ext4_find_inline_data_nolock(inode); 4262f19d5870STao Ma } else 4263f19d5870STao Ma EXT4_I(inode)->i_inline_off = 0; 4264152a7b0aSTao Ma } 4265152a7b0aSTao Ma 42661d1fe1eeSDavid Howells struct inode *ext4_iget(struct super_block *sb, unsigned long ino) 4267ac27a0ecSDave Kleikamp { 4268617ba13bSMingming Cao struct ext4_iloc iloc; 4269617ba13bSMingming Cao struct ext4_inode *raw_inode; 42701d1fe1eeSDavid Howells struct ext4_inode_info *ei; 42711d1fe1eeSDavid Howells struct inode *inode; 4272b436b9beSJan Kara journal_t *journal = EXT4_SB(sb)->s_journal; 42731d1fe1eeSDavid Howells long ret; 4274ac27a0ecSDave Kleikamp int block; 427508cefc7aSEric W. Biederman uid_t i_uid; 427608cefc7aSEric W. Biederman gid_t i_gid; 4277ac27a0ecSDave Kleikamp 42781d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 42791d1fe1eeSDavid Howells if (!inode) 42801d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 42811d1fe1eeSDavid Howells if (!(inode->i_state & I_NEW)) 42821d1fe1eeSDavid Howells return inode; 42831d1fe1eeSDavid Howells 42841d1fe1eeSDavid Howells ei = EXT4_I(inode); 42857dc57615SPeter Huewe iloc.bh = NULL; 4286ac27a0ecSDave Kleikamp 42871d1fe1eeSDavid Howells ret = __ext4_get_inode_loc(inode, &iloc, 0); 42881d1fe1eeSDavid Howells if (ret < 0) 4289ac27a0ecSDave Kleikamp goto bad_inode; 4290617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 4291814525f4SDarrick J. Wong 4292814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4293814525f4SDarrick J. Wong ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 4294814525f4SDarrick J. Wong if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 4295814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)) { 4296814525f4SDarrick J. Wong EXT4_ERROR_INODE(inode, "bad extra_isize (%u != %u)", 4297814525f4SDarrick J. Wong EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize, 4298814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)); 4299814525f4SDarrick J. Wong ret = -EIO; 4300814525f4SDarrick J. Wong goto bad_inode; 4301814525f4SDarrick J. Wong } 4302814525f4SDarrick J. Wong } else 4303814525f4SDarrick J. Wong ei->i_extra_isize = 0; 4304814525f4SDarrick J. Wong 4305814525f4SDarrick J. Wong /* Precompute checksum seed for inode metadata */ 4306814525f4SDarrick J. Wong if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 4307814525f4SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) { 4308814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4309814525f4SDarrick J. Wong __u32 csum; 4310814525f4SDarrick J. Wong __le32 inum = cpu_to_le32(inode->i_ino); 4311814525f4SDarrick J. Wong __le32 gen = raw_inode->i_generation; 4312814525f4SDarrick J. Wong csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, 4313814525f4SDarrick J. Wong sizeof(inum)); 4314814525f4SDarrick J. Wong ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, 4315814525f4SDarrick J. Wong sizeof(gen)); 4316814525f4SDarrick J. Wong } 4317814525f4SDarrick J. Wong 4318814525f4SDarrick J. Wong if (!ext4_inode_csum_verify(inode, raw_inode, ei)) { 4319814525f4SDarrick J. Wong EXT4_ERROR_INODE(inode, "checksum invalid"); 4320814525f4SDarrick J. Wong ret = -EIO; 4321814525f4SDarrick J. Wong goto bad_inode; 4322814525f4SDarrick J. Wong } 4323814525f4SDarrick J. Wong 4324ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 432508cefc7aSEric W. Biederman i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 432608cefc7aSEric W. Biederman i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 4327ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 432808cefc7aSEric W. Biederman i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 432908cefc7aSEric W. Biederman i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 4330ac27a0ecSDave Kleikamp } 433108cefc7aSEric W. Biederman i_uid_write(inode, i_uid); 433208cefc7aSEric W. Biederman i_gid_write(inode, i_gid); 4333bfe86848SMiklos Szeredi set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); 4334ac27a0ecSDave Kleikamp 4335353eb83cSTheodore Ts'o ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 433667cf5b09STao Ma ei->i_inline_off = 0; 4337ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 4338ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 4339ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 4340ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 4341ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 4342ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 4343ac27a0ecSDave Kleikamp */ 4344ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 4345393d1d1dSDr. Tilmann Bubeck if ((inode->i_mode == 0 || 4346393d1d1dSDr. Tilmann Bubeck !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) && 4347393d1d1dSDr. Tilmann Bubeck ino != EXT4_BOOT_LOADER_INO) { 4348ac27a0ecSDave Kleikamp /* this inode is deleted */ 43491d1fe1eeSDavid Howells ret = -ESTALE; 4350ac27a0ecSDave Kleikamp goto bad_inode; 4351ac27a0ecSDave Kleikamp } 4352ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 4353ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 4354ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 4355393d1d1dSDr. Tilmann Bubeck * the process of deleting those. 4356393d1d1dSDr. Tilmann Bubeck * OR it is the EXT4_BOOT_LOADER_INO which is 4357393d1d1dSDr. Tilmann Bubeck * not initialized on a new filesystem. */ 4358ac27a0ecSDave Kleikamp } 4359ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 43600fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 43617973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 4362a9e81742STheodore Ts'o if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) 4363a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 4364a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 4365a48380f7SAneesh Kumar K.V inode->i_size = ext4_isize(raw_inode); 4366ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 4367a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 4368a9e7f447SDmitry Monakhov ei->i_reserved_quota = 0; 4369a9e7f447SDmitry Monakhov #endif 4370ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 4371ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 4372a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 4373ac27a0ecSDave Kleikamp /* 4374ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 4375ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 4376ac27a0ecSDave Kleikamp */ 4377617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 4378ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 4379ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 4380ac27a0ecSDave Kleikamp 4381b436b9beSJan Kara /* 4382b436b9beSJan Kara * Set transaction id's of transactions that have to be committed 4383b436b9beSJan Kara * to finish f[data]sync. We set them to currently running transaction 4384b436b9beSJan Kara * as we cannot be sure that the inode or some of its metadata isn't 4385b436b9beSJan Kara * part of the transaction - the inode could have been reclaimed and 4386b436b9beSJan Kara * now it is reread from disk. 4387b436b9beSJan Kara */ 4388b436b9beSJan Kara if (journal) { 4389b436b9beSJan Kara transaction_t *transaction; 4390b436b9beSJan Kara tid_t tid; 4391b436b9beSJan Kara 4392a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 4393b436b9beSJan Kara if (journal->j_running_transaction) 4394b436b9beSJan Kara transaction = journal->j_running_transaction; 4395b436b9beSJan Kara else 4396b436b9beSJan Kara transaction = journal->j_committing_transaction; 4397b436b9beSJan Kara if (transaction) 4398b436b9beSJan Kara tid = transaction->t_tid; 4399b436b9beSJan Kara else 4400b436b9beSJan Kara tid = journal->j_commit_sequence; 4401a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 4402b436b9beSJan Kara ei->i_sync_tid = tid; 4403b436b9beSJan Kara ei->i_datasync_tid = tid; 4404b436b9beSJan Kara } 4405b436b9beSJan Kara 44060040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4407ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 4408ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 4409617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 4410617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 4411ac27a0ecSDave Kleikamp } else { 4412152a7b0aSTao Ma ext4_iget_extra_inode(inode, raw_inode, ei); 4413ac27a0ecSDave Kleikamp } 4414814525f4SDarrick J. Wong } 4415ac27a0ecSDave Kleikamp 4416ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 4417ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 4418ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 4419ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 4420ef7f3835SKalpak Shah 442125ec56b5SJean Noel Cordenner inode->i_version = le32_to_cpu(raw_inode->i_disk_version); 442225ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 442325ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 442425ec56b5SJean Noel Cordenner inode->i_version |= 442525ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 442625ec56b5SJean Noel Cordenner } 442725ec56b5SJean Noel Cordenner 4428c4b5a614STheodore Ts'o ret = 0; 4429485c26ecSTheodore Ts'o if (ei->i_file_acl && 44301032988cSTheodore Ts'o !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) { 443124676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "bad extended attribute block %llu", 443224676da4STheodore Ts'o ei->i_file_acl); 4433485c26ecSTheodore Ts'o ret = -EIO; 4434485c26ecSTheodore Ts'o goto bad_inode; 4435f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 4436f19d5870STao Ma if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 4437f19d5870STao Ma if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 4438c4b5a614STheodore Ts'o (S_ISLNK(inode->i_mode) && 4439f19d5870STao Ma !ext4_inode_is_fast_symlink(inode)))) 44407a262f7cSAneesh Kumar K.V /* Validate extent which is part of inode */ 44417a262f7cSAneesh Kumar K.V ret = ext4_ext_check_inode(inode); 4442fe2c8191SThiemo Nagel } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 4443fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 4444fe2c8191SThiemo Nagel !ext4_inode_is_fast_symlink(inode))) { 4445fe2c8191SThiemo Nagel /* Validate block references which are part of inode */ 44461f7d1e77STheodore Ts'o ret = ext4_ind_check_inode(inode); 4447fe2c8191SThiemo Nagel } 4448f19d5870STao Ma } 4449567f3e9aSTheodore Ts'o if (ret) 44507a262f7cSAneesh Kumar K.V goto bad_inode; 44517a262f7cSAneesh Kumar K.V 4452ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 4453617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 4454617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 4455617ba13bSMingming Cao ext4_set_aops(inode); 4456ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 4457617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 4458617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 4459ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 4460e83c1397SDuane Griffin if (ext4_inode_is_fast_symlink(inode)) { 4461617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 4462e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 4463e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 4464e83c1397SDuane Griffin } else { 4465617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 4466617ba13bSMingming Cao ext4_set_aops(inode); 4467ac27a0ecSDave Kleikamp } 4468563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 4469563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 4470617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 4471ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 4472ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4473ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 4474ac27a0ecSDave Kleikamp else 4475ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4476ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 4477393d1d1dSDr. Tilmann Bubeck } else if (ino == EXT4_BOOT_LOADER_INO) { 4478393d1d1dSDr. Tilmann Bubeck make_bad_inode(inode); 4479563bdd61STheodore Ts'o } else { 4480563bdd61STheodore Ts'o ret = -EIO; 448124676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode); 4482563bdd61STheodore Ts'o goto bad_inode; 4483ac27a0ecSDave Kleikamp } 4484ac27a0ecSDave Kleikamp brelse(iloc.bh); 4485617ba13bSMingming Cao ext4_set_inode_flags(inode); 44861d1fe1eeSDavid Howells unlock_new_inode(inode); 44871d1fe1eeSDavid Howells return inode; 4488ac27a0ecSDave Kleikamp 4489ac27a0ecSDave Kleikamp bad_inode: 4490567f3e9aSTheodore Ts'o brelse(iloc.bh); 44911d1fe1eeSDavid Howells iget_failed(inode); 44921d1fe1eeSDavid Howells return ERR_PTR(ret); 4493ac27a0ecSDave Kleikamp } 4494ac27a0ecSDave Kleikamp 44950fc1b451SAneesh Kumar K.V static int ext4_inode_blocks_set(handle_t *handle, 44960fc1b451SAneesh Kumar K.V struct ext4_inode *raw_inode, 44970fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 44980fc1b451SAneesh Kumar K.V { 44990fc1b451SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 45000fc1b451SAneesh Kumar K.V u64 i_blocks = inode->i_blocks; 45010fc1b451SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 45020fc1b451SAneesh Kumar K.V 45030fc1b451SAneesh Kumar K.V if (i_blocks <= ~0U) { 45040fc1b451SAneesh Kumar K.V /* 45054907cb7bSAnatol Pomozov * i_blocks can be represented in a 32 bit variable 45060fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 45070fc1b451SAneesh Kumar K.V */ 45088180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 45090fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = 0; 451084a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 4511f287a1a5STheodore Ts'o return 0; 4512f287a1a5STheodore Ts'o } 4513f287a1a5STheodore Ts'o if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) 4514f287a1a5STheodore Ts'o return -EFBIG; 4515f287a1a5STheodore Ts'o 4516f287a1a5STheodore Ts'o if (i_blocks <= 0xffffffffffffULL) { 45170fc1b451SAneesh Kumar K.V /* 45180fc1b451SAneesh Kumar K.V * i_blocks can be represented in a 48 bit variable 45190fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 45200fc1b451SAneesh Kumar K.V */ 45218180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 45220fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 452384a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 45240fc1b451SAneesh Kumar K.V } else { 452584a8dce2SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE); 45268180a562SAneesh Kumar K.V /* i_block is stored in file system block size */ 45278180a562SAneesh Kumar K.V i_blocks = i_blocks >> (inode->i_blkbits - 9); 45288180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 45298180a562SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 45300fc1b451SAneesh Kumar K.V } 4531f287a1a5STheodore Ts'o return 0; 45320fc1b451SAneesh Kumar K.V } 45330fc1b451SAneesh Kumar K.V 4534ac27a0ecSDave Kleikamp /* 4535ac27a0ecSDave Kleikamp * Post the struct inode info into an on-disk inode location in the 4536ac27a0ecSDave Kleikamp * buffer-cache. This gobbles the caller's reference to the 4537ac27a0ecSDave Kleikamp * buffer_head in the inode location struct. 4538ac27a0ecSDave Kleikamp * 4539ac27a0ecSDave Kleikamp * The caller must have write access to iloc->bh. 4540ac27a0ecSDave Kleikamp */ 4541617ba13bSMingming Cao static int ext4_do_update_inode(handle_t *handle, 4542ac27a0ecSDave Kleikamp struct inode *inode, 4543830156c7SFrank Mayhar struct ext4_iloc *iloc) 4544ac27a0ecSDave Kleikamp { 4545617ba13bSMingming Cao struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 4546617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 4547ac27a0ecSDave Kleikamp struct buffer_head *bh = iloc->bh; 4548ac27a0ecSDave Kleikamp int err = 0, rc, block; 4549b71fc079SJan Kara int need_datasync = 0; 455008cefc7aSEric W. Biederman uid_t i_uid; 455108cefc7aSEric W. Biederman gid_t i_gid; 4552ac27a0ecSDave Kleikamp 4553ac27a0ecSDave Kleikamp /* For fields not not tracking in the in-memory inode, 4554ac27a0ecSDave Kleikamp * initialise them to zero for new inodes. */ 455519f5fb7aSTheodore Ts'o if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) 4556617ba13bSMingming Cao memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 4557ac27a0ecSDave Kleikamp 4558ff9ddf7eSJan Kara ext4_get_inode_flags(ei); 4559ac27a0ecSDave Kleikamp raw_inode->i_mode = cpu_to_le16(inode->i_mode); 456008cefc7aSEric W. Biederman i_uid = i_uid_read(inode); 456108cefc7aSEric W. Biederman i_gid = i_gid_read(inode); 4562ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 456308cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid)); 456408cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid)); 4565ac27a0ecSDave Kleikamp /* 4566ac27a0ecSDave Kleikamp * Fix up interoperability with old kernels. Otherwise, old inodes get 4567ac27a0ecSDave Kleikamp * re-used with the upper 16 bits of the uid/gid intact 4568ac27a0ecSDave Kleikamp */ 4569ac27a0ecSDave Kleikamp if (!ei->i_dtime) { 4570ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 457108cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_uid)); 4572ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 457308cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_gid)); 4574ac27a0ecSDave Kleikamp } else { 4575ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4576ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4577ac27a0ecSDave Kleikamp } 4578ac27a0ecSDave Kleikamp } else { 457908cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid)); 458008cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid)); 4581ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4582ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4583ac27a0ecSDave Kleikamp } 4584ac27a0ecSDave Kleikamp raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 4585ef7f3835SKalpak Shah 4586ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 4587ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 4588ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 4589ef7f3835SKalpak Shah EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 4590ef7f3835SKalpak Shah 45910fc1b451SAneesh Kumar K.V if (ext4_inode_blocks_set(handle, raw_inode, ei)) 45920fc1b451SAneesh Kumar K.V goto out_brelse; 4593ac27a0ecSDave Kleikamp raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 4594353eb83cSTheodore Ts'o raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF); 45959b8f1f01SMingming Cao if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 45969b8f1f01SMingming Cao cpu_to_le32(EXT4_OS_HURD)) 4597a1ddeb7eSBadari Pulavarty raw_inode->i_file_acl_high = 4598a1ddeb7eSBadari Pulavarty cpu_to_le16(ei->i_file_acl >> 32); 45997973c0c1SAneesh Kumar K.V raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 4600b71fc079SJan Kara if (ei->i_disksize != ext4_isize(raw_inode)) { 4601a48380f7SAneesh Kumar K.V ext4_isize_set(raw_inode, ei->i_disksize); 4602b71fc079SJan Kara need_datasync = 1; 4603b71fc079SJan Kara } 4604ac27a0ecSDave Kleikamp if (ei->i_disksize > 0x7fffffffULL) { 4605ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 4606617ba13bSMingming Cao if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, 4607617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE) || 4608617ba13bSMingming Cao EXT4_SB(sb)->s_es->s_rev_level == 4609617ba13bSMingming Cao cpu_to_le32(EXT4_GOOD_OLD_REV)) { 4610ac27a0ecSDave Kleikamp /* If this is the first large file 4611ac27a0ecSDave Kleikamp * created, add a flag to the superblock. 4612ac27a0ecSDave Kleikamp */ 4613617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, 4614617ba13bSMingming Cao EXT4_SB(sb)->s_sbh); 4615ac27a0ecSDave Kleikamp if (err) 4616ac27a0ecSDave Kleikamp goto out_brelse; 4617617ba13bSMingming Cao ext4_update_dynamic_rev(sb); 4618617ba13bSMingming Cao EXT4_SET_RO_COMPAT_FEATURE(sb, 4619617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE); 46200390131bSFrank Mayhar ext4_handle_sync(handle); 4621b50924c2SArtem Bityutskiy err = ext4_handle_dirty_super(handle, sb); 4622ac27a0ecSDave Kleikamp } 4623ac27a0ecSDave Kleikamp } 4624ac27a0ecSDave Kleikamp raw_inode->i_generation = cpu_to_le32(inode->i_generation); 4625ac27a0ecSDave Kleikamp if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 4626ac27a0ecSDave Kleikamp if (old_valid_dev(inode->i_rdev)) { 4627ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 4628ac27a0ecSDave Kleikamp cpu_to_le32(old_encode_dev(inode->i_rdev)); 4629ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 0; 4630ac27a0ecSDave Kleikamp } else { 4631ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 0; 4632ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 4633ac27a0ecSDave Kleikamp cpu_to_le32(new_encode_dev(inode->i_rdev)); 4634ac27a0ecSDave Kleikamp raw_inode->i_block[2] = 0; 4635ac27a0ecSDave Kleikamp } 4636f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 4637de9a55b8STheodore Ts'o for (block = 0; block < EXT4_N_BLOCKS; block++) 4638ac27a0ecSDave Kleikamp raw_inode->i_block[block] = ei->i_data[block]; 4639f19d5870STao Ma } 4640ac27a0ecSDave Kleikamp 464125ec56b5SJean Noel Cordenner raw_inode->i_disk_version = cpu_to_le32(inode->i_version); 464225ec56b5SJean Noel Cordenner if (ei->i_extra_isize) { 464325ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 464425ec56b5SJean Noel Cordenner raw_inode->i_version_hi = 464525ec56b5SJean Noel Cordenner cpu_to_le32(inode->i_version >> 32); 4646ac27a0ecSDave Kleikamp raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize); 464725ec56b5SJean Noel Cordenner } 464825ec56b5SJean Noel Cordenner 4649814525f4SDarrick J. Wong ext4_inode_csum_set(inode, raw_inode, ei); 4650814525f4SDarrick J. Wong 46510390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 465273b50c1cSCurt Wohlgemuth rc = ext4_handle_dirty_metadata(handle, NULL, bh); 4653ac27a0ecSDave Kleikamp if (!err) 4654ac27a0ecSDave Kleikamp err = rc; 465519f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_NEW); 4656ac27a0ecSDave Kleikamp 4657b71fc079SJan Kara ext4_update_inode_fsync_trans(handle, inode, need_datasync); 4658ac27a0ecSDave Kleikamp out_brelse: 4659ac27a0ecSDave Kleikamp brelse(bh); 4660617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4661ac27a0ecSDave Kleikamp return err; 4662ac27a0ecSDave Kleikamp } 4663ac27a0ecSDave Kleikamp 4664ac27a0ecSDave Kleikamp /* 4665617ba13bSMingming Cao * ext4_write_inode() 4666ac27a0ecSDave Kleikamp * 4667ac27a0ecSDave Kleikamp * We are called from a few places: 4668ac27a0ecSDave Kleikamp * 4669ac27a0ecSDave Kleikamp * - Within generic_file_write() for O_SYNC files. 4670ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 46714907cb7bSAnatol Pomozov * transaction to commit. 4672ac27a0ecSDave Kleikamp * 4673ac27a0ecSDave Kleikamp * - Within sys_sync(), kupdate and such. 4674ac27a0ecSDave Kleikamp * We wait on commit, if tol to. 4675ac27a0ecSDave Kleikamp * 4676ac27a0ecSDave Kleikamp * - Within prune_icache() (PF_MEMALLOC == true) 4677ac27a0ecSDave Kleikamp * Here we simply return. We can't afford to block kswapd on the 4678ac27a0ecSDave Kleikamp * journal commit. 4679ac27a0ecSDave Kleikamp * 4680ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 4681ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 4682617ba13bSMingming Cao * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for 4683ac27a0ecSDave Kleikamp * knfsd. 4684ac27a0ecSDave Kleikamp * 4685ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 4686ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 4687ac27a0ecSDave Kleikamp * which we are interested. 4688ac27a0ecSDave Kleikamp * 4689ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 4690ac27a0ecSDave Kleikamp * 4691ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 4692ac27a0ecSDave Kleikamp * stuff(); 4693ac27a0ecSDave Kleikamp * inode->i_size = expr; 4694ac27a0ecSDave Kleikamp * 4695ac27a0ecSDave Kleikamp * is in error because a kswapd-driven write_inode() could occur while 4696ac27a0ecSDave Kleikamp * `stuff()' is running, and the new i_size will be lost. Plus the inode 4697ac27a0ecSDave Kleikamp * will no longer be on the superblock's dirty inode list. 4698ac27a0ecSDave Kleikamp */ 4699a9185b41SChristoph Hellwig int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) 4700ac27a0ecSDave Kleikamp { 470191ac6f43SFrank Mayhar int err; 470291ac6f43SFrank Mayhar 4703ac27a0ecSDave Kleikamp if (current->flags & PF_MEMALLOC) 4704ac27a0ecSDave Kleikamp return 0; 4705ac27a0ecSDave Kleikamp 470691ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal) { 4707617ba13bSMingming Cao if (ext4_journal_current_handle()) { 4708b38bd33aSMingming Cao jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); 4709ac27a0ecSDave Kleikamp dump_stack(); 4710ac27a0ecSDave Kleikamp return -EIO; 4711ac27a0ecSDave Kleikamp } 4712ac27a0ecSDave Kleikamp 4713a9185b41SChristoph Hellwig if (wbc->sync_mode != WB_SYNC_ALL) 4714ac27a0ecSDave Kleikamp return 0; 4715ac27a0ecSDave Kleikamp 471691ac6f43SFrank Mayhar err = ext4_force_commit(inode->i_sb); 471791ac6f43SFrank Mayhar } else { 471891ac6f43SFrank Mayhar struct ext4_iloc iloc; 471991ac6f43SFrank Mayhar 47208b472d73SCurt Wohlgemuth err = __ext4_get_inode_loc(inode, &iloc, 0); 472191ac6f43SFrank Mayhar if (err) 472291ac6f43SFrank Mayhar return err; 4723a9185b41SChristoph Hellwig if (wbc->sync_mode == WB_SYNC_ALL) 4724830156c7SFrank Mayhar sync_dirty_buffer(iloc.bh); 4725830156c7SFrank Mayhar if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { 4726c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr, 4727c398eda0STheodore Ts'o "IO error syncing inode"); 4728830156c7SFrank Mayhar err = -EIO; 4729830156c7SFrank Mayhar } 4730fd2dd9fbSCurt Wohlgemuth brelse(iloc.bh); 473191ac6f43SFrank Mayhar } 473291ac6f43SFrank Mayhar return err; 4733ac27a0ecSDave Kleikamp } 4734ac27a0ecSDave Kleikamp 4735ac27a0ecSDave Kleikamp /* 473653e87268SJan Kara * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate 473753e87268SJan Kara * buffers that are attached to a page stradding i_size and are undergoing 473853e87268SJan Kara * commit. In that case we have to wait for commit to finish and try again. 473953e87268SJan Kara */ 474053e87268SJan Kara static void ext4_wait_for_tail_page_commit(struct inode *inode) 474153e87268SJan Kara { 474253e87268SJan Kara struct page *page; 474353e87268SJan Kara unsigned offset; 474453e87268SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 474553e87268SJan Kara tid_t commit_tid = 0; 474653e87268SJan Kara int ret; 474753e87268SJan Kara 474853e87268SJan Kara offset = inode->i_size & (PAGE_CACHE_SIZE - 1); 474953e87268SJan Kara /* 475053e87268SJan Kara * All buffers in the last page remain valid? Then there's nothing to 475153e87268SJan Kara * do. We do the check mainly to optimize the common PAGE_CACHE_SIZE == 475253e87268SJan Kara * blocksize case 475353e87268SJan Kara */ 475453e87268SJan Kara if (offset > PAGE_CACHE_SIZE - (1 << inode->i_blkbits)) 475553e87268SJan Kara return; 475653e87268SJan Kara while (1) { 475753e87268SJan Kara page = find_lock_page(inode->i_mapping, 475853e87268SJan Kara inode->i_size >> PAGE_CACHE_SHIFT); 475953e87268SJan Kara if (!page) 476053e87268SJan Kara return; 4761ca99fdd2SLukas Czerner ret = __ext4_journalled_invalidatepage(page, offset, 4762ca99fdd2SLukas Czerner PAGE_CACHE_SIZE - offset); 476353e87268SJan Kara unlock_page(page); 476453e87268SJan Kara page_cache_release(page); 476553e87268SJan Kara if (ret != -EBUSY) 476653e87268SJan Kara return; 476753e87268SJan Kara commit_tid = 0; 476853e87268SJan Kara read_lock(&journal->j_state_lock); 476953e87268SJan Kara if (journal->j_committing_transaction) 477053e87268SJan Kara commit_tid = journal->j_committing_transaction->t_tid; 477153e87268SJan Kara read_unlock(&journal->j_state_lock); 477253e87268SJan Kara if (commit_tid) 477353e87268SJan Kara jbd2_log_wait_commit(journal, commit_tid); 477453e87268SJan Kara } 477553e87268SJan Kara } 477653e87268SJan Kara 477753e87268SJan Kara /* 4778617ba13bSMingming Cao * ext4_setattr() 4779ac27a0ecSDave Kleikamp * 4780ac27a0ecSDave Kleikamp * Called from notify_change. 4781ac27a0ecSDave Kleikamp * 4782ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 4783ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 4784ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 4785ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 4786ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 4787ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 4788ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 4789ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 4790ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 4791ac27a0ecSDave Kleikamp * 4792678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 4793678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 4794678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 4795678aaf48SJan Kara * This way we are sure that all the data written in the previous 4796678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 4797678aaf48SJan Kara * writeback). 4798678aaf48SJan Kara * 4799678aaf48SJan Kara * Called with inode->i_mutex down. 4800ac27a0ecSDave Kleikamp */ 4801617ba13bSMingming Cao int ext4_setattr(struct dentry *dentry, struct iattr *attr) 4802ac27a0ecSDave Kleikamp { 4803ac27a0ecSDave Kleikamp struct inode *inode = dentry->d_inode; 4804ac27a0ecSDave Kleikamp int error, rc = 0; 48053d287de3SDmitry Monakhov int orphan = 0; 4806ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 4807ac27a0ecSDave Kleikamp 4808ac27a0ecSDave Kleikamp error = inode_change_ok(inode, attr); 4809ac27a0ecSDave Kleikamp if (error) 4810ac27a0ecSDave Kleikamp return error; 4811ac27a0ecSDave Kleikamp 481212755627SDmitry Monakhov if (is_quota_modification(inode, attr)) 4813871a2931SChristoph Hellwig dquot_initialize(inode); 481408cefc7aSEric W. Biederman if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) || 481508cefc7aSEric W. Biederman (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) { 4816ac27a0ecSDave Kleikamp handle_t *handle; 4817ac27a0ecSDave Kleikamp 4818ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 4819ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 48209924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 48219924a92aSTheodore Ts'o (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) + 4822194074acSDmitry Monakhov EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3); 4823ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 4824ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 4825ac27a0ecSDave Kleikamp goto err_out; 4826ac27a0ecSDave Kleikamp } 4827b43fa828SChristoph Hellwig error = dquot_transfer(inode, attr); 4828ac27a0ecSDave Kleikamp if (error) { 4829617ba13bSMingming Cao ext4_journal_stop(handle); 4830ac27a0ecSDave Kleikamp return error; 4831ac27a0ecSDave Kleikamp } 4832ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 4833ac27a0ecSDave Kleikamp * one transaction */ 4834ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_UID) 4835ac27a0ecSDave Kleikamp inode->i_uid = attr->ia_uid; 4836ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_GID) 4837ac27a0ecSDave Kleikamp inode->i_gid = attr->ia_gid; 4838617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 4839617ba13bSMingming Cao ext4_journal_stop(handle); 4840ac27a0ecSDave Kleikamp } 4841ac27a0ecSDave Kleikamp 4842e2b46574SEric Sandeen if (attr->ia_valid & ATTR_SIZE) { 4843562c72aaSChristoph Hellwig 484412e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 4845e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4846e2b46574SEric Sandeen 48470c095c7fSTheodore Ts'o if (attr->ia_size > sbi->s_bitmap_maxbytes) 48480c095c7fSTheodore Ts'o return -EFBIG; 4849e2b46574SEric Sandeen } 4850e2b46574SEric Sandeen } 4851e2b46574SEric Sandeen 4852ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode) && 4853c8d46e41SJiaying Zhang attr->ia_valid & ATTR_SIZE && 4854072bd7eaSTheodore Ts'o (attr->ia_size < inode->i_size)) { 4855ac27a0ecSDave Kleikamp handle_t *handle; 4856ac27a0ecSDave Kleikamp 48579924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 3); 4858ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 4859ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 4860ac27a0ecSDave Kleikamp goto err_out; 4861ac27a0ecSDave Kleikamp } 48623d287de3SDmitry Monakhov if (ext4_handle_valid(handle)) { 4863617ba13bSMingming Cao error = ext4_orphan_add(handle, inode); 48643d287de3SDmitry Monakhov orphan = 1; 48653d287de3SDmitry Monakhov } 4866617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 4867617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 4868ac27a0ecSDave Kleikamp if (!error) 4869ac27a0ecSDave Kleikamp error = rc; 4870617ba13bSMingming Cao ext4_journal_stop(handle); 4871678aaf48SJan Kara 4872678aaf48SJan Kara if (ext4_should_order_data(inode)) { 4873678aaf48SJan Kara error = ext4_begin_ordered_truncate(inode, 4874678aaf48SJan Kara attr->ia_size); 4875678aaf48SJan Kara if (error) { 4876678aaf48SJan Kara /* Do as much error cleanup as possible */ 48779924a92aSTheodore Ts'o handle = ext4_journal_start(inode, 48789924a92aSTheodore Ts'o EXT4_HT_INODE, 3); 4879678aaf48SJan Kara if (IS_ERR(handle)) { 4880678aaf48SJan Kara ext4_orphan_del(NULL, inode); 4881678aaf48SJan Kara goto err_out; 4882678aaf48SJan Kara } 4883678aaf48SJan Kara ext4_orphan_del(handle, inode); 48843d287de3SDmitry Monakhov orphan = 0; 4885678aaf48SJan Kara ext4_journal_stop(handle); 4886678aaf48SJan Kara goto err_out; 4887678aaf48SJan Kara } 4888678aaf48SJan Kara } 4889ac27a0ecSDave Kleikamp } 4890ac27a0ecSDave Kleikamp 4891072bd7eaSTheodore Ts'o if (attr->ia_valid & ATTR_SIZE) { 489253e87268SJan Kara if (attr->ia_size != inode->i_size) { 489353e87268SJan Kara loff_t oldsize = inode->i_size; 489453e87268SJan Kara 489553e87268SJan Kara i_size_write(inode, attr->ia_size); 489653e87268SJan Kara /* 489753e87268SJan Kara * Blocks are going to be removed from the inode. Wait 489853e87268SJan Kara * for dio in flight. Temporarily disable 489953e87268SJan Kara * dioread_nolock to prevent livelock. 490053e87268SJan Kara */ 49011b65007eSDmitry Monakhov if (orphan) { 490253e87268SJan Kara if (!ext4_should_journal_data(inode)) { 49031b65007eSDmitry Monakhov ext4_inode_block_unlocked_dio(inode); 49041c9114f9SDmitry Monakhov inode_dio_wait(inode); 49051b65007eSDmitry Monakhov ext4_inode_resume_unlocked_dio(inode); 490653e87268SJan Kara } else 490753e87268SJan Kara ext4_wait_for_tail_page_commit(inode); 49081b65007eSDmitry Monakhov } 490953e87268SJan Kara /* 491053e87268SJan Kara * Truncate pagecache after we've waited for commit 491153e87268SJan Kara * in data=journal mode to make pages freeable. 491253e87268SJan Kara */ 491353e87268SJan Kara truncate_pagecache(inode, oldsize, inode->i_size); 49141c9114f9SDmitry Monakhov } 4915072bd7eaSTheodore Ts'o ext4_truncate(inode); 4916072bd7eaSTheodore Ts'o } 4917ac27a0ecSDave Kleikamp 49181025774cSChristoph Hellwig if (!rc) { 49191025774cSChristoph Hellwig setattr_copy(inode, attr); 49201025774cSChristoph Hellwig mark_inode_dirty(inode); 49211025774cSChristoph Hellwig } 49221025774cSChristoph Hellwig 49231025774cSChristoph Hellwig /* 49241025774cSChristoph Hellwig * If the call to ext4_truncate failed to get a transaction handle at 49251025774cSChristoph Hellwig * all, we need to clean up the in-core orphan list manually. 49261025774cSChristoph Hellwig */ 49273d287de3SDmitry Monakhov if (orphan && inode->i_nlink) 4928617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 4929ac27a0ecSDave Kleikamp 4930ac27a0ecSDave Kleikamp if (!rc && (ia_valid & ATTR_MODE)) 4931617ba13bSMingming Cao rc = ext4_acl_chmod(inode); 4932ac27a0ecSDave Kleikamp 4933ac27a0ecSDave Kleikamp err_out: 4934617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 4935ac27a0ecSDave Kleikamp if (!error) 4936ac27a0ecSDave Kleikamp error = rc; 4937ac27a0ecSDave Kleikamp return error; 4938ac27a0ecSDave Kleikamp } 4939ac27a0ecSDave Kleikamp 49403e3398a0SMingming Cao int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry, 49413e3398a0SMingming Cao struct kstat *stat) 49423e3398a0SMingming Cao { 49433e3398a0SMingming Cao struct inode *inode; 49443e3398a0SMingming Cao unsigned long delalloc_blocks; 49453e3398a0SMingming Cao 49463e3398a0SMingming Cao inode = dentry->d_inode; 49473e3398a0SMingming Cao generic_fillattr(inode, stat); 49483e3398a0SMingming Cao 49493e3398a0SMingming Cao /* 49503e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 49513e3398a0SMingming Cao * otherwise in the case of system crash before the real block 49523e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 49533e3398a0SMingming Cao * on-disk file blocks. 49543e3398a0SMingming Cao * We always keep i_blocks updated together with real 49553e3398a0SMingming Cao * allocation. But to not confuse with user, stat 49563e3398a0SMingming Cao * will return the blocks that include the delayed allocation 49573e3398a0SMingming Cao * blocks for this file. 49583e3398a0SMingming Cao */ 495996607551STao Ma delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb), 496096607551STao Ma EXT4_I(inode)->i_reserved_data_blocks); 49613e3398a0SMingming Cao 49623e3398a0SMingming Cao stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9; 49633e3398a0SMingming Cao return 0; 49643e3398a0SMingming Cao } 4965ac27a0ecSDave Kleikamp 4966a02908f1SMingming Cao static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4967a02908f1SMingming Cao { 496812e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 49698bb2b247SAmir Goldstein return ext4_ind_trans_blocks(inode, nrblocks, chunk); 4970ac51d837STheodore Ts'o return ext4_ext_index_trans_blocks(inode, nrblocks, chunk); 4971a02908f1SMingming Cao } 4972ac51d837STheodore Ts'o 4973a02908f1SMingming Cao /* 4974a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 4975a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 4976a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 4977a02908f1SMingming Cao * 4978a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 49794907cb7bSAnatol Pomozov * different block groups too. If they are contiguous, with flexbg, 4980a02908f1SMingming Cao * they could still across block group boundary. 4981a02908f1SMingming Cao * 4982a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 4983a02908f1SMingming Cao */ 49841f109d5aSTheodore Ts'o static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4985a02908f1SMingming Cao { 49868df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 49878df9675fSTheodore Ts'o int gdpblocks; 4988a02908f1SMingming Cao int idxblocks; 4989a02908f1SMingming Cao int ret = 0; 4990a02908f1SMingming Cao 4991a02908f1SMingming Cao /* 4992a02908f1SMingming Cao * How many index blocks need to touch to modify nrblocks? 4993a02908f1SMingming Cao * The "Chunk" flag indicating whether the nrblocks is 4994a02908f1SMingming Cao * physically contiguous on disk 4995a02908f1SMingming Cao * 4996a02908f1SMingming Cao * For Direct IO and fallocate, they calls get_block to allocate 4997a02908f1SMingming Cao * one single extent at a time, so they could set the "Chunk" flag 4998a02908f1SMingming Cao */ 4999a02908f1SMingming Cao idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk); 5000a02908f1SMingming Cao 5001a02908f1SMingming Cao ret = idxblocks; 5002a02908f1SMingming Cao 5003a02908f1SMingming Cao /* 5004a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 5005a02908f1SMingming Cao * to account 5006a02908f1SMingming Cao */ 5007a02908f1SMingming Cao groups = idxblocks; 5008a02908f1SMingming Cao if (chunk) 5009a02908f1SMingming Cao groups += 1; 5010ac27a0ecSDave Kleikamp else 5011a02908f1SMingming Cao groups += nrblocks; 5012ac27a0ecSDave Kleikamp 5013a02908f1SMingming Cao gdpblocks = groups; 50148df9675fSTheodore Ts'o if (groups > ngroups) 50158df9675fSTheodore Ts'o groups = ngroups; 5016a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 5017a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 5018a02908f1SMingming Cao 5019a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 5020a02908f1SMingming Cao ret += groups + gdpblocks; 5021a02908f1SMingming Cao 5022a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 5023a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 5024ac27a0ecSDave Kleikamp 5025ac27a0ecSDave Kleikamp return ret; 5026ac27a0ecSDave Kleikamp } 5027ac27a0ecSDave Kleikamp 5028ac27a0ecSDave Kleikamp /* 502925985edcSLucas De Marchi * Calculate the total number of credits to reserve to fit 5030f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 5031f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 5032a02908f1SMingming Cao * 5033525f4ed8SMingming Cao * This could be called via ext4_write_begin() 5034a02908f1SMingming Cao * 5035525f4ed8SMingming Cao * We need to consider the worse case, when 5036a02908f1SMingming Cao * one new block per extent. 5037a02908f1SMingming Cao */ 5038a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 5039a02908f1SMingming Cao { 5040a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 5041a02908f1SMingming Cao int ret; 5042a02908f1SMingming Cao 5043a02908f1SMingming Cao ret = ext4_meta_trans_blocks(inode, bpp, 0); 5044a02908f1SMingming Cao 5045a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 5046a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 5047a02908f1SMingming Cao ret += bpp; 5048a02908f1SMingming Cao return ret; 5049a02908f1SMingming Cao } 5050f3bd1f3fSMingming Cao 5051f3bd1f3fSMingming Cao /* 5052f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 5053f3bd1f3fSMingming Cao * 5054f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 505579e83036SEric Sandeen * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks. 5056f3bd1f3fSMingming Cao * 5057f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 5058f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 5059f3bd1f3fSMingming Cao */ 5060f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 5061f3bd1f3fSMingming Cao { 5062f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 5063f3bd1f3fSMingming Cao } 5064f3bd1f3fSMingming Cao 5065a02908f1SMingming Cao /* 5066617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 5067ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 5068ac27a0ecSDave Kleikamp */ 5069617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 5070617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 5071ac27a0ecSDave Kleikamp { 5072ac27a0ecSDave Kleikamp int err = 0; 5073ac27a0ecSDave Kleikamp 5074c64db50eSTheodore Ts'o if (IS_I_VERSION(inode)) 507525ec56b5SJean Noel Cordenner inode_inc_iversion(inode); 507625ec56b5SJean Noel Cordenner 5077ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 5078ac27a0ecSDave Kleikamp get_bh(iloc->bh); 5079ac27a0ecSDave Kleikamp 5080dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 5081830156c7SFrank Mayhar err = ext4_do_update_inode(handle, inode, iloc); 5082ac27a0ecSDave Kleikamp put_bh(iloc->bh); 5083ac27a0ecSDave Kleikamp return err; 5084ac27a0ecSDave Kleikamp } 5085ac27a0ecSDave Kleikamp 5086ac27a0ecSDave Kleikamp /* 5087ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 5088ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 5089ac27a0ecSDave Kleikamp */ 5090ac27a0ecSDave Kleikamp 5091ac27a0ecSDave Kleikamp int 5092617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 5093617ba13bSMingming Cao struct ext4_iloc *iloc) 5094ac27a0ecSDave Kleikamp { 50950390131bSFrank Mayhar int err; 50960390131bSFrank Mayhar 5097617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 5098ac27a0ecSDave Kleikamp if (!err) { 5099ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 5100617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, iloc->bh); 5101ac27a0ecSDave Kleikamp if (err) { 5102ac27a0ecSDave Kleikamp brelse(iloc->bh); 5103ac27a0ecSDave Kleikamp iloc->bh = NULL; 5104ac27a0ecSDave Kleikamp } 5105ac27a0ecSDave Kleikamp } 5106617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5107ac27a0ecSDave Kleikamp return err; 5108ac27a0ecSDave Kleikamp } 5109ac27a0ecSDave Kleikamp 5110ac27a0ecSDave Kleikamp /* 51116dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 51126dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 51136dd4ee7cSKalpak Shah */ 51141d03ec98SAneesh Kumar K.V static int ext4_expand_extra_isize(struct inode *inode, 51151d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 51161d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 51171d03ec98SAneesh Kumar K.V handle_t *handle) 51186dd4ee7cSKalpak Shah { 51196dd4ee7cSKalpak Shah struct ext4_inode *raw_inode; 51206dd4ee7cSKalpak Shah struct ext4_xattr_ibody_header *header; 51216dd4ee7cSKalpak Shah 51226dd4ee7cSKalpak Shah if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) 51236dd4ee7cSKalpak Shah return 0; 51246dd4ee7cSKalpak Shah 51256dd4ee7cSKalpak Shah raw_inode = ext4_raw_inode(&iloc); 51266dd4ee7cSKalpak Shah 51276dd4ee7cSKalpak Shah header = IHDR(inode, raw_inode); 51286dd4ee7cSKalpak Shah 51296dd4ee7cSKalpak Shah /* No extended attributes present */ 513019f5fb7aSTheodore Ts'o if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) || 51316dd4ee7cSKalpak Shah header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 51326dd4ee7cSKalpak Shah memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0, 51336dd4ee7cSKalpak Shah new_extra_isize); 51346dd4ee7cSKalpak Shah EXT4_I(inode)->i_extra_isize = new_extra_isize; 51356dd4ee7cSKalpak Shah return 0; 51366dd4ee7cSKalpak Shah } 51376dd4ee7cSKalpak Shah 51386dd4ee7cSKalpak Shah /* try to expand with EAs present */ 51396dd4ee7cSKalpak Shah return ext4_expand_extra_isize_ea(inode, new_extra_isize, 51406dd4ee7cSKalpak Shah raw_inode, handle); 51416dd4ee7cSKalpak Shah } 51426dd4ee7cSKalpak Shah 51436dd4ee7cSKalpak Shah /* 5144ac27a0ecSDave Kleikamp * What we do here is to mark the in-core inode as clean with respect to inode 5145ac27a0ecSDave Kleikamp * dirtiness (it may still be data-dirty). 5146ac27a0ecSDave Kleikamp * This means that the in-core inode may be reaped by prune_icache 5147ac27a0ecSDave Kleikamp * without having to perform any I/O. This is a very good thing, 5148ac27a0ecSDave Kleikamp * because *any* task may call prune_icache - even ones which 5149ac27a0ecSDave Kleikamp * have a transaction open against a different journal. 5150ac27a0ecSDave Kleikamp * 5151ac27a0ecSDave Kleikamp * Is this cheating? Not really. Sure, we haven't written the 5152ac27a0ecSDave Kleikamp * inode out, but prune_icache isn't a user-visible syncing function. 5153ac27a0ecSDave Kleikamp * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 5154ac27a0ecSDave Kleikamp * we start and wait on commits. 5155ac27a0ecSDave Kleikamp */ 5156617ba13bSMingming Cao int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode) 5157ac27a0ecSDave Kleikamp { 5158617ba13bSMingming Cao struct ext4_iloc iloc; 51596dd4ee7cSKalpak Shah struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 51606dd4ee7cSKalpak Shah static unsigned int mnt_count; 51616dd4ee7cSKalpak Shah int err, ret; 5162ac27a0ecSDave Kleikamp 5163ac27a0ecSDave Kleikamp might_sleep(); 51647ff9c073STheodore Ts'o trace_ext4_mark_inode_dirty(inode, _RET_IP_); 5165617ba13bSMingming Cao err = ext4_reserve_inode_write(handle, inode, &iloc); 51660390131bSFrank Mayhar if (ext4_handle_valid(handle) && 51670390131bSFrank Mayhar EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize && 516819f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) { 51696dd4ee7cSKalpak Shah /* 51706dd4ee7cSKalpak Shah * We need extra buffer credits since we may write into EA block 51716dd4ee7cSKalpak Shah * with this same handle. If journal_extend fails, then it will 51726dd4ee7cSKalpak Shah * only result in a minor loss of functionality for that inode. 51736dd4ee7cSKalpak Shah * If this is felt to be critical, then e2fsck should be run to 51746dd4ee7cSKalpak Shah * force a large enough s_min_extra_isize. 51756dd4ee7cSKalpak Shah */ 51766dd4ee7cSKalpak Shah if ((jbd2_journal_extend(handle, 51776dd4ee7cSKalpak Shah EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) { 51786dd4ee7cSKalpak Shah ret = ext4_expand_extra_isize(inode, 51796dd4ee7cSKalpak Shah sbi->s_want_extra_isize, 51806dd4ee7cSKalpak Shah iloc, handle); 51816dd4ee7cSKalpak Shah if (ret) { 518219f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, 518319f5fb7aSTheodore Ts'o EXT4_STATE_NO_EXPAND); 5184c1bddad9SAneesh Kumar K.V if (mnt_count != 5185c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count)) { 518612062dddSEric Sandeen ext4_warning(inode->i_sb, 51876dd4ee7cSKalpak Shah "Unable to expand inode %lu. Delete" 51886dd4ee7cSKalpak Shah " some EAs or run e2fsck.", 51896dd4ee7cSKalpak Shah inode->i_ino); 5190c1bddad9SAneesh Kumar K.V mnt_count = 5191c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count); 51926dd4ee7cSKalpak Shah } 51936dd4ee7cSKalpak Shah } 51946dd4ee7cSKalpak Shah } 51956dd4ee7cSKalpak Shah } 5196ac27a0ecSDave Kleikamp if (!err) 5197617ba13bSMingming Cao err = ext4_mark_iloc_dirty(handle, inode, &iloc); 5198ac27a0ecSDave Kleikamp return err; 5199ac27a0ecSDave Kleikamp } 5200ac27a0ecSDave Kleikamp 5201ac27a0ecSDave Kleikamp /* 5202617ba13bSMingming Cao * ext4_dirty_inode() is called from __mark_inode_dirty() 5203ac27a0ecSDave Kleikamp * 5204ac27a0ecSDave Kleikamp * We're really interested in the case where a file is being extended. 5205ac27a0ecSDave Kleikamp * i_size has been changed by generic_commit_write() and we thus need 5206ac27a0ecSDave Kleikamp * to include the updated inode in the current transaction. 5207ac27a0ecSDave Kleikamp * 52085dd4056dSChristoph Hellwig * Also, dquot_alloc_block() will always dirty the inode when blocks 5209ac27a0ecSDave Kleikamp * are allocated to the file. 5210ac27a0ecSDave Kleikamp * 5211ac27a0ecSDave Kleikamp * If the inode is marked synchronous, we don't honour that here - doing 5212ac27a0ecSDave Kleikamp * so would cause a commit on atime updates, which we don't bother doing. 5213ac27a0ecSDave Kleikamp * We handle synchronous inodes at the highest possible level. 5214ac27a0ecSDave Kleikamp */ 5215aa385729SChristoph Hellwig void ext4_dirty_inode(struct inode *inode, int flags) 5216ac27a0ecSDave Kleikamp { 5217ac27a0ecSDave Kleikamp handle_t *handle; 5218ac27a0ecSDave Kleikamp 52199924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 2); 5220ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5221ac27a0ecSDave Kleikamp goto out; 5222f3dc272fSCurt Wohlgemuth 5223617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 5224f3dc272fSCurt Wohlgemuth 5225617ba13bSMingming Cao ext4_journal_stop(handle); 5226ac27a0ecSDave Kleikamp out: 5227ac27a0ecSDave Kleikamp return; 5228ac27a0ecSDave Kleikamp } 5229ac27a0ecSDave Kleikamp 5230ac27a0ecSDave Kleikamp #if 0 5231ac27a0ecSDave Kleikamp /* 5232ac27a0ecSDave Kleikamp * Bind an inode's backing buffer_head into this transaction, to prevent 5233ac27a0ecSDave Kleikamp * it from being flushed to disk early. Unlike 5234617ba13bSMingming Cao * ext4_reserve_inode_write, this leaves behind no bh reference and 5235ac27a0ecSDave Kleikamp * returns no iloc structure, so the caller needs to repeat the iloc 5236ac27a0ecSDave Kleikamp * lookup to mark the inode dirty later. 5237ac27a0ecSDave Kleikamp */ 5238617ba13bSMingming Cao static int ext4_pin_inode(handle_t *handle, struct inode *inode) 5239ac27a0ecSDave Kleikamp { 5240617ba13bSMingming Cao struct ext4_iloc iloc; 5241ac27a0ecSDave Kleikamp 5242ac27a0ecSDave Kleikamp int err = 0; 5243ac27a0ecSDave Kleikamp if (handle) { 5244617ba13bSMingming Cao err = ext4_get_inode_loc(inode, &iloc); 5245ac27a0ecSDave Kleikamp if (!err) { 5246ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc.bh, "get_write_access"); 5247dab291afSMingming Cao err = jbd2_journal_get_write_access(handle, iloc.bh); 5248ac27a0ecSDave Kleikamp if (!err) 52490390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, 525073b50c1cSCurt Wohlgemuth NULL, 5251ac27a0ecSDave Kleikamp iloc.bh); 5252ac27a0ecSDave Kleikamp brelse(iloc.bh); 5253ac27a0ecSDave Kleikamp } 5254ac27a0ecSDave Kleikamp } 5255617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5256ac27a0ecSDave Kleikamp return err; 5257ac27a0ecSDave Kleikamp } 5258ac27a0ecSDave Kleikamp #endif 5259ac27a0ecSDave Kleikamp 5260617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val) 5261ac27a0ecSDave Kleikamp { 5262ac27a0ecSDave Kleikamp journal_t *journal; 5263ac27a0ecSDave Kleikamp handle_t *handle; 5264ac27a0ecSDave Kleikamp int err; 5265ac27a0ecSDave Kleikamp 5266ac27a0ecSDave Kleikamp /* 5267ac27a0ecSDave Kleikamp * We have to be very careful here: changing a data block's 5268ac27a0ecSDave Kleikamp * journaling status dynamically is dangerous. If we write a 5269ac27a0ecSDave Kleikamp * data block to the journal, change the status and then delete 5270ac27a0ecSDave Kleikamp * that block, we risk forgetting to revoke the old log record 5271ac27a0ecSDave Kleikamp * from the journal and so a subsequent replay can corrupt data. 5272ac27a0ecSDave Kleikamp * So, first we make sure that the journal is empty and that 5273ac27a0ecSDave Kleikamp * nobody is changing anything. 5274ac27a0ecSDave Kleikamp */ 5275ac27a0ecSDave Kleikamp 5276617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 52770390131bSFrank Mayhar if (!journal) 52780390131bSFrank Mayhar return 0; 5279d699594dSDave Hansen if (is_journal_aborted(journal)) 5280ac27a0ecSDave Kleikamp return -EROFS; 52812aff57b0SYongqiang Yang /* We have to allocate physical blocks for delalloc blocks 52822aff57b0SYongqiang Yang * before flushing journal. otherwise delalloc blocks can not 52832aff57b0SYongqiang Yang * be allocated any more. even more truncate on delalloc blocks 52842aff57b0SYongqiang Yang * could trigger BUG by flushing delalloc blocks in journal. 52852aff57b0SYongqiang Yang * There is no delalloc block in non-journal data mode. 52862aff57b0SYongqiang Yang */ 52872aff57b0SYongqiang Yang if (val && test_opt(inode->i_sb, DELALLOC)) { 52882aff57b0SYongqiang Yang err = ext4_alloc_da_blocks(inode); 52892aff57b0SYongqiang Yang if (err < 0) 52902aff57b0SYongqiang Yang return err; 52912aff57b0SYongqiang Yang } 5292ac27a0ecSDave Kleikamp 529317335dccSDmitry Monakhov /* Wait for all existing dio workers */ 529417335dccSDmitry Monakhov ext4_inode_block_unlocked_dio(inode); 529517335dccSDmitry Monakhov inode_dio_wait(inode); 529617335dccSDmitry Monakhov 5297dab291afSMingming Cao jbd2_journal_lock_updates(journal); 5298ac27a0ecSDave Kleikamp 5299ac27a0ecSDave Kleikamp /* 5300ac27a0ecSDave Kleikamp * OK, there are no updates running now, and all cached data is 5301ac27a0ecSDave Kleikamp * synced to disk. We are now in a completely consistent state 5302ac27a0ecSDave Kleikamp * which doesn't have anything in the journal, and we know that 5303ac27a0ecSDave Kleikamp * no filesystem updates are running, so it is safe to modify 5304ac27a0ecSDave Kleikamp * the inode's in-core data-journaling state flag now. 5305ac27a0ecSDave Kleikamp */ 5306ac27a0ecSDave Kleikamp 5307ac27a0ecSDave Kleikamp if (val) 530812e9b892SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 53095872ddaaSYongqiang Yang else { 53105872ddaaSYongqiang Yang jbd2_journal_flush(journal); 531112e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 53125872ddaaSYongqiang Yang } 5313617ba13bSMingming Cao ext4_set_aops(inode); 5314ac27a0ecSDave Kleikamp 5315dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 531617335dccSDmitry Monakhov ext4_inode_resume_unlocked_dio(inode); 5317ac27a0ecSDave Kleikamp 5318ac27a0ecSDave Kleikamp /* Finally we can mark the inode as dirty. */ 5319ac27a0ecSDave Kleikamp 53209924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 5321ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5322ac27a0ecSDave Kleikamp return PTR_ERR(handle); 5323ac27a0ecSDave Kleikamp 5324617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 53250390131bSFrank Mayhar ext4_handle_sync(handle); 5326617ba13bSMingming Cao ext4_journal_stop(handle); 5327617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5328ac27a0ecSDave Kleikamp 5329ac27a0ecSDave Kleikamp return err; 5330ac27a0ecSDave Kleikamp } 53312e9ee850SAneesh Kumar K.V 53322e9ee850SAneesh Kumar K.V static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) 53332e9ee850SAneesh Kumar K.V { 53342e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 53352e9ee850SAneesh Kumar K.V } 53362e9ee850SAneesh Kumar K.V 5337c2ec175cSNick Piggin int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) 53382e9ee850SAneesh Kumar K.V { 5339c2ec175cSNick Piggin struct page *page = vmf->page; 53402e9ee850SAneesh Kumar K.V loff_t size; 53412e9ee850SAneesh Kumar K.V unsigned long len; 53429ea7df53SJan Kara int ret; 53432e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 5344496ad9aaSAl Viro struct inode *inode = file_inode(file); 53452e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 53469ea7df53SJan Kara handle_t *handle; 53479ea7df53SJan Kara get_block_t *get_block; 53489ea7df53SJan Kara int retries = 0; 53492e9ee850SAneesh Kumar K.V 53508e8ad8a5SJan Kara sb_start_pagefault(inode->i_sb); 5351041bbb6dSTheodore Ts'o file_update_time(vma->vm_file); 53529ea7df53SJan Kara /* Delalloc case is easy... */ 53539ea7df53SJan Kara if (test_opt(inode->i_sb, DELALLOC) && 53549ea7df53SJan Kara !ext4_should_journal_data(inode) && 53559ea7df53SJan Kara !ext4_nonda_switch(inode->i_sb)) { 53569ea7df53SJan Kara do { 53579ea7df53SJan Kara ret = __block_page_mkwrite(vma, vmf, 53589ea7df53SJan Kara ext4_da_get_block_prep); 53599ea7df53SJan Kara } while (ret == -ENOSPC && 53609ea7df53SJan Kara ext4_should_retry_alloc(inode->i_sb, &retries)); 53619ea7df53SJan Kara goto out_ret; 53622e9ee850SAneesh Kumar K.V } 53630e499890SDarrick J. Wong 53640e499890SDarrick J. Wong lock_page(page); 53659ea7df53SJan Kara size = i_size_read(inode); 53669ea7df53SJan Kara /* Page got truncated from under us? */ 53679ea7df53SJan Kara if (page->mapping != mapping || page_offset(page) > size) { 53689ea7df53SJan Kara unlock_page(page); 53699ea7df53SJan Kara ret = VM_FAULT_NOPAGE; 53709ea7df53SJan Kara goto out; 53710e499890SDarrick J. Wong } 53722e9ee850SAneesh Kumar K.V 53732e9ee850SAneesh Kumar K.V if (page->index == size >> PAGE_CACHE_SHIFT) 53742e9ee850SAneesh Kumar K.V len = size & ~PAGE_CACHE_MASK; 53752e9ee850SAneesh Kumar K.V else 53762e9ee850SAneesh Kumar K.V len = PAGE_CACHE_SIZE; 5377a827eaffSAneesh Kumar K.V /* 53789ea7df53SJan Kara * Return if we have all the buffers mapped. This avoids the need to do 53799ea7df53SJan Kara * journal_start/journal_stop which can block and take a long time 5380a827eaffSAneesh Kumar K.V */ 53812e9ee850SAneesh Kumar K.V if (page_has_buffers(page)) { 5382f19d5870STao Ma if (!ext4_walk_page_buffers(NULL, page_buffers(page), 5383f19d5870STao Ma 0, len, NULL, 5384a827eaffSAneesh Kumar K.V ext4_bh_unmapped)) { 53859ea7df53SJan Kara /* Wait so that we don't change page under IO */ 53861d1d1a76SDarrick J. Wong wait_for_stable_page(page); 53879ea7df53SJan Kara ret = VM_FAULT_LOCKED; 53889ea7df53SJan Kara goto out; 53892e9ee850SAneesh Kumar K.V } 5390a827eaffSAneesh Kumar K.V } 5391a827eaffSAneesh Kumar K.V unlock_page(page); 53929ea7df53SJan Kara /* OK, we need to fill the hole... */ 53939ea7df53SJan Kara if (ext4_should_dioread_nolock(inode)) 53949ea7df53SJan Kara get_block = ext4_get_block_write; 53959ea7df53SJan Kara else 53969ea7df53SJan Kara get_block = ext4_get_block; 53979ea7df53SJan Kara retry_alloc: 53989924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 53999924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 54009ea7df53SJan Kara if (IS_ERR(handle)) { 5401c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 54029ea7df53SJan Kara goto out; 54039ea7df53SJan Kara } 54049ea7df53SJan Kara ret = __block_page_mkwrite(vma, vmf, get_block); 54059ea7df53SJan Kara if (!ret && ext4_should_journal_data(inode)) { 5406f19d5870STao Ma if (ext4_walk_page_buffers(handle, page_buffers(page), 0, 54079ea7df53SJan Kara PAGE_CACHE_SIZE, NULL, do_journal_get_write_access)) { 54089ea7df53SJan Kara unlock_page(page); 54099ea7df53SJan Kara ret = VM_FAULT_SIGBUS; 5410fcbb5515SYongqiang Yang ext4_journal_stop(handle); 54119ea7df53SJan Kara goto out; 54129ea7df53SJan Kara } 54139ea7df53SJan Kara ext4_set_inode_state(inode, EXT4_STATE_JDATA); 54149ea7df53SJan Kara } 54159ea7df53SJan Kara ext4_journal_stop(handle); 54169ea7df53SJan Kara if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 54179ea7df53SJan Kara goto retry_alloc; 54189ea7df53SJan Kara out_ret: 54199ea7df53SJan Kara ret = block_page_mkwrite_return(ret); 54209ea7df53SJan Kara out: 54218e8ad8a5SJan Kara sb_end_pagefault(inode->i_sb); 54222e9ee850SAneesh Kumar K.V return ret; 54232e9ee850SAneesh Kumar K.V } 5424