1ac27a0ecSDave Kleikamp /* 2617ba13bSMingming Cao * linux/fs/ext4/inode.c 3ac27a0ecSDave Kleikamp * 4ac27a0ecSDave Kleikamp * Copyright (C) 1992, 1993, 1994, 1995 5ac27a0ecSDave Kleikamp * Remy Card (card@masi.ibp.fr) 6ac27a0ecSDave Kleikamp * Laboratoire MASI - Institut Blaise Pascal 7ac27a0ecSDave Kleikamp * Universite Pierre et Marie Curie (Paris VI) 8ac27a0ecSDave Kleikamp * 9ac27a0ecSDave Kleikamp * from 10ac27a0ecSDave Kleikamp * 11ac27a0ecSDave Kleikamp * linux/fs/minix/inode.c 12ac27a0ecSDave Kleikamp * 13ac27a0ecSDave Kleikamp * Copyright (C) 1991, 1992 Linus Torvalds 14ac27a0ecSDave Kleikamp * 15ac27a0ecSDave Kleikamp * 64-bit file support on 64-bit platforms by Jakub Jelinek 16ac27a0ecSDave Kleikamp * (jj@sunsite.ms.mff.cuni.cz) 17ac27a0ecSDave Kleikamp * 18617ba13bSMingming Cao * Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000 19ac27a0ecSDave Kleikamp */ 20ac27a0ecSDave Kleikamp 21ac27a0ecSDave Kleikamp #include <linux/fs.h> 22ac27a0ecSDave Kleikamp #include <linux/time.h> 23dab291afSMingming Cao #include <linux/jbd2.h> 24ac27a0ecSDave Kleikamp #include <linux/highuid.h> 25ac27a0ecSDave Kleikamp #include <linux/pagemap.h> 26ac27a0ecSDave Kleikamp #include <linux/quotaops.h> 27ac27a0ecSDave Kleikamp #include <linux/string.h> 28ac27a0ecSDave Kleikamp #include <linux/buffer_head.h> 29ac27a0ecSDave Kleikamp #include <linux/writeback.h> 3064769240SAlex Tomas #include <linux/pagevec.h> 31ac27a0ecSDave Kleikamp #include <linux/mpage.h> 32e83c1397SDuane Griffin #include <linux/namei.h> 33ac27a0ecSDave Kleikamp #include <linux/uio.h> 34ac27a0ecSDave Kleikamp #include <linux/bio.h> 354c0425ffSMingming Cao #include <linux/workqueue.h> 36744692dcSJiaying Zhang #include <linux/kernel.h> 376db26ffcSAndrew Morton #include <linux/printk.h> 385a0e3ad6STejun Heo #include <linux/slab.h> 39a8901d34STheodore Ts'o #include <linux/ratelimit.h> 409bffad1eSTheodore Ts'o 413dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 42ac27a0ecSDave Kleikamp #include "xattr.h" 43ac27a0ecSDave Kleikamp #include "acl.h" 449f125d64STheodore Ts'o #include "truncate.h" 45ac27a0ecSDave Kleikamp 469bffad1eSTheodore Ts'o #include <trace/events/ext4.h> 479bffad1eSTheodore Ts'o 48a1d6cc56SAneesh Kumar K.V #define MPAGE_DA_EXTENT_TAIL 0x01 49a1d6cc56SAneesh Kumar K.V 50814525f4SDarrick J. Wong static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw, 51814525f4SDarrick J. Wong struct ext4_inode_info *ei) 52814525f4SDarrick J. Wong { 53814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 54814525f4SDarrick J. Wong __u16 csum_lo; 55814525f4SDarrick J. Wong __u16 csum_hi = 0; 56814525f4SDarrick J. Wong __u32 csum; 57814525f4SDarrick J. Wong 58814525f4SDarrick J. Wong csum_lo = raw->i_checksum_lo; 59814525f4SDarrick J. Wong raw->i_checksum_lo = 0; 60814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 61814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) { 62814525f4SDarrick J. Wong csum_hi = raw->i_checksum_hi; 63814525f4SDarrick J. Wong raw->i_checksum_hi = 0; 64814525f4SDarrick J. Wong } 65814525f4SDarrick J. Wong 66814525f4SDarrick J. Wong csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw, 67814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)); 68814525f4SDarrick J. Wong 69814525f4SDarrick J. Wong raw->i_checksum_lo = csum_lo; 70814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 71814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 72814525f4SDarrick J. Wong raw->i_checksum_hi = csum_hi; 73814525f4SDarrick J. Wong 74814525f4SDarrick J. Wong return csum; 75814525f4SDarrick J. Wong } 76814525f4SDarrick J. Wong 77814525f4SDarrick J. Wong static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw, 78814525f4SDarrick J. Wong struct ext4_inode_info *ei) 79814525f4SDarrick J. Wong { 80814525f4SDarrick J. Wong __u32 provided, calculated; 81814525f4SDarrick J. Wong 82814525f4SDarrick J. Wong if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 83814525f4SDarrick J. Wong cpu_to_le32(EXT4_OS_LINUX) || 84814525f4SDarrick J. Wong !EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, 85814525f4SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) 86814525f4SDarrick J. Wong return 1; 87814525f4SDarrick J. Wong 88814525f4SDarrick J. Wong provided = le16_to_cpu(raw->i_checksum_lo); 89814525f4SDarrick J. Wong calculated = ext4_inode_csum(inode, raw, ei); 90814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 91814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 92814525f4SDarrick J. Wong provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16; 93814525f4SDarrick J. Wong else 94814525f4SDarrick J. Wong calculated &= 0xFFFF; 95814525f4SDarrick J. Wong 96814525f4SDarrick J. Wong return provided == calculated; 97814525f4SDarrick J. Wong } 98814525f4SDarrick J. Wong 99814525f4SDarrick J. Wong static void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw, 100814525f4SDarrick J. Wong struct ext4_inode_info *ei) 101814525f4SDarrick J. Wong { 102814525f4SDarrick J. Wong __u32 csum; 103814525f4SDarrick J. Wong 104814525f4SDarrick J. Wong if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 105814525f4SDarrick J. Wong cpu_to_le32(EXT4_OS_LINUX) || 106814525f4SDarrick J. Wong !EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, 107814525f4SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) 108814525f4SDarrick J. Wong return; 109814525f4SDarrick J. Wong 110814525f4SDarrick J. Wong csum = ext4_inode_csum(inode, raw, ei); 111814525f4SDarrick J. Wong raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF); 112814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 113814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 114814525f4SDarrick J. Wong raw->i_checksum_hi = cpu_to_le16(csum >> 16); 115814525f4SDarrick J. Wong } 116814525f4SDarrick J. Wong 117678aaf48SJan Kara static inline int ext4_begin_ordered_truncate(struct inode *inode, 118678aaf48SJan Kara loff_t new_size) 119678aaf48SJan Kara { 1207ff9c073STheodore Ts'o trace_ext4_begin_ordered_truncate(inode, new_size); 1218aefcd55STheodore Ts'o /* 1228aefcd55STheodore Ts'o * If jinode is zero, then we never opened the file for 1238aefcd55STheodore Ts'o * writing, so there's no need to call 1248aefcd55STheodore Ts'o * jbd2_journal_begin_ordered_truncate() since there's no 1258aefcd55STheodore Ts'o * outstanding writes we need to flush. 1268aefcd55STheodore Ts'o */ 1278aefcd55STheodore Ts'o if (!EXT4_I(inode)->jinode) 1288aefcd55STheodore Ts'o return 0; 1298aefcd55STheodore Ts'o return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode), 1308aefcd55STheodore Ts'o EXT4_I(inode)->jinode, 131678aaf48SJan Kara new_size); 132678aaf48SJan Kara } 133678aaf48SJan Kara 13464769240SAlex Tomas static void ext4_invalidatepage(struct page *page, unsigned long offset); 135cb20d518STheodore Ts'o static int noalloc_get_block_write(struct inode *inode, sector_t iblock, 136cb20d518STheodore Ts'o struct buffer_head *bh_result, int create); 137cb20d518STheodore Ts'o static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode); 138cb20d518STheodore Ts'o static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate); 139cb20d518STheodore Ts'o static int __ext4_journalled_writepage(struct page *page, unsigned int len); 140cb20d518STheodore Ts'o static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh); 1415f163cc7SEric Sandeen static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle, 1425f163cc7SEric Sandeen struct inode *inode, struct page *page, loff_t from, 1435f163cc7SEric Sandeen loff_t length, int flags); 14464769240SAlex Tomas 145ac27a0ecSDave Kleikamp /* 146ac27a0ecSDave Kleikamp * Test whether an inode is a fast symlink. 147ac27a0ecSDave Kleikamp */ 148617ba13bSMingming Cao static int ext4_inode_is_fast_symlink(struct inode *inode) 149ac27a0ecSDave Kleikamp { 150617ba13bSMingming Cao int ea_blocks = EXT4_I(inode)->i_file_acl ? 151ac27a0ecSDave Kleikamp (inode->i_sb->s_blocksize >> 9) : 0; 152ac27a0ecSDave Kleikamp 153ac27a0ecSDave Kleikamp return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0); 154ac27a0ecSDave Kleikamp } 155ac27a0ecSDave Kleikamp 156ac27a0ecSDave Kleikamp /* 157ac27a0ecSDave Kleikamp * Restart the transaction associated with *handle. This does a commit, 158ac27a0ecSDave Kleikamp * so before we call here everything must be consistently dirtied against 159ac27a0ecSDave Kleikamp * this transaction. 160ac27a0ecSDave Kleikamp */ 161487caeefSJan Kara int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode, 162487caeefSJan Kara int nblocks) 163ac27a0ecSDave Kleikamp { 164487caeefSJan Kara int ret; 165487caeefSJan Kara 166487caeefSJan Kara /* 167e35fd660STheodore Ts'o * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this 168487caeefSJan Kara * moment, get_block can be called only for blocks inside i_size since 169487caeefSJan Kara * page cache has been already dropped and writes are blocked by 170487caeefSJan Kara * i_mutex. So we can safely drop the i_data_sem here. 171487caeefSJan Kara */ 1720390131bSFrank Mayhar BUG_ON(EXT4_JOURNAL(inode) == NULL); 173ac27a0ecSDave Kleikamp jbd_debug(2, "restarting handle %p\n", handle); 174487caeefSJan Kara up_write(&EXT4_I(inode)->i_data_sem); 1758e8eaabeSAmir Goldstein ret = ext4_journal_restart(handle, nblocks); 176487caeefSJan Kara down_write(&EXT4_I(inode)->i_data_sem); 177fa5d1113SAneesh Kumar K.V ext4_discard_preallocations(inode); 178487caeefSJan Kara 179487caeefSJan Kara return ret; 180ac27a0ecSDave Kleikamp } 181ac27a0ecSDave Kleikamp 182ac27a0ecSDave Kleikamp /* 183ac27a0ecSDave Kleikamp * Called at the last iput() if i_nlink is zero. 184ac27a0ecSDave Kleikamp */ 1850930fcc1SAl Viro void ext4_evict_inode(struct inode *inode) 186ac27a0ecSDave Kleikamp { 187ac27a0ecSDave Kleikamp handle_t *handle; 188bc965ab3STheodore Ts'o int err; 189ac27a0ecSDave Kleikamp 1907ff9c073STheodore Ts'o trace_ext4_evict_inode(inode); 1912581fdc8SJiaying Zhang 1922581fdc8SJiaying Zhang ext4_ioend_wait(inode); 1932581fdc8SJiaying Zhang 1940930fcc1SAl Viro if (inode->i_nlink) { 1952d859db3SJan Kara /* 1962d859db3SJan Kara * When journalling data dirty buffers are tracked only in the 1972d859db3SJan Kara * journal. So although mm thinks everything is clean and 1982d859db3SJan Kara * ready for reaping the inode might still have some pages to 1992d859db3SJan Kara * write in the running transaction or waiting to be 2002d859db3SJan Kara * checkpointed. Thus calling jbd2_journal_invalidatepage() 2012d859db3SJan Kara * (via truncate_inode_pages()) to discard these buffers can 2022d859db3SJan Kara * cause data loss. Also even if we did not discard these 2032d859db3SJan Kara * buffers, we would have no way to find them after the inode 2042d859db3SJan Kara * is reaped and thus user could see stale data if he tries to 2052d859db3SJan Kara * read them before the transaction is checkpointed. So be 2062d859db3SJan Kara * careful and force everything to disk here... We use 2072d859db3SJan Kara * ei->i_datasync_tid to store the newest transaction 2082d859db3SJan Kara * containing inode's data. 2092d859db3SJan Kara * 2102d859db3SJan Kara * Note that directories do not have this problem because they 2112d859db3SJan Kara * don't use page cache. 2122d859db3SJan Kara */ 2132d859db3SJan Kara if (ext4_should_journal_data(inode) && 2142d859db3SJan Kara (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode))) { 2152d859db3SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 2162d859db3SJan Kara tid_t commit_tid = EXT4_I(inode)->i_datasync_tid; 2172d859db3SJan Kara 2182d859db3SJan Kara jbd2_log_start_commit(journal, commit_tid); 2192d859db3SJan Kara jbd2_log_wait_commit(journal, commit_tid); 2202d859db3SJan Kara filemap_write_and_wait(&inode->i_data); 2212d859db3SJan Kara } 2220930fcc1SAl Viro truncate_inode_pages(&inode->i_data, 0); 2230930fcc1SAl Viro goto no_delete; 2240930fcc1SAl Viro } 2250930fcc1SAl Viro 226907f4554SChristoph Hellwig if (!is_bad_inode(inode)) 227871a2931SChristoph Hellwig dquot_initialize(inode); 228907f4554SChristoph Hellwig 229678aaf48SJan Kara if (ext4_should_order_data(inode)) 230678aaf48SJan Kara ext4_begin_ordered_truncate(inode, 0); 231ac27a0ecSDave Kleikamp truncate_inode_pages(&inode->i_data, 0); 232ac27a0ecSDave Kleikamp 233ac27a0ecSDave Kleikamp if (is_bad_inode(inode)) 234ac27a0ecSDave Kleikamp goto no_delete; 235ac27a0ecSDave Kleikamp 2368e8ad8a5SJan Kara /* 2378e8ad8a5SJan Kara * Protect us against freezing - iput() caller didn't have to have any 2388e8ad8a5SJan Kara * protection against it 2398e8ad8a5SJan Kara */ 2408e8ad8a5SJan Kara sb_start_intwrite(inode->i_sb); 2419f125d64STheodore Ts'o handle = ext4_journal_start(inode, ext4_blocks_for_truncate(inode)+3); 242ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 243bc965ab3STheodore Ts'o ext4_std_error(inode->i_sb, PTR_ERR(handle)); 244ac27a0ecSDave Kleikamp /* 245ac27a0ecSDave Kleikamp * If we're going to skip the normal cleanup, we still need to 246ac27a0ecSDave Kleikamp * make sure that the in-core orphan linked list is properly 247ac27a0ecSDave Kleikamp * cleaned up. 248ac27a0ecSDave Kleikamp */ 249617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 2508e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 251ac27a0ecSDave Kleikamp goto no_delete; 252ac27a0ecSDave Kleikamp } 253ac27a0ecSDave Kleikamp 254ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 2550390131bSFrank Mayhar ext4_handle_sync(handle); 256ac27a0ecSDave Kleikamp inode->i_size = 0; 257bc965ab3STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 258bc965ab3STheodore Ts'o if (err) { 25912062dddSEric Sandeen ext4_warning(inode->i_sb, 260bc965ab3STheodore Ts'o "couldn't mark inode dirty (err %d)", err); 261bc965ab3STheodore Ts'o goto stop_handle; 262bc965ab3STheodore Ts'o } 263ac27a0ecSDave Kleikamp if (inode->i_blocks) 264617ba13bSMingming Cao ext4_truncate(inode); 265bc965ab3STheodore Ts'o 266bc965ab3STheodore Ts'o /* 267bc965ab3STheodore Ts'o * ext4_ext_truncate() doesn't reserve any slop when it 268bc965ab3STheodore Ts'o * restarts journal transactions; therefore there may not be 269bc965ab3STheodore Ts'o * enough credits left in the handle to remove the inode from 270bc965ab3STheodore Ts'o * the orphan list and set the dtime field. 271bc965ab3STheodore Ts'o */ 2720390131bSFrank Mayhar if (!ext4_handle_has_enough_credits(handle, 3)) { 273bc965ab3STheodore Ts'o err = ext4_journal_extend(handle, 3); 274bc965ab3STheodore Ts'o if (err > 0) 275bc965ab3STheodore Ts'o err = ext4_journal_restart(handle, 3); 276bc965ab3STheodore Ts'o if (err != 0) { 27712062dddSEric Sandeen ext4_warning(inode->i_sb, 278bc965ab3STheodore Ts'o "couldn't extend journal (err %d)", err); 279bc965ab3STheodore Ts'o stop_handle: 280bc965ab3STheodore Ts'o ext4_journal_stop(handle); 28145388219STheodore Ts'o ext4_orphan_del(NULL, inode); 2828e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 283bc965ab3STheodore Ts'o goto no_delete; 284bc965ab3STheodore Ts'o } 285bc965ab3STheodore Ts'o } 286bc965ab3STheodore Ts'o 287ac27a0ecSDave Kleikamp /* 288617ba13bSMingming Cao * Kill off the orphan record which ext4_truncate created. 289ac27a0ecSDave Kleikamp * AKPM: I think this can be inside the above `if'. 290617ba13bSMingming Cao * Note that ext4_orphan_del() has to be able to cope with the 291ac27a0ecSDave Kleikamp * deletion of a non-existent orphan - this is because we don't 292617ba13bSMingming Cao * know if ext4_truncate() actually created an orphan record. 293ac27a0ecSDave Kleikamp * (Well, we could do this if we need to, but heck - it works) 294ac27a0ecSDave Kleikamp */ 295617ba13bSMingming Cao ext4_orphan_del(handle, inode); 296617ba13bSMingming Cao EXT4_I(inode)->i_dtime = get_seconds(); 297ac27a0ecSDave Kleikamp 298ac27a0ecSDave Kleikamp /* 299ac27a0ecSDave Kleikamp * One subtle ordering requirement: if anything has gone wrong 300ac27a0ecSDave Kleikamp * (transaction abort, IO errors, whatever), then we can still 301ac27a0ecSDave Kleikamp * do these next steps (the fs will already have been marked as 302ac27a0ecSDave Kleikamp * having errors), but we can't free the inode if the mark_dirty 303ac27a0ecSDave Kleikamp * fails. 304ac27a0ecSDave Kleikamp */ 305617ba13bSMingming Cao if (ext4_mark_inode_dirty(handle, inode)) 306ac27a0ecSDave Kleikamp /* If that failed, just do the required in-core inode clear. */ 3070930fcc1SAl Viro ext4_clear_inode(inode); 308ac27a0ecSDave Kleikamp else 309617ba13bSMingming Cao ext4_free_inode(handle, inode); 310617ba13bSMingming Cao ext4_journal_stop(handle); 3118e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 312ac27a0ecSDave Kleikamp return; 313ac27a0ecSDave Kleikamp no_delete: 3140930fcc1SAl Viro ext4_clear_inode(inode); /* We must guarantee clearing of inode... */ 315ac27a0ecSDave Kleikamp } 316ac27a0ecSDave Kleikamp 317a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 318a9e7f447SDmitry Monakhov qsize_t *ext4_get_reserved_space(struct inode *inode) 31960e58e0fSMingming Cao { 320a9e7f447SDmitry Monakhov return &EXT4_I(inode)->i_reserved_quota; 32160e58e0fSMingming Cao } 322a9e7f447SDmitry Monakhov #endif 3239d0be502STheodore Ts'o 32412219aeaSAneesh Kumar K.V /* 32512219aeaSAneesh Kumar K.V * Calculate the number of metadata blocks need to reserve 3269d0be502STheodore Ts'o * to allocate a block located at @lblock 32712219aeaSAneesh Kumar K.V */ 32801f49d0bSTheodore Ts'o static int ext4_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock) 32912219aeaSAneesh Kumar K.V { 33012e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3319d0be502STheodore Ts'o return ext4_ext_calc_metadata_amount(inode, lblock); 33212219aeaSAneesh Kumar K.V 3338bb2b247SAmir Goldstein return ext4_ind_calc_metadata_amount(inode, lblock); 33412219aeaSAneesh Kumar K.V } 33512219aeaSAneesh Kumar K.V 3360637c6f4STheodore Ts'o /* 3370637c6f4STheodore Ts'o * Called with i_data_sem down, which is important since we can call 3380637c6f4STheodore Ts'o * ext4_discard_preallocations() from here. 3390637c6f4STheodore Ts'o */ 3405f634d06SAneesh Kumar K.V void ext4_da_update_reserve_space(struct inode *inode, 3415f634d06SAneesh Kumar K.V int used, int quota_claim) 34212219aeaSAneesh Kumar K.V { 34312219aeaSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3440637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 34512219aeaSAneesh Kumar K.V 3460637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 347d8990240SAditya Kali trace_ext4_da_update_reserve_space(inode, used, quota_claim); 3480637c6f4STheodore Ts'o if (unlikely(used > ei->i_reserved_data_blocks)) { 3490637c6f4STheodore Ts'o ext4_msg(inode->i_sb, KERN_NOTICE, "%s: ino %lu, used %d " 3501084f252STheodore Ts'o "with only %d reserved data blocks", 3510637c6f4STheodore Ts'o __func__, inode->i_ino, used, 3520637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 3530637c6f4STheodore Ts'o WARN_ON(1); 3540637c6f4STheodore Ts'o used = ei->i_reserved_data_blocks; 3556bc6e63fSAneesh Kumar K.V } 35612219aeaSAneesh Kumar K.V 35797795d2aSBrian Foster if (unlikely(ei->i_allocated_meta_blocks > ei->i_reserved_meta_blocks)) { 35897795d2aSBrian Foster ext4_msg(inode->i_sb, KERN_NOTICE, "%s: ino %lu, allocated %d " 35997795d2aSBrian Foster "with only %d reserved metadata blocks\n", __func__, 36097795d2aSBrian Foster inode->i_ino, ei->i_allocated_meta_blocks, 36197795d2aSBrian Foster ei->i_reserved_meta_blocks); 36297795d2aSBrian Foster WARN_ON(1); 36397795d2aSBrian Foster ei->i_allocated_meta_blocks = ei->i_reserved_meta_blocks; 36497795d2aSBrian Foster } 36597795d2aSBrian Foster 3660637c6f4STheodore Ts'o /* Update per-inode reservations */ 3670637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= used; 3680637c6f4STheodore Ts'o ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks; 36957042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 37072b8ab9dSEric Sandeen used + ei->i_allocated_meta_blocks); 3710637c6f4STheodore Ts'o ei->i_allocated_meta_blocks = 0; 3720637c6f4STheodore Ts'o 3730637c6f4STheodore Ts'o if (ei->i_reserved_data_blocks == 0) { 3740637c6f4STheodore Ts'o /* 3750637c6f4STheodore Ts'o * We can release all of the reserved metadata blocks 3760637c6f4STheodore Ts'o * only when we have written all of the delayed 3770637c6f4STheodore Ts'o * allocation blocks. 3780637c6f4STheodore Ts'o */ 37957042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 38072b8ab9dSEric Sandeen ei->i_reserved_meta_blocks); 381ee5f4d9cSTheodore Ts'o ei->i_reserved_meta_blocks = 0; 3829d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 3830637c6f4STheodore Ts'o } 38412219aeaSAneesh Kumar K.V spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 38560e58e0fSMingming Cao 38672b8ab9dSEric Sandeen /* Update quota subsystem for data blocks */ 38772b8ab9dSEric Sandeen if (quota_claim) 3887b415bf6SAditya Kali dquot_claim_block(inode, EXT4_C2B(sbi, used)); 38972b8ab9dSEric Sandeen else { 3905f634d06SAneesh Kumar K.V /* 3915f634d06SAneesh Kumar K.V * We did fallocate with an offset that is already delayed 3925f634d06SAneesh Kumar K.V * allocated. So on delayed allocated writeback we should 39372b8ab9dSEric Sandeen * not re-claim the quota for fallocated blocks. 3945f634d06SAneesh Kumar K.V */ 3957b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, used)); 3965f634d06SAneesh Kumar K.V } 397d6014301SAneesh Kumar K.V 398d6014301SAneesh Kumar K.V /* 399d6014301SAneesh Kumar K.V * If we have done all the pending block allocations and if 400d6014301SAneesh Kumar K.V * there aren't any writers on the inode, we can discard the 401d6014301SAneesh Kumar K.V * inode's preallocations. 402d6014301SAneesh Kumar K.V */ 4030637c6f4STheodore Ts'o if ((ei->i_reserved_data_blocks == 0) && 4040637c6f4STheodore Ts'o (atomic_read(&inode->i_writecount) == 0)) 405d6014301SAneesh Kumar K.V ext4_discard_preallocations(inode); 40612219aeaSAneesh Kumar K.V } 40712219aeaSAneesh Kumar K.V 408e29136f8STheodore Ts'o static int __check_block_validity(struct inode *inode, const char *func, 409c398eda0STheodore Ts'o unsigned int line, 41024676da4STheodore Ts'o struct ext4_map_blocks *map) 4116fd058f7STheodore Ts'o { 41224676da4STheodore Ts'o if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk, 41324676da4STheodore Ts'o map->m_len)) { 414c398eda0STheodore Ts'o ext4_error_inode(inode, func, line, map->m_pblk, 415c398eda0STheodore Ts'o "lblock %lu mapped to illegal pblock " 41624676da4STheodore Ts'o "(length %d)", (unsigned long) map->m_lblk, 417c398eda0STheodore Ts'o map->m_len); 4186fd058f7STheodore Ts'o return -EIO; 4196fd058f7STheodore Ts'o } 4206fd058f7STheodore Ts'o return 0; 4216fd058f7STheodore Ts'o } 4226fd058f7STheodore Ts'o 423e29136f8STheodore Ts'o #define check_block_validity(inode, map) \ 424c398eda0STheodore Ts'o __check_block_validity((inode), __func__, __LINE__, (map)) 425e29136f8STheodore Ts'o 426f5ab0d1fSMingming Cao /* 4271f94533dSTheodore Ts'o * Return the number of contiguous dirty pages in a given inode 4281f94533dSTheodore Ts'o * starting at page frame idx. 42955138e0bSTheodore Ts'o */ 43055138e0bSTheodore Ts'o static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx, 43155138e0bSTheodore Ts'o unsigned int max_pages) 43255138e0bSTheodore Ts'o { 43355138e0bSTheodore Ts'o struct address_space *mapping = inode->i_mapping; 43455138e0bSTheodore Ts'o pgoff_t index; 43555138e0bSTheodore Ts'o struct pagevec pvec; 43655138e0bSTheodore Ts'o pgoff_t num = 0; 43755138e0bSTheodore Ts'o int i, nr_pages, done = 0; 43855138e0bSTheodore Ts'o 43955138e0bSTheodore Ts'o if (max_pages == 0) 44055138e0bSTheodore Ts'o return 0; 44155138e0bSTheodore Ts'o pagevec_init(&pvec, 0); 44255138e0bSTheodore Ts'o while (!done) { 44355138e0bSTheodore Ts'o index = idx; 44455138e0bSTheodore Ts'o nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 44555138e0bSTheodore Ts'o PAGECACHE_TAG_DIRTY, 44655138e0bSTheodore Ts'o (pgoff_t)PAGEVEC_SIZE); 44755138e0bSTheodore Ts'o if (nr_pages == 0) 44855138e0bSTheodore Ts'o break; 44955138e0bSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 45055138e0bSTheodore Ts'o struct page *page = pvec.pages[i]; 45155138e0bSTheodore Ts'o struct buffer_head *bh, *head; 45255138e0bSTheodore Ts'o 45355138e0bSTheodore Ts'o lock_page(page); 45455138e0bSTheodore Ts'o if (unlikely(page->mapping != mapping) || 45555138e0bSTheodore Ts'o !PageDirty(page) || 45655138e0bSTheodore Ts'o PageWriteback(page) || 45755138e0bSTheodore Ts'o page->index != idx) { 45855138e0bSTheodore Ts'o done = 1; 45955138e0bSTheodore Ts'o unlock_page(page); 46055138e0bSTheodore Ts'o break; 46155138e0bSTheodore Ts'o } 4621f94533dSTheodore Ts'o if (page_has_buffers(page)) { 4631f94533dSTheodore Ts'o bh = head = page_buffers(page); 46455138e0bSTheodore Ts'o do { 46555138e0bSTheodore Ts'o if (!buffer_delay(bh) && 4661f94533dSTheodore Ts'o !buffer_unwritten(bh)) 46755138e0bSTheodore Ts'o done = 1; 4681f94533dSTheodore Ts'o bh = bh->b_this_page; 4691f94533dSTheodore Ts'o } while (!done && (bh != head)); 47055138e0bSTheodore Ts'o } 47155138e0bSTheodore Ts'o unlock_page(page); 47255138e0bSTheodore Ts'o if (done) 47355138e0bSTheodore Ts'o break; 47455138e0bSTheodore Ts'o idx++; 47555138e0bSTheodore Ts'o num++; 476659c6009SEric Sandeen if (num >= max_pages) { 477659c6009SEric Sandeen done = 1; 47855138e0bSTheodore Ts'o break; 47955138e0bSTheodore Ts'o } 480659c6009SEric Sandeen } 48155138e0bSTheodore Ts'o pagevec_release(&pvec); 48255138e0bSTheodore Ts'o } 48355138e0bSTheodore Ts'o return num; 48455138e0bSTheodore Ts'o } 48555138e0bSTheodore Ts'o 48655138e0bSTheodore Ts'o /* 4875356f261SAditya Kali * Sets the BH_Da_Mapped bit on the buffer heads corresponding to the given map. 4885356f261SAditya Kali */ 4895356f261SAditya Kali static void set_buffers_da_mapped(struct inode *inode, 4905356f261SAditya Kali struct ext4_map_blocks *map) 4915356f261SAditya Kali { 4925356f261SAditya Kali struct address_space *mapping = inode->i_mapping; 4935356f261SAditya Kali struct pagevec pvec; 4945356f261SAditya Kali int i, nr_pages; 4955356f261SAditya Kali pgoff_t index, end; 4965356f261SAditya Kali 4975356f261SAditya Kali index = map->m_lblk >> (PAGE_CACHE_SHIFT - inode->i_blkbits); 4985356f261SAditya Kali end = (map->m_lblk + map->m_len - 1) >> 4995356f261SAditya Kali (PAGE_CACHE_SHIFT - inode->i_blkbits); 5005356f261SAditya Kali 5015356f261SAditya Kali pagevec_init(&pvec, 0); 5025356f261SAditya Kali while (index <= end) { 5035356f261SAditya Kali nr_pages = pagevec_lookup(&pvec, mapping, index, 5045356f261SAditya Kali min(end - index + 1, 5055356f261SAditya Kali (pgoff_t)PAGEVEC_SIZE)); 5065356f261SAditya Kali if (nr_pages == 0) 5075356f261SAditya Kali break; 5085356f261SAditya Kali for (i = 0; i < nr_pages; i++) { 5095356f261SAditya Kali struct page *page = pvec.pages[i]; 5105356f261SAditya Kali struct buffer_head *bh, *head; 5115356f261SAditya Kali 5125356f261SAditya Kali if (unlikely(page->mapping != mapping) || 5135356f261SAditya Kali !PageDirty(page)) 5145356f261SAditya Kali break; 5155356f261SAditya Kali 5165356f261SAditya Kali if (page_has_buffers(page)) { 5175356f261SAditya Kali bh = head = page_buffers(page); 5185356f261SAditya Kali do { 5195356f261SAditya Kali set_buffer_da_mapped(bh); 5205356f261SAditya Kali bh = bh->b_this_page; 5215356f261SAditya Kali } while (bh != head); 5225356f261SAditya Kali } 5235356f261SAditya Kali index++; 5245356f261SAditya Kali } 5255356f261SAditya Kali pagevec_release(&pvec); 5265356f261SAditya Kali } 5275356f261SAditya Kali } 5285356f261SAditya Kali 5295356f261SAditya Kali /* 530e35fd660STheodore Ts'o * The ext4_map_blocks() function tries to look up the requested blocks, 5312b2d6d01STheodore Ts'o * and returns if the blocks are already mapped. 532f5ab0d1fSMingming Cao * 533f5ab0d1fSMingming Cao * Otherwise it takes the write lock of the i_data_sem and allocate blocks 534f5ab0d1fSMingming Cao * and store the allocated blocks in the result buffer head and mark it 535f5ab0d1fSMingming Cao * mapped. 536f5ab0d1fSMingming Cao * 537e35fd660STheodore Ts'o * If file type is extents based, it will call ext4_ext_map_blocks(), 538e35fd660STheodore Ts'o * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping 539f5ab0d1fSMingming Cao * based files 540f5ab0d1fSMingming Cao * 541f5ab0d1fSMingming Cao * On success, it returns the number of blocks being mapped or allocate. 542f5ab0d1fSMingming Cao * if create==0 and the blocks are pre-allocated and uninitialized block, 543f5ab0d1fSMingming Cao * the result buffer head is unmapped. If the create ==1, it will make sure 544f5ab0d1fSMingming Cao * the buffer head is mapped. 545f5ab0d1fSMingming Cao * 546f5ab0d1fSMingming Cao * It returns 0 if plain look up failed (blocks have not been allocated), in 547df3ab170STao Ma * that case, buffer head is unmapped 548f5ab0d1fSMingming Cao * 549f5ab0d1fSMingming Cao * It returns the error in case of allocation failure. 550f5ab0d1fSMingming Cao */ 551e35fd660STheodore Ts'o int ext4_map_blocks(handle_t *handle, struct inode *inode, 552e35fd660STheodore Ts'o struct ext4_map_blocks *map, int flags) 5530e855ac8SAneesh Kumar K.V { 5540e855ac8SAneesh Kumar K.V int retval; 555f5ab0d1fSMingming Cao 556e35fd660STheodore Ts'o map->m_flags = 0; 557e35fd660STheodore Ts'o ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u," 558e35fd660STheodore Ts'o "logical block %lu\n", inode->i_ino, flags, map->m_len, 559e35fd660STheodore Ts'o (unsigned long) map->m_lblk); 5604df3d265SAneesh Kumar K.V /* 561b920c755STheodore Ts'o * Try to see if we can get the block without requesting a new 562b920c755STheodore Ts'o * file system block. 5634df3d265SAneesh Kumar K.V */ 564729f52c6SZheng Liu if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 5650e855ac8SAneesh Kumar K.V down_read((&EXT4_I(inode)->i_data_sem)); 56612e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 567a4e5d88bSDmitry Monakhov retval = ext4_ext_map_blocks(handle, inode, map, flags & 568a4e5d88bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 5694df3d265SAneesh Kumar K.V } else { 570a4e5d88bSDmitry Monakhov retval = ext4_ind_map_blocks(handle, inode, map, flags & 571a4e5d88bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 5720e855ac8SAneesh Kumar K.V } 573729f52c6SZheng Liu if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 5744df3d265SAneesh Kumar K.V up_read((&EXT4_I(inode)->i_data_sem)); 575f5ab0d1fSMingming Cao 576e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 577*51865fdaSZheng Liu int ret; 578*51865fdaSZheng Liu if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) { 579*51865fdaSZheng Liu /* delayed alloc may be allocated by fallocate and 580*51865fdaSZheng Liu * coverted to initialized by directIO. 581*51865fdaSZheng Liu * we need to handle delayed extent here. 582*51865fdaSZheng Liu */ 583*51865fdaSZheng Liu down_write((&EXT4_I(inode)->i_data_sem)); 584*51865fdaSZheng Liu goto delayed_mapped; 585*51865fdaSZheng Liu } 586*51865fdaSZheng Liu ret = check_block_validity(inode, map); 5876fd058f7STheodore Ts'o if (ret != 0) 5886fd058f7STheodore Ts'o return ret; 5896fd058f7STheodore Ts'o } 5906fd058f7STheodore Ts'o 591f5ab0d1fSMingming Cao /* If it is only a block(s) look up */ 592c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 5934df3d265SAneesh Kumar K.V return retval; 5944df3d265SAneesh Kumar K.V 5954df3d265SAneesh Kumar K.V /* 596f5ab0d1fSMingming Cao * Returns if the blocks have already allocated 597f5ab0d1fSMingming Cao * 598f5ab0d1fSMingming Cao * Note that if blocks have been preallocated 599df3ab170STao Ma * ext4_ext_get_block() returns the create = 0 600f5ab0d1fSMingming Cao * with buffer head unmapped. 601f5ab0d1fSMingming Cao */ 602e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) 603f5ab0d1fSMingming Cao return retval; 604f5ab0d1fSMingming Cao 605f5ab0d1fSMingming Cao /* 6062a8964d6SAneesh Kumar K.V * When we call get_blocks without the create flag, the 6072a8964d6SAneesh Kumar K.V * BH_Unwritten flag could have gotten set if the blocks 6082a8964d6SAneesh Kumar K.V * requested were part of a uninitialized extent. We need to 6092a8964d6SAneesh Kumar K.V * clear this flag now that we are committed to convert all or 6102a8964d6SAneesh Kumar K.V * part of the uninitialized extent to be an initialized 6112a8964d6SAneesh Kumar K.V * extent. This is because we need to avoid the combination 6122a8964d6SAneesh Kumar K.V * of BH_Unwritten and BH_Mapped flags being simultaneously 6132a8964d6SAneesh Kumar K.V * set on the buffer_head. 6142a8964d6SAneesh Kumar K.V */ 615e35fd660STheodore Ts'o map->m_flags &= ~EXT4_MAP_UNWRITTEN; 6162a8964d6SAneesh Kumar K.V 6172a8964d6SAneesh Kumar K.V /* 618f5ab0d1fSMingming Cao * New blocks allocate and/or writing to uninitialized extent 619f5ab0d1fSMingming Cao * will possibly result in updating i_data, so we take 620f5ab0d1fSMingming Cao * the write lock of i_data_sem, and call get_blocks() 621f5ab0d1fSMingming Cao * with create == 1 flag. 6224df3d265SAneesh Kumar K.V */ 6234df3d265SAneesh Kumar K.V down_write((&EXT4_I(inode)->i_data_sem)); 624d2a17637SMingming Cao 625d2a17637SMingming Cao /* 626d2a17637SMingming Cao * if the caller is from delayed allocation writeout path 627d2a17637SMingming Cao * we have already reserved fs blocks for allocation 628d2a17637SMingming Cao * let the underlying get_block() function know to 629d2a17637SMingming Cao * avoid double accounting 630d2a17637SMingming Cao */ 631c2177057STheodore Ts'o if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 632f2321097STheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED); 6334df3d265SAneesh Kumar K.V /* 6344df3d265SAneesh Kumar K.V * We need to check for EXT4 here because migrate 6354df3d265SAneesh Kumar K.V * could have changed the inode type in between 6364df3d265SAneesh Kumar K.V */ 63712e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 638e35fd660STheodore Ts'o retval = ext4_ext_map_blocks(handle, inode, map, flags); 6390e855ac8SAneesh Kumar K.V } else { 640e35fd660STheodore Ts'o retval = ext4_ind_map_blocks(handle, inode, map, flags); 641267e4db9SAneesh Kumar K.V 642e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_NEW) { 643267e4db9SAneesh Kumar K.V /* 644267e4db9SAneesh Kumar K.V * We allocated new blocks which will result in 645267e4db9SAneesh Kumar K.V * i_data's format changing. Force the migrate 646267e4db9SAneesh Kumar K.V * to fail by clearing migrate flags 647267e4db9SAneesh Kumar K.V */ 64819f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE); 649267e4db9SAneesh Kumar K.V } 6502ac3b6e0STheodore Ts'o 651d2a17637SMingming Cao /* 6522ac3b6e0STheodore Ts'o * Update reserved blocks/metadata blocks after successful 6535f634d06SAneesh Kumar K.V * block allocation which had been deferred till now. We don't 6545f634d06SAneesh Kumar K.V * support fallocate for non extent files. So we can update 6555f634d06SAneesh Kumar K.V * reserve space here. 656d2a17637SMingming Cao */ 6575f634d06SAneesh Kumar K.V if ((retval > 0) && 6581296cc85SAneesh Kumar K.V (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)) 6595f634d06SAneesh Kumar K.V ext4_da_update_reserve_space(inode, retval, 1); 6605f634d06SAneesh Kumar K.V } 6615356f261SAditya Kali if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) { 662f2321097STheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED); 663d2a17637SMingming Cao 6645356f261SAditya Kali /* If we have successfully mapped the delayed allocated blocks, 6655356f261SAditya Kali * set the BH_Da_Mapped bit on them. Its important to do this 6665356f261SAditya Kali * under the protection of i_data_sem. 6675356f261SAditya Kali */ 668*51865fdaSZheng Liu if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 669*51865fdaSZheng Liu int ret; 6705356f261SAditya Kali set_buffers_da_mapped(inode, map); 671*51865fdaSZheng Liu delayed_mapped: 672*51865fdaSZheng Liu /* delayed allocation blocks has been allocated */ 673*51865fdaSZheng Liu ret = ext4_es_remove_extent(inode, map->m_lblk, 674*51865fdaSZheng Liu map->m_len); 675*51865fdaSZheng Liu if (ret < 0) 676*51865fdaSZheng Liu retval = ret; 677*51865fdaSZheng Liu } 6785356f261SAditya Kali } 6795356f261SAditya Kali 6800e855ac8SAneesh Kumar K.V up_write((&EXT4_I(inode)->i_data_sem)); 681e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 682e29136f8STheodore Ts'o int ret = check_block_validity(inode, map); 6836fd058f7STheodore Ts'o if (ret != 0) 6846fd058f7STheodore Ts'o return ret; 6856fd058f7STheodore Ts'o } 6860e855ac8SAneesh Kumar K.V return retval; 6870e855ac8SAneesh Kumar K.V } 6880e855ac8SAneesh Kumar K.V 689f3bd1f3fSMingming Cao /* Maximum number of blocks we map for direct IO at once. */ 690f3bd1f3fSMingming Cao #define DIO_MAX_BLOCKS 4096 691f3bd1f3fSMingming Cao 6922ed88685STheodore Ts'o static int _ext4_get_block(struct inode *inode, sector_t iblock, 6932ed88685STheodore Ts'o struct buffer_head *bh, int flags) 694ac27a0ecSDave Kleikamp { 6953e4fdaf8SDmitriy Monakhov handle_t *handle = ext4_journal_current_handle(); 6962ed88685STheodore Ts'o struct ext4_map_blocks map; 6977fb5409dSJan Kara int ret = 0, started = 0; 698f3bd1f3fSMingming Cao int dio_credits; 699ac27a0ecSDave Kleikamp 7002ed88685STheodore Ts'o map.m_lblk = iblock; 7012ed88685STheodore Ts'o map.m_len = bh->b_size >> inode->i_blkbits; 7022ed88685STheodore Ts'o 7038b0f165fSAnatol Pomozov if (flags && !(flags & EXT4_GET_BLOCKS_NO_LOCK) && !handle) { 7047fb5409dSJan Kara /* Direct IO write... */ 7052ed88685STheodore Ts'o if (map.m_len > DIO_MAX_BLOCKS) 7062ed88685STheodore Ts'o map.m_len = DIO_MAX_BLOCKS; 7072ed88685STheodore Ts'o dio_credits = ext4_chunk_trans_blocks(inode, map.m_len); 708f3bd1f3fSMingming Cao handle = ext4_journal_start(inode, dio_credits); 7097fb5409dSJan Kara if (IS_ERR(handle)) { 710ac27a0ecSDave Kleikamp ret = PTR_ERR(handle); 7112ed88685STheodore Ts'o return ret; 7127fb5409dSJan Kara } 7137fb5409dSJan Kara started = 1; 714ac27a0ecSDave Kleikamp } 715ac27a0ecSDave Kleikamp 7162ed88685STheodore Ts'o ret = ext4_map_blocks(handle, inode, &map, flags); 717ac27a0ecSDave Kleikamp if (ret > 0) { 7182ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 7192ed88685STheodore Ts'o bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; 7202ed88685STheodore Ts'o bh->b_size = inode->i_sb->s_blocksize * map.m_len; 721ac27a0ecSDave Kleikamp ret = 0; 722ac27a0ecSDave Kleikamp } 7237fb5409dSJan Kara if (started) 7247fb5409dSJan Kara ext4_journal_stop(handle); 725ac27a0ecSDave Kleikamp return ret; 726ac27a0ecSDave Kleikamp } 727ac27a0ecSDave Kleikamp 7282ed88685STheodore Ts'o int ext4_get_block(struct inode *inode, sector_t iblock, 7292ed88685STheodore Ts'o struct buffer_head *bh, int create) 7302ed88685STheodore Ts'o { 7312ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh, 7322ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 7332ed88685STheodore Ts'o } 7342ed88685STheodore Ts'o 735ac27a0ecSDave Kleikamp /* 736ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 737ac27a0ecSDave Kleikamp */ 738617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 739725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *errp) 740ac27a0ecSDave Kleikamp { 7412ed88685STheodore Ts'o struct ext4_map_blocks map; 7422ed88685STheodore Ts'o struct buffer_head *bh; 743ac27a0ecSDave Kleikamp int fatal = 0, err; 744ac27a0ecSDave Kleikamp 745ac27a0ecSDave Kleikamp J_ASSERT(handle != NULL || create == 0); 746ac27a0ecSDave Kleikamp 7472ed88685STheodore Ts'o map.m_lblk = block; 7482ed88685STheodore Ts'o map.m_len = 1; 7492ed88685STheodore Ts'o err = ext4_map_blocks(handle, inode, &map, 7502ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 7512ed88685STheodore Ts'o 75290b0a973SCarlos Maiolino /* ensure we send some value back into *errp */ 75390b0a973SCarlos Maiolino *errp = 0; 75490b0a973SCarlos Maiolino 7552ed88685STheodore Ts'o if (err < 0) 756ac27a0ecSDave Kleikamp *errp = err; 7572ed88685STheodore Ts'o if (err <= 0) 7582ed88685STheodore Ts'o return NULL; 7592ed88685STheodore Ts'o 7602ed88685STheodore Ts'o bh = sb_getblk(inode->i_sb, map.m_pblk); 761ac27a0ecSDave Kleikamp if (!bh) { 762ac27a0ecSDave Kleikamp *errp = -EIO; 7632ed88685STheodore Ts'o return NULL; 764ac27a0ecSDave Kleikamp } 7652ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 766ac27a0ecSDave Kleikamp J_ASSERT(create != 0); 767ac39849dSAneesh Kumar K.V J_ASSERT(handle != NULL); 768ac27a0ecSDave Kleikamp 769ac27a0ecSDave Kleikamp /* 770ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 771ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 772ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 773617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 774ac27a0ecSDave Kleikamp * problem. 775ac27a0ecSDave Kleikamp */ 776ac27a0ecSDave Kleikamp lock_buffer(bh); 777ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 778617ba13bSMingming Cao fatal = ext4_journal_get_create_access(handle, bh); 779ac27a0ecSDave Kleikamp if (!fatal && !buffer_uptodate(bh)) { 780ac27a0ecSDave Kleikamp memset(bh->b_data, 0, inode->i_sb->s_blocksize); 781ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 782ac27a0ecSDave Kleikamp } 783ac27a0ecSDave Kleikamp unlock_buffer(bh); 7840390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 7850390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 786ac27a0ecSDave Kleikamp if (!fatal) 787ac27a0ecSDave Kleikamp fatal = err; 788ac27a0ecSDave Kleikamp } else { 789ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "not a new buffer"); 790ac27a0ecSDave Kleikamp } 791ac27a0ecSDave Kleikamp if (fatal) { 792ac27a0ecSDave Kleikamp *errp = fatal; 793ac27a0ecSDave Kleikamp brelse(bh); 794ac27a0ecSDave Kleikamp bh = NULL; 795ac27a0ecSDave Kleikamp } 796ac27a0ecSDave Kleikamp return bh; 797ac27a0ecSDave Kleikamp } 798ac27a0ecSDave Kleikamp 799617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 800725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *err) 801ac27a0ecSDave Kleikamp { 802ac27a0ecSDave Kleikamp struct buffer_head *bh; 803ac27a0ecSDave Kleikamp 804617ba13bSMingming Cao bh = ext4_getblk(handle, inode, block, create, err); 805ac27a0ecSDave Kleikamp if (!bh) 806ac27a0ecSDave Kleikamp return bh; 807ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 808ac27a0ecSDave Kleikamp return bh; 80965299a3bSChristoph Hellwig ll_rw_block(READ | REQ_META | REQ_PRIO, 1, &bh); 810ac27a0ecSDave Kleikamp wait_on_buffer(bh); 811ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 812ac27a0ecSDave Kleikamp return bh; 813ac27a0ecSDave Kleikamp put_bh(bh); 814ac27a0ecSDave Kleikamp *err = -EIO; 815ac27a0ecSDave Kleikamp return NULL; 816ac27a0ecSDave Kleikamp } 817ac27a0ecSDave Kleikamp 818ac27a0ecSDave Kleikamp static int walk_page_buffers(handle_t *handle, 819ac27a0ecSDave Kleikamp struct buffer_head *head, 820ac27a0ecSDave Kleikamp unsigned from, 821ac27a0ecSDave Kleikamp unsigned to, 822ac27a0ecSDave Kleikamp int *partial, 823ac27a0ecSDave Kleikamp int (*fn)(handle_t *handle, 824ac27a0ecSDave Kleikamp struct buffer_head *bh)) 825ac27a0ecSDave Kleikamp { 826ac27a0ecSDave Kleikamp struct buffer_head *bh; 827ac27a0ecSDave Kleikamp unsigned block_start, block_end; 828ac27a0ecSDave Kleikamp unsigned blocksize = head->b_size; 829ac27a0ecSDave Kleikamp int err, ret = 0; 830ac27a0ecSDave Kleikamp struct buffer_head *next; 831ac27a0ecSDave Kleikamp 832ac27a0ecSDave Kleikamp for (bh = head, block_start = 0; 833ac27a0ecSDave Kleikamp ret == 0 && (bh != head || !block_start); 834de9a55b8STheodore Ts'o block_start = block_end, bh = next) { 835ac27a0ecSDave Kleikamp next = bh->b_this_page; 836ac27a0ecSDave Kleikamp block_end = block_start + blocksize; 837ac27a0ecSDave Kleikamp if (block_end <= from || block_start >= to) { 838ac27a0ecSDave Kleikamp if (partial && !buffer_uptodate(bh)) 839ac27a0ecSDave Kleikamp *partial = 1; 840ac27a0ecSDave Kleikamp continue; 841ac27a0ecSDave Kleikamp } 842ac27a0ecSDave Kleikamp err = (*fn)(handle, bh); 843ac27a0ecSDave Kleikamp if (!ret) 844ac27a0ecSDave Kleikamp ret = err; 845ac27a0ecSDave Kleikamp } 846ac27a0ecSDave Kleikamp return ret; 847ac27a0ecSDave Kleikamp } 848ac27a0ecSDave Kleikamp 849ac27a0ecSDave Kleikamp /* 850ac27a0ecSDave Kleikamp * To preserve ordering, it is essential that the hole instantiation and 851ac27a0ecSDave Kleikamp * the data write be encapsulated in a single transaction. We cannot 852617ba13bSMingming Cao * close off a transaction and start a new one between the ext4_get_block() 853dab291afSMingming Cao * and the commit_write(). So doing the jbd2_journal_start at the start of 854ac27a0ecSDave Kleikamp * prepare_write() is the right place. 855ac27a0ecSDave Kleikamp * 856617ba13bSMingming Cao * Also, this function can nest inside ext4_writepage() -> 857617ba13bSMingming Cao * block_write_full_page(). In that case, we *know* that ext4_writepage() 858ac27a0ecSDave Kleikamp * has generated enough buffer credits to do the whole page. So we won't 859ac27a0ecSDave Kleikamp * block on the journal in that case, which is good, because the caller may 860ac27a0ecSDave Kleikamp * be PF_MEMALLOC. 861ac27a0ecSDave Kleikamp * 862617ba13bSMingming Cao * By accident, ext4 can be reentered when a transaction is open via 863ac27a0ecSDave Kleikamp * quota file writes. If we were to commit the transaction while thus 864ac27a0ecSDave Kleikamp * reentered, there can be a deadlock - we would be holding a quota 865ac27a0ecSDave Kleikamp * lock, and the commit would never complete if another thread had a 866ac27a0ecSDave Kleikamp * transaction open and was blocking on the quota lock - a ranking 867ac27a0ecSDave Kleikamp * violation. 868ac27a0ecSDave Kleikamp * 869dab291afSMingming Cao * So what we do is to rely on the fact that jbd2_journal_stop/journal_start 870ac27a0ecSDave Kleikamp * will _not_ run commit under these circumstances because handle->h_ref 871ac27a0ecSDave Kleikamp * is elevated. We'll still have enough credits for the tiny quotafile 872ac27a0ecSDave Kleikamp * write. 873ac27a0ecSDave Kleikamp */ 874ac27a0ecSDave Kleikamp static int do_journal_get_write_access(handle_t *handle, 875ac27a0ecSDave Kleikamp struct buffer_head *bh) 876ac27a0ecSDave Kleikamp { 87756d35a4cSJan Kara int dirty = buffer_dirty(bh); 87856d35a4cSJan Kara int ret; 87956d35a4cSJan Kara 880ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 881ac27a0ecSDave Kleikamp return 0; 88256d35a4cSJan Kara /* 883ebdec241SChristoph Hellwig * __block_write_begin() could have dirtied some buffers. Clean 88456d35a4cSJan Kara * the dirty bit as jbd2_journal_get_write_access() could complain 88556d35a4cSJan Kara * otherwise about fs integrity issues. Setting of the dirty bit 886ebdec241SChristoph Hellwig * by __block_write_begin() isn't a real problem here as we clear 88756d35a4cSJan Kara * the bit before releasing a page lock and thus writeback cannot 88856d35a4cSJan Kara * ever write the buffer. 88956d35a4cSJan Kara */ 89056d35a4cSJan Kara if (dirty) 89156d35a4cSJan Kara clear_buffer_dirty(bh); 89256d35a4cSJan Kara ret = ext4_journal_get_write_access(handle, bh); 89356d35a4cSJan Kara if (!ret && dirty) 89456d35a4cSJan Kara ret = ext4_handle_dirty_metadata(handle, NULL, bh); 89556d35a4cSJan Kara return ret; 896ac27a0ecSDave Kleikamp } 897ac27a0ecSDave Kleikamp 898744692dcSJiaying Zhang static int ext4_get_block_write(struct inode *inode, sector_t iblock, 899744692dcSJiaying Zhang struct buffer_head *bh_result, int create); 9008b0f165fSAnatol Pomozov static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock, 9018b0f165fSAnatol Pomozov struct buffer_head *bh_result, int create); 902bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 903bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned flags, 904bfc1af65SNick Piggin struct page **pagep, void **fsdata) 905ac27a0ecSDave Kleikamp { 906bfc1af65SNick Piggin struct inode *inode = mapping->host; 9071938a150SAneesh Kumar K.V int ret, needed_blocks; 908ac27a0ecSDave Kleikamp handle_t *handle; 909ac27a0ecSDave Kleikamp int retries = 0; 910bfc1af65SNick Piggin struct page *page; 911bfc1af65SNick Piggin pgoff_t index; 912bfc1af65SNick Piggin unsigned from, to; 913bfc1af65SNick Piggin 9149bffad1eSTheodore Ts'o trace_ext4_write_begin(inode, pos, len, flags); 9151938a150SAneesh Kumar K.V /* 9161938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 9171938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 9181938a150SAneesh Kumar K.V */ 9191938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 920bfc1af65SNick Piggin index = pos >> PAGE_CACHE_SHIFT; 921bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 922bfc1af65SNick Piggin to = from + len; 923ac27a0ecSDave Kleikamp 924ac27a0ecSDave Kleikamp retry: 925617ba13bSMingming Cao handle = ext4_journal_start(inode, needed_blocks); 9267479d2b9SAndrew Morton if (IS_ERR(handle)) { 9277479d2b9SAndrew Morton ret = PTR_ERR(handle); 9287479d2b9SAndrew Morton goto out; 9297479d2b9SAndrew Morton } 930ac27a0ecSDave Kleikamp 931ebd3610bSJan Kara /* We cannot recurse into the filesystem as the transaction is already 932ebd3610bSJan Kara * started */ 933ebd3610bSJan Kara flags |= AOP_FLAG_NOFS; 934ebd3610bSJan Kara 93554566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 936cf108bcaSJan Kara if (!page) { 937cf108bcaSJan Kara ext4_journal_stop(handle); 938cf108bcaSJan Kara ret = -ENOMEM; 939cf108bcaSJan Kara goto out; 940cf108bcaSJan Kara } 941cf108bcaSJan Kara *pagep = page; 942cf108bcaSJan Kara 943744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 9446e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block_write); 945744692dcSJiaying Zhang else 9466e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block); 947bfc1af65SNick Piggin 948bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 949ac27a0ecSDave Kleikamp ret = walk_page_buffers(handle, page_buffers(page), 950ac27a0ecSDave Kleikamp from, to, NULL, do_journal_get_write_access); 951b46be050SAndrey Savochkin } 952bfc1af65SNick Piggin 953bfc1af65SNick Piggin if (ret) { 954bfc1af65SNick Piggin unlock_page(page); 955bfc1af65SNick Piggin page_cache_release(page); 956ae4d5372SAneesh Kumar K.V /* 9576e1db88dSChristoph Hellwig * __block_write_begin may have instantiated a few blocks 958ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 959ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 9601938a150SAneesh Kumar K.V * 9611938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 9621938a150SAneesh Kumar K.V * truncate finishes 963ae4d5372SAneesh Kumar K.V */ 964ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 9651938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 9661938a150SAneesh Kumar K.V 9671938a150SAneesh Kumar K.V ext4_journal_stop(handle); 9681938a150SAneesh Kumar K.V if (pos + len > inode->i_size) { 969b9a4207dSJan Kara ext4_truncate_failed_write(inode); 9701938a150SAneesh Kumar K.V /* 971ffacfa7aSJan Kara * If truncate failed early the inode might 9721938a150SAneesh Kumar K.V * still be on the orphan list; we need to 9731938a150SAneesh Kumar K.V * make sure the inode is removed from the 9741938a150SAneesh Kumar K.V * orphan list in that case. 9751938a150SAneesh Kumar K.V */ 9761938a150SAneesh Kumar K.V if (inode->i_nlink) 9771938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 9781938a150SAneesh Kumar K.V } 979bfc1af65SNick Piggin } 980bfc1af65SNick Piggin 981617ba13bSMingming Cao if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 982ac27a0ecSDave Kleikamp goto retry; 9837479d2b9SAndrew Morton out: 984ac27a0ecSDave Kleikamp return ret; 985ac27a0ecSDave Kleikamp } 986ac27a0ecSDave Kleikamp 987bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 988bfc1af65SNick Piggin static int write_end_fn(handle_t *handle, struct buffer_head *bh) 989ac27a0ecSDave Kleikamp { 990ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 991ac27a0ecSDave Kleikamp return 0; 992ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 9930390131bSFrank Mayhar return ext4_handle_dirty_metadata(handle, NULL, bh); 994ac27a0ecSDave Kleikamp } 995ac27a0ecSDave Kleikamp 996f8514083SAneesh Kumar K.V static int ext4_generic_write_end(struct file *file, 997f8514083SAneesh Kumar K.V struct address_space *mapping, 998f8514083SAneesh Kumar K.V loff_t pos, unsigned len, unsigned copied, 999f8514083SAneesh Kumar K.V struct page *page, void *fsdata) 1000f8514083SAneesh Kumar K.V { 1001f8514083SAneesh Kumar K.V int i_size_changed = 0; 1002f8514083SAneesh Kumar K.V struct inode *inode = mapping->host; 1003f8514083SAneesh Kumar K.V handle_t *handle = ext4_journal_current_handle(); 1004f8514083SAneesh Kumar K.V 1005f8514083SAneesh Kumar K.V copied = block_write_end(file, mapping, pos, len, copied, page, fsdata); 1006f8514083SAneesh Kumar K.V 1007f8514083SAneesh Kumar K.V /* 1008f8514083SAneesh Kumar K.V * No need to use i_size_read() here, the i_size 1009f8514083SAneesh Kumar K.V * cannot change under us because we hold i_mutex. 1010f8514083SAneesh Kumar K.V * 1011f8514083SAneesh Kumar K.V * But it's important to update i_size while still holding page lock: 1012f8514083SAneesh Kumar K.V * page writeout could otherwise come in and zero beyond i_size. 1013f8514083SAneesh Kumar K.V */ 1014f8514083SAneesh Kumar K.V if (pos + copied > inode->i_size) { 1015f8514083SAneesh Kumar K.V i_size_write(inode, pos + copied); 1016f8514083SAneesh Kumar K.V i_size_changed = 1; 1017f8514083SAneesh Kumar K.V } 1018f8514083SAneesh Kumar K.V 1019f8514083SAneesh Kumar K.V if (pos + copied > EXT4_I(inode)->i_disksize) { 1020f8514083SAneesh Kumar K.V /* We need to mark inode dirty even if 1021f8514083SAneesh Kumar K.V * new_i_size is less that inode->i_size 1022f8514083SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 1023f8514083SAneesh Kumar K.V */ 1024f8514083SAneesh Kumar K.V ext4_update_i_disksize(inode, (pos + copied)); 1025f8514083SAneesh Kumar K.V i_size_changed = 1; 1026f8514083SAneesh Kumar K.V } 1027f8514083SAneesh Kumar K.V unlock_page(page); 1028f8514083SAneesh Kumar K.V page_cache_release(page); 1029f8514083SAneesh Kumar K.V 1030f8514083SAneesh Kumar K.V /* 1031f8514083SAneesh Kumar K.V * Don't mark the inode dirty under page lock. First, it unnecessarily 1032f8514083SAneesh Kumar K.V * makes the holding time of page lock longer. Second, it forces lock 1033f8514083SAneesh Kumar K.V * ordering of page lock and transaction start for journaling 1034f8514083SAneesh Kumar K.V * filesystems. 1035f8514083SAneesh Kumar K.V */ 1036f8514083SAneesh Kumar K.V if (i_size_changed) 1037f8514083SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 1038f8514083SAneesh Kumar K.V 1039f8514083SAneesh Kumar K.V return copied; 1040f8514083SAneesh Kumar K.V } 1041f8514083SAneesh Kumar K.V 1042ac27a0ecSDave Kleikamp /* 1043ac27a0ecSDave Kleikamp * We need to pick up the new inode size which generic_commit_write gave us 1044ac27a0ecSDave Kleikamp * `file' can be NULL - eg, when called from page_symlink(). 1045ac27a0ecSDave Kleikamp * 1046617ba13bSMingming Cao * ext4 never places buffers on inode->i_mapping->private_list. metadata 1047ac27a0ecSDave Kleikamp * buffers are managed internally. 1048ac27a0ecSDave Kleikamp */ 1049bfc1af65SNick Piggin static int ext4_ordered_write_end(struct file *file, 1050bfc1af65SNick Piggin struct address_space *mapping, 1051bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1052bfc1af65SNick Piggin struct page *page, void *fsdata) 1053ac27a0ecSDave Kleikamp { 1054617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1055cf108bcaSJan Kara struct inode *inode = mapping->host; 1056ac27a0ecSDave Kleikamp int ret = 0, ret2; 1057ac27a0ecSDave Kleikamp 10589bffad1eSTheodore Ts'o trace_ext4_ordered_write_end(inode, pos, len, copied); 1059678aaf48SJan Kara ret = ext4_jbd2_file_inode(handle, inode); 1060ac27a0ecSDave Kleikamp 1061ac27a0ecSDave Kleikamp if (ret == 0) { 1062f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 1063bfc1af65SNick Piggin page, fsdata); 1064f8a87d89SRoel Kluin copied = ret2; 1065ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1066f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1067f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1068f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1069f8514083SAneesh Kumar K.V */ 1070f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1071f8a87d89SRoel Kluin if (ret2 < 0) 1072f8a87d89SRoel Kluin ret = ret2; 107309e0834fSAkira Fujita } else { 107409e0834fSAkira Fujita unlock_page(page); 107509e0834fSAkira Fujita page_cache_release(page); 1076ac27a0ecSDave Kleikamp } 107709e0834fSAkira Fujita 1078617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1079ac27a0ecSDave Kleikamp if (!ret) 1080ac27a0ecSDave Kleikamp ret = ret2; 1081bfc1af65SNick Piggin 1082f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1083b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1084f8514083SAneesh Kumar K.V /* 1085ffacfa7aSJan Kara * If truncate failed early the inode might still be 1086f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1087f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1088f8514083SAneesh Kumar K.V */ 1089f8514083SAneesh Kumar K.V if (inode->i_nlink) 1090f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1091f8514083SAneesh Kumar K.V } 1092f8514083SAneesh Kumar K.V 1093f8514083SAneesh Kumar K.V 1094bfc1af65SNick Piggin return ret ? ret : copied; 1095ac27a0ecSDave Kleikamp } 1096ac27a0ecSDave Kleikamp 1097bfc1af65SNick Piggin static int ext4_writeback_write_end(struct file *file, 1098bfc1af65SNick Piggin struct address_space *mapping, 1099bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1100bfc1af65SNick Piggin struct page *page, void *fsdata) 1101ac27a0ecSDave Kleikamp { 1102617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1103cf108bcaSJan Kara struct inode *inode = mapping->host; 1104ac27a0ecSDave Kleikamp int ret = 0, ret2; 1105ac27a0ecSDave Kleikamp 11069bffad1eSTheodore Ts'o trace_ext4_writeback_write_end(inode, pos, len, copied); 1107f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 1108bfc1af65SNick Piggin page, fsdata); 1109f8a87d89SRoel Kluin copied = ret2; 1110ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1111f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1112f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1113f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1114f8514083SAneesh Kumar K.V */ 1115f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1116f8514083SAneesh Kumar K.V 1117f8a87d89SRoel Kluin if (ret2 < 0) 1118f8a87d89SRoel Kluin ret = ret2; 1119ac27a0ecSDave Kleikamp 1120617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1121ac27a0ecSDave Kleikamp if (!ret) 1122ac27a0ecSDave Kleikamp ret = ret2; 1123bfc1af65SNick Piggin 1124f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1125b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1126f8514083SAneesh Kumar K.V /* 1127ffacfa7aSJan Kara * If truncate failed early the inode might still be 1128f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1129f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1130f8514083SAneesh Kumar K.V */ 1131f8514083SAneesh Kumar K.V if (inode->i_nlink) 1132f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1133f8514083SAneesh Kumar K.V } 1134f8514083SAneesh Kumar K.V 1135bfc1af65SNick Piggin return ret ? ret : copied; 1136ac27a0ecSDave Kleikamp } 1137ac27a0ecSDave Kleikamp 1138bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 1139bfc1af65SNick Piggin struct address_space *mapping, 1140bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1141bfc1af65SNick Piggin struct page *page, void *fsdata) 1142ac27a0ecSDave Kleikamp { 1143617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1144bfc1af65SNick Piggin struct inode *inode = mapping->host; 1145ac27a0ecSDave Kleikamp int ret = 0, ret2; 1146ac27a0ecSDave Kleikamp int partial = 0; 1147bfc1af65SNick Piggin unsigned from, to; 1148cf17fea6SAneesh Kumar K.V loff_t new_i_size; 1149ac27a0ecSDave Kleikamp 11509bffad1eSTheodore Ts'o trace_ext4_journalled_write_end(inode, pos, len, copied); 1151bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 1152bfc1af65SNick Piggin to = from + len; 1153bfc1af65SNick Piggin 1154441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1155441c8508SCurt Wohlgemuth 1156bfc1af65SNick Piggin if (copied < len) { 1157bfc1af65SNick Piggin if (!PageUptodate(page)) 1158bfc1af65SNick Piggin copied = 0; 1159bfc1af65SNick Piggin page_zero_new_buffers(page, from+copied, to); 1160bfc1af65SNick Piggin } 1161ac27a0ecSDave Kleikamp 1162ac27a0ecSDave Kleikamp ret = walk_page_buffers(handle, page_buffers(page), from, 1163bfc1af65SNick Piggin to, &partial, write_end_fn); 1164ac27a0ecSDave Kleikamp if (!partial) 1165ac27a0ecSDave Kleikamp SetPageUptodate(page); 1166cf17fea6SAneesh Kumar K.V new_i_size = pos + copied; 1167cf17fea6SAneesh Kumar K.V if (new_i_size > inode->i_size) 1168bfc1af65SNick Piggin i_size_write(inode, pos+copied); 116919f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 11702d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 1171cf17fea6SAneesh Kumar K.V if (new_i_size > EXT4_I(inode)->i_disksize) { 1172cf17fea6SAneesh Kumar K.V ext4_update_i_disksize(inode, new_i_size); 1173617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1174ac27a0ecSDave Kleikamp if (!ret) 1175ac27a0ecSDave Kleikamp ret = ret2; 1176ac27a0ecSDave Kleikamp } 1177bfc1af65SNick Piggin 1178cf108bcaSJan Kara unlock_page(page); 1179f8514083SAneesh Kumar K.V page_cache_release(page); 1180ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1181f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1182f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1183f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1184f8514083SAneesh Kumar K.V */ 1185f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1186f8514083SAneesh Kumar K.V 1187617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1188ac27a0ecSDave Kleikamp if (!ret) 1189ac27a0ecSDave Kleikamp ret = ret2; 1190f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1191b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1192f8514083SAneesh Kumar K.V /* 1193ffacfa7aSJan Kara * If truncate failed early the inode might still be 1194f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1195f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1196f8514083SAneesh Kumar K.V */ 1197f8514083SAneesh Kumar K.V if (inode->i_nlink) 1198f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1199f8514083SAneesh Kumar K.V } 1200bfc1af65SNick Piggin 1201bfc1af65SNick Piggin return ret ? ret : copied; 1202ac27a0ecSDave Kleikamp } 1203d2a17637SMingming Cao 12049d0be502STheodore Ts'o /* 12057b415bf6SAditya Kali * Reserve a single cluster located at lblock 12069d0be502STheodore Ts'o */ 120701f49d0bSTheodore Ts'o static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock) 1208d2a17637SMingming Cao { 1209030ba6bcSAneesh Kumar K.V int retries = 0; 1210d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 12110637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 12127b415bf6SAditya Kali unsigned int md_needed; 12135dd4056dSChristoph Hellwig int ret; 121403179fe9STheodore Ts'o ext4_lblk_t save_last_lblock; 121503179fe9STheodore Ts'o int save_len; 1216d2a17637SMingming Cao 121760e58e0fSMingming Cao /* 121872b8ab9dSEric Sandeen * We will charge metadata quota at writeout time; this saves 121972b8ab9dSEric Sandeen * us from metadata over-estimation, though we may go over by 122072b8ab9dSEric Sandeen * a small amount in the end. Here we just reserve for data. 122160e58e0fSMingming Cao */ 12227b415bf6SAditya Kali ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1)); 12235dd4056dSChristoph Hellwig if (ret) 12245dd4056dSChristoph Hellwig return ret; 122503179fe9STheodore Ts'o 122603179fe9STheodore Ts'o /* 122703179fe9STheodore Ts'o * recalculate the amount of metadata blocks to reserve 122803179fe9STheodore Ts'o * in order to allocate nrblocks 122903179fe9STheodore Ts'o * worse case is one extent per block 123003179fe9STheodore Ts'o */ 123103179fe9STheodore Ts'o repeat: 123203179fe9STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 123303179fe9STheodore Ts'o /* 123403179fe9STheodore Ts'o * ext4_calc_metadata_amount() has side effects, which we have 123503179fe9STheodore Ts'o * to be prepared undo if we fail to claim space. 123603179fe9STheodore Ts'o */ 123703179fe9STheodore Ts'o save_len = ei->i_da_metadata_calc_len; 123803179fe9STheodore Ts'o save_last_lblock = ei->i_da_metadata_calc_last_lblock; 123903179fe9STheodore Ts'o md_needed = EXT4_NUM_B2C(sbi, 124003179fe9STheodore Ts'o ext4_calc_metadata_amount(inode, lblock)); 124103179fe9STheodore Ts'o trace_ext4_da_reserve_space(inode, md_needed); 124203179fe9STheodore Ts'o 124372b8ab9dSEric Sandeen /* 124472b8ab9dSEric Sandeen * We do still charge estimated metadata to the sb though; 124572b8ab9dSEric Sandeen * we cannot afford to run out of free blocks. 124672b8ab9dSEric Sandeen */ 1247e7d5f315STheodore Ts'o if (ext4_claim_free_clusters(sbi, md_needed + 1, 0)) { 124803179fe9STheodore Ts'o ei->i_da_metadata_calc_len = save_len; 124903179fe9STheodore Ts'o ei->i_da_metadata_calc_last_lblock = save_last_lblock; 125003179fe9STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 1251030ba6bcSAneesh Kumar K.V if (ext4_should_retry_alloc(inode->i_sb, &retries)) { 1252030ba6bcSAneesh Kumar K.V yield(); 1253030ba6bcSAneesh Kumar K.V goto repeat; 1254030ba6bcSAneesh Kumar K.V } 125503179fe9STheodore Ts'o dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1)); 1256d2a17637SMingming Cao return -ENOSPC; 1257d2a17637SMingming Cao } 12589d0be502STheodore Ts'o ei->i_reserved_data_blocks++; 12590637c6f4STheodore Ts'o ei->i_reserved_meta_blocks += md_needed; 12600637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 126139bc680aSDmitry Monakhov 1262d2a17637SMingming Cao return 0; /* success */ 1263d2a17637SMingming Cao } 1264d2a17637SMingming Cao 126512219aeaSAneesh Kumar K.V static void ext4_da_release_space(struct inode *inode, int to_free) 1266d2a17637SMingming Cao { 1267d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 12680637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 1269d2a17637SMingming Cao 1270cd213226SMingming Cao if (!to_free) 1271cd213226SMingming Cao return; /* Nothing to release, exit */ 1272cd213226SMingming Cao 1273d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1274cd213226SMingming Cao 12755a58ec87SLi Zefan trace_ext4_da_release_space(inode, to_free); 12760637c6f4STheodore Ts'o if (unlikely(to_free > ei->i_reserved_data_blocks)) { 1277cd213226SMingming Cao /* 12780637c6f4STheodore Ts'o * if there aren't enough reserved blocks, then the 12790637c6f4STheodore Ts'o * counter is messed up somewhere. Since this 12800637c6f4STheodore Ts'o * function is called from invalidate page, it's 12810637c6f4STheodore Ts'o * harmless to return without any action. 1282cd213226SMingming Cao */ 12830637c6f4STheodore Ts'o ext4_msg(inode->i_sb, KERN_NOTICE, "ext4_da_release_space: " 12840637c6f4STheodore Ts'o "ino %lu, to_free %d with only %d reserved " 12851084f252STheodore Ts'o "data blocks", inode->i_ino, to_free, 12860637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 12870637c6f4STheodore Ts'o WARN_ON(1); 12880637c6f4STheodore Ts'o to_free = ei->i_reserved_data_blocks; 12890637c6f4STheodore Ts'o } 12900637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= to_free; 12910637c6f4STheodore Ts'o 12920637c6f4STheodore Ts'o if (ei->i_reserved_data_blocks == 0) { 12930637c6f4STheodore Ts'o /* 12940637c6f4STheodore Ts'o * We can release all of the reserved metadata blocks 12950637c6f4STheodore Ts'o * only when we have written all of the delayed 12960637c6f4STheodore Ts'o * allocation blocks. 12977b415bf6SAditya Kali * Note that in case of bigalloc, i_reserved_meta_blocks, 12987b415bf6SAditya Kali * i_reserved_data_blocks, etc. refer to number of clusters. 12990637c6f4STheodore Ts'o */ 130057042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 130172b8ab9dSEric Sandeen ei->i_reserved_meta_blocks); 1302ee5f4d9cSTheodore Ts'o ei->i_reserved_meta_blocks = 0; 13039d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 1304cd213226SMingming Cao } 1305cd213226SMingming Cao 130672b8ab9dSEric Sandeen /* update fs dirty data blocks counter */ 130757042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free); 1308d2a17637SMingming Cao 1309d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 131060e58e0fSMingming Cao 13117b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free)); 1312d2a17637SMingming Cao } 1313d2a17637SMingming Cao 1314d2a17637SMingming Cao static void ext4_da_page_release_reservation(struct page *page, 1315d2a17637SMingming Cao unsigned long offset) 1316d2a17637SMingming Cao { 1317d2a17637SMingming Cao int to_release = 0; 1318d2a17637SMingming Cao struct buffer_head *head, *bh; 1319d2a17637SMingming Cao unsigned int curr_off = 0; 13207b415bf6SAditya Kali struct inode *inode = page->mapping->host; 13217b415bf6SAditya Kali struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 13227b415bf6SAditya Kali int num_clusters; 1323*51865fdaSZheng Liu ext4_fsblk_t lblk; 1324d2a17637SMingming Cao 1325d2a17637SMingming Cao head = page_buffers(page); 1326d2a17637SMingming Cao bh = head; 1327d2a17637SMingming Cao do { 1328d2a17637SMingming Cao unsigned int next_off = curr_off + bh->b_size; 1329d2a17637SMingming Cao 1330d2a17637SMingming Cao if ((offset <= curr_off) && (buffer_delay(bh))) { 1331d2a17637SMingming Cao to_release++; 1332d2a17637SMingming Cao clear_buffer_delay(bh); 13335356f261SAditya Kali clear_buffer_da_mapped(bh); 1334d2a17637SMingming Cao } 1335d2a17637SMingming Cao curr_off = next_off; 1336d2a17637SMingming Cao } while ((bh = bh->b_this_page) != head); 13377b415bf6SAditya Kali 1338*51865fdaSZheng Liu if (to_release) { 1339*51865fdaSZheng Liu lblk = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits); 1340*51865fdaSZheng Liu ext4_es_remove_extent(inode, lblk, to_release); 1341*51865fdaSZheng Liu } 1342*51865fdaSZheng Liu 13437b415bf6SAditya Kali /* If we have released all the blocks belonging to a cluster, then we 13447b415bf6SAditya Kali * need to release the reserved space for that cluster. */ 13457b415bf6SAditya Kali num_clusters = EXT4_NUM_B2C(sbi, to_release); 13467b415bf6SAditya Kali while (num_clusters > 0) { 13477b415bf6SAditya Kali lblk = (page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits)) + 13487b415bf6SAditya Kali ((num_clusters - 1) << sbi->s_cluster_bits); 13497b415bf6SAditya Kali if (sbi->s_cluster_ratio == 1 || 13507b415bf6SAditya Kali !ext4_find_delalloc_cluster(inode, lblk, 1)) 13517b415bf6SAditya Kali ext4_da_release_space(inode, 1); 13527b415bf6SAditya Kali 13537b415bf6SAditya Kali num_clusters--; 13547b415bf6SAditya Kali } 1355d2a17637SMingming Cao } 1356ac27a0ecSDave Kleikamp 1357ac27a0ecSDave Kleikamp /* 135864769240SAlex Tomas * Delayed allocation stuff 135964769240SAlex Tomas */ 136064769240SAlex Tomas 136164769240SAlex Tomas /* 136264769240SAlex Tomas * mpage_da_submit_io - walks through extent of pages and try to write 1363a1d6cc56SAneesh Kumar K.V * them with writepage() call back 136464769240SAlex Tomas * 136564769240SAlex Tomas * @mpd->inode: inode 136664769240SAlex Tomas * @mpd->first_page: first page of the extent 136764769240SAlex Tomas * @mpd->next_page: page after the last page of the extent 136864769240SAlex Tomas * 136964769240SAlex Tomas * By the time mpage_da_submit_io() is called we expect all blocks 137064769240SAlex Tomas * to be allocated. this may be wrong if allocation failed. 137164769240SAlex Tomas * 137264769240SAlex Tomas * As pages are already locked by write_cache_pages(), we can't use it 137364769240SAlex Tomas */ 13741de3e3dfSTheodore Ts'o static int mpage_da_submit_io(struct mpage_da_data *mpd, 13751de3e3dfSTheodore Ts'o struct ext4_map_blocks *map) 137664769240SAlex Tomas { 1377791b7f08SAneesh Kumar K.V struct pagevec pvec; 1378791b7f08SAneesh Kumar K.V unsigned long index, end; 1379791b7f08SAneesh Kumar K.V int ret = 0, err, nr_pages, i; 1380791b7f08SAneesh Kumar K.V struct inode *inode = mpd->inode; 1381791b7f08SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 1382cb20d518STheodore Ts'o loff_t size = i_size_read(inode); 13833ecdb3a1STheodore Ts'o unsigned int len, block_start; 13843ecdb3a1STheodore Ts'o struct buffer_head *bh, *page_bufs = NULL; 1385cb20d518STheodore Ts'o int journal_data = ext4_should_journal_data(inode); 13861de3e3dfSTheodore Ts'o sector_t pblock = 0, cur_logical = 0; 1387bd2d0210STheodore Ts'o struct ext4_io_submit io_submit; 138864769240SAlex Tomas 138964769240SAlex Tomas BUG_ON(mpd->next_page <= mpd->first_page); 1390bd2d0210STheodore Ts'o memset(&io_submit, 0, sizeof(io_submit)); 1391791b7f08SAneesh Kumar K.V /* 1392791b7f08SAneesh Kumar K.V * We need to start from the first_page to the next_page - 1 1393791b7f08SAneesh Kumar K.V * to make sure we also write the mapped dirty buffer_heads. 13948dc207c0STheodore Ts'o * If we look at mpd->b_blocknr we would only be looking 1395791b7f08SAneesh Kumar K.V * at the currently mapped buffer_heads. 1396791b7f08SAneesh Kumar K.V */ 139764769240SAlex Tomas index = mpd->first_page; 139864769240SAlex Tomas end = mpd->next_page - 1; 139964769240SAlex Tomas 1400791b7f08SAneesh Kumar K.V pagevec_init(&pvec, 0); 140164769240SAlex Tomas while (index <= end) { 1402791b7f08SAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 140364769240SAlex Tomas if (nr_pages == 0) 140464769240SAlex Tomas break; 140564769240SAlex Tomas for (i = 0; i < nr_pages; i++) { 140697498956STheodore Ts'o int commit_write = 0, skip_page = 0; 140764769240SAlex Tomas struct page *page = pvec.pages[i]; 140864769240SAlex Tomas 1409791b7f08SAneesh Kumar K.V index = page->index; 1410791b7f08SAneesh Kumar K.V if (index > end) 1411791b7f08SAneesh Kumar K.V break; 1412cb20d518STheodore Ts'o 1413cb20d518STheodore Ts'o if (index == size >> PAGE_CACHE_SHIFT) 1414cb20d518STheodore Ts'o len = size & ~PAGE_CACHE_MASK; 1415cb20d518STheodore Ts'o else 1416cb20d518STheodore Ts'o len = PAGE_CACHE_SIZE; 14171de3e3dfSTheodore Ts'o if (map) { 14181de3e3dfSTheodore Ts'o cur_logical = index << (PAGE_CACHE_SHIFT - 14191de3e3dfSTheodore Ts'o inode->i_blkbits); 14201de3e3dfSTheodore Ts'o pblock = map->m_pblk + (cur_logical - 14211de3e3dfSTheodore Ts'o map->m_lblk); 14221de3e3dfSTheodore Ts'o } 1423791b7f08SAneesh Kumar K.V index++; 1424791b7f08SAneesh Kumar K.V 1425791b7f08SAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1426791b7f08SAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1427791b7f08SAneesh Kumar K.V 142822208dedSAneesh Kumar K.V /* 1429cb20d518STheodore Ts'o * If the page does not have buffers (for 1430cb20d518STheodore Ts'o * whatever reason), try to create them using 1431a107e5a3STheodore Ts'o * __block_write_begin. If this fails, 143297498956STheodore Ts'o * skip the page and move on. 143322208dedSAneesh Kumar K.V */ 1434cb20d518STheodore Ts'o if (!page_has_buffers(page)) { 1435a107e5a3STheodore Ts'o if (__block_write_begin(page, 0, len, 1436cb20d518STheodore Ts'o noalloc_get_block_write)) { 143797498956STheodore Ts'o skip_page: 1438cb20d518STheodore Ts'o unlock_page(page); 1439cb20d518STheodore Ts'o continue; 1440cb20d518STheodore Ts'o } 1441cb20d518STheodore Ts'o commit_write = 1; 1442cb20d518STheodore Ts'o } 14433ecdb3a1STheodore Ts'o 14443ecdb3a1STheodore Ts'o bh = page_bufs = page_buffers(page); 14453ecdb3a1STheodore Ts'o block_start = 0; 14463ecdb3a1STheodore Ts'o do { 14471de3e3dfSTheodore Ts'o if (!bh) 144897498956STheodore Ts'o goto skip_page; 14491de3e3dfSTheodore Ts'o if (map && (cur_logical >= map->m_lblk) && 14501de3e3dfSTheodore Ts'o (cur_logical <= (map->m_lblk + 14511de3e3dfSTheodore Ts'o (map->m_len - 1)))) { 14521de3e3dfSTheodore Ts'o if (buffer_delay(bh)) { 14531de3e3dfSTheodore Ts'o clear_buffer_delay(bh); 14541de3e3dfSTheodore Ts'o bh->b_blocknr = pblock; 14551de3e3dfSTheodore Ts'o } 14565356f261SAditya Kali if (buffer_da_mapped(bh)) 14575356f261SAditya Kali clear_buffer_da_mapped(bh); 14581de3e3dfSTheodore Ts'o if (buffer_unwritten(bh) || 14591de3e3dfSTheodore Ts'o buffer_mapped(bh)) 14601de3e3dfSTheodore Ts'o BUG_ON(bh->b_blocknr != pblock); 14611de3e3dfSTheodore Ts'o if (map->m_flags & EXT4_MAP_UNINIT) 14621de3e3dfSTheodore Ts'o set_buffer_uninit(bh); 14631de3e3dfSTheodore Ts'o clear_buffer_unwritten(bh); 14641de3e3dfSTheodore Ts'o } 14651de3e3dfSTheodore Ts'o 146613a79a47SYongqiang Yang /* 146713a79a47SYongqiang Yang * skip page if block allocation undone and 146813a79a47SYongqiang Yang * block is dirty 146913a79a47SYongqiang Yang */ 147013a79a47SYongqiang Yang if (ext4_bh_delay_or_unwritten(NULL, bh)) 147197498956STheodore Ts'o skip_page = 1; 14723ecdb3a1STheodore Ts'o bh = bh->b_this_page; 14733ecdb3a1STheodore Ts'o block_start += bh->b_size; 14741de3e3dfSTheodore Ts'o cur_logical++; 14751de3e3dfSTheodore Ts'o pblock++; 14761de3e3dfSTheodore Ts'o } while (bh != page_bufs); 14771de3e3dfSTheodore Ts'o 147897498956STheodore Ts'o if (skip_page) 147997498956STheodore Ts'o goto skip_page; 1480cb20d518STheodore Ts'o 1481cb20d518STheodore Ts'o if (commit_write) 1482cb20d518STheodore Ts'o /* mark the buffer_heads as dirty & uptodate */ 1483cb20d518STheodore Ts'o block_commit_write(page, 0, len); 1484cb20d518STheodore Ts'o 148597498956STheodore Ts'o clear_page_dirty_for_io(page); 1486bd2d0210STheodore Ts'o /* 1487bd2d0210STheodore Ts'o * Delalloc doesn't support data journalling, 1488bd2d0210STheodore Ts'o * but eventually maybe we'll lift this 1489bd2d0210STheodore Ts'o * restriction. 1490bd2d0210STheodore Ts'o */ 1491bd2d0210STheodore Ts'o if (unlikely(journal_data && PageChecked(page))) 1492cb20d518STheodore Ts'o err = __ext4_journalled_writepage(page, len); 14931449032bSTheodore Ts'o else if (test_opt(inode->i_sb, MBLK_IO_SUBMIT)) 1494bd2d0210STheodore Ts'o err = ext4_bio_write_page(&io_submit, page, 1495bd2d0210STheodore Ts'o len, mpd->wbc); 14969dd75f1fSTheodore Ts'o else if (buffer_uninit(page_bufs)) { 14979dd75f1fSTheodore Ts'o ext4_set_bh_endio(page_bufs, inode); 14989dd75f1fSTheodore Ts'o err = block_write_full_page_endio(page, 14999dd75f1fSTheodore Ts'o noalloc_get_block_write, 15009dd75f1fSTheodore Ts'o mpd->wbc, ext4_end_io_buffer_write); 15019dd75f1fSTheodore Ts'o } else 15021449032bSTheodore Ts'o err = block_write_full_page(page, 15031449032bSTheodore Ts'o noalloc_get_block_write, mpd->wbc); 1504cb20d518STheodore Ts'o 1505cb20d518STheodore Ts'o if (!err) 1506a1d6cc56SAneesh Kumar K.V mpd->pages_written++; 150764769240SAlex Tomas /* 150864769240SAlex Tomas * In error case, we have to continue because 150964769240SAlex Tomas * remaining pages are still locked 151064769240SAlex Tomas */ 151164769240SAlex Tomas if (ret == 0) 151264769240SAlex Tomas ret = err; 151364769240SAlex Tomas } 151464769240SAlex Tomas pagevec_release(&pvec); 151564769240SAlex Tomas } 1516bd2d0210STheodore Ts'o ext4_io_submit(&io_submit); 151764769240SAlex Tomas return ret; 151864769240SAlex Tomas } 151964769240SAlex Tomas 1520c7f5938aSCurt Wohlgemuth static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd) 1521c4a0c46eSAneesh Kumar K.V { 1522c4a0c46eSAneesh Kumar K.V int nr_pages, i; 1523c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 1524c4a0c46eSAneesh Kumar K.V struct pagevec pvec; 1525c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 1526c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 1527*51865fdaSZheng Liu ext4_lblk_t start, last; 1528c4a0c46eSAneesh Kumar K.V 1529c7f5938aSCurt Wohlgemuth index = mpd->first_page; 1530c7f5938aSCurt Wohlgemuth end = mpd->next_page - 1; 1531*51865fdaSZheng Liu 1532*51865fdaSZheng Liu start = index << (PAGE_CACHE_SHIFT - inode->i_blkbits); 1533*51865fdaSZheng Liu last = end << (PAGE_CACHE_SHIFT - inode->i_blkbits); 1534*51865fdaSZheng Liu ext4_es_remove_extent(inode, start, last - start + 1); 1535*51865fdaSZheng Liu 1536c4a0c46eSAneesh Kumar K.V while (index <= end) { 1537c4a0c46eSAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 1538c4a0c46eSAneesh Kumar K.V if (nr_pages == 0) 1539c4a0c46eSAneesh Kumar K.V break; 1540c4a0c46eSAneesh Kumar K.V for (i = 0; i < nr_pages; i++) { 1541c4a0c46eSAneesh Kumar K.V struct page *page = pvec.pages[i]; 15429b1d0998SJan Kara if (page->index > end) 1543c4a0c46eSAneesh Kumar K.V break; 1544c4a0c46eSAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1545c4a0c46eSAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1546c4a0c46eSAneesh Kumar K.V block_invalidatepage(page, 0); 1547c4a0c46eSAneesh Kumar K.V ClearPageUptodate(page); 1548c4a0c46eSAneesh Kumar K.V unlock_page(page); 1549c4a0c46eSAneesh Kumar K.V } 15509b1d0998SJan Kara index = pvec.pages[nr_pages - 1]->index + 1; 15519b1d0998SJan Kara pagevec_release(&pvec); 1552c4a0c46eSAneesh Kumar K.V } 1553c4a0c46eSAneesh Kumar K.V return; 1554c4a0c46eSAneesh Kumar K.V } 1555c4a0c46eSAneesh Kumar K.V 1556df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 1557df22291fSAneesh Kumar K.V { 1558df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 155992b97816STheodore Ts'o struct super_block *sb = inode->i_sb; 156092b97816STheodore Ts'o 156192b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld", 15625dee5437STheodore Ts'o EXT4_C2B(EXT4_SB(inode->i_sb), 15635dee5437STheodore Ts'o ext4_count_free_clusters(inode->i_sb))); 156492b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Free/Dirty block details"); 156592b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "free_blocks=%lld", 156657042651STheodore Ts'o (long long) EXT4_C2B(EXT4_SB(inode->i_sb), 156757042651STheodore Ts'o percpu_counter_sum(&sbi->s_freeclusters_counter))); 156892b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld", 15697b415bf6SAditya Kali (long long) EXT4_C2B(EXT4_SB(inode->i_sb), 15707b415bf6SAditya Kali percpu_counter_sum(&sbi->s_dirtyclusters_counter))); 157192b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Block reservation details"); 157292b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u", 1573df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_data_blocks); 157492b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_meta_blocks=%u", 1575df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_meta_blocks); 1576df22291fSAneesh Kumar K.V return; 1577df22291fSAneesh Kumar K.V } 1578df22291fSAneesh Kumar K.V 1579b920c755STheodore Ts'o /* 15805a87b7a5STheodore Ts'o * mpage_da_map_and_submit - go through given space, map them 15815a87b7a5STheodore Ts'o * if necessary, and then submit them for I/O 158264769240SAlex Tomas * 15838dc207c0STheodore Ts'o * @mpd - bh describing space 158464769240SAlex Tomas * 158564769240SAlex Tomas * The function skips space we know is already mapped to disk blocks. 158664769240SAlex Tomas * 158764769240SAlex Tomas */ 15885a87b7a5STheodore Ts'o static void mpage_da_map_and_submit(struct mpage_da_data *mpd) 158964769240SAlex Tomas { 15902ac3b6e0STheodore Ts'o int err, blks, get_blocks_flags; 15911de3e3dfSTheodore Ts'o struct ext4_map_blocks map, *mapp = NULL; 15922fa3cdfbSTheodore Ts'o sector_t next = mpd->b_blocknr; 15932fa3cdfbSTheodore Ts'o unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits; 15942fa3cdfbSTheodore Ts'o loff_t disksize = EXT4_I(mpd->inode)->i_disksize; 15952fa3cdfbSTheodore Ts'o handle_t *handle = NULL; 159664769240SAlex Tomas 159764769240SAlex Tomas /* 15985a87b7a5STheodore Ts'o * If the blocks are mapped already, or we couldn't accumulate 15995a87b7a5STheodore Ts'o * any blocks, then proceed immediately to the submission stage. 160064769240SAlex Tomas */ 16015a87b7a5STheodore Ts'o if ((mpd->b_size == 0) || 16025a87b7a5STheodore Ts'o ((mpd->b_state & (1 << BH_Mapped)) && 160329fa89d0SAneesh Kumar K.V !(mpd->b_state & (1 << BH_Delay)) && 16045a87b7a5STheodore Ts'o !(mpd->b_state & (1 << BH_Unwritten)))) 16055a87b7a5STheodore Ts'o goto submit_io; 16062fa3cdfbSTheodore Ts'o 16072fa3cdfbSTheodore Ts'o handle = ext4_journal_current_handle(); 16082fa3cdfbSTheodore Ts'o BUG_ON(!handle); 16092fa3cdfbSTheodore Ts'o 161079ffab34SAneesh Kumar K.V /* 161179e83036SEric Sandeen * Call ext4_map_blocks() to allocate any delayed allocation 16122ac3b6e0STheodore Ts'o * blocks, or to convert an uninitialized extent to be 16132ac3b6e0STheodore Ts'o * initialized (in the case where we have written into 16142ac3b6e0STheodore Ts'o * one or more preallocated blocks). 16152ac3b6e0STheodore Ts'o * 16162ac3b6e0STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to 16172ac3b6e0STheodore Ts'o * indicate that we are on the delayed allocation path. This 16182ac3b6e0STheodore Ts'o * affects functions in many different parts of the allocation 16192ac3b6e0STheodore Ts'o * call path. This flag exists primarily because we don't 162079e83036SEric Sandeen * want to change *many* call functions, so ext4_map_blocks() 1621f2321097STheodore Ts'o * will set the EXT4_STATE_DELALLOC_RESERVED flag once the 16222ac3b6e0STheodore Ts'o * inode's allocation semaphore is taken. 16232ac3b6e0STheodore Ts'o * 16242ac3b6e0STheodore Ts'o * If the blocks in questions were delalloc blocks, set 16252ac3b6e0STheodore Ts'o * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting 16262ac3b6e0STheodore Ts'o * variables are updated after the blocks have been allocated. 162779ffab34SAneesh Kumar K.V */ 16282ed88685STheodore Ts'o map.m_lblk = next; 16292ed88685STheodore Ts'o map.m_len = max_blocks; 16301296cc85SAneesh Kumar K.V get_blocks_flags = EXT4_GET_BLOCKS_CREATE; 1631744692dcSJiaying Zhang if (ext4_should_dioread_nolock(mpd->inode)) 1632744692dcSJiaying Zhang get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; 16332ac3b6e0STheodore Ts'o if (mpd->b_state & (1 << BH_Delay)) 16341296cc85SAneesh Kumar K.V get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; 16351296cc85SAneesh Kumar K.V 16362ed88685STheodore Ts'o blks = ext4_map_blocks(handle, mpd->inode, &map, get_blocks_flags); 16372fa3cdfbSTheodore Ts'o if (blks < 0) { 1638e3570639SEric Sandeen struct super_block *sb = mpd->inode->i_sb; 1639e3570639SEric Sandeen 16402fa3cdfbSTheodore Ts'o err = blks; 1641ed5bde0bSTheodore Ts'o /* 16425a87b7a5STheodore Ts'o * If get block returns EAGAIN or ENOSPC and there 164397498956STheodore Ts'o * appears to be free blocks we will just let 164497498956STheodore Ts'o * mpage_da_submit_io() unlock all of the pages. 1645c4a0c46eSAneesh Kumar K.V */ 1646c4a0c46eSAneesh Kumar K.V if (err == -EAGAIN) 16475a87b7a5STheodore Ts'o goto submit_io; 1648df22291fSAneesh Kumar K.V 16495dee5437STheodore Ts'o if (err == -ENOSPC && ext4_count_free_clusters(sb)) { 1650df22291fSAneesh Kumar K.V mpd->retval = err; 16515a87b7a5STheodore Ts'o goto submit_io; 1652df22291fSAneesh Kumar K.V } 1653df22291fSAneesh Kumar K.V 1654c4a0c46eSAneesh Kumar K.V /* 1655ed5bde0bSTheodore Ts'o * get block failure will cause us to loop in 1656ed5bde0bSTheodore Ts'o * writepages, because a_ops->writepage won't be able 1657ed5bde0bSTheodore Ts'o * to make progress. The page will be redirtied by 1658ed5bde0bSTheodore Ts'o * writepage and writepages will again try to write 1659ed5bde0bSTheodore Ts'o * the same. 1660c4a0c46eSAneesh Kumar K.V */ 1661e3570639SEric Sandeen if (!(EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) { 1662e3570639SEric Sandeen ext4_msg(sb, KERN_CRIT, 1663e3570639SEric Sandeen "delayed block allocation failed for inode %lu " 1664e3570639SEric Sandeen "at logical offset %llu with max blocks %zd " 1665e3570639SEric Sandeen "with error %d", mpd->inode->i_ino, 1666c4a0c46eSAneesh Kumar K.V (unsigned long long) next, 16678dc207c0STheodore Ts'o mpd->b_size >> mpd->inode->i_blkbits, err); 1668e3570639SEric Sandeen ext4_msg(sb, KERN_CRIT, 1669e3570639SEric Sandeen "This should not happen!! Data will be lost\n"); 1670e3570639SEric Sandeen if (err == -ENOSPC) 1671df22291fSAneesh Kumar K.V ext4_print_free_blocks(mpd->inode); 1672030ba6bcSAneesh Kumar K.V } 16732fa3cdfbSTheodore Ts'o /* invalidate all the pages */ 1674c7f5938aSCurt Wohlgemuth ext4_da_block_invalidatepages(mpd); 1675e0fd9b90SCurt Wohlgemuth 1676e0fd9b90SCurt Wohlgemuth /* Mark this page range as having been completed */ 1677e0fd9b90SCurt Wohlgemuth mpd->io_done = 1; 16785a87b7a5STheodore Ts'o return; 1679c4a0c46eSAneesh Kumar K.V } 16802fa3cdfbSTheodore Ts'o BUG_ON(blks == 0); 16812fa3cdfbSTheodore Ts'o 16821de3e3dfSTheodore Ts'o mapp = ↦ 16832ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 16842ed88685STheodore Ts'o struct block_device *bdev = mpd->inode->i_sb->s_bdev; 16852ed88685STheodore Ts'o int i; 168664769240SAlex Tomas 16872ed88685STheodore Ts'o for (i = 0; i < map.m_len; i++) 16882ed88685STheodore Ts'o unmap_underlying_metadata(bdev, map.m_pblk + i); 168964769240SAlex Tomas 16902fa3cdfbSTheodore Ts'o if (ext4_should_order_data(mpd->inode)) { 16912fa3cdfbSTheodore Ts'o err = ext4_jbd2_file_inode(handle, mpd->inode); 16928de49e67SKazuya Mio if (err) { 1693decbd919STheodore Ts'o /* Only if the journal is aborted */ 16948de49e67SKazuya Mio mpd->retval = err; 16958de49e67SKazuya Mio goto submit_io; 16968de49e67SKazuya Mio } 16972fa3cdfbSTheodore Ts'o } 16982fa3cdfbSTheodore Ts'o } 16992fa3cdfbSTheodore Ts'o 17002fa3cdfbSTheodore Ts'o /* 170103f5d8bcSJan Kara * Update on-disk size along with block allocation. 17022fa3cdfbSTheodore Ts'o */ 17032fa3cdfbSTheodore Ts'o disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits; 17042fa3cdfbSTheodore Ts'o if (disksize > i_size_read(mpd->inode)) 17052fa3cdfbSTheodore Ts'o disksize = i_size_read(mpd->inode); 17062fa3cdfbSTheodore Ts'o if (disksize > EXT4_I(mpd->inode)->i_disksize) { 17072fa3cdfbSTheodore Ts'o ext4_update_i_disksize(mpd->inode, disksize); 17085a87b7a5STheodore Ts'o err = ext4_mark_inode_dirty(handle, mpd->inode); 17095a87b7a5STheodore Ts'o if (err) 17105a87b7a5STheodore Ts'o ext4_error(mpd->inode->i_sb, 17115a87b7a5STheodore Ts'o "Failed to mark inode %lu dirty", 17125a87b7a5STheodore Ts'o mpd->inode->i_ino); 17132fa3cdfbSTheodore Ts'o } 17142fa3cdfbSTheodore Ts'o 17155a87b7a5STheodore Ts'o submit_io: 17161de3e3dfSTheodore Ts'o mpage_da_submit_io(mpd, mapp); 17175a87b7a5STheodore Ts'o mpd->io_done = 1; 171864769240SAlex Tomas } 171964769240SAlex Tomas 1720bf068ee2SAneesh Kumar K.V #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \ 1721bf068ee2SAneesh Kumar K.V (1 << BH_Delay) | (1 << BH_Unwritten)) 172264769240SAlex Tomas 172364769240SAlex Tomas /* 172464769240SAlex Tomas * mpage_add_bh_to_extent - try to add one more block to extent of blocks 172564769240SAlex Tomas * 172664769240SAlex Tomas * @mpd->lbh - extent of blocks 172764769240SAlex Tomas * @logical - logical number of the block in the file 172864769240SAlex Tomas * @bh - bh of the block (used to access block's state) 172964769240SAlex Tomas * 173064769240SAlex Tomas * the function is used to collect contig. blocks in same state 173164769240SAlex Tomas */ 173264769240SAlex Tomas static void mpage_add_bh_to_extent(struct mpage_da_data *mpd, 17338dc207c0STheodore Ts'o sector_t logical, size_t b_size, 17348dc207c0STheodore Ts'o unsigned long b_state) 173564769240SAlex Tomas { 173664769240SAlex Tomas sector_t next; 17378dc207c0STheodore Ts'o int nrblocks = mpd->b_size >> mpd->inode->i_blkbits; 173864769240SAlex Tomas 1739c445e3e0SEric Sandeen /* 1740c445e3e0SEric Sandeen * XXX Don't go larger than mballoc is willing to allocate 1741c445e3e0SEric Sandeen * This is a stopgap solution. We eventually need to fold 1742c445e3e0SEric Sandeen * mpage_da_submit_io() into this function and then call 174379e83036SEric Sandeen * ext4_map_blocks() multiple times in a loop 1744c445e3e0SEric Sandeen */ 1745c445e3e0SEric Sandeen if (nrblocks >= 8*1024*1024/mpd->inode->i_sb->s_blocksize) 1746c445e3e0SEric Sandeen goto flush_it; 1747c445e3e0SEric Sandeen 1748525f4ed8SMingming Cao /* check if thereserved journal credits might overflow */ 174912e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(mpd->inode, EXT4_INODE_EXTENTS))) { 1750525f4ed8SMingming Cao if (nrblocks >= EXT4_MAX_TRANS_DATA) { 1751525f4ed8SMingming Cao /* 1752525f4ed8SMingming Cao * With non-extent format we are limited by the journal 1753525f4ed8SMingming Cao * credit available. Total credit needed to insert 1754525f4ed8SMingming Cao * nrblocks contiguous blocks is dependent on the 1755525f4ed8SMingming Cao * nrblocks. So limit nrblocks. 1756525f4ed8SMingming Cao */ 1757525f4ed8SMingming Cao goto flush_it; 1758525f4ed8SMingming Cao } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) > 1759525f4ed8SMingming Cao EXT4_MAX_TRANS_DATA) { 1760525f4ed8SMingming Cao /* 1761525f4ed8SMingming Cao * Adding the new buffer_head would make it cross the 1762525f4ed8SMingming Cao * allowed limit for which we have journal credit 1763525f4ed8SMingming Cao * reserved. So limit the new bh->b_size 1764525f4ed8SMingming Cao */ 1765525f4ed8SMingming Cao b_size = (EXT4_MAX_TRANS_DATA - nrblocks) << 1766525f4ed8SMingming Cao mpd->inode->i_blkbits; 1767525f4ed8SMingming Cao /* we will do mpage_da_submit_io in the next loop */ 1768525f4ed8SMingming Cao } 1769525f4ed8SMingming Cao } 177064769240SAlex Tomas /* 177164769240SAlex Tomas * First block in the extent 177264769240SAlex Tomas */ 17738dc207c0STheodore Ts'o if (mpd->b_size == 0) { 17748dc207c0STheodore Ts'o mpd->b_blocknr = logical; 17758dc207c0STheodore Ts'o mpd->b_size = b_size; 17768dc207c0STheodore Ts'o mpd->b_state = b_state & BH_FLAGS; 177764769240SAlex Tomas return; 177864769240SAlex Tomas } 177964769240SAlex Tomas 17808dc207c0STheodore Ts'o next = mpd->b_blocknr + nrblocks; 178164769240SAlex Tomas /* 178264769240SAlex Tomas * Can we merge the block to our big extent? 178364769240SAlex Tomas */ 17848dc207c0STheodore Ts'o if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) { 17858dc207c0STheodore Ts'o mpd->b_size += b_size; 178664769240SAlex Tomas return; 178764769240SAlex Tomas } 178864769240SAlex Tomas 1789525f4ed8SMingming Cao flush_it: 179064769240SAlex Tomas /* 179164769240SAlex Tomas * We couldn't merge the block to our extent, so we 179264769240SAlex Tomas * need to flush current extent and start new one 179364769240SAlex Tomas */ 17945a87b7a5STheodore Ts'o mpage_da_map_and_submit(mpd); 1795a1d6cc56SAneesh Kumar K.V return; 179664769240SAlex Tomas } 179764769240SAlex Tomas 1798c364b22cSAneesh Kumar K.V static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh) 179929fa89d0SAneesh Kumar K.V { 1800c364b22cSAneesh Kumar K.V return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 180129fa89d0SAneesh Kumar K.V } 180229fa89d0SAneesh Kumar K.V 180364769240SAlex Tomas /* 18045356f261SAditya Kali * This function is grabs code from the very beginning of 18055356f261SAditya Kali * ext4_map_blocks, but assumes that the caller is from delayed write 18065356f261SAditya Kali * time. This function looks up the requested blocks and sets the 18075356f261SAditya Kali * buffer delay bit under the protection of i_data_sem. 18085356f261SAditya Kali */ 18095356f261SAditya Kali static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, 18105356f261SAditya Kali struct ext4_map_blocks *map, 18115356f261SAditya Kali struct buffer_head *bh) 18125356f261SAditya Kali { 18135356f261SAditya Kali int retval; 18145356f261SAditya Kali sector_t invalid_block = ~((sector_t) 0xffff); 18155356f261SAditya Kali 18165356f261SAditya Kali if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 18175356f261SAditya Kali invalid_block = ~0; 18185356f261SAditya Kali 18195356f261SAditya Kali map->m_flags = 0; 18205356f261SAditya Kali ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u," 18215356f261SAditya Kali "logical block %lu\n", inode->i_ino, map->m_len, 18225356f261SAditya Kali (unsigned long) map->m_lblk); 18235356f261SAditya Kali /* 18245356f261SAditya Kali * Try to see if we can get the block without requesting a new 18255356f261SAditya Kali * file system block. 18265356f261SAditya Kali */ 18275356f261SAditya Kali down_read((&EXT4_I(inode)->i_data_sem)); 18285356f261SAditya Kali if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 18295356f261SAditya Kali retval = ext4_ext_map_blocks(NULL, inode, map, 0); 18305356f261SAditya Kali else 18315356f261SAditya Kali retval = ext4_ind_map_blocks(NULL, inode, map, 0); 18325356f261SAditya Kali 18335356f261SAditya Kali if (retval == 0) { 18345356f261SAditya Kali /* 18355356f261SAditya Kali * XXX: __block_prepare_write() unmaps passed block, 18365356f261SAditya Kali * is it OK? 18375356f261SAditya Kali */ 18385356f261SAditya Kali /* If the block was allocated from previously allocated cluster, 18395356f261SAditya Kali * then we dont need to reserve it again. */ 18405356f261SAditya Kali if (!(map->m_flags & EXT4_MAP_FROM_CLUSTER)) { 18415356f261SAditya Kali retval = ext4_da_reserve_space(inode, iblock); 18425356f261SAditya Kali if (retval) 18435356f261SAditya Kali /* not enough space to reserve */ 18445356f261SAditya Kali goto out_unlock; 18455356f261SAditya Kali } 18465356f261SAditya Kali 1847*51865fdaSZheng Liu retval = ext4_es_insert_extent(inode, map->m_lblk, map->m_len); 1848*51865fdaSZheng Liu if (retval) 1849*51865fdaSZheng Liu goto out_unlock; 1850*51865fdaSZheng Liu 18515356f261SAditya Kali /* Clear EXT4_MAP_FROM_CLUSTER flag since its purpose is served 18525356f261SAditya Kali * and it should not appear on the bh->b_state. 18535356f261SAditya Kali */ 18545356f261SAditya Kali map->m_flags &= ~EXT4_MAP_FROM_CLUSTER; 18555356f261SAditya Kali 18565356f261SAditya Kali map_bh(bh, inode->i_sb, invalid_block); 18575356f261SAditya Kali set_buffer_new(bh); 18585356f261SAditya Kali set_buffer_delay(bh); 18595356f261SAditya Kali } 18605356f261SAditya Kali 18615356f261SAditya Kali out_unlock: 18625356f261SAditya Kali up_read((&EXT4_I(inode)->i_data_sem)); 18635356f261SAditya Kali 18645356f261SAditya Kali return retval; 18655356f261SAditya Kali } 18665356f261SAditya Kali 18675356f261SAditya Kali /* 1868b920c755STheodore Ts'o * This is a special get_blocks_t callback which is used by 1869b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 1870b920c755STheodore Ts'o * reserve space for a single block. 187129fa89d0SAneesh Kumar K.V * 187229fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 187329fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 187429fa89d0SAneesh Kumar K.V * 187529fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 187629fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 187729fa89d0SAneesh Kumar K.V * initialized properly. 187864769240SAlex Tomas */ 187964769240SAlex Tomas static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 18802ed88685STheodore Ts'o struct buffer_head *bh, int create) 188164769240SAlex Tomas { 18822ed88685STheodore Ts'o struct ext4_map_blocks map; 188364769240SAlex Tomas int ret = 0; 188464769240SAlex Tomas 188564769240SAlex Tomas BUG_ON(create == 0); 18862ed88685STheodore Ts'o BUG_ON(bh->b_size != inode->i_sb->s_blocksize); 18872ed88685STheodore Ts'o 18882ed88685STheodore Ts'o map.m_lblk = iblock; 18892ed88685STheodore Ts'o map.m_len = 1; 189064769240SAlex Tomas 189164769240SAlex Tomas /* 189264769240SAlex Tomas * first, we need to know whether the block is allocated already 189364769240SAlex Tomas * preallocated blocks are unmapped but should treated 189464769240SAlex Tomas * the same as allocated blocks. 189564769240SAlex Tomas */ 18965356f261SAditya Kali ret = ext4_da_map_blocks(inode, iblock, &map, bh); 18975356f261SAditya Kali if (ret <= 0) 18982ed88685STheodore Ts'o return ret; 189964769240SAlex Tomas 19002ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 19012ed88685STheodore Ts'o bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; 19022ed88685STheodore Ts'o 19032ed88685STheodore Ts'o if (buffer_unwritten(bh)) { 19042ed88685STheodore Ts'o /* A delayed write to unwritten bh should be marked 19052ed88685STheodore Ts'o * new and mapped. Mapped ensures that we don't do 19062ed88685STheodore Ts'o * get_block multiple times when we write to the same 19072ed88685STheodore Ts'o * offset and new ensures that we do proper zero out 19082ed88685STheodore Ts'o * for partial write. 19092ed88685STheodore Ts'o */ 19102ed88685STheodore Ts'o set_buffer_new(bh); 1911c8205636STheodore Ts'o set_buffer_mapped(bh); 19122ed88685STheodore Ts'o } 19132ed88685STheodore Ts'o return 0; 191464769240SAlex Tomas } 191561628a3fSMingming Cao 1916b920c755STheodore Ts'o /* 1917b920c755STheodore Ts'o * This function is used as a standard get_block_t calback function 1918b920c755STheodore Ts'o * when there is no desire to allocate any blocks. It is used as a 1919ebdec241SChristoph Hellwig * callback function for block_write_begin() and block_write_full_page(). 1920206f7ab4SChristoph Hellwig * These functions should only try to map a single block at a time. 1921b920c755STheodore Ts'o * 1922b920c755STheodore Ts'o * Since this function doesn't do block allocations even if the caller 1923b920c755STheodore Ts'o * requests it by passing in create=1, it is critically important that 1924b920c755STheodore Ts'o * any caller checks to make sure that any buffer heads are returned 1925b920c755STheodore Ts'o * by this function are either all already mapped or marked for 1926206f7ab4SChristoph Hellwig * delayed allocation before calling block_write_full_page(). Otherwise, 1927206f7ab4SChristoph Hellwig * b_blocknr could be left unitialized, and the page write functions will 1928206f7ab4SChristoph Hellwig * be taken by surprise. 1929b920c755STheodore Ts'o */ 1930b920c755STheodore Ts'o static int noalloc_get_block_write(struct inode *inode, sector_t iblock, 1931f0e6c985SAneesh Kumar K.V struct buffer_head *bh_result, int create) 1932f0e6c985SAneesh Kumar K.V { 1933a2dc52b5STheodore Ts'o BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize); 19342ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh_result, 0); 193561628a3fSMingming Cao } 193661628a3fSMingming Cao 193762e086beSAneesh Kumar K.V static int bget_one(handle_t *handle, struct buffer_head *bh) 193862e086beSAneesh Kumar K.V { 193962e086beSAneesh Kumar K.V get_bh(bh); 194062e086beSAneesh Kumar K.V return 0; 194162e086beSAneesh Kumar K.V } 194262e086beSAneesh Kumar K.V 194362e086beSAneesh Kumar K.V static int bput_one(handle_t *handle, struct buffer_head *bh) 194462e086beSAneesh Kumar K.V { 194562e086beSAneesh Kumar K.V put_bh(bh); 194662e086beSAneesh Kumar K.V return 0; 194762e086beSAneesh Kumar K.V } 194862e086beSAneesh Kumar K.V 194962e086beSAneesh Kumar K.V static int __ext4_journalled_writepage(struct page *page, 195062e086beSAneesh Kumar K.V unsigned int len) 195162e086beSAneesh Kumar K.V { 195262e086beSAneesh Kumar K.V struct address_space *mapping = page->mapping; 195362e086beSAneesh Kumar K.V struct inode *inode = mapping->host; 195462e086beSAneesh Kumar K.V struct buffer_head *page_bufs; 195562e086beSAneesh Kumar K.V handle_t *handle = NULL; 195662e086beSAneesh Kumar K.V int ret = 0; 195762e086beSAneesh Kumar K.V int err; 195862e086beSAneesh Kumar K.V 1959cb20d518STheodore Ts'o ClearPageChecked(page); 196062e086beSAneesh Kumar K.V page_bufs = page_buffers(page); 196162e086beSAneesh Kumar K.V BUG_ON(!page_bufs); 196262e086beSAneesh Kumar K.V walk_page_buffers(handle, page_bufs, 0, len, NULL, bget_one); 196362e086beSAneesh Kumar K.V /* As soon as we unlock the page, it can go away, but we have 196462e086beSAneesh Kumar K.V * references to buffers so we are safe */ 196562e086beSAneesh Kumar K.V unlock_page(page); 196662e086beSAneesh Kumar K.V 196762e086beSAneesh Kumar K.V handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode)); 196862e086beSAneesh Kumar K.V if (IS_ERR(handle)) { 196962e086beSAneesh Kumar K.V ret = PTR_ERR(handle); 197062e086beSAneesh Kumar K.V goto out; 197162e086beSAneesh Kumar K.V } 197262e086beSAneesh Kumar K.V 1973441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1974441c8508SCurt Wohlgemuth 197562e086beSAneesh Kumar K.V ret = walk_page_buffers(handle, page_bufs, 0, len, NULL, 197662e086beSAneesh Kumar K.V do_journal_get_write_access); 197762e086beSAneesh Kumar K.V 197862e086beSAneesh Kumar K.V err = walk_page_buffers(handle, page_bufs, 0, len, NULL, 197962e086beSAneesh Kumar K.V write_end_fn); 198062e086beSAneesh Kumar K.V if (ret == 0) 198162e086beSAneesh Kumar K.V ret = err; 19822d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 198362e086beSAneesh Kumar K.V err = ext4_journal_stop(handle); 198462e086beSAneesh Kumar K.V if (!ret) 198562e086beSAneesh Kumar K.V ret = err; 198662e086beSAneesh Kumar K.V 198762e086beSAneesh Kumar K.V walk_page_buffers(handle, page_bufs, 0, len, NULL, bput_one); 198819f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 198962e086beSAneesh Kumar K.V out: 199062e086beSAneesh Kumar K.V return ret; 199162e086beSAneesh Kumar K.V } 199262e086beSAneesh Kumar K.V 199361628a3fSMingming Cao /* 199443ce1d23SAneesh Kumar K.V * Note that we don't need to start a transaction unless we're journaling data 199543ce1d23SAneesh Kumar K.V * because we should have holes filled from ext4_page_mkwrite(). We even don't 199643ce1d23SAneesh Kumar K.V * need to file the inode to the transaction's list in ordered mode because if 199743ce1d23SAneesh Kumar K.V * we are writing back data added by write(), the inode is already there and if 199843ce1d23SAneesh Kumar K.V * we are writing back data modified via mmap(), no one guarantees in which 199943ce1d23SAneesh Kumar K.V * transaction the data will hit the disk. In case we are journaling data, we 200043ce1d23SAneesh Kumar K.V * cannot start transaction directly because transaction start ranks above page 200143ce1d23SAneesh Kumar K.V * lock so we have to do some magic. 200243ce1d23SAneesh Kumar K.V * 2003b920c755STheodore Ts'o * This function can get called via... 2004b920c755STheodore Ts'o * - ext4_da_writepages after taking page lock (have journal handle) 2005b920c755STheodore Ts'o * - journal_submit_inode_data_buffers (no journal handle) 2006f6463b0dSArtem Bityutskiy * - shrink_page_list via the kswapd/direct reclaim (no journal handle) 2007b920c755STheodore Ts'o * - grab_page_cache when doing write_begin (have journal handle) 200843ce1d23SAneesh Kumar K.V * 200943ce1d23SAneesh Kumar K.V * We don't do any block allocation in this function. If we have page with 201043ce1d23SAneesh Kumar K.V * multiple blocks we need to write those buffer_heads that are mapped. This 201143ce1d23SAneesh Kumar K.V * is important for mmaped based write. So if we do with blocksize 1K 201243ce1d23SAneesh Kumar K.V * truncate(f, 1024); 201343ce1d23SAneesh Kumar K.V * a = mmap(f, 0, 4096); 201443ce1d23SAneesh Kumar K.V * a[0] = 'a'; 201543ce1d23SAneesh Kumar K.V * truncate(f, 4096); 201643ce1d23SAneesh Kumar K.V * we have in the page first buffer_head mapped via page_mkwrite call back 201790802ed9SPaul Bolle * but other buffer_heads would be unmapped but dirty (dirty done via the 201843ce1d23SAneesh Kumar K.V * do_wp_page). So writepage should write the first block. If we modify 201943ce1d23SAneesh Kumar K.V * the mmap area beyond 1024 we will again get a page_fault and the 202043ce1d23SAneesh Kumar K.V * page_mkwrite callback will do the block allocation and mark the 202143ce1d23SAneesh Kumar K.V * buffer_heads mapped. 202243ce1d23SAneesh Kumar K.V * 202343ce1d23SAneesh Kumar K.V * We redirty the page if we have any buffer_heads that is either delay or 202443ce1d23SAneesh Kumar K.V * unwritten in the page. 202543ce1d23SAneesh Kumar K.V * 202643ce1d23SAneesh Kumar K.V * We can get recursively called as show below. 202743ce1d23SAneesh Kumar K.V * 202843ce1d23SAneesh Kumar K.V * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> 202943ce1d23SAneesh Kumar K.V * ext4_writepage() 203043ce1d23SAneesh Kumar K.V * 203143ce1d23SAneesh Kumar K.V * But since we don't do any block allocation we should not deadlock. 203243ce1d23SAneesh Kumar K.V * Page also have the dirty flag cleared so we don't get recurive page_lock. 203361628a3fSMingming Cao */ 203443ce1d23SAneesh Kumar K.V static int ext4_writepage(struct page *page, 203564769240SAlex Tomas struct writeback_control *wbc) 203664769240SAlex Tomas { 2037a42afc5fSTheodore Ts'o int ret = 0, commit_write = 0; 203861628a3fSMingming Cao loff_t size; 2039498e5f24STheodore Ts'o unsigned int len; 2040744692dcSJiaying Zhang struct buffer_head *page_bufs = NULL; 204161628a3fSMingming Cao struct inode *inode = page->mapping->host; 204264769240SAlex Tomas 2043a9c667f8SLukas Czerner trace_ext4_writepage(page); 204461628a3fSMingming Cao size = i_size_read(inode); 204561628a3fSMingming Cao if (page->index == size >> PAGE_CACHE_SHIFT) 204661628a3fSMingming Cao len = size & ~PAGE_CACHE_MASK; 204761628a3fSMingming Cao else 204861628a3fSMingming Cao len = PAGE_CACHE_SIZE; 204961628a3fSMingming Cao 2050a42afc5fSTheodore Ts'o /* 2051a42afc5fSTheodore Ts'o * If the page does not have buffers (for whatever reason), 2052a107e5a3STheodore Ts'o * try to create them using __block_write_begin. If this 2053a42afc5fSTheodore Ts'o * fails, redirty the page and move on. 2054a42afc5fSTheodore Ts'o */ 2055b1142e8fSTheodore Ts'o if (!page_has_buffers(page)) { 2056a107e5a3STheodore Ts'o if (__block_write_begin(page, 0, len, 2057a42afc5fSTheodore Ts'o noalloc_get_block_write)) { 2058a42afc5fSTheodore Ts'o redirty_page: 2059a42afc5fSTheodore Ts'o redirty_page_for_writepage(wbc, page); 2060a42afc5fSTheodore Ts'o unlock_page(page); 2061a42afc5fSTheodore Ts'o return 0; 2062a42afc5fSTheodore Ts'o } 2063a42afc5fSTheodore Ts'o commit_write = 1; 2064a42afc5fSTheodore Ts'o } 2065f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 2066f0e6c985SAneesh Kumar K.V if (walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2067c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 206861628a3fSMingming Cao /* 2069b1142e8fSTheodore Ts'o * We don't want to do block allocation, so redirty 2070b1142e8fSTheodore Ts'o * the page and return. We may reach here when we do 2071b1142e8fSTheodore Ts'o * a journal commit via journal_submit_inode_data_buffers. 2072966dbde2SMel Gorman * We can also reach here via shrink_page_list but it 2073966dbde2SMel Gorman * should never be for direct reclaim so warn if that 2074966dbde2SMel Gorman * happens 2075f0e6c985SAneesh Kumar K.V */ 2076966dbde2SMel Gorman WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) == 2077966dbde2SMel Gorman PF_MEMALLOC); 2078a42afc5fSTheodore Ts'o goto redirty_page; 2079f0e6c985SAneesh Kumar K.V } 2080a42afc5fSTheodore Ts'o if (commit_write) 2081ed9b3e33SAneesh Kumar K.V /* now mark the buffer_heads as dirty and uptodate */ 2082b767e78aSAneesh Kumar K.V block_commit_write(page, 0, len); 208364769240SAlex Tomas 2084cb20d518STheodore Ts'o if (PageChecked(page) && ext4_should_journal_data(inode)) 208543ce1d23SAneesh Kumar K.V /* 208643ce1d23SAneesh Kumar K.V * It's mmapped pagecache. Add buffers and journal it. There 208743ce1d23SAneesh Kumar K.V * doesn't seem much point in redirtying the page here. 208843ce1d23SAneesh Kumar K.V */ 20893f0ca309SWu Fengguang return __ext4_journalled_writepage(page, len); 209043ce1d23SAneesh Kumar K.V 2091a42afc5fSTheodore Ts'o if (buffer_uninit(page_bufs)) { 2092744692dcSJiaying Zhang ext4_set_bh_endio(page_bufs, inode); 2093744692dcSJiaying Zhang ret = block_write_full_page_endio(page, noalloc_get_block_write, 2094744692dcSJiaying Zhang wbc, ext4_end_io_buffer_write); 2095744692dcSJiaying Zhang } else 2096b920c755STheodore Ts'o ret = block_write_full_page(page, noalloc_get_block_write, 2097f0e6c985SAneesh Kumar K.V wbc); 209864769240SAlex Tomas 209964769240SAlex Tomas return ret; 210064769240SAlex Tomas } 210164769240SAlex Tomas 210261628a3fSMingming Cao /* 2103525f4ed8SMingming Cao * This is called via ext4_da_writepages() to 210425985edcSLucas De Marchi * calculate the total number of credits to reserve to fit 2105525f4ed8SMingming Cao * a single extent allocation into a single transaction, 2106525f4ed8SMingming Cao * ext4_da_writpeages() will loop calling this before 2107525f4ed8SMingming Cao * the block allocation. 210861628a3fSMingming Cao */ 2109525f4ed8SMingming Cao 2110525f4ed8SMingming Cao static int ext4_da_writepages_trans_blocks(struct inode *inode) 2111525f4ed8SMingming Cao { 2112525f4ed8SMingming Cao int max_blocks = EXT4_I(inode)->i_reserved_data_blocks; 2113525f4ed8SMingming Cao 2114525f4ed8SMingming Cao /* 2115525f4ed8SMingming Cao * With non-extent format the journal credit needed to 2116525f4ed8SMingming Cao * insert nrblocks contiguous block is dependent on 2117525f4ed8SMingming Cao * number of contiguous block. So we will limit 2118525f4ed8SMingming Cao * number of contiguous block to a sane value 2119525f4ed8SMingming Cao */ 212012e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) && 2121525f4ed8SMingming Cao (max_blocks > EXT4_MAX_TRANS_DATA)) 2122525f4ed8SMingming Cao max_blocks = EXT4_MAX_TRANS_DATA; 2123525f4ed8SMingming Cao 2124525f4ed8SMingming Cao return ext4_chunk_trans_blocks(inode, max_blocks); 2125525f4ed8SMingming Cao } 212661628a3fSMingming Cao 21278e48dcfbSTheodore Ts'o /* 21288e48dcfbSTheodore Ts'o * write_cache_pages_da - walk the list of dirty pages of the given 21298eb9e5ceSTheodore Ts'o * address space and accumulate pages that need writing, and call 2130168fc022STheodore Ts'o * mpage_da_map_and_submit to map a single contiguous memory region 2131168fc022STheodore Ts'o * and then write them. 21328e48dcfbSTheodore Ts'o */ 21338e48dcfbSTheodore Ts'o static int write_cache_pages_da(struct address_space *mapping, 21348e48dcfbSTheodore Ts'o struct writeback_control *wbc, 213572f84e65SEric Sandeen struct mpage_da_data *mpd, 213672f84e65SEric Sandeen pgoff_t *done_index) 21378e48dcfbSTheodore Ts'o { 21388eb9e5ceSTheodore Ts'o struct buffer_head *bh, *head; 2139168fc022STheodore Ts'o struct inode *inode = mapping->host; 21408e48dcfbSTheodore Ts'o struct pagevec pvec; 21414f01b02cSTheodore Ts'o unsigned int nr_pages; 21424f01b02cSTheodore Ts'o sector_t logical; 21434f01b02cSTheodore Ts'o pgoff_t index, end; 21448e48dcfbSTheodore Ts'o long nr_to_write = wbc->nr_to_write; 21454f01b02cSTheodore Ts'o int i, tag, ret = 0; 21468e48dcfbSTheodore Ts'o 2147168fc022STheodore Ts'o memset(mpd, 0, sizeof(struct mpage_da_data)); 2148168fc022STheodore Ts'o mpd->wbc = wbc; 2149168fc022STheodore Ts'o mpd->inode = inode; 21508e48dcfbSTheodore Ts'o pagevec_init(&pvec, 0); 21518e48dcfbSTheodore Ts'o index = wbc->range_start >> PAGE_CACHE_SHIFT; 21528e48dcfbSTheodore Ts'o end = wbc->range_end >> PAGE_CACHE_SHIFT; 21538e48dcfbSTheodore Ts'o 21546e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 21555b41d924SEric Sandeen tag = PAGECACHE_TAG_TOWRITE; 21565b41d924SEric Sandeen else 21575b41d924SEric Sandeen tag = PAGECACHE_TAG_DIRTY; 21585b41d924SEric Sandeen 215972f84e65SEric Sandeen *done_index = index; 21604f01b02cSTheodore Ts'o while (index <= end) { 21615b41d924SEric Sandeen nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, 21628e48dcfbSTheodore Ts'o min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); 21638e48dcfbSTheodore Ts'o if (nr_pages == 0) 21644f01b02cSTheodore Ts'o return 0; 21658e48dcfbSTheodore Ts'o 21668e48dcfbSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 21678e48dcfbSTheodore Ts'o struct page *page = pvec.pages[i]; 21688e48dcfbSTheodore Ts'o 21698e48dcfbSTheodore Ts'o /* 21708e48dcfbSTheodore Ts'o * At this point, the page may be truncated or 21718e48dcfbSTheodore Ts'o * invalidated (changing page->mapping to NULL), or 21728e48dcfbSTheodore Ts'o * even swizzled back from swapper_space to tmpfs file 21738e48dcfbSTheodore Ts'o * mapping. However, page->index will not change 21748e48dcfbSTheodore Ts'o * because we have a reference on the page. 21758e48dcfbSTheodore Ts'o */ 21764f01b02cSTheodore Ts'o if (page->index > end) 21774f01b02cSTheodore Ts'o goto out; 21788e48dcfbSTheodore Ts'o 217972f84e65SEric Sandeen *done_index = page->index + 1; 218072f84e65SEric Sandeen 218178aaced3STheodore Ts'o /* 218278aaced3STheodore Ts'o * If we can't merge this page, and we have 218378aaced3STheodore Ts'o * accumulated an contiguous region, write it 218478aaced3STheodore Ts'o */ 218578aaced3STheodore Ts'o if ((mpd->next_page != page->index) && 218678aaced3STheodore Ts'o (mpd->next_page != mpd->first_page)) { 218778aaced3STheodore Ts'o mpage_da_map_and_submit(mpd); 218878aaced3STheodore Ts'o goto ret_extent_tail; 218978aaced3STheodore Ts'o } 219078aaced3STheodore Ts'o 21918e48dcfbSTheodore Ts'o lock_page(page); 21928e48dcfbSTheodore Ts'o 21938e48dcfbSTheodore Ts'o /* 21944f01b02cSTheodore Ts'o * If the page is no longer dirty, or its 21954f01b02cSTheodore Ts'o * mapping no longer corresponds to inode we 21964f01b02cSTheodore Ts'o * are writing (which means it has been 21974f01b02cSTheodore Ts'o * truncated or invalidated), or the page is 21984f01b02cSTheodore Ts'o * already under writeback and we are not 21994f01b02cSTheodore Ts'o * doing a data integrity writeback, skip the page 22008e48dcfbSTheodore Ts'o */ 22014f01b02cSTheodore Ts'o if (!PageDirty(page) || 22024f01b02cSTheodore Ts'o (PageWriteback(page) && 22034f01b02cSTheodore Ts'o (wbc->sync_mode == WB_SYNC_NONE)) || 22044f01b02cSTheodore Ts'o unlikely(page->mapping != mapping)) { 22058e48dcfbSTheodore Ts'o unlock_page(page); 22068e48dcfbSTheodore Ts'o continue; 22078e48dcfbSTheodore Ts'o } 22088e48dcfbSTheodore Ts'o 22098e48dcfbSTheodore Ts'o wait_on_page_writeback(page); 22108e48dcfbSTheodore Ts'o BUG_ON(PageWriteback(page)); 22118e48dcfbSTheodore Ts'o 2212168fc022STheodore Ts'o if (mpd->next_page != page->index) 22138eb9e5ceSTheodore Ts'o mpd->first_page = page->index; 22148eb9e5ceSTheodore Ts'o mpd->next_page = page->index + 1; 22158eb9e5ceSTheodore Ts'o logical = (sector_t) page->index << 22168eb9e5ceSTheodore Ts'o (PAGE_CACHE_SHIFT - inode->i_blkbits); 22178eb9e5ceSTheodore Ts'o 22188eb9e5ceSTheodore Ts'o if (!page_has_buffers(page)) { 22194f01b02cSTheodore Ts'o mpage_add_bh_to_extent(mpd, logical, 22204f01b02cSTheodore Ts'o PAGE_CACHE_SIZE, 22218eb9e5ceSTheodore Ts'o (1 << BH_Dirty) | (1 << BH_Uptodate)); 22224f01b02cSTheodore Ts'o if (mpd->io_done) 22234f01b02cSTheodore Ts'o goto ret_extent_tail; 22248e48dcfbSTheodore Ts'o } else { 22258eb9e5ceSTheodore Ts'o /* 22264f01b02cSTheodore Ts'o * Page with regular buffer heads, 22274f01b02cSTheodore Ts'o * just add all dirty ones 22288eb9e5ceSTheodore Ts'o */ 22298eb9e5ceSTheodore Ts'o head = page_buffers(page); 22308eb9e5ceSTheodore Ts'o bh = head; 22318eb9e5ceSTheodore Ts'o do { 22328eb9e5ceSTheodore Ts'o BUG_ON(buffer_locked(bh)); 22338eb9e5ceSTheodore Ts'o /* 22348eb9e5ceSTheodore Ts'o * We need to try to allocate 22358eb9e5ceSTheodore Ts'o * unmapped blocks in the same page. 22368eb9e5ceSTheodore Ts'o * Otherwise we won't make progress 22378eb9e5ceSTheodore Ts'o * with the page in ext4_writepage 22388eb9e5ceSTheodore Ts'o */ 22398eb9e5ceSTheodore Ts'o if (ext4_bh_delay_or_unwritten(NULL, bh)) { 22408eb9e5ceSTheodore Ts'o mpage_add_bh_to_extent(mpd, logical, 22418eb9e5ceSTheodore Ts'o bh->b_size, 22428eb9e5ceSTheodore Ts'o bh->b_state); 22434f01b02cSTheodore Ts'o if (mpd->io_done) 22444f01b02cSTheodore Ts'o goto ret_extent_tail; 22458eb9e5ceSTheodore Ts'o } else if (buffer_dirty(bh) && (buffer_mapped(bh))) { 22468eb9e5ceSTheodore Ts'o /* 22474f01b02cSTheodore Ts'o * mapped dirty buffer. We need 22484f01b02cSTheodore Ts'o * to update the b_state 22494f01b02cSTheodore Ts'o * because we look at b_state 22504f01b02cSTheodore Ts'o * in mpage_da_map_blocks. We 22514f01b02cSTheodore Ts'o * don't update b_size because 22524f01b02cSTheodore Ts'o * if we find an unmapped 22534f01b02cSTheodore Ts'o * buffer_head later we need to 22544f01b02cSTheodore Ts'o * use the b_state flag of that 22554f01b02cSTheodore Ts'o * buffer_head. 22568eb9e5ceSTheodore Ts'o */ 22578eb9e5ceSTheodore Ts'o if (mpd->b_size == 0) 22588eb9e5ceSTheodore Ts'o mpd->b_state = bh->b_state & BH_FLAGS; 22598e48dcfbSTheodore Ts'o } 22608eb9e5ceSTheodore Ts'o logical++; 22618eb9e5ceSTheodore Ts'o } while ((bh = bh->b_this_page) != head); 22628e48dcfbSTheodore Ts'o } 22638e48dcfbSTheodore Ts'o 22648e48dcfbSTheodore Ts'o if (nr_to_write > 0) { 22658e48dcfbSTheodore Ts'o nr_to_write--; 22668e48dcfbSTheodore Ts'o if (nr_to_write == 0 && 22674f01b02cSTheodore Ts'o wbc->sync_mode == WB_SYNC_NONE) 22688e48dcfbSTheodore Ts'o /* 22698e48dcfbSTheodore Ts'o * We stop writing back only if we are 22708e48dcfbSTheodore Ts'o * not doing integrity sync. In case of 22718e48dcfbSTheodore Ts'o * integrity sync we have to keep going 22728e48dcfbSTheodore Ts'o * because someone may be concurrently 22738e48dcfbSTheodore Ts'o * dirtying pages, and we might have 22748e48dcfbSTheodore Ts'o * synced a lot of newly appeared dirty 22758e48dcfbSTheodore Ts'o * pages, but have not synced all of the 22768e48dcfbSTheodore Ts'o * old dirty pages. 22778e48dcfbSTheodore Ts'o */ 22784f01b02cSTheodore Ts'o goto out; 22798e48dcfbSTheodore Ts'o } 22808e48dcfbSTheodore Ts'o } 22818e48dcfbSTheodore Ts'o pagevec_release(&pvec); 22828e48dcfbSTheodore Ts'o cond_resched(); 22838e48dcfbSTheodore Ts'o } 22844f01b02cSTheodore Ts'o return 0; 22854f01b02cSTheodore Ts'o ret_extent_tail: 22864f01b02cSTheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 22878eb9e5ceSTheodore Ts'o out: 22888eb9e5ceSTheodore Ts'o pagevec_release(&pvec); 22898eb9e5ceSTheodore Ts'o cond_resched(); 22908e48dcfbSTheodore Ts'o return ret; 22918e48dcfbSTheodore Ts'o } 22928e48dcfbSTheodore Ts'o 22938e48dcfbSTheodore Ts'o 229464769240SAlex Tomas static int ext4_da_writepages(struct address_space *mapping, 229564769240SAlex Tomas struct writeback_control *wbc) 229664769240SAlex Tomas { 229722208dedSAneesh Kumar K.V pgoff_t index; 229822208dedSAneesh Kumar K.V int range_whole = 0; 229961628a3fSMingming Cao handle_t *handle = NULL; 2300df22291fSAneesh Kumar K.V struct mpage_da_data mpd; 23015e745b04SAneesh Kumar K.V struct inode *inode = mapping->host; 2302498e5f24STheodore Ts'o int pages_written = 0; 230355138e0bSTheodore Ts'o unsigned int max_pages; 23042acf2c26SAneesh Kumar K.V int range_cyclic, cycled = 1, io_done = 0; 230555138e0bSTheodore Ts'o int needed_blocks, ret = 0; 230655138e0bSTheodore Ts'o long desired_nr_to_write, nr_to_writebump = 0; 2307de89de6eSTheodore Ts'o loff_t range_start = wbc->range_start; 23085e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 230972f84e65SEric Sandeen pgoff_t done_index = 0; 23105b41d924SEric Sandeen pgoff_t end; 23111bce63d1SShaohua Li struct blk_plug plug; 231261628a3fSMingming Cao 23139bffad1eSTheodore Ts'o trace_ext4_da_writepages(inode, wbc); 2314ba80b101STheodore Ts'o 231561628a3fSMingming Cao /* 231661628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 231761628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 231861628a3fSMingming Cao * because that could violate lock ordering on umount 231961628a3fSMingming Cao */ 2320a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 232161628a3fSMingming Cao return 0; 23222a21e37eSTheodore Ts'o 23232a21e37eSTheodore Ts'o /* 23242a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 23252a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 23262a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 23274ab2f15bSTheodore Ts'o * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because 23282a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 23292a21e37eSTheodore Ts'o * read-only, and in that case, ext4_da_writepages should 23302a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 23312a21e37eSTheodore Ts'o * the stack trace. 23322a21e37eSTheodore Ts'o */ 23334ab2f15bSTheodore Ts'o if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) 23342a21e37eSTheodore Ts'o return -EROFS; 23352a21e37eSTheodore Ts'o 233622208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 233722208dedSAneesh Kumar K.V range_whole = 1; 233861628a3fSMingming Cao 23392acf2c26SAneesh Kumar K.V range_cyclic = wbc->range_cyclic; 23402acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 234122208dedSAneesh Kumar K.V index = mapping->writeback_index; 23422acf2c26SAneesh Kumar K.V if (index) 23432acf2c26SAneesh Kumar K.V cycled = 0; 23442acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 23452acf2c26SAneesh Kumar K.V wbc->range_end = LLONG_MAX; 23462acf2c26SAneesh Kumar K.V wbc->range_cyclic = 0; 23475b41d924SEric Sandeen end = -1; 23485b41d924SEric Sandeen } else { 234922208dedSAneesh Kumar K.V index = wbc->range_start >> PAGE_CACHE_SHIFT; 23505b41d924SEric Sandeen end = wbc->range_end >> PAGE_CACHE_SHIFT; 23515b41d924SEric Sandeen } 2352a1d6cc56SAneesh Kumar K.V 235355138e0bSTheodore Ts'o /* 235455138e0bSTheodore Ts'o * This works around two forms of stupidity. The first is in 235555138e0bSTheodore Ts'o * the writeback code, which caps the maximum number of pages 235655138e0bSTheodore Ts'o * written to be 1024 pages. This is wrong on multiple 235755138e0bSTheodore Ts'o * levels; different architectues have a different page size, 235855138e0bSTheodore Ts'o * which changes the maximum amount of data which gets 235955138e0bSTheodore Ts'o * written. Secondly, 4 megabytes is way too small. XFS 236055138e0bSTheodore Ts'o * forces this value to be 16 megabytes by multiplying 236155138e0bSTheodore Ts'o * nr_to_write parameter by four, and then relies on its 236255138e0bSTheodore Ts'o * allocator to allocate larger extents to make them 236355138e0bSTheodore Ts'o * contiguous. Unfortunately this brings us to the second 236455138e0bSTheodore Ts'o * stupidity, which is that ext4's mballoc code only allocates 236555138e0bSTheodore Ts'o * at most 2048 blocks. So we force contiguous writes up to 236655138e0bSTheodore Ts'o * the number of dirty blocks in the inode, or 236755138e0bSTheodore Ts'o * sbi->max_writeback_mb_bump whichever is smaller. 236855138e0bSTheodore Ts'o */ 236955138e0bSTheodore Ts'o max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT); 2370b443e733SEric Sandeen if (!range_cyclic && range_whole) { 2371b443e733SEric Sandeen if (wbc->nr_to_write == LONG_MAX) 2372b443e733SEric Sandeen desired_nr_to_write = wbc->nr_to_write; 237355138e0bSTheodore Ts'o else 2374b443e733SEric Sandeen desired_nr_to_write = wbc->nr_to_write * 8; 2375b443e733SEric Sandeen } else 237655138e0bSTheodore Ts'o desired_nr_to_write = ext4_num_dirty_pages(inode, index, 237755138e0bSTheodore Ts'o max_pages); 237855138e0bSTheodore Ts'o if (desired_nr_to_write > max_pages) 237955138e0bSTheodore Ts'o desired_nr_to_write = max_pages; 238055138e0bSTheodore Ts'o 238155138e0bSTheodore Ts'o if (wbc->nr_to_write < desired_nr_to_write) { 238255138e0bSTheodore Ts'o nr_to_writebump = desired_nr_to_write - wbc->nr_to_write; 238355138e0bSTheodore Ts'o wbc->nr_to_write = desired_nr_to_write; 238455138e0bSTheodore Ts'o } 238555138e0bSTheodore Ts'o 23862acf2c26SAneesh Kumar K.V retry: 23876e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 23885b41d924SEric Sandeen tag_pages_for_writeback(mapping, index, end); 23895b41d924SEric Sandeen 23901bce63d1SShaohua Li blk_start_plug(&plug); 239122208dedSAneesh Kumar K.V while (!ret && wbc->nr_to_write > 0) { 2392a1d6cc56SAneesh Kumar K.V 2393a1d6cc56SAneesh Kumar K.V /* 2394a1d6cc56SAneesh Kumar K.V * we insert one extent at a time. So we need 2395a1d6cc56SAneesh Kumar K.V * credit needed for single extent allocation. 2396a1d6cc56SAneesh Kumar K.V * journalled mode is currently not supported 2397a1d6cc56SAneesh Kumar K.V * by delalloc 2398a1d6cc56SAneesh Kumar K.V */ 2399a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 2400525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 2401a1d6cc56SAneesh Kumar K.V 240261628a3fSMingming Cao /* start a new transaction*/ 240361628a3fSMingming Cao handle = ext4_journal_start(inode, needed_blocks); 240461628a3fSMingming Cao if (IS_ERR(handle)) { 240561628a3fSMingming Cao ret = PTR_ERR(handle); 24061693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2407fbe845ddSCurt Wohlgemuth "%ld pages, ino %lu; err %d", __func__, 2408a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 24093c1fcb2cSNamjae Jeon blk_finish_plug(&plug); 241061628a3fSMingming Cao goto out_writepages; 241161628a3fSMingming Cao } 2412f63e6005STheodore Ts'o 2413f63e6005STheodore Ts'o /* 24148eb9e5ceSTheodore Ts'o * Now call write_cache_pages_da() to find the next 2415f63e6005STheodore Ts'o * contiguous region of logical blocks that need 24168eb9e5ceSTheodore Ts'o * blocks to be allocated by ext4 and submit them. 2417f63e6005STheodore Ts'o */ 241872f84e65SEric Sandeen ret = write_cache_pages_da(mapping, wbc, &mpd, &done_index); 2419f63e6005STheodore Ts'o /* 2420af901ca1SAndré Goddard Rosa * If we have a contiguous extent of pages and we 2421f63e6005STheodore Ts'o * haven't done the I/O yet, map the blocks and submit 2422f63e6005STheodore Ts'o * them for I/O. 2423f63e6005STheodore Ts'o */ 2424f63e6005STheodore Ts'o if (!mpd.io_done && mpd.next_page != mpd.first_page) { 24255a87b7a5STheodore Ts'o mpage_da_map_and_submit(&mpd); 2426f63e6005STheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 2427f63e6005STheodore Ts'o } 2428b3a3ca8cSTheodore Ts'o trace_ext4_da_write_pages(inode, &mpd); 2429f63e6005STheodore Ts'o wbc->nr_to_write -= mpd.pages_written; 2430df22291fSAneesh Kumar K.V 243161628a3fSMingming Cao ext4_journal_stop(handle); 2432df22291fSAneesh Kumar K.V 24338f64b32eSEric Sandeen if ((mpd.retval == -ENOSPC) && sbi->s_journal) { 243422208dedSAneesh Kumar K.V /* commit the transaction which would 243522208dedSAneesh Kumar K.V * free blocks released in the transaction 243622208dedSAneesh Kumar K.V * and try again 243722208dedSAneesh Kumar K.V */ 2438df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 243922208dedSAneesh Kumar K.V ret = 0; 244022208dedSAneesh Kumar K.V } else if (ret == MPAGE_DA_EXTENT_TAIL) { 2441a1d6cc56SAneesh Kumar K.V /* 24428de49e67SKazuya Mio * Got one extent now try with rest of the pages. 24438de49e67SKazuya Mio * If mpd.retval is set -EIO, journal is aborted. 24448de49e67SKazuya Mio * So we don't need to write any more. 2445a1d6cc56SAneesh Kumar K.V */ 244622208dedSAneesh Kumar K.V pages_written += mpd.pages_written; 24478de49e67SKazuya Mio ret = mpd.retval; 24482acf2c26SAneesh Kumar K.V io_done = 1; 244922208dedSAneesh Kumar K.V } else if (wbc->nr_to_write) 245061628a3fSMingming Cao /* 245161628a3fSMingming Cao * There is no more writeout needed 245261628a3fSMingming Cao * or we requested for a noblocking writeout 245361628a3fSMingming Cao * and we found the device congested 245461628a3fSMingming Cao */ 245561628a3fSMingming Cao break; 245661628a3fSMingming Cao } 24571bce63d1SShaohua Li blk_finish_plug(&plug); 24582acf2c26SAneesh Kumar K.V if (!io_done && !cycled) { 24592acf2c26SAneesh Kumar K.V cycled = 1; 24602acf2c26SAneesh Kumar K.V index = 0; 24612acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 24622acf2c26SAneesh Kumar K.V wbc->range_end = mapping->writeback_index - 1; 24632acf2c26SAneesh Kumar K.V goto retry; 24642acf2c26SAneesh Kumar K.V } 246561628a3fSMingming Cao 246622208dedSAneesh Kumar K.V /* Update index */ 24672acf2c26SAneesh Kumar K.V wbc->range_cyclic = range_cyclic; 246822208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 246922208dedSAneesh Kumar K.V /* 247022208dedSAneesh Kumar K.V * set the writeback_index so that range_cyclic 247122208dedSAneesh Kumar K.V * mode will write it back later 247222208dedSAneesh Kumar K.V */ 247372f84e65SEric Sandeen mapping->writeback_index = done_index; 2474a1d6cc56SAneesh Kumar K.V 247561628a3fSMingming Cao out_writepages: 247622208dedSAneesh Kumar K.V wbc->nr_to_write -= nr_to_writebump; 2477de89de6eSTheodore Ts'o wbc->range_start = range_start; 24789bffad1eSTheodore Ts'o trace_ext4_da_writepages_result(inode, wbc, ret, pages_written); 247961628a3fSMingming Cao return ret; 248064769240SAlex Tomas } 248164769240SAlex Tomas 248279f0be8dSAneesh Kumar K.V #define FALL_BACK_TO_NONDELALLOC 1 248379f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 248479f0be8dSAneesh Kumar K.V { 248579f0be8dSAneesh Kumar K.V s64 free_blocks, dirty_blocks; 248679f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 248779f0be8dSAneesh Kumar K.V 248879f0be8dSAneesh Kumar K.V /* 248979f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 249079f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 2491179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 249279f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 249379f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 249479f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 249579f0be8dSAneesh Kumar K.V */ 249657042651STheodore Ts'o free_blocks = EXT4_C2B(sbi, 249757042651STheodore Ts'o percpu_counter_read_positive(&sbi->s_freeclusters_counter)); 249857042651STheodore Ts'o dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyclusters_counter); 249900d4e736STheodore Ts'o /* 250000d4e736STheodore Ts'o * Start pushing delalloc when 1/2 of free blocks are dirty. 250100d4e736STheodore Ts'o */ 250200d4e736STheodore Ts'o if (dirty_blocks && (free_blocks < 2 * dirty_blocks) && 250300d4e736STheodore Ts'o !writeback_in_progress(sb->s_bdi) && 250400d4e736STheodore Ts'o down_read_trylock(&sb->s_umount)) { 250500d4e736STheodore Ts'o writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE); 250600d4e736STheodore Ts'o up_read(&sb->s_umount); 250700d4e736STheodore Ts'o } 250800d4e736STheodore Ts'o 250979f0be8dSAneesh Kumar K.V if (2 * free_blocks < 3 * dirty_blocks || 2510df55c99dSTheodore Ts'o free_blocks < (dirty_blocks + EXT4_FREECLUSTERS_WATERMARK)) { 251179f0be8dSAneesh Kumar K.V /* 2512c8afb446SEric Sandeen * free block count is less than 150% of dirty blocks 2513c8afb446SEric Sandeen * or free blocks is less than watermark 251479f0be8dSAneesh Kumar K.V */ 251579f0be8dSAneesh Kumar K.V return 1; 251679f0be8dSAneesh Kumar K.V } 251779f0be8dSAneesh Kumar K.V return 0; 251879f0be8dSAneesh Kumar K.V } 251979f0be8dSAneesh Kumar K.V 252064769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 252164769240SAlex Tomas loff_t pos, unsigned len, unsigned flags, 252264769240SAlex Tomas struct page **pagep, void **fsdata) 252364769240SAlex Tomas { 252472b8ab9dSEric Sandeen int ret, retries = 0; 252564769240SAlex Tomas struct page *page; 252664769240SAlex Tomas pgoff_t index; 252764769240SAlex Tomas struct inode *inode = mapping->host; 252864769240SAlex Tomas handle_t *handle; 252964769240SAlex Tomas 253064769240SAlex Tomas index = pos >> PAGE_CACHE_SHIFT; 253179f0be8dSAneesh Kumar K.V 253279f0be8dSAneesh Kumar K.V if (ext4_nonda_switch(inode->i_sb)) { 253379f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 253479f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 253579f0be8dSAneesh Kumar K.V len, flags, pagep, fsdata); 253679f0be8dSAneesh Kumar K.V } 253779f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 25389bffad1eSTheodore Ts'o trace_ext4_da_write_begin(inode, pos, len, flags); 2539d2a17637SMingming Cao retry: 254064769240SAlex Tomas /* 254164769240SAlex Tomas * With delayed allocation, we don't log the i_disksize update 254264769240SAlex Tomas * if there is delayed block allocation. But we still need 254364769240SAlex Tomas * to journalling the i_disksize update if writes to the end 254464769240SAlex Tomas * of file which has an already mapped buffer. 254564769240SAlex Tomas */ 254664769240SAlex Tomas handle = ext4_journal_start(inode, 1); 254764769240SAlex Tomas if (IS_ERR(handle)) { 254864769240SAlex Tomas ret = PTR_ERR(handle); 254964769240SAlex Tomas goto out; 255064769240SAlex Tomas } 2551ebd3610bSJan Kara /* We cannot recurse into the filesystem as the transaction is already 2552ebd3610bSJan Kara * started */ 2553ebd3610bSJan Kara flags |= AOP_FLAG_NOFS; 255464769240SAlex Tomas 255554566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 2556d5a0d4f7SEric Sandeen if (!page) { 2557d5a0d4f7SEric Sandeen ext4_journal_stop(handle); 2558d5a0d4f7SEric Sandeen ret = -ENOMEM; 2559d5a0d4f7SEric Sandeen goto out; 2560d5a0d4f7SEric Sandeen } 256164769240SAlex Tomas *pagep = page; 256264769240SAlex Tomas 25636e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep); 256464769240SAlex Tomas if (ret < 0) { 256564769240SAlex Tomas unlock_page(page); 256664769240SAlex Tomas ext4_journal_stop(handle); 256764769240SAlex Tomas page_cache_release(page); 2568ae4d5372SAneesh Kumar K.V /* 2569ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 2570ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 2571ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 2572ae4d5372SAneesh Kumar K.V */ 2573ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 2574b9a4207dSJan Kara ext4_truncate_failed_write(inode); 257564769240SAlex Tomas } 257664769240SAlex Tomas 2577d2a17637SMingming Cao if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 2578d2a17637SMingming Cao goto retry; 257964769240SAlex Tomas out: 258064769240SAlex Tomas return ret; 258164769240SAlex Tomas } 258264769240SAlex Tomas 2583632eaeabSMingming Cao /* 2584632eaeabSMingming Cao * Check if we should update i_disksize 2585632eaeabSMingming Cao * when write to the end of file but not require block allocation 2586632eaeabSMingming Cao */ 2587632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page, 2588632eaeabSMingming Cao unsigned long offset) 2589632eaeabSMingming Cao { 2590632eaeabSMingming Cao struct buffer_head *bh; 2591632eaeabSMingming Cao struct inode *inode = page->mapping->host; 2592632eaeabSMingming Cao unsigned int idx; 2593632eaeabSMingming Cao int i; 2594632eaeabSMingming Cao 2595632eaeabSMingming Cao bh = page_buffers(page); 2596632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 2597632eaeabSMingming Cao 2598632eaeabSMingming Cao for (i = 0; i < idx; i++) 2599632eaeabSMingming Cao bh = bh->b_this_page; 2600632eaeabSMingming Cao 260129fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 2602632eaeabSMingming Cao return 0; 2603632eaeabSMingming Cao return 1; 2604632eaeabSMingming Cao } 2605632eaeabSMingming Cao 260664769240SAlex Tomas static int ext4_da_write_end(struct file *file, 260764769240SAlex Tomas struct address_space *mapping, 260864769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 260964769240SAlex Tomas struct page *page, void *fsdata) 261064769240SAlex Tomas { 261164769240SAlex Tomas struct inode *inode = mapping->host; 261264769240SAlex Tomas int ret = 0, ret2; 261364769240SAlex Tomas handle_t *handle = ext4_journal_current_handle(); 261464769240SAlex Tomas loff_t new_i_size; 2615632eaeabSMingming Cao unsigned long start, end; 261679f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 261779f0be8dSAneesh Kumar K.V 261879f0be8dSAneesh Kumar K.V if (write_mode == FALL_BACK_TO_NONDELALLOC) { 26193d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 26203d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 262179f0be8dSAneesh Kumar K.V return ext4_ordered_write_end(file, mapping, pos, 262279f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 26233d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 262479f0be8dSAneesh Kumar K.V return ext4_writeback_write_end(file, mapping, pos, 262579f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 26263d2b1582SLukas Czerner default: 262779f0be8dSAneesh Kumar K.V BUG(); 262879f0be8dSAneesh Kumar K.V } 262979f0be8dSAneesh Kumar K.V } 2630632eaeabSMingming Cao 26319bffad1eSTheodore Ts'o trace_ext4_da_write_end(inode, pos, len, copied); 2632632eaeabSMingming Cao start = pos & (PAGE_CACHE_SIZE - 1); 2633632eaeabSMingming Cao end = start + copied - 1; 263464769240SAlex Tomas 263564769240SAlex Tomas /* 263664769240SAlex Tomas * generic_write_end() will run mark_inode_dirty() if i_size 263764769240SAlex Tomas * changes. So let's piggyback the i_disksize mark_inode_dirty 263864769240SAlex Tomas * into that. 263964769240SAlex Tomas */ 264064769240SAlex Tomas 264164769240SAlex Tomas new_i_size = pos + copied; 2642ea51d132SAndrea Arcangeli if (copied && new_i_size > EXT4_I(inode)->i_disksize) { 2643632eaeabSMingming Cao if (ext4_da_should_update_i_disksize(page, end)) { 2644632eaeabSMingming Cao down_write(&EXT4_I(inode)->i_data_sem); 2645632eaeabSMingming Cao if (new_i_size > EXT4_I(inode)->i_disksize) { 264664769240SAlex Tomas /* 2647632eaeabSMingming Cao * Updating i_disksize when extending file 2648632eaeabSMingming Cao * without needing block allocation 264964769240SAlex Tomas */ 265064769240SAlex Tomas if (ext4_should_order_data(inode)) 2651632eaeabSMingming Cao ret = ext4_jbd2_file_inode(handle, 2652632eaeabSMingming Cao inode); 265364769240SAlex Tomas 265464769240SAlex Tomas EXT4_I(inode)->i_disksize = new_i_size; 265564769240SAlex Tomas } 2656632eaeabSMingming Cao up_write(&EXT4_I(inode)->i_data_sem); 2657cf17fea6SAneesh Kumar K.V /* We need to mark inode dirty even if 2658cf17fea6SAneesh Kumar K.V * new_i_size is less that inode->i_size 2659cf17fea6SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 2660cf17fea6SAneesh Kumar K.V */ 2661cf17fea6SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 2662632eaeabSMingming Cao } 2663632eaeabSMingming Cao } 266464769240SAlex Tomas ret2 = generic_write_end(file, mapping, pos, len, copied, 266564769240SAlex Tomas page, fsdata); 266664769240SAlex Tomas copied = ret2; 266764769240SAlex Tomas if (ret2 < 0) 266864769240SAlex Tomas ret = ret2; 266964769240SAlex Tomas ret2 = ext4_journal_stop(handle); 267064769240SAlex Tomas if (!ret) 267164769240SAlex Tomas ret = ret2; 267264769240SAlex Tomas 267364769240SAlex Tomas return ret ? ret : copied; 267464769240SAlex Tomas } 267564769240SAlex Tomas 267664769240SAlex Tomas static void ext4_da_invalidatepage(struct page *page, unsigned long offset) 267764769240SAlex Tomas { 267864769240SAlex Tomas /* 267964769240SAlex Tomas * Drop reserved blocks 268064769240SAlex Tomas */ 268164769240SAlex Tomas BUG_ON(!PageLocked(page)); 268264769240SAlex Tomas if (!page_has_buffers(page)) 268364769240SAlex Tomas goto out; 268464769240SAlex Tomas 2685d2a17637SMingming Cao ext4_da_page_release_reservation(page, offset); 268664769240SAlex Tomas 268764769240SAlex Tomas out: 268864769240SAlex Tomas ext4_invalidatepage(page, offset); 268964769240SAlex Tomas 269064769240SAlex Tomas return; 269164769240SAlex Tomas } 269264769240SAlex Tomas 2693ccd2506bSTheodore Ts'o /* 2694ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 2695ccd2506bSTheodore Ts'o */ 2696ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 2697ccd2506bSTheodore Ts'o { 2698fb40ba0dSTheodore Ts'o trace_ext4_alloc_da_blocks(inode); 2699fb40ba0dSTheodore Ts'o 2700ccd2506bSTheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks && 2701ccd2506bSTheodore Ts'o !EXT4_I(inode)->i_reserved_meta_blocks) 2702ccd2506bSTheodore Ts'o return 0; 2703ccd2506bSTheodore Ts'o 2704ccd2506bSTheodore Ts'o /* 2705ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 2706ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 2707ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 2708ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 2709ccd2506bSTheodore Ts'o * would require replicating code paths in: 2710ccd2506bSTheodore Ts'o * 2711ccd2506bSTheodore Ts'o * ext4_da_writepages() -> 2712ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 2713ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 2714ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 2715ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 2716ccd2506bSTheodore Ts'o * 2717ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 2718ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 2719ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 2720ccd2506bSTheodore Ts'o * doing I/O at all. 2721ccd2506bSTheodore Ts'o * 2722ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 2723380cf090SWu Fengguang * the pages by calling redirty_page_for_writepage() but that 2724ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 2725ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 272625985edcSLucas De Marchi * simplifying them because we wouldn't actually intend to 2727ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 2728ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 2729ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 2730ccd2506bSTheodore Ts'o * 2731ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 2732ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 2733ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 2734ccd2506bSTheodore Ts'o */ 2735ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 2736ccd2506bSTheodore Ts'o } 273764769240SAlex Tomas 273864769240SAlex Tomas /* 2739ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 2740ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 2741ac27a0ecSDave Kleikamp * 2742ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 2743617ba13bSMingming Cao * journal. If somebody makes a swapfile on an ext4 data-journaling 2744ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 2745ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 2746ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 2747ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 2748ac27a0ecSDave Kleikamp * 2749ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 2750ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 2751ac27a0ecSDave Kleikamp */ 2752617ba13bSMingming Cao static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 2753ac27a0ecSDave Kleikamp { 2754ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 2755ac27a0ecSDave Kleikamp journal_t *journal; 2756ac27a0ecSDave Kleikamp int err; 2757ac27a0ecSDave Kleikamp 275864769240SAlex Tomas if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 275964769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) { 276064769240SAlex Tomas /* 276164769240SAlex Tomas * With delalloc we want to sync the file 276264769240SAlex Tomas * so that we can make sure we allocate 276364769240SAlex Tomas * blocks for file 276464769240SAlex Tomas */ 276564769240SAlex Tomas filemap_write_and_wait(mapping); 276664769240SAlex Tomas } 276764769240SAlex Tomas 276819f5fb7aSTheodore Ts'o if (EXT4_JOURNAL(inode) && 276919f5fb7aSTheodore Ts'o ext4_test_inode_state(inode, EXT4_STATE_JDATA)) { 2770ac27a0ecSDave Kleikamp /* 2771ac27a0ecSDave Kleikamp * This is a REALLY heavyweight approach, but the use of 2772ac27a0ecSDave Kleikamp * bmap on dirty files is expected to be extremely rare: 2773ac27a0ecSDave Kleikamp * only if we run lilo or swapon on a freshly made file 2774ac27a0ecSDave Kleikamp * do we expect this to happen. 2775ac27a0ecSDave Kleikamp * 2776ac27a0ecSDave Kleikamp * (bmap requires CAP_SYS_RAWIO so this does not 2777ac27a0ecSDave Kleikamp * represent an unprivileged user DOS attack --- we'd be 2778ac27a0ecSDave Kleikamp * in trouble if mortal users could trigger this path at 2779ac27a0ecSDave Kleikamp * will.) 2780ac27a0ecSDave Kleikamp * 2781617ba13bSMingming Cao * NB. EXT4_STATE_JDATA is not set on files other than 2782ac27a0ecSDave Kleikamp * regular files. If somebody wants to bmap a directory 2783ac27a0ecSDave Kleikamp * or symlink and gets confused because the buffer 2784ac27a0ecSDave Kleikamp * hasn't yet been flushed to disk, they deserve 2785ac27a0ecSDave Kleikamp * everything they get. 2786ac27a0ecSDave Kleikamp */ 2787ac27a0ecSDave Kleikamp 278819f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_JDATA); 2789617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 2790dab291afSMingming Cao jbd2_journal_lock_updates(journal); 2791dab291afSMingming Cao err = jbd2_journal_flush(journal); 2792dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 2793ac27a0ecSDave Kleikamp 2794ac27a0ecSDave Kleikamp if (err) 2795ac27a0ecSDave Kleikamp return 0; 2796ac27a0ecSDave Kleikamp } 2797ac27a0ecSDave Kleikamp 2798617ba13bSMingming Cao return generic_block_bmap(mapping, block, ext4_get_block); 2799ac27a0ecSDave Kleikamp } 2800ac27a0ecSDave Kleikamp 2801617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page) 2802ac27a0ecSDave Kleikamp { 28030562e0baSJiaying Zhang trace_ext4_readpage(page); 2804617ba13bSMingming Cao return mpage_readpage(page, ext4_get_block); 2805ac27a0ecSDave Kleikamp } 2806ac27a0ecSDave Kleikamp 2807ac27a0ecSDave Kleikamp static int 2808617ba13bSMingming Cao ext4_readpages(struct file *file, struct address_space *mapping, 2809ac27a0ecSDave Kleikamp struct list_head *pages, unsigned nr_pages) 2810ac27a0ecSDave Kleikamp { 2811617ba13bSMingming Cao return mpage_readpages(mapping, pages, nr_pages, ext4_get_block); 2812ac27a0ecSDave Kleikamp } 2813ac27a0ecSDave Kleikamp 2814744692dcSJiaying Zhang static void ext4_invalidatepage_free_endio(struct page *page, unsigned long offset) 2815744692dcSJiaying Zhang { 2816744692dcSJiaying Zhang struct buffer_head *head, *bh; 2817744692dcSJiaying Zhang unsigned int curr_off = 0; 2818744692dcSJiaying Zhang 2819744692dcSJiaying Zhang if (!page_has_buffers(page)) 2820744692dcSJiaying Zhang return; 2821744692dcSJiaying Zhang head = bh = page_buffers(page); 2822744692dcSJiaying Zhang do { 2823744692dcSJiaying Zhang if (offset <= curr_off && test_clear_buffer_uninit(bh) 2824744692dcSJiaying Zhang && bh->b_private) { 2825744692dcSJiaying Zhang ext4_free_io_end(bh->b_private); 2826744692dcSJiaying Zhang bh->b_private = NULL; 2827744692dcSJiaying Zhang bh->b_end_io = NULL; 2828744692dcSJiaying Zhang } 2829744692dcSJiaying Zhang curr_off = curr_off + bh->b_size; 2830744692dcSJiaying Zhang bh = bh->b_this_page; 2831744692dcSJiaying Zhang } while (bh != head); 2832744692dcSJiaying Zhang } 2833744692dcSJiaying Zhang 2834617ba13bSMingming Cao static void ext4_invalidatepage(struct page *page, unsigned long offset) 2835ac27a0ecSDave Kleikamp { 2836617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 2837ac27a0ecSDave Kleikamp 28380562e0baSJiaying Zhang trace_ext4_invalidatepage(page, offset); 28390562e0baSJiaying Zhang 2840ac27a0ecSDave Kleikamp /* 2841744692dcSJiaying Zhang * free any io_end structure allocated for buffers to be discarded 2842744692dcSJiaying Zhang */ 2843744692dcSJiaying Zhang if (ext4_should_dioread_nolock(page->mapping->host)) 2844744692dcSJiaying Zhang ext4_invalidatepage_free_endio(page, offset); 2845744692dcSJiaying Zhang /* 2846ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 2847ac27a0ecSDave Kleikamp */ 2848ac27a0ecSDave Kleikamp if (offset == 0) 2849ac27a0ecSDave Kleikamp ClearPageChecked(page); 2850ac27a0ecSDave Kleikamp 28510390131bSFrank Mayhar if (journal) 2852dab291afSMingming Cao jbd2_journal_invalidatepage(journal, page, offset); 28530390131bSFrank Mayhar else 28540390131bSFrank Mayhar block_invalidatepage(page, offset); 2855ac27a0ecSDave Kleikamp } 2856ac27a0ecSDave Kleikamp 2857617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait) 2858ac27a0ecSDave Kleikamp { 2859617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 2860ac27a0ecSDave Kleikamp 28610562e0baSJiaying Zhang trace_ext4_releasepage(page); 28620562e0baSJiaying Zhang 2863ac27a0ecSDave Kleikamp WARN_ON(PageChecked(page)); 2864ac27a0ecSDave Kleikamp if (!page_has_buffers(page)) 2865ac27a0ecSDave Kleikamp return 0; 28660390131bSFrank Mayhar if (journal) 2867dab291afSMingming Cao return jbd2_journal_try_to_free_buffers(journal, page, wait); 28680390131bSFrank Mayhar else 28690390131bSFrank Mayhar return try_to_free_buffers(page); 2870ac27a0ecSDave Kleikamp } 2871ac27a0ecSDave Kleikamp 2872ac27a0ecSDave Kleikamp /* 28732ed88685STheodore Ts'o * ext4_get_block used when preparing for a DIO write or buffer write. 28742ed88685STheodore Ts'o * We allocate an uinitialized extent if blocks haven't been allocated. 28752ed88685STheodore Ts'o * The extent will be converted to initialized after the IO is complete. 28762ed88685STheodore Ts'o */ 2877c7064ef1SJiaying Zhang static int ext4_get_block_write(struct inode *inode, sector_t iblock, 28784c0425ffSMingming Cao struct buffer_head *bh_result, int create) 28794c0425ffSMingming Cao { 2880c7064ef1SJiaying Zhang ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n", 28818d5d02e6SMingming Cao inode->i_ino, create); 28822ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh_result, 28832ed88685STheodore Ts'o EXT4_GET_BLOCKS_IO_CREATE_EXT); 28844c0425ffSMingming Cao } 28854c0425ffSMingming Cao 2886729f52c6SZheng Liu static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock, 28878b0f165fSAnatol Pomozov struct buffer_head *bh_result, int create) 2888729f52c6SZheng Liu { 28898b0f165fSAnatol Pomozov ext4_debug("ext4_get_block_write_nolock: inode %lu, create flag %d\n", 28908b0f165fSAnatol Pomozov inode->i_ino, create); 28918b0f165fSAnatol Pomozov return _ext4_get_block(inode, iblock, bh_result, 28928b0f165fSAnatol Pomozov EXT4_GET_BLOCKS_NO_LOCK); 2893729f52c6SZheng Liu } 2894729f52c6SZheng Liu 28954c0425ffSMingming Cao static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset, 2896552ef802SChristoph Hellwig ssize_t size, void *private, int ret, 2897552ef802SChristoph Hellwig bool is_async) 28984c0425ffSMingming Cao { 289972c5052dSChristoph Hellwig struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode; 29004c0425ffSMingming Cao ext4_io_end_t *io_end = iocb->private; 29014c0425ffSMingming Cao 29024b70df18SMingming /* if not async direct IO or dio with 0 bytes write, just return */ 29034b70df18SMingming if (!io_end || !size) 2904552ef802SChristoph Hellwig goto out; 29054b70df18SMingming 29068d5d02e6SMingming Cao ext_debug("ext4_end_io_dio(): io_end 0x%p " 2907ace36ad4SJoe Perches "for inode %lu, iocb 0x%p, offset %llu, size %zd\n", 29088d5d02e6SMingming Cao iocb->private, io_end->inode->i_ino, iocb, offset, 29098d5d02e6SMingming Cao size); 29108d5d02e6SMingming Cao 2911b5a7e970STheodore Ts'o iocb->private = NULL; 2912b5a7e970STheodore Ts'o 29138d5d02e6SMingming Cao /* if not aio dio with unwritten extents, just free io and return */ 2914bd2d0210STheodore Ts'o if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) { 29158d5d02e6SMingming Cao ext4_free_io_end(io_end); 29165b3ff237Sjiayingz@google.com (Jiaying Zhang) out: 29175b3ff237Sjiayingz@google.com (Jiaying Zhang) if (is_async) 29185b3ff237Sjiayingz@google.com (Jiaying Zhang) aio_complete(iocb, ret, 0); 291972c5052dSChristoph Hellwig inode_dio_done(inode); 29205b3ff237Sjiayingz@google.com (Jiaying Zhang) return; 29218d5d02e6SMingming Cao } 29228d5d02e6SMingming Cao 29234c0425ffSMingming Cao io_end->offset = offset; 29244c0425ffSMingming Cao io_end->size = size; 29255b3ff237Sjiayingz@google.com (Jiaying Zhang) if (is_async) { 29265b3ff237Sjiayingz@google.com (Jiaying Zhang) io_end->iocb = iocb; 29275b3ff237Sjiayingz@google.com (Jiaying Zhang) io_end->result = ret; 29285b3ff237Sjiayingz@google.com (Jiaying Zhang) } 29294c0425ffSMingming Cao 293028a535f9SDmitry Monakhov ext4_add_complete_io(io_end); 29314c0425ffSMingming Cao } 2932c7064ef1SJiaying Zhang 2933744692dcSJiaying Zhang static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate) 2934744692dcSJiaying Zhang { 2935744692dcSJiaying Zhang ext4_io_end_t *io_end = bh->b_private; 2936744692dcSJiaying Zhang struct inode *inode; 2937744692dcSJiaying Zhang 2938744692dcSJiaying Zhang if (!test_clear_buffer_uninit(bh) || !io_end) 2939744692dcSJiaying Zhang goto out; 2940744692dcSJiaying Zhang 2941744692dcSJiaying Zhang if (!(io_end->inode->i_sb->s_flags & MS_ACTIVE)) { 294292b97816STheodore Ts'o ext4_msg(io_end->inode->i_sb, KERN_INFO, 294392b97816STheodore Ts'o "sb umounted, discard end_io request for inode %lu", 2944744692dcSJiaying Zhang io_end->inode->i_ino); 2945744692dcSJiaying Zhang ext4_free_io_end(io_end); 2946744692dcSJiaying Zhang goto out; 2947744692dcSJiaying Zhang } 2948744692dcSJiaying Zhang 294932c80b32STao Ma /* 295032c80b32STao Ma * It may be over-defensive here to check EXT4_IO_END_UNWRITTEN now, 295132c80b32STao Ma * but being more careful is always safe for the future change. 295232c80b32STao Ma */ 2953744692dcSJiaying Zhang inode = io_end->inode; 29540edeb71dSTao Ma ext4_set_io_unwritten_flag(inode, io_end); 295528a535f9SDmitry Monakhov ext4_add_complete_io(io_end); 2956744692dcSJiaying Zhang out: 2957744692dcSJiaying Zhang bh->b_private = NULL; 2958744692dcSJiaying Zhang bh->b_end_io = NULL; 2959744692dcSJiaying Zhang clear_buffer_uninit(bh); 2960744692dcSJiaying Zhang end_buffer_async_write(bh, uptodate); 2961744692dcSJiaying Zhang } 2962744692dcSJiaying Zhang 2963744692dcSJiaying Zhang static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode) 2964744692dcSJiaying Zhang { 2965744692dcSJiaying Zhang ext4_io_end_t *io_end; 2966744692dcSJiaying Zhang struct page *page = bh->b_page; 2967744692dcSJiaying Zhang loff_t offset = (sector_t)page->index << PAGE_CACHE_SHIFT; 2968744692dcSJiaying Zhang size_t size = bh->b_size; 2969744692dcSJiaying Zhang 2970744692dcSJiaying Zhang retry: 2971744692dcSJiaying Zhang io_end = ext4_init_io_end(inode, GFP_ATOMIC); 2972744692dcSJiaying Zhang if (!io_end) { 29736db26ffcSAndrew Morton pr_warn_ratelimited("%s: allocation fail\n", __func__); 2974744692dcSJiaying Zhang schedule(); 2975744692dcSJiaying Zhang goto retry; 2976744692dcSJiaying Zhang } 2977744692dcSJiaying Zhang io_end->offset = offset; 2978744692dcSJiaying Zhang io_end->size = size; 2979744692dcSJiaying Zhang /* 2980744692dcSJiaying Zhang * We need to hold a reference to the page to make sure it 2981744692dcSJiaying Zhang * doesn't get evicted before ext4_end_io_work() has a chance 2982744692dcSJiaying Zhang * to convert the extent from written to unwritten. 2983744692dcSJiaying Zhang */ 2984744692dcSJiaying Zhang io_end->page = page; 2985744692dcSJiaying Zhang get_page(io_end->page); 2986744692dcSJiaying Zhang 2987744692dcSJiaying Zhang bh->b_private = io_end; 2988744692dcSJiaying Zhang bh->b_end_io = ext4_end_io_buffer_write; 2989744692dcSJiaying Zhang return 0; 2990744692dcSJiaying Zhang } 2991744692dcSJiaying Zhang 29924c0425ffSMingming Cao /* 29934c0425ffSMingming Cao * For ext4 extent files, ext4 will do direct-io write to holes, 29944c0425ffSMingming Cao * preallocated extents, and those write extend the file, no need to 29954c0425ffSMingming Cao * fall back to buffered IO. 29964c0425ffSMingming Cao * 2997b595076aSUwe Kleine-König * For holes, we fallocate those blocks, mark them as uninitialized 29984c0425ffSMingming Cao * If those blocks were preallocated, we mark sure they are splited, but 2999b595076aSUwe Kleine-König * still keep the range to write as uninitialized. 30004c0425ffSMingming Cao * 30018d5d02e6SMingming Cao * The unwrritten extents will be converted to written when DIO is completed. 30028d5d02e6SMingming Cao * For async direct IO, since the IO may still pending when return, we 300325985edcSLucas De Marchi * set up an end_io call back function, which will do the conversion 30048d5d02e6SMingming Cao * when async direct IO completed. 30054c0425ffSMingming Cao * 30064c0425ffSMingming Cao * If the O_DIRECT write will extend the file then add this inode to the 30074c0425ffSMingming Cao * orphan list. So recovery will truncate it back to the original size 30084c0425ffSMingming Cao * if the machine crashes during the write. 30094c0425ffSMingming Cao * 30104c0425ffSMingming Cao */ 30114c0425ffSMingming Cao static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb, 30124c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 30134c0425ffSMingming Cao unsigned long nr_segs) 30144c0425ffSMingming Cao { 30154c0425ffSMingming Cao struct file *file = iocb->ki_filp; 30164c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 30174c0425ffSMingming Cao ssize_t ret; 30184c0425ffSMingming Cao size_t count = iov_length(iov, nr_segs); 30194c0425ffSMingming Cao 30204c0425ffSMingming Cao loff_t final_size = offset + count; 30214c0425ffSMingming Cao if (rw == WRITE && final_size <= inode->i_size) { 3022729f52c6SZheng Liu int overwrite = 0; 30238b0f165fSAnatol Pomozov get_block_t *get_block_func = NULL; 30248b0f165fSAnatol Pomozov int dio_flags = 0; 3025729f52c6SZheng Liu 30264bd809dbSZheng Liu BUG_ON(iocb->private == NULL); 30274bd809dbSZheng Liu 30284bd809dbSZheng Liu /* If we do a overwrite dio, i_mutex locking can be released */ 30294bd809dbSZheng Liu overwrite = *((int *)iocb->private); 30304bd809dbSZheng Liu 30314bd809dbSZheng Liu if (overwrite) { 30321f555cfaSDmitry Monakhov atomic_inc(&inode->i_dio_count); 30334bd809dbSZheng Liu down_read(&EXT4_I(inode)->i_data_sem); 30344bd809dbSZheng Liu mutex_unlock(&inode->i_mutex); 30354bd809dbSZheng Liu } 30364bd809dbSZheng Liu 30374c0425ffSMingming Cao /* 30388d5d02e6SMingming Cao * We could direct write to holes and fallocate. 30398d5d02e6SMingming Cao * 30408d5d02e6SMingming Cao * Allocated blocks to fill the hole are marked as uninitialized 304125985edcSLucas De Marchi * to prevent parallel buffered read to expose the stale data 30424c0425ffSMingming Cao * before DIO complete the data IO. 30438d5d02e6SMingming Cao * 30448d5d02e6SMingming Cao * As to previously fallocated extents, ext4 get_block 30454c0425ffSMingming Cao * will just simply mark the buffer mapped but still 30464c0425ffSMingming Cao * keep the extents uninitialized. 30474c0425ffSMingming Cao * 30488d5d02e6SMingming Cao * for non AIO case, we will convert those unwritten extents 30498d5d02e6SMingming Cao * to written after return back from blockdev_direct_IO. 30504c0425ffSMingming Cao * 30518d5d02e6SMingming Cao * for async DIO, the conversion needs to be defered when 30528d5d02e6SMingming Cao * the IO is completed. The ext4 end_io callback function 30538d5d02e6SMingming Cao * will be called to take care of the conversion work. 30548d5d02e6SMingming Cao * Here for async case, we allocate an io_end structure to 30558d5d02e6SMingming Cao * hook to the iocb. 30564c0425ffSMingming Cao */ 30578d5d02e6SMingming Cao iocb->private = NULL; 3058f45ee3a1SDmitry Monakhov ext4_inode_aio_set(inode, NULL); 30598d5d02e6SMingming Cao if (!is_sync_kiocb(iocb)) { 3060266991b1SJeff Moyer ext4_io_end_t *io_end = 3061266991b1SJeff Moyer ext4_init_io_end(inode, GFP_NOFS); 30624bd809dbSZheng Liu if (!io_end) { 30634bd809dbSZheng Liu ret = -ENOMEM; 30644bd809dbSZheng Liu goto retake_lock; 30654bd809dbSZheng Liu } 3066266991b1SJeff Moyer io_end->flag |= EXT4_IO_END_DIRECT; 3067266991b1SJeff Moyer iocb->private = io_end; 30688d5d02e6SMingming Cao /* 30698d5d02e6SMingming Cao * we save the io structure for current async 307079e83036SEric Sandeen * direct IO, so that later ext4_map_blocks() 30718d5d02e6SMingming Cao * could flag the io structure whether there 30728d5d02e6SMingming Cao * is a unwritten extents needs to be converted 30738d5d02e6SMingming Cao * when IO is completed. 30748d5d02e6SMingming Cao */ 3075f45ee3a1SDmitry Monakhov ext4_inode_aio_set(inode, io_end); 30768d5d02e6SMingming Cao } 30778d5d02e6SMingming Cao 30788b0f165fSAnatol Pomozov if (overwrite) { 30798b0f165fSAnatol Pomozov get_block_func = ext4_get_block_write_nolock; 30808b0f165fSAnatol Pomozov } else { 30818b0f165fSAnatol Pomozov get_block_func = ext4_get_block_write; 30828b0f165fSAnatol Pomozov dio_flags = DIO_LOCKING; 30838b0f165fSAnatol Pomozov } 3084729f52c6SZheng Liu ret = __blockdev_direct_IO(rw, iocb, inode, 3085729f52c6SZheng Liu inode->i_sb->s_bdev, iov, 3086729f52c6SZheng Liu offset, nr_segs, 30878b0f165fSAnatol Pomozov get_block_func, 3088729f52c6SZheng Liu ext4_end_io_dio, 3089729f52c6SZheng Liu NULL, 30908b0f165fSAnatol Pomozov dio_flags); 30918b0f165fSAnatol Pomozov 30928d5d02e6SMingming Cao if (iocb->private) 3093f45ee3a1SDmitry Monakhov ext4_inode_aio_set(inode, NULL); 30948d5d02e6SMingming Cao /* 30958d5d02e6SMingming Cao * The io_end structure takes a reference to the inode, 30968d5d02e6SMingming Cao * that structure needs to be destroyed and the 30978d5d02e6SMingming Cao * reference to the inode need to be dropped, when IO is 30988d5d02e6SMingming Cao * complete, even with 0 byte write, or failed. 30998d5d02e6SMingming Cao * 31008d5d02e6SMingming Cao * In the successful AIO DIO case, the io_end structure will be 31018d5d02e6SMingming Cao * desctroyed and the reference to the inode will be dropped 31028d5d02e6SMingming Cao * after the end_io call back function is called. 31038d5d02e6SMingming Cao * 31048d5d02e6SMingming Cao * In the case there is 0 byte write, or error case, since 31058d5d02e6SMingming Cao * VFS direct IO won't invoke the end_io call back function, 31068d5d02e6SMingming Cao * we need to free the end_io structure here. 31078d5d02e6SMingming Cao */ 31088d5d02e6SMingming Cao if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) { 31098d5d02e6SMingming Cao ext4_free_io_end(iocb->private); 31108d5d02e6SMingming Cao iocb->private = NULL; 3111729f52c6SZheng Liu } else if (ret > 0 && !overwrite && ext4_test_inode_state(inode, 31125f524950SMingming EXT4_STATE_DIO_UNWRITTEN)) { 3113109f5565SMingming int err; 31148d5d02e6SMingming Cao /* 31158d5d02e6SMingming Cao * for non AIO case, since the IO is already 311625985edcSLucas De Marchi * completed, we could do the conversion right here 31178d5d02e6SMingming Cao */ 3118109f5565SMingming err = ext4_convert_unwritten_extents(inode, 31198d5d02e6SMingming Cao offset, ret); 3120109f5565SMingming if (err < 0) 3121109f5565SMingming ret = err; 312219f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN); 3123109f5565SMingming } 31244bd809dbSZheng Liu 31254bd809dbSZheng Liu retake_lock: 31264bd809dbSZheng Liu /* take i_mutex locking again if we do a ovewrite dio */ 31274bd809dbSZheng Liu if (overwrite) { 31281f555cfaSDmitry Monakhov inode_dio_done(inode); 31294bd809dbSZheng Liu up_read(&EXT4_I(inode)->i_data_sem); 31304bd809dbSZheng Liu mutex_lock(&inode->i_mutex); 31314bd809dbSZheng Liu } 31324bd809dbSZheng Liu 31334c0425ffSMingming Cao return ret; 31344c0425ffSMingming Cao } 31358d5d02e6SMingming Cao 31368d5d02e6SMingming Cao /* for write the the end of file case, we fall back to old way */ 31374c0425ffSMingming Cao return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 31384c0425ffSMingming Cao } 31394c0425ffSMingming Cao 31404c0425ffSMingming Cao static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb, 31414c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 31424c0425ffSMingming Cao unsigned long nr_segs) 31434c0425ffSMingming Cao { 31444c0425ffSMingming Cao struct file *file = iocb->ki_filp; 31454c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 31460562e0baSJiaying Zhang ssize_t ret; 31474c0425ffSMingming Cao 314884ebd795STheodore Ts'o /* 314984ebd795STheodore Ts'o * If we are doing data journalling we don't support O_DIRECT 315084ebd795STheodore Ts'o */ 315184ebd795STheodore Ts'o if (ext4_should_journal_data(inode)) 315284ebd795STheodore Ts'o return 0; 315384ebd795STheodore Ts'o 31540562e0baSJiaying Zhang trace_ext4_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw); 315512e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 31560562e0baSJiaying Zhang ret = ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs); 31570562e0baSJiaying Zhang else 31580562e0baSJiaying Zhang ret = ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 31590562e0baSJiaying Zhang trace_ext4_direct_IO_exit(inode, offset, 31600562e0baSJiaying Zhang iov_length(iov, nr_segs), rw, ret); 31610562e0baSJiaying Zhang return ret; 31624c0425ffSMingming Cao } 31634c0425ffSMingming Cao 3164ac27a0ecSDave Kleikamp /* 3165617ba13bSMingming Cao * Pages can be marked dirty completely asynchronously from ext4's journalling 3166ac27a0ecSDave Kleikamp * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do 3167ac27a0ecSDave Kleikamp * much here because ->set_page_dirty is called under VFS locks. The page is 3168ac27a0ecSDave Kleikamp * not necessarily locked. 3169ac27a0ecSDave Kleikamp * 3170ac27a0ecSDave Kleikamp * We cannot just dirty the page and leave attached buffers clean, because the 3171ac27a0ecSDave Kleikamp * buffers' dirty state is "definitive". We cannot just set the buffers dirty 3172ac27a0ecSDave Kleikamp * or jbddirty because all the journalling code will explode. 3173ac27a0ecSDave Kleikamp * 3174ac27a0ecSDave Kleikamp * So what we do is to mark the page "pending dirty" and next time writepage 3175ac27a0ecSDave Kleikamp * is called, propagate that into the buffers appropriately. 3176ac27a0ecSDave Kleikamp */ 3177617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page) 3178ac27a0ecSDave Kleikamp { 3179ac27a0ecSDave Kleikamp SetPageChecked(page); 3180ac27a0ecSDave Kleikamp return __set_page_dirty_nobuffers(page); 3181ac27a0ecSDave Kleikamp } 3182ac27a0ecSDave Kleikamp 3183617ba13bSMingming Cao static const struct address_space_operations ext4_ordered_aops = { 3184617ba13bSMingming Cao .readpage = ext4_readpage, 3185617ba13bSMingming Cao .readpages = ext4_readpages, 318643ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3187bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3188bfc1af65SNick Piggin .write_end = ext4_ordered_write_end, 3189617ba13bSMingming Cao .bmap = ext4_bmap, 3190617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3191617ba13bSMingming Cao .releasepage = ext4_releasepage, 3192617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 3193ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 31948ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3195aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3196ac27a0ecSDave Kleikamp }; 3197ac27a0ecSDave Kleikamp 3198617ba13bSMingming Cao static const struct address_space_operations ext4_writeback_aops = { 3199617ba13bSMingming Cao .readpage = ext4_readpage, 3200617ba13bSMingming Cao .readpages = ext4_readpages, 320143ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3202bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3203bfc1af65SNick Piggin .write_end = ext4_writeback_write_end, 3204617ba13bSMingming Cao .bmap = ext4_bmap, 3205617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3206617ba13bSMingming Cao .releasepage = ext4_releasepage, 3207617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 3208ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 32098ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3210aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3211ac27a0ecSDave Kleikamp }; 3212ac27a0ecSDave Kleikamp 3213617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 3214617ba13bSMingming Cao .readpage = ext4_readpage, 3215617ba13bSMingming Cao .readpages = ext4_readpages, 321643ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3217bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3218bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 3219617ba13bSMingming Cao .set_page_dirty = ext4_journalled_set_page_dirty, 3220617ba13bSMingming Cao .bmap = ext4_bmap, 3221617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3222617ba13bSMingming Cao .releasepage = ext4_releasepage, 322384ebd795STheodore Ts'o .direct_IO = ext4_direct_IO, 32248ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3225aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3226ac27a0ecSDave Kleikamp }; 3227ac27a0ecSDave Kleikamp 322864769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 322964769240SAlex Tomas .readpage = ext4_readpage, 323064769240SAlex Tomas .readpages = ext4_readpages, 323143ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 323264769240SAlex Tomas .writepages = ext4_da_writepages, 323364769240SAlex Tomas .write_begin = ext4_da_write_begin, 323464769240SAlex Tomas .write_end = ext4_da_write_end, 323564769240SAlex Tomas .bmap = ext4_bmap, 323664769240SAlex Tomas .invalidatepage = ext4_da_invalidatepage, 323764769240SAlex Tomas .releasepage = ext4_releasepage, 323864769240SAlex Tomas .direct_IO = ext4_direct_IO, 323964769240SAlex Tomas .migratepage = buffer_migrate_page, 32408ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3241aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 324264769240SAlex Tomas }; 324364769240SAlex Tomas 3244617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 3245ac27a0ecSDave Kleikamp { 32463d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 32473d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 32483d2b1582SLukas Czerner if (test_opt(inode->i_sb, DELALLOC)) 3249cd1aac32SAneesh Kumar K.V inode->i_mapping->a_ops = &ext4_da_aops; 3250ac27a0ecSDave Kleikamp else 32513d2b1582SLukas Czerner inode->i_mapping->a_ops = &ext4_ordered_aops; 32523d2b1582SLukas Czerner break; 32533d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 32543d2b1582SLukas Czerner if (test_opt(inode->i_sb, DELALLOC)) 32553d2b1582SLukas Czerner inode->i_mapping->a_ops = &ext4_da_aops; 32563d2b1582SLukas Czerner else 32573d2b1582SLukas Czerner inode->i_mapping->a_ops = &ext4_writeback_aops; 32583d2b1582SLukas Czerner break; 32593d2b1582SLukas Czerner case EXT4_INODE_JOURNAL_DATA_MODE: 3260617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 32613d2b1582SLukas Czerner break; 32623d2b1582SLukas Czerner default: 32633d2b1582SLukas Czerner BUG(); 32643d2b1582SLukas Czerner } 3265ac27a0ecSDave Kleikamp } 3266ac27a0ecSDave Kleikamp 32674e96b2dbSAllison Henderson 32684e96b2dbSAllison Henderson /* 32694e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers() 32704e96b2dbSAllison Henderson * Wrapper function for ext4_discard_partial_page_buffers_no_lock. 32714e96b2dbSAllison Henderson * This function finds and locks the page containing the offset 32724e96b2dbSAllison Henderson * "from" and passes it to ext4_discard_partial_page_buffers_no_lock. 32734e96b2dbSAllison Henderson * Calling functions that already have the page locked should call 32744e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers_no_lock directly. 32754e96b2dbSAllison Henderson */ 32764e96b2dbSAllison Henderson int ext4_discard_partial_page_buffers(handle_t *handle, 32774e96b2dbSAllison Henderson struct address_space *mapping, loff_t from, 32784e96b2dbSAllison Henderson loff_t length, int flags) 32794e96b2dbSAllison Henderson { 32804e96b2dbSAllison Henderson struct inode *inode = mapping->host; 32814e96b2dbSAllison Henderson struct page *page; 32824e96b2dbSAllison Henderson int err = 0; 32834e96b2dbSAllison Henderson 32844e96b2dbSAllison Henderson page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT, 32854e96b2dbSAllison Henderson mapping_gfp_mask(mapping) & ~__GFP_FS); 32864e96b2dbSAllison Henderson if (!page) 32875129d05fSYongqiang Yang return -ENOMEM; 32884e96b2dbSAllison Henderson 32894e96b2dbSAllison Henderson err = ext4_discard_partial_page_buffers_no_lock(handle, inode, page, 32904e96b2dbSAllison Henderson from, length, flags); 32914e96b2dbSAllison Henderson 32924e96b2dbSAllison Henderson unlock_page(page); 32934e96b2dbSAllison Henderson page_cache_release(page); 32944e96b2dbSAllison Henderson return err; 32954e96b2dbSAllison Henderson } 32964e96b2dbSAllison Henderson 32974e96b2dbSAllison Henderson /* 32984e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers_no_lock() 32994e96b2dbSAllison Henderson * Zeros a page range of length 'length' starting from offset 'from'. 33004e96b2dbSAllison Henderson * Buffer heads that correspond to the block aligned regions of the 33014e96b2dbSAllison Henderson * zeroed range will be unmapped. Unblock aligned regions 33024e96b2dbSAllison Henderson * will have the corresponding buffer head mapped if needed so that 33034e96b2dbSAllison Henderson * that region of the page can be updated with the partial zero out. 33044e96b2dbSAllison Henderson * 33054e96b2dbSAllison Henderson * This function assumes that the page has already been locked. The 33064e96b2dbSAllison Henderson * The range to be discarded must be contained with in the given page. 33074e96b2dbSAllison Henderson * If the specified range exceeds the end of the page it will be shortened 33084e96b2dbSAllison Henderson * to the end of the page that corresponds to 'from'. This function is 33094e96b2dbSAllison Henderson * appropriate for updating a page and it buffer heads to be unmapped and 33104e96b2dbSAllison Henderson * zeroed for blocks that have been either released, or are going to be 33114e96b2dbSAllison Henderson * released. 33124e96b2dbSAllison Henderson * 33134e96b2dbSAllison Henderson * handle: The journal handle 33144e96b2dbSAllison Henderson * inode: The files inode 33154e96b2dbSAllison Henderson * page: A locked page that contains the offset "from" 33164907cb7bSAnatol Pomozov * from: The starting byte offset (from the beginning of the file) 33174e96b2dbSAllison Henderson * to begin discarding 33184e96b2dbSAllison Henderson * len: The length of bytes to discard 33194e96b2dbSAllison Henderson * flags: Optional flags that may be used: 33204e96b2dbSAllison Henderson * 33214e96b2dbSAllison Henderson * EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED 33224e96b2dbSAllison Henderson * Only zero the regions of the page whose buffer heads 33234e96b2dbSAllison Henderson * have already been unmapped. This flag is appropriate 33244907cb7bSAnatol Pomozov * for updating the contents of a page whose blocks may 33254e96b2dbSAllison Henderson * have already been released, and we only want to zero 33264e96b2dbSAllison Henderson * out the regions that correspond to those released blocks. 33274e96b2dbSAllison Henderson * 33284907cb7bSAnatol Pomozov * Returns zero on success or negative on failure. 33294e96b2dbSAllison Henderson */ 33305f163cc7SEric Sandeen static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle, 33314e96b2dbSAllison Henderson struct inode *inode, struct page *page, loff_t from, 33324e96b2dbSAllison Henderson loff_t length, int flags) 33334e96b2dbSAllison Henderson { 33344e96b2dbSAllison Henderson ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; 33354e96b2dbSAllison Henderson unsigned int offset = from & (PAGE_CACHE_SIZE-1); 33364e96b2dbSAllison Henderson unsigned int blocksize, max, pos; 33374e96b2dbSAllison Henderson ext4_lblk_t iblock; 33384e96b2dbSAllison Henderson struct buffer_head *bh; 33394e96b2dbSAllison Henderson int err = 0; 33404e96b2dbSAllison Henderson 33414e96b2dbSAllison Henderson blocksize = inode->i_sb->s_blocksize; 33424e96b2dbSAllison Henderson max = PAGE_CACHE_SIZE - offset; 33434e96b2dbSAllison Henderson 33444e96b2dbSAllison Henderson if (index != page->index) 33454e96b2dbSAllison Henderson return -EINVAL; 33464e96b2dbSAllison Henderson 33474e96b2dbSAllison Henderson /* 33484e96b2dbSAllison Henderson * correct length if it does not fall between 33494e96b2dbSAllison Henderson * 'from' and the end of the page 33504e96b2dbSAllison Henderson */ 33514e96b2dbSAllison Henderson if (length > max || length < 0) 33524e96b2dbSAllison Henderson length = max; 33534e96b2dbSAllison Henderson 33544e96b2dbSAllison Henderson iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); 33554e96b2dbSAllison Henderson 3356093e6e36SYongqiang Yang if (!page_has_buffers(page)) 33574e96b2dbSAllison Henderson create_empty_buffers(page, blocksize, 0); 33584e96b2dbSAllison Henderson 33594e96b2dbSAllison Henderson /* Find the buffer that contains "offset" */ 33604e96b2dbSAllison Henderson bh = page_buffers(page); 33614e96b2dbSAllison Henderson pos = blocksize; 33624e96b2dbSAllison Henderson while (offset >= pos) { 33634e96b2dbSAllison Henderson bh = bh->b_this_page; 33644e96b2dbSAllison Henderson iblock++; 33654e96b2dbSAllison Henderson pos += blocksize; 33664e96b2dbSAllison Henderson } 33674e96b2dbSAllison Henderson 33684e96b2dbSAllison Henderson pos = offset; 33694e96b2dbSAllison Henderson while (pos < offset + length) { 3370e260daf2SYongqiang Yang unsigned int end_of_block, range_to_discard; 3371e260daf2SYongqiang Yang 33724e96b2dbSAllison Henderson err = 0; 33734e96b2dbSAllison Henderson 33744e96b2dbSAllison Henderson /* The length of space left to zero and unmap */ 33754e96b2dbSAllison Henderson range_to_discard = offset + length - pos; 33764e96b2dbSAllison Henderson 33774e96b2dbSAllison Henderson /* The length of space until the end of the block */ 33784e96b2dbSAllison Henderson end_of_block = blocksize - (pos & (blocksize-1)); 33794e96b2dbSAllison Henderson 33804e96b2dbSAllison Henderson /* 33814e96b2dbSAllison Henderson * Do not unmap or zero past end of block 33824e96b2dbSAllison Henderson * for this buffer head 33834e96b2dbSAllison Henderson */ 33844e96b2dbSAllison Henderson if (range_to_discard > end_of_block) 33854e96b2dbSAllison Henderson range_to_discard = end_of_block; 33864e96b2dbSAllison Henderson 33874e96b2dbSAllison Henderson 33884e96b2dbSAllison Henderson /* 33894e96b2dbSAllison Henderson * Skip this buffer head if we are only zeroing unampped 33904e96b2dbSAllison Henderson * regions of the page 33914e96b2dbSAllison Henderson */ 33924e96b2dbSAllison Henderson if (flags & EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED && 33934e96b2dbSAllison Henderson buffer_mapped(bh)) 33944e96b2dbSAllison Henderson goto next; 33954e96b2dbSAllison Henderson 33964e96b2dbSAllison Henderson /* If the range is block aligned, unmap */ 33974e96b2dbSAllison Henderson if (range_to_discard == blocksize) { 33984e96b2dbSAllison Henderson clear_buffer_dirty(bh); 33994e96b2dbSAllison Henderson bh->b_bdev = NULL; 34004e96b2dbSAllison Henderson clear_buffer_mapped(bh); 34014e96b2dbSAllison Henderson clear_buffer_req(bh); 34024e96b2dbSAllison Henderson clear_buffer_new(bh); 34034e96b2dbSAllison Henderson clear_buffer_delay(bh); 34044e96b2dbSAllison Henderson clear_buffer_unwritten(bh); 34054e96b2dbSAllison Henderson clear_buffer_uptodate(bh); 34064e96b2dbSAllison Henderson zero_user(page, pos, range_to_discard); 34074e96b2dbSAllison Henderson BUFFER_TRACE(bh, "Buffer discarded"); 34084e96b2dbSAllison Henderson goto next; 34094e96b2dbSAllison Henderson } 34104e96b2dbSAllison Henderson 34114e96b2dbSAllison Henderson /* 34124e96b2dbSAllison Henderson * If this block is not completely contained in the range 34134e96b2dbSAllison Henderson * to be discarded, then it is not going to be released. Because 34144e96b2dbSAllison Henderson * we need to keep this block, we need to make sure this part 34154e96b2dbSAllison Henderson * of the page is uptodate before we modify it by writeing 34164e96b2dbSAllison Henderson * partial zeros on it. 34174e96b2dbSAllison Henderson */ 34184e96b2dbSAllison Henderson if (!buffer_mapped(bh)) { 34194e96b2dbSAllison Henderson /* 34204e96b2dbSAllison Henderson * Buffer head must be mapped before we can read 34214e96b2dbSAllison Henderson * from the block 34224e96b2dbSAllison Henderson */ 34234e96b2dbSAllison Henderson BUFFER_TRACE(bh, "unmapped"); 34244e96b2dbSAllison Henderson ext4_get_block(inode, iblock, bh, 0); 34254e96b2dbSAllison Henderson /* unmapped? It's a hole - nothing to do */ 34264e96b2dbSAllison Henderson if (!buffer_mapped(bh)) { 34274e96b2dbSAllison Henderson BUFFER_TRACE(bh, "still unmapped"); 34284e96b2dbSAllison Henderson goto next; 34294e96b2dbSAllison Henderson } 34304e96b2dbSAllison Henderson } 34314e96b2dbSAllison Henderson 34324e96b2dbSAllison Henderson /* Ok, it's mapped. Make sure it's up-to-date */ 34334e96b2dbSAllison Henderson if (PageUptodate(page)) 34344e96b2dbSAllison Henderson set_buffer_uptodate(bh); 34354e96b2dbSAllison Henderson 34364e96b2dbSAllison Henderson if (!buffer_uptodate(bh)) { 34374e96b2dbSAllison Henderson err = -EIO; 34384e96b2dbSAllison Henderson ll_rw_block(READ, 1, &bh); 34394e96b2dbSAllison Henderson wait_on_buffer(bh); 34404e96b2dbSAllison Henderson /* Uhhuh. Read error. Complain and punt.*/ 34414e96b2dbSAllison Henderson if (!buffer_uptodate(bh)) 34424e96b2dbSAllison Henderson goto next; 34434e96b2dbSAllison Henderson } 34444e96b2dbSAllison Henderson 34454e96b2dbSAllison Henderson if (ext4_should_journal_data(inode)) { 34464e96b2dbSAllison Henderson BUFFER_TRACE(bh, "get write access"); 34474e96b2dbSAllison Henderson err = ext4_journal_get_write_access(handle, bh); 34484e96b2dbSAllison Henderson if (err) 34494e96b2dbSAllison Henderson goto next; 34504e96b2dbSAllison Henderson } 34514e96b2dbSAllison Henderson 34524e96b2dbSAllison Henderson zero_user(page, pos, range_to_discard); 34534e96b2dbSAllison Henderson 34544e96b2dbSAllison Henderson err = 0; 34554e96b2dbSAllison Henderson if (ext4_should_journal_data(inode)) { 34564e96b2dbSAllison Henderson err = ext4_handle_dirty_metadata(handle, inode, bh); 3457decbd919STheodore Ts'o } else 34584e96b2dbSAllison Henderson mark_buffer_dirty(bh); 34594e96b2dbSAllison Henderson 34604e96b2dbSAllison Henderson BUFFER_TRACE(bh, "Partial buffer zeroed"); 34614e96b2dbSAllison Henderson next: 34624e96b2dbSAllison Henderson bh = bh->b_this_page; 34634e96b2dbSAllison Henderson iblock++; 34644e96b2dbSAllison Henderson pos += range_to_discard; 34654e96b2dbSAllison Henderson } 34664e96b2dbSAllison Henderson 34674e96b2dbSAllison Henderson return err; 34684e96b2dbSAllison Henderson } 34694e96b2dbSAllison Henderson 347091ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 347191ef4cafSDuane Griffin { 347291ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 347391ef4cafSDuane Griffin return 1; 347491ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 347591ef4cafSDuane Griffin return 1; 347691ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 347791ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 347891ef4cafSDuane Griffin return 0; 347991ef4cafSDuane Griffin } 348091ef4cafSDuane Griffin 3481ac27a0ecSDave Kleikamp /* 3482a4bb6b64SAllison Henderson * ext4_punch_hole: punches a hole in a file by releaseing the blocks 3483a4bb6b64SAllison Henderson * associated with the given offset and length 3484a4bb6b64SAllison Henderson * 3485a4bb6b64SAllison Henderson * @inode: File inode 3486a4bb6b64SAllison Henderson * @offset: The offset where the hole will begin 3487a4bb6b64SAllison Henderson * @len: The length of the hole 3488a4bb6b64SAllison Henderson * 34894907cb7bSAnatol Pomozov * Returns: 0 on success or negative on failure 3490a4bb6b64SAllison Henderson */ 3491a4bb6b64SAllison Henderson 3492a4bb6b64SAllison Henderson int ext4_punch_hole(struct file *file, loff_t offset, loff_t length) 3493a4bb6b64SAllison Henderson { 3494a4bb6b64SAllison Henderson struct inode *inode = file->f_path.dentry->d_inode; 3495a4bb6b64SAllison Henderson if (!S_ISREG(inode->i_mode)) 349673355192SAllison Henderson return -EOPNOTSUPP; 3497a4bb6b64SAllison Henderson 3498a4bb6b64SAllison Henderson if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 3499a4bb6b64SAllison Henderson /* TODO: Add support for non extent hole punching */ 350073355192SAllison Henderson return -EOPNOTSUPP; 3501a4bb6b64SAllison Henderson } 3502a4bb6b64SAllison Henderson 3503bab08ab9STheodore Ts'o if (EXT4_SB(inode->i_sb)->s_cluster_ratio > 1) { 3504bab08ab9STheodore Ts'o /* TODO: Add support for bigalloc file systems */ 350573355192SAllison Henderson return -EOPNOTSUPP; 3506bab08ab9STheodore Ts'o } 3507bab08ab9STheodore Ts'o 3508a4bb6b64SAllison Henderson return ext4_ext_punch_hole(file, offset, length); 3509a4bb6b64SAllison Henderson } 3510a4bb6b64SAllison Henderson 3511a4bb6b64SAllison Henderson /* 3512617ba13bSMingming Cao * ext4_truncate() 3513ac27a0ecSDave Kleikamp * 3514617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 3515617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 3516ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 3517ac27a0ecSDave Kleikamp * 351842b2aa86SJustin P. Mattock * As we work through the truncate and commit bits of it to the journal there 3519ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 3520ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 3521ac27a0ecSDave Kleikamp * 3522ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 3523ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 3524ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 3525ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 3526ac27a0ecSDave Kleikamp * left-to-right works OK too). 3527ac27a0ecSDave Kleikamp * 3528ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 3529ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 3530ac27a0ecSDave Kleikamp * 3531ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 3532617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 3533ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 3534617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 3535617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 3536ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 3537617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 3538ac27a0ecSDave Kleikamp */ 3539617ba13bSMingming Cao void ext4_truncate(struct inode *inode) 3540ac27a0ecSDave Kleikamp { 35410562e0baSJiaying Zhang trace_ext4_truncate_enter(inode); 35420562e0baSJiaying Zhang 354391ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 3544ac27a0ecSDave Kleikamp return; 3545ac27a0ecSDave Kleikamp 354612e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS); 3547c8d46e41SJiaying Zhang 35485534fb5bSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 354919f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE); 35507d8f9f7dSTheodore Ts'o 3551ff9893dcSAmir Goldstein if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3552cf108bcaSJan Kara ext4_ext_truncate(inode); 3553ff9893dcSAmir Goldstein else 3554ff9893dcSAmir Goldstein ext4_ind_truncate(inode); 3555a86c6181SAlex Tomas 35560562e0baSJiaying Zhang trace_ext4_truncate_exit(inode); 3557ac27a0ecSDave Kleikamp } 3558ac27a0ecSDave Kleikamp 3559ac27a0ecSDave Kleikamp /* 3560617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 3561ac27a0ecSDave Kleikamp * underlying buffer_head on success. If 'in_mem' is true, we have all 3562ac27a0ecSDave Kleikamp * data in memory that is needed to recreate the on-disk version of this 3563ac27a0ecSDave Kleikamp * inode. 3564ac27a0ecSDave Kleikamp */ 3565617ba13bSMingming Cao static int __ext4_get_inode_loc(struct inode *inode, 3566617ba13bSMingming Cao struct ext4_iloc *iloc, int in_mem) 3567ac27a0ecSDave Kleikamp { 3568240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 3569ac27a0ecSDave Kleikamp struct buffer_head *bh; 3570240799cdSTheodore Ts'o struct super_block *sb = inode->i_sb; 3571240799cdSTheodore Ts'o ext4_fsblk_t block; 3572240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 3573ac27a0ecSDave Kleikamp 35743a06d778SAneesh Kumar K.V iloc->bh = NULL; 3575240799cdSTheodore Ts'o if (!ext4_valid_inum(sb, inode->i_ino)) 3576ac27a0ecSDave Kleikamp return -EIO; 3577ac27a0ecSDave Kleikamp 3578240799cdSTheodore Ts'o iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb); 3579240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 3580240799cdSTheodore Ts'o if (!gdp) 3581240799cdSTheodore Ts'o return -EIO; 3582240799cdSTheodore Ts'o 3583240799cdSTheodore Ts'o /* 3584240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 3585240799cdSTheodore Ts'o */ 358600d09882STao Ma inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 3587240799cdSTheodore Ts'o inode_offset = ((inode->i_ino - 1) % 3588240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 3589240799cdSTheodore Ts'o block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); 3590240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 3591240799cdSTheodore Ts'o 3592240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 3593ac27a0ecSDave Kleikamp if (!bh) { 3594c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, block, 3595c398eda0STheodore Ts'o "unable to read itable block"); 3596ac27a0ecSDave Kleikamp return -EIO; 3597ac27a0ecSDave Kleikamp } 3598ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 3599ac27a0ecSDave Kleikamp lock_buffer(bh); 36009c83a923SHidehiro Kawai 36019c83a923SHidehiro Kawai /* 36029c83a923SHidehiro Kawai * If the buffer has the write error flag, we have failed 36039c83a923SHidehiro Kawai * to write out another inode in the same block. In this 36049c83a923SHidehiro Kawai * case, we don't have to read the block because we may 36059c83a923SHidehiro Kawai * read the old inode data successfully. 36069c83a923SHidehiro Kawai */ 36079c83a923SHidehiro Kawai if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) 36089c83a923SHidehiro Kawai set_buffer_uptodate(bh); 36099c83a923SHidehiro Kawai 3610ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) { 3611ac27a0ecSDave Kleikamp /* someone brought it uptodate while we waited */ 3612ac27a0ecSDave Kleikamp unlock_buffer(bh); 3613ac27a0ecSDave Kleikamp goto has_buffer; 3614ac27a0ecSDave Kleikamp } 3615ac27a0ecSDave Kleikamp 3616ac27a0ecSDave Kleikamp /* 3617ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 3618ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 3619ac27a0ecSDave Kleikamp * block. 3620ac27a0ecSDave Kleikamp */ 3621ac27a0ecSDave Kleikamp if (in_mem) { 3622ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 3623240799cdSTheodore Ts'o int i, start; 3624ac27a0ecSDave Kleikamp 3625240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 3626ac27a0ecSDave Kleikamp 3627ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 3628240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 3629ac27a0ecSDave Kleikamp if (!bitmap_bh) 3630ac27a0ecSDave Kleikamp goto make_io; 3631ac27a0ecSDave Kleikamp 3632ac27a0ecSDave Kleikamp /* 3633ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 3634ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 3635ac27a0ecSDave Kleikamp * of one, so skip it. 3636ac27a0ecSDave Kleikamp */ 3637ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 3638ac27a0ecSDave Kleikamp brelse(bitmap_bh); 3639ac27a0ecSDave Kleikamp goto make_io; 3640ac27a0ecSDave Kleikamp } 3641240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 3642ac27a0ecSDave Kleikamp if (i == inode_offset) 3643ac27a0ecSDave Kleikamp continue; 3644617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 3645ac27a0ecSDave Kleikamp break; 3646ac27a0ecSDave Kleikamp } 3647ac27a0ecSDave Kleikamp brelse(bitmap_bh); 3648240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 3649ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 3650ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 3651ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 3652ac27a0ecSDave Kleikamp unlock_buffer(bh); 3653ac27a0ecSDave Kleikamp goto has_buffer; 3654ac27a0ecSDave Kleikamp } 3655ac27a0ecSDave Kleikamp } 3656ac27a0ecSDave Kleikamp 3657ac27a0ecSDave Kleikamp make_io: 3658ac27a0ecSDave Kleikamp /* 3659240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 3660240799cdSTheodore Ts'o * blocks from the inode table. 3661240799cdSTheodore Ts'o */ 3662240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 3663240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 3664240799cdSTheodore Ts'o unsigned num; 3665240799cdSTheodore Ts'o 3666240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 3667b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 3668240799cdSTheodore Ts'o b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1); 3669240799cdSTheodore Ts'o if (table > b) 3670240799cdSTheodore Ts'o b = table; 3671240799cdSTheodore Ts'o end = b + EXT4_SB(sb)->s_inode_readahead_blks; 3672240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 3673feb0ab32SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) 3674560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 3675240799cdSTheodore Ts'o table += num / inodes_per_block; 3676240799cdSTheodore Ts'o if (end > table) 3677240799cdSTheodore Ts'o end = table; 3678240799cdSTheodore Ts'o while (b <= end) 3679240799cdSTheodore Ts'o sb_breadahead(sb, b++); 3680240799cdSTheodore Ts'o } 3681240799cdSTheodore Ts'o 3682240799cdSTheodore Ts'o /* 3683ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 3684ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 3685ac27a0ecSDave Kleikamp * Read the block from disk. 3686ac27a0ecSDave Kleikamp */ 36870562e0baSJiaying Zhang trace_ext4_load_inode(inode); 3688ac27a0ecSDave Kleikamp get_bh(bh); 3689ac27a0ecSDave Kleikamp bh->b_end_io = end_buffer_read_sync; 369065299a3bSChristoph Hellwig submit_bh(READ | REQ_META | REQ_PRIO, bh); 3691ac27a0ecSDave Kleikamp wait_on_buffer(bh); 3692ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 3693c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, block, 3694c398eda0STheodore Ts'o "unable to read itable block"); 3695ac27a0ecSDave Kleikamp brelse(bh); 3696ac27a0ecSDave Kleikamp return -EIO; 3697ac27a0ecSDave Kleikamp } 3698ac27a0ecSDave Kleikamp } 3699ac27a0ecSDave Kleikamp has_buffer: 3700ac27a0ecSDave Kleikamp iloc->bh = bh; 3701ac27a0ecSDave Kleikamp return 0; 3702ac27a0ecSDave Kleikamp } 3703ac27a0ecSDave Kleikamp 3704617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 3705ac27a0ecSDave Kleikamp { 3706ac27a0ecSDave Kleikamp /* We have all inode data except xattrs in memory here. */ 3707617ba13bSMingming Cao return __ext4_get_inode_loc(inode, iloc, 370819f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_XATTR)); 3709ac27a0ecSDave Kleikamp } 3710ac27a0ecSDave Kleikamp 3711617ba13bSMingming Cao void ext4_set_inode_flags(struct inode *inode) 3712ac27a0ecSDave Kleikamp { 3713617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 3714ac27a0ecSDave Kleikamp 3715ac27a0ecSDave Kleikamp inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); 3716617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 3717ac27a0ecSDave Kleikamp inode->i_flags |= S_SYNC; 3718617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 3719ac27a0ecSDave Kleikamp inode->i_flags |= S_APPEND; 3720617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 3721ac27a0ecSDave Kleikamp inode->i_flags |= S_IMMUTABLE; 3722617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 3723ac27a0ecSDave Kleikamp inode->i_flags |= S_NOATIME; 3724617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 3725ac27a0ecSDave Kleikamp inode->i_flags |= S_DIRSYNC; 3726ac27a0ecSDave Kleikamp } 3727ac27a0ecSDave Kleikamp 3728ff9ddf7eSJan Kara /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */ 3729ff9ddf7eSJan Kara void ext4_get_inode_flags(struct ext4_inode_info *ei) 3730ff9ddf7eSJan Kara { 373184a8dce2SDmitry Monakhov unsigned int vfs_fl; 373284a8dce2SDmitry Monakhov unsigned long old_fl, new_fl; 3733ff9ddf7eSJan Kara 373484a8dce2SDmitry Monakhov do { 373584a8dce2SDmitry Monakhov vfs_fl = ei->vfs_inode.i_flags; 373684a8dce2SDmitry Monakhov old_fl = ei->i_flags; 373784a8dce2SDmitry Monakhov new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL| 373884a8dce2SDmitry Monakhov EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL| 373984a8dce2SDmitry Monakhov EXT4_DIRSYNC_FL); 374084a8dce2SDmitry Monakhov if (vfs_fl & S_SYNC) 374184a8dce2SDmitry Monakhov new_fl |= EXT4_SYNC_FL; 374284a8dce2SDmitry Monakhov if (vfs_fl & S_APPEND) 374384a8dce2SDmitry Monakhov new_fl |= EXT4_APPEND_FL; 374484a8dce2SDmitry Monakhov if (vfs_fl & S_IMMUTABLE) 374584a8dce2SDmitry Monakhov new_fl |= EXT4_IMMUTABLE_FL; 374684a8dce2SDmitry Monakhov if (vfs_fl & S_NOATIME) 374784a8dce2SDmitry Monakhov new_fl |= EXT4_NOATIME_FL; 374884a8dce2SDmitry Monakhov if (vfs_fl & S_DIRSYNC) 374984a8dce2SDmitry Monakhov new_fl |= EXT4_DIRSYNC_FL; 375084a8dce2SDmitry Monakhov } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl); 3751ff9ddf7eSJan Kara } 3752de9a55b8STheodore Ts'o 37530fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 37540fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 37550fc1b451SAneesh Kumar K.V { 37560fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 37578180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 37588180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 37590fc1b451SAneesh Kumar K.V 37600fc1b451SAneesh Kumar K.V if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 37610fc1b451SAneesh Kumar K.V EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) { 37620fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 37630fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 37640fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 376507a03824STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) { 37668180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 37678180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 37688180a562SAneesh Kumar K.V } else { 37690fc1b451SAneesh Kumar K.V return i_blocks; 37708180a562SAneesh Kumar K.V } 37710fc1b451SAneesh Kumar K.V } else { 37720fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 37730fc1b451SAneesh Kumar K.V } 37740fc1b451SAneesh Kumar K.V } 3775ff9ddf7eSJan Kara 37761d1fe1eeSDavid Howells struct inode *ext4_iget(struct super_block *sb, unsigned long ino) 3777ac27a0ecSDave Kleikamp { 3778617ba13bSMingming Cao struct ext4_iloc iloc; 3779617ba13bSMingming Cao struct ext4_inode *raw_inode; 37801d1fe1eeSDavid Howells struct ext4_inode_info *ei; 37811d1fe1eeSDavid Howells struct inode *inode; 3782b436b9beSJan Kara journal_t *journal = EXT4_SB(sb)->s_journal; 37831d1fe1eeSDavid Howells long ret; 3784ac27a0ecSDave Kleikamp int block; 378508cefc7aSEric W. Biederman uid_t i_uid; 378608cefc7aSEric W. Biederman gid_t i_gid; 3787ac27a0ecSDave Kleikamp 37881d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 37891d1fe1eeSDavid Howells if (!inode) 37901d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 37911d1fe1eeSDavid Howells if (!(inode->i_state & I_NEW)) 37921d1fe1eeSDavid Howells return inode; 37931d1fe1eeSDavid Howells 37941d1fe1eeSDavid Howells ei = EXT4_I(inode); 37957dc57615SPeter Huewe iloc.bh = NULL; 3796ac27a0ecSDave Kleikamp 37971d1fe1eeSDavid Howells ret = __ext4_get_inode_loc(inode, &iloc, 0); 37981d1fe1eeSDavid Howells if (ret < 0) 3799ac27a0ecSDave Kleikamp goto bad_inode; 3800617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 3801814525f4SDarrick J. Wong 3802814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 3803814525f4SDarrick J. Wong ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 3804814525f4SDarrick J. Wong if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 3805814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)) { 3806814525f4SDarrick J. Wong EXT4_ERROR_INODE(inode, "bad extra_isize (%u != %u)", 3807814525f4SDarrick J. Wong EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize, 3808814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)); 3809814525f4SDarrick J. Wong ret = -EIO; 3810814525f4SDarrick J. Wong goto bad_inode; 3811814525f4SDarrick J. Wong } 3812814525f4SDarrick J. Wong } else 3813814525f4SDarrick J. Wong ei->i_extra_isize = 0; 3814814525f4SDarrick J. Wong 3815814525f4SDarrick J. Wong /* Precompute checksum seed for inode metadata */ 3816814525f4SDarrick J. Wong if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 3817814525f4SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) { 3818814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3819814525f4SDarrick J. Wong __u32 csum; 3820814525f4SDarrick J. Wong __le32 inum = cpu_to_le32(inode->i_ino); 3821814525f4SDarrick J. Wong __le32 gen = raw_inode->i_generation; 3822814525f4SDarrick J. Wong csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, 3823814525f4SDarrick J. Wong sizeof(inum)); 3824814525f4SDarrick J. Wong ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, 3825814525f4SDarrick J. Wong sizeof(gen)); 3826814525f4SDarrick J. Wong } 3827814525f4SDarrick J. Wong 3828814525f4SDarrick J. Wong if (!ext4_inode_csum_verify(inode, raw_inode, ei)) { 3829814525f4SDarrick J. Wong EXT4_ERROR_INODE(inode, "checksum invalid"); 3830814525f4SDarrick J. Wong ret = -EIO; 3831814525f4SDarrick J. Wong goto bad_inode; 3832814525f4SDarrick J. Wong } 3833814525f4SDarrick J. Wong 3834ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 383508cefc7aSEric W. Biederman i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 383608cefc7aSEric W. Biederman i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 3837ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 383808cefc7aSEric W. Biederman i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 383908cefc7aSEric W. Biederman i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 3840ac27a0ecSDave Kleikamp } 384108cefc7aSEric W. Biederman i_uid_write(inode, i_uid); 384208cefc7aSEric W. Biederman i_gid_write(inode, i_gid); 3843bfe86848SMiklos Szeredi set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); 3844ac27a0ecSDave Kleikamp 3845353eb83cSTheodore Ts'o ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 3846ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 3847ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 3848ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 3849ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 3850ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 3851ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 3852ac27a0ecSDave Kleikamp */ 3853ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 3854ac27a0ecSDave Kleikamp if (inode->i_mode == 0 || 3855617ba13bSMingming Cao !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) { 3856ac27a0ecSDave Kleikamp /* this inode is deleted */ 38571d1fe1eeSDavid Howells ret = -ESTALE; 3858ac27a0ecSDave Kleikamp goto bad_inode; 3859ac27a0ecSDave Kleikamp } 3860ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 3861ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 3862ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 3863ac27a0ecSDave Kleikamp * the process of deleting those. */ 3864ac27a0ecSDave Kleikamp } 3865ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 38660fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 38677973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 3868a9e81742STheodore Ts'o if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) 3869a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 3870a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 3871a48380f7SAneesh Kumar K.V inode->i_size = ext4_isize(raw_inode); 3872ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 3873a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 3874a9e7f447SDmitry Monakhov ei->i_reserved_quota = 0; 3875a9e7f447SDmitry Monakhov #endif 3876ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 3877ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 3878a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 3879ac27a0ecSDave Kleikamp /* 3880ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 3881ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 3882ac27a0ecSDave Kleikamp */ 3883617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 3884ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 3885ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 3886ac27a0ecSDave Kleikamp 3887b436b9beSJan Kara /* 3888b436b9beSJan Kara * Set transaction id's of transactions that have to be committed 3889b436b9beSJan Kara * to finish f[data]sync. We set them to currently running transaction 3890b436b9beSJan Kara * as we cannot be sure that the inode or some of its metadata isn't 3891b436b9beSJan Kara * part of the transaction - the inode could have been reclaimed and 3892b436b9beSJan Kara * now it is reread from disk. 3893b436b9beSJan Kara */ 3894b436b9beSJan Kara if (journal) { 3895b436b9beSJan Kara transaction_t *transaction; 3896b436b9beSJan Kara tid_t tid; 3897b436b9beSJan Kara 3898a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 3899b436b9beSJan Kara if (journal->j_running_transaction) 3900b436b9beSJan Kara transaction = journal->j_running_transaction; 3901b436b9beSJan Kara else 3902b436b9beSJan Kara transaction = journal->j_committing_transaction; 3903b436b9beSJan Kara if (transaction) 3904b436b9beSJan Kara tid = transaction->t_tid; 3905b436b9beSJan Kara else 3906b436b9beSJan Kara tid = journal->j_commit_sequence; 3907a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 3908b436b9beSJan Kara ei->i_sync_tid = tid; 3909b436b9beSJan Kara ei->i_datasync_tid = tid; 3910b436b9beSJan Kara } 3911b436b9beSJan Kara 39120040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 3913ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 3914ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 3915617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 3916617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 3917ac27a0ecSDave Kleikamp } else { 3918ac27a0ecSDave Kleikamp __le32 *magic = (void *)raw_inode + 3919617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE + 3920ac27a0ecSDave Kleikamp ei->i_extra_isize; 3921617ba13bSMingming Cao if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) 392219f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_XATTR); 3923ac27a0ecSDave Kleikamp } 3924814525f4SDarrick J. Wong } 3925ac27a0ecSDave Kleikamp 3926ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 3927ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 3928ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 3929ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 3930ef7f3835SKalpak Shah 393125ec56b5SJean Noel Cordenner inode->i_version = le32_to_cpu(raw_inode->i_disk_version); 393225ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 393325ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 393425ec56b5SJean Noel Cordenner inode->i_version |= 393525ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 393625ec56b5SJean Noel Cordenner } 393725ec56b5SJean Noel Cordenner 3938c4b5a614STheodore Ts'o ret = 0; 3939485c26ecSTheodore Ts'o if (ei->i_file_acl && 39401032988cSTheodore Ts'o !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) { 394124676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "bad extended attribute block %llu", 394224676da4STheodore Ts'o ei->i_file_acl); 3943485c26ecSTheodore Ts'o ret = -EIO; 3944485c26ecSTheodore Ts'o goto bad_inode; 394507a03824STheodore Ts'o } else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 3946c4b5a614STheodore Ts'o if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 3947c4b5a614STheodore Ts'o (S_ISLNK(inode->i_mode) && 3948c4b5a614STheodore Ts'o !ext4_inode_is_fast_symlink(inode))) 39497a262f7cSAneesh Kumar K.V /* Validate extent which is part of inode */ 39507a262f7cSAneesh Kumar K.V ret = ext4_ext_check_inode(inode); 3951fe2c8191SThiemo Nagel } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 3952fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 3953fe2c8191SThiemo Nagel !ext4_inode_is_fast_symlink(inode))) { 3954fe2c8191SThiemo Nagel /* Validate block references which are part of inode */ 39551f7d1e77STheodore Ts'o ret = ext4_ind_check_inode(inode); 3956fe2c8191SThiemo Nagel } 3957567f3e9aSTheodore Ts'o if (ret) 39587a262f7cSAneesh Kumar K.V goto bad_inode; 39597a262f7cSAneesh Kumar K.V 3960ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 3961617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 3962617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 3963617ba13bSMingming Cao ext4_set_aops(inode); 3964ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 3965617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 3966617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 3967ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 3968e83c1397SDuane Griffin if (ext4_inode_is_fast_symlink(inode)) { 3969617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 3970e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 3971e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 3972e83c1397SDuane Griffin } else { 3973617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 3974617ba13bSMingming Cao ext4_set_aops(inode); 3975ac27a0ecSDave Kleikamp } 3976563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 3977563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 3978617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 3979ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 3980ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 3981ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 3982ac27a0ecSDave Kleikamp else 3983ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 3984ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 3985563bdd61STheodore Ts'o } else { 3986563bdd61STheodore Ts'o ret = -EIO; 398724676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode); 3988563bdd61STheodore Ts'o goto bad_inode; 3989ac27a0ecSDave Kleikamp } 3990ac27a0ecSDave Kleikamp brelse(iloc.bh); 3991617ba13bSMingming Cao ext4_set_inode_flags(inode); 39921d1fe1eeSDavid Howells unlock_new_inode(inode); 39931d1fe1eeSDavid Howells return inode; 3994ac27a0ecSDave Kleikamp 3995ac27a0ecSDave Kleikamp bad_inode: 3996567f3e9aSTheodore Ts'o brelse(iloc.bh); 39971d1fe1eeSDavid Howells iget_failed(inode); 39981d1fe1eeSDavid Howells return ERR_PTR(ret); 3999ac27a0ecSDave Kleikamp } 4000ac27a0ecSDave Kleikamp 40010fc1b451SAneesh Kumar K.V static int ext4_inode_blocks_set(handle_t *handle, 40020fc1b451SAneesh Kumar K.V struct ext4_inode *raw_inode, 40030fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 40040fc1b451SAneesh Kumar K.V { 40050fc1b451SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 40060fc1b451SAneesh Kumar K.V u64 i_blocks = inode->i_blocks; 40070fc1b451SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 40080fc1b451SAneesh Kumar K.V 40090fc1b451SAneesh Kumar K.V if (i_blocks <= ~0U) { 40100fc1b451SAneesh Kumar K.V /* 40114907cb7bSAnatol Pomozov * i_blocks can be represented in a 32 bit variable 40120fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 40130fc1b451SAneesh Kumar K.V */ 40148180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 40150fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = 0; 401684a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 4017f287a1a5STheodore Ts'o return 0; 4018f287a1a5STheodore Ts'o } 4019f287a1a5STheodore Ts'o if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) 4020f287a1a5STheodore Ts'o return -EFBIG; 4021f287a1a5STheodore Ts'o 4022f287a1a5STheodore Ts'o if (i_blocks <= 0xffffffffffffULL) { 40230fc1b451SAneesh Kumar K.V /* 40240fc1b451SAneesh Kumar K.V * i_blocks can be represented in a 48 bit variable 40250fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 40260fc1b451SAneesh Kumar K.V */ 40278180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 40280fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 402984a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 40300fc1b451SAneesh Kumar K.V } else { 403184a8dce2SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE); 40328180a562SAneesh Kumar K.V /* i_block is stored in file system block size */ 40338180a562SAneesh Kumar K.V i_blocks = i_blocks >> (inode->i_blkbits - 9); 40348180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 40358180a562SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 40360fc1b451SAneesh Kumar K.V } 4037f287a1a5STheodore Ts'o return 0; 40380fc1b451SAneesh Kumar K.V } 40390fc1b451SAneesh Kumar K.V 4040ac27a0ecSDave Kleikamp /* 4041ac27a0ecSDave Kleikamp * Post the struct inode info into an on-disk inode location in the 4042ac27a0ecSDave Kleikamp * buffer-cache. This gobbles the caller's reference to the 4043ac27a0ecSDave Kleikamp * buffer_head in the inode location struct. 4044ac27a0ecSDave Kleikamp * 4045ac27a0ecSDave Kleikamp * The caller must have write access to iloc->bh. 4046ac27a0ecSDave Kleikamp */ 4047617ba13bSMingming Cao static int ext4_do_update_inode(handle_t *handle, 4048ac27a0ecSDave Kleikamp struct inode *inode, 4049830156c7SFrank Mayhar struct ext4_iloc *iloc) 4050ac27a0ecSDave Kleikamp { 4051617ba13bSMingming Cao struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 4052617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 4053ac27a0ecSDave Kleikamp struct buffer_head *bh = iloc->bh; 4054ac27a0ecSDave Kleikamp int err = 0, rc, block; 4055b71fc079SJan Kara int need_datasync = 0; 405608cefc7aSEric W. Biederman uid_t i_uid; 405708cefc7aSEric W. Biederman gid_t i_gid; 4058ac27a0ecSDave Kleikamp 4059ac27a0ecSDave Kleikamp /* For fields not not tracking in the in-memory inode, 4060ac27a0ecSDave Kleikamp * initialise them to zero for new inodes. */ 406119f5fb7aSTheodore Ts'o if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) 4062617ba13bSMingming Cao memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 4063ac27a0ecSDave Kleikamp 4064ff9ddf7eSJan Kara ext4_get_inode_flags(ei); 4065ac27a0ecSDave Kleikamp raw_inode->i_mode = cpu_to_le16(inode->i_mode); 406608cefc7aSEric W. Biederman i_uid = i_uid_read(inode); 406708cefc7aSEric W. Biederman i_gid = i_gid_read(inode); 4068ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 406908cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid)); 407008cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid)); 4071ac27a0ecSDave Kleikamp /* 4072ac27a0ecSDave Kleikamp * Fix up interoperability with old kernels. Otherwise, old inodes get 4073ac27a0ecSDave Kleikamp * re-used with the upper 16 bits of the uid/gid intact 4074ac27a0ecSDave Kleikamp */ 4075ac27a0ecSDave Kleikamp if (!ei->i_dtime) { 4076ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 407708cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_uid)); 4078ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 407908cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_gid)); 4080ac27a0ecSDave Kleikamp } else { 4081ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4082ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4083ac27a0ecSDave Kleikamp } 4084ac27a0ecSDave Kleikamp } else { 408508cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid)); 408608cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid)); 4087ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4088ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4089ac27a0ecSDave Kleikamp } 4090ac27a0ecSDave Kleikamp raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 4091ef7f3835SKalpak Shah 4092ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 4093ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 4094ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 4095ef7f3835SKalpak Shah EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 4096ef7f3835SKalpak Shah 40970fc1b451SAneesh Kumar K.V if (ext4_inode_blocks_set(handle, raw_inode, ei)) 40980fc1b451SAneesh Kumar K.V goto out_brelse; 4099ac27a0ecSDave Kleikamp raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 4100353eb83cSTheodore Ts'o raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF); 41019b8f1f01SMingming Cao if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 41029b8f1f01SMingming Cao cpu_to_le32(EXT4_OS_HURD)) 4103a1ddeb7eSBadari Pulavarty raw_inode->i_file_acl_high = 4104a1ddeb7eSBadari Pulavarty cpu_to_le16(ei->i_file_acl >> 32); 41057973c0c1SAneesh Kumar K.V raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 4106b71fc079SJan Kara if (ei->i_disksize != ext4_isize(raw_inode)) { 4107a48380f7SAneesh Kumar K.V ext4_isize_set(raw_inode, ei->i_disksize); 4108b71fc079SJan Kara need_datasync = 1; 4109b71fc079SJan Kara } 4110ac27a0ecSDave Kleikamp if (ei->i_disksize > 0x7fffffffULL) { 4111ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 4112617ba13bSMingming Cao if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, 4113617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE) || 4114617ba13bSMingming Cao EXT4_SB(sb)->s_es->s_rev_level == 4115617ba13bSMingming Cao cpu_to_le32(EXT4_GOOD_OLD_REV)) { 4116ac27a0ecSDave Kleikamp /* If this is the first large file 4117ac27a0ecSDave Kleikamp * created, add a flag to the superblock. 4118ac27a0ecSDave Kleikamp */ 4119617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, 4120617ba13bSMingming Cao EXT4_SB(sb)->s_sbh); 4121ac27a0ecSDave Kleikamp if (err) 4122ac27a0ecSDave Kleikamp goto out_brelse; 4123617ba13bSMingming Cao ext4_update_dynamic_rev(sb); 4124617ba13bSMingming Cao EXT4_SET_RO_COMPAT_FEATURE(sb, 4125617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE); 41260390131bSFrank Mayhar ext4_handle_sync(handle); 4127b50924c2SArtem Bityutskiy err = ext4_handle_dirty_super(handle, sb); 4128ac27a0ecSDave Kleikamp } 4129ac27a0ecSDave Kleikamp } 4130ac27a0ecSDave Kleikamp raw_inode->i_generation = cpu_to_le32(inode->i_generation); 4131ac27a0ecSDave Kleikamp if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 4132ac27a0ecSDave Kleikamp if (old_valid_dev(inode->i_rdev)) { 4133ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 4134ac27a0ecSDave Kleikamp cpu_to_le32(old_encode_dev(inode->i_rdev)); 4135ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 0; 4136ac27a0ecSDave Kleikamp } else { 4137ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 0; 4138ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 4139ac27a0ecSDave Kleikamp cpu_to_le32(new_encode_dev(inode->i_rdev)); 4140ac27a0ecSDave Kleikamp raw_inode->i_block[2] = 0; 4141ac27a0ecSDave Kleikamp } 4142de9a55b8STheodore Ts'o } else 4143de9a55b8STheodore Ts'o for (block = 0; block < EXT4_N_BLOCKS; block++) 4144ac27a0ecSDave Kleikamp raw_inode->i_block[block] = ei->i_data[block]; 4145ac27a0ecSDave Kleikamp 414625ec56b5SJean Noel Cordenner raw_inode->i_disk_version = cpu_to_le32(inode->i_version); 414725ec56b5SJean Noel Cordenner if (ei->i_extra_isize) { 414825ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 414925ec56b5SJean Noel Cordenner raw_inode->i_version_hi = 415025ec56b5SJean Noel Cordenner cpu_to_le32(inode->i_version >> 32); 4151ac27a0ecSDave Kleikamp raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize); 415225ec56b5SJean Noel Cordenner } 415325ec56b5SJean Noel Cordenner 4154814525f4SDarrick J. Wong ext4_inode_csum_set(inode, raw_inode, ei); 4155814525f4SDarrick J. Wong 41560390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 415773b50c1cSCurt Wohlgemuth rc = ext4_handle_dirty_metadata(handle, NULL, bh); 4158ac27a0ecSDave Kleikamp if (!err) 4159ac27a0ecSDave Kleikamp err = rc; 416019f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_NEW); 4161ac27a0ecSDave Kleikamp 4162b71fc079SJan Kara ext4_update_inode_fsync_trans(handle, inode, need_datasync); 4163ac27a0ecSDave Kleikamp out_brelse: 4164ac27a0ecSDave Kleikamp brelse(bh); 4165617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4166ac27a0ecSDave Kleikamp return err; 4167ac27a0ecSDave Kleikamp } 4168ac27a0ecSDave Kleikamp 4169ac27a0ecSDave Kleikamp /* 4170617ba13bSMingming Cao * ext4_write_inode() 4171ac27a0ecSDave Kleikamp * 4172ac27a0ecSDave Kleikamp * We are called from a few places: 4173ac27a0ecSDave Kleikamp * 4174ac27a0ecSDave Kleikamp * - Within generic_file_write() for O_SYNC files. 4175ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 41764907cb7bSAnatol Pomozov * transaction to commit. 4177ac27a0ecSDave Kleikamp * 4178ac27a0ecSDave Kleikamp * - Within sys_sync(), kupdate and such. 4179ac27a0ecSDave Kleikamp * We wait on commit, if tol to. 4180ac27a0ecSDave Kleikamp * 4181ac27a0ecSDave Kleikamp * - Within prune_icache() (PF_MEMALLOC == true) 4182ac27a0ecSDave Kleikamp * Here we simply return. We can't afford to block kswapd on the 4183ac27a0ecSDave Kleikamp * journal commit. 4184ac27a0ecSDave Kleikamp * 4185ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 4186ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 4187617ba13bSMingming Cao * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for 4188ac27a0ecSDave Kleikamp * knfsd. 4189ac27a0ecSDave Kleikamp * 4190ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 4191ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 4192ac27a0ecSDave Kleikamp * which we are interested. 4193ac27a0ecSDave Kleikamp * 4194ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 4195ac27a0ecSDave Kleikamp * 4196ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 4197ac27a0ecSDave Kleikamp * stuff(); 4198ac27a0ecSDave Kleikamp * inode->i_size = expr; 4199ac27a0ecSDave Kleikamp * 4200ac27a0ecSDave Kleikamp * is in error because a kswapd-driven write_inode() could occur while 4201ac27a0ecSDave Kleikamp * `stuff()' is running, and the new i_size will be lost. Plus the inode 4202ac27a0ecSDave Kleikamp * will no longer be on the superblock's dirty inode list. 4203ac27a0ecSDave Kleikamp */ 4204a9185b41SChristoph Hellwig int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) 4205ac27a0ecSDave Kleikamp { 420691ac6f43SFrank Mayhar int err; 420791ac6f43SFrank Mayhar 4208ac27a0ecSDave Kleikamp if (current->flags & PF_MEMALLOC) 4209ac27a0ecSDave Kleikamp return 0; 4210ac27a0ecSDave Kleikamp 421191ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal) { 4212617ba13bSMingming Cao if (ext4_journal_current_handle()) { 4213b38bd33aSMingming Cao jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); 4214ac27a0ecSDave Kleikamp dump_stack(); 4215ac27a0ecSDave Kleikamp return -EIO; 4216ac27a0ecSDave Kleikamp } 4217ac27a0ecSDave Kleikamp 4218a9185b41SChristoph Hellwig if (wbc->sync_mode != WB_SYNC_ALL) 4219ac27a0ecSDave Kleikamp return 0; 4220ac27a0ecSDave Kleikamp 422191ac6f43SFrank Mayhar err = ext4_force_commit(inode->i_sb); 422291ac6f43SFrank Mayhar } else { 422391ac6f43SFrank Mayhar struct ext4_iloc iloc; 422491ac6f43SFrank Mayhar 42258b472d73SCurt Wohlgemuth err = __ext4_get_inode_loc(inode, &iloc, 0); 422691ac6f43SFrank Mayhar if (err) 422791ac6f43SFrank Mayhar return err; 4228a9185b41SChristoph Hellwig if (wbc->sync_mode == WB_SYNC_ALL) 4229830156c7SFrank Mayhar sync_dirty_buffer(iloc.bh); 4230830156c7SFrank Mayhar if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { 4231c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr, 4232c398eda0STheodore Ts'o "IO error syncing inode"); 4233830156c7SFrank Mayhar err = -EIO; 4234830156c7SFrank Mayhar } 4235fd2dd9fbSCurt Wohlgemuth brelse(iloc.bh); 423691ac6f43SFrank Mayhar } 423791ac6f43SFrank Mayhar return err; 4238ac27a0ecSDave Kleikamp } 4239ac27a0ecSDave Kleikamp 4240ac27a0ecSDave Kleikamp /* 4241617ba13bSMingming Cao * ext4_setattr() 4242ac27a0ecSDave Kleikamp * 4243ac27a0ecSDave Kleikamp * Called from notify_change. 4244ac27a0ecSDave Kleikamp * 4245ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 4246ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 4247ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 4248ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 4249ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 4250ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 4251ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 4252ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 4253ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 4254ac27a0ecSDave Kleikamp * 4255678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 4256678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 4257678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 4258678aaf48SJan Kara * This way we are sure that all the data written in the previous 4259678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 4260678aaf48SJan Kara * writeback). 4261678aaf48SJan Kara * 4262678aaf48SJan Kara * Called with inode->i_mutex down. 4263ac27a0ecSDave Kleikamp */ 4264617ba13bSMingming Cao int ext4_setattr(struct dentry *dentry, struct iattr *attr) 4265ac27a0ecSDave Kleikamp { 4266ac27a0ecSDave Kleikamp struct inode *inode = dentry->d_inode; 4267ac27a0ecSDave Kleikamp int error, rc = 0; 42683d287de3SDmitry Monakhov int orphan = 0; 4269ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 4270ac27a0ecSDave Kleikamp 4271ac27a0ecSDave Kleikamp error = inode_change_ok(inode, attr); 4272ac27a0ecSDave Kleikamp if (error) 4273ac27a0ecSDave Kleikamp return error; 4274ac27a0ecSDave Kleikamp 427512755627SDmitry Monakhov if (is_quota_modification(inode, attr)) 4276871a2931SChristoph Hellwig dquot_initialize(inode); 427708cefc7aSEric W. Biederman if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) || 427808cefc7aSEric W. Biederman (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) { 4279ac27a0ecSDave Kleikamp handle_t *handle; 4280ac27a0ecSDave Kleikamp 4281ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 4282ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 42835aca07ebSDmitry Monakhov handle = ext4_journal_start(inode, (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb)+ 4284194074acSDmitry Monakhov EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb))+3); 4285ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 4286ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 4287ac27a0ecSDave Kleikamp goto err_out; 4288ac27a0ecSDave Kleikamp } 4289b43fa828SChristoph Hellwig error = dquot_transfer(inode, attr); 4290ac27a0ecSDave Kleikamp if (error) { 4291617ba13bSMingming Cao ext4_journal_stop(handle); 4292ac27a0ecSDave Kleikamp return error; 4293ac27a0ecSDave Kleikamp } 4294ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 4295ac27a0ecSDave Kleikamp * one transaction */ 4296ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_UID) 4297ac27a0ecSDave Kleikamp inode->i_uid = attr->ia_uid; 4298ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_GID) 4299ac27a0ecSDave Kleikamp inode->i_gid = attr->ia_gid; 4300617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 4301617ba13bSMingming Cao ext4_journal_stop(handle); 4302ac27a0ecSDave Kleikamp } 4303ac27a0ecSDave Kleikamp 4304e2b46574SEric Sandeen if (attr->ia_valid & ATTR_SIZE) { 4305562c72aaSChristoph Hellwig 430612e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 4307e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4308e2b46574SEric Sandeen 43090c095c7fSTheodore Ts'o if (attr->ia_size > sbi->s_bitmap_maxbytes) 43100c095c7fSTheodore Ts'o return -EFBIG; 4311e2b46574SEric Sandeen } 4312e2b46574SEric Sandeen } 4313e2b46574SEric Sandeen 4314ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode) && 4315c8d46e41SJiaying Zhang attr->ia_valid & ATTR_SIZE && 4316072bd7eaSTheodore Ts'o (attr->ia_size < inode->i_size)) { 4317ac27a0ecSDave Kleikamp handle_t *handle; 4318ac27a0ecSDave Kleikamp 4319617ba13bSMingming Cao handle = ext4_journal_start(inode, 3); 4320ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 4321ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 4322ac27a0ecSDave Kleikamp goto err_out; 4323ac27a0ecSDave Kleikamp } 43243d287de3SDmitry Monakhov if (ext4_handle_valid(handle)) { 4325617ba13bSMingming Cao error = ext4_orphan_add(handle, inode); 43263d287de3SDmitry Monakhov orphan = 1; 43273d287de3SDmitry Monakhov } 4328617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 4329617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 4330ac27a0ecSDave Kleikamp if (!error) 4331ac27a0ecSDave Kleikamp error = rc; 4332617ba13bSMingming Cao ext4_journal_stop(handle); 4333678aaf48SJan Kara 4334678aaf48SJan Kara if (ext4_should_order_data(inode)) { 4335678aaf48SJan Kara error = ext4_begin_ordered_truncate(inode, 4336678aaf48SJan Kara attr->ia_size); 4337678aaf48SJan Kara if (error) { 4338678aaf48SJan Kara /* Do as much error cleanup as possible */ 4339678aaf48SJan Kara handle = ext4_journal_start(inode, 3); 4340678aaf48SJan Kara if (IS_ERR(handle)) { 4341678aaf48SJan Kara ext4_orphan_del(NULL, inode); 4342678aaf48SJan Kara goto err_out; 4343678aaf48SJan Kara } 4344678aaf48SJan Kara ext4_orphan_del(handle, inode); 43453d287de3SDmitry Monakhov orphan = 0; 4346678aaf48SJan Kara ext4_journal_stop(handle); 4347678aaf48SJan Kara goto err_out; 4348678aaf48SJan Kara } 4349678aaf48SJan Kara } 4350ac27a0ecSDave Kleikamp } 4351ac27a0ecSDave Kleikamp 4352072bd7eaSTheodore Ts'o if (attr->ia_valid & ATTR_SIZE) { 43531c9114f9SDmitry Monakhov if (attr->ia_size != i_size_read(inode)) { 4354072bd7eaSTheodore Ts'o truncate_setsize(inode, attr->ia_size); 43551b65007eSDmitry Monakhov /* Inode size will be reduced, wait for dio in flight. 43561b65007eSDmitry Monakhov * Temporarily disable dioread_nolock to prevent 43571b65007eSDmitry Monakhov * livelock. */ 43581b65007eSDmitry Monakhov if (orphan) { 43591b65007eSDmitry Monakhov ext4_inode_block_unlocked_dio(inode); 43601c9114f9SDmitry Monakhov inode_dio_wait(inode); 43611b65007eSDmitry Monakhov ext4_inode_resume_unlocked_dio(inode); 43621b65007eSDmitry Monakhov } 43631c9114f9SDmitry Monakhov } 4364072bd7eaSTheodore Ts'o ext4_truncate(inode); 4365072bd7eaSTheodore Ts'o } 4366ac27a0ecSDave Kleikamp 43671025774cSChristoph Hellwig if (!rc) { 43681025774cSChristoph Hellwig setattr_copy(inode, attr); 43691025774cSChristoph Hellwig mark_inode_dirty(inode); 43701025774cSChristoph Hellwig } 43711025774cSChristoph Hellwig 43721025774cSChristoph Hellwig /* 43731025774cSChristoph Hellwig * If the call to ext4_truncate failed to get a transaction handle at 43741025774cSChristoph Hellwig * all, we need to clean up the in-core orphan list manually. 43751025774cSChristoph Hellwig */ 43763d287de3SDmitry Monakhov if (orphan && inode->i_nlink) 4377617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 4378ac27a0ecSDave Kleikamp 4379ac27a0ecSDave Kleikamp if (!rc && (ia_valid & ATTR_MODE)) 4380617ba13bSMingming Cao rc = ext4_acl_chmod(inode); 4381ac27a0ecSDave Kleikamp 4382ac27a0ecSDave Kleikamp err_out: 4383617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 4384ac27a0ecSDave Kleikamp if (!error) 4385ac27a0ecSDave Kleikamp error = rc; 4386ac27a0ecSDave Kleikamp return error; 4387ac27a0ecSDave Kleikamp } 4388ac27a0ecSDave Kleikamp 43893e3398a0SMingming Cao int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry, 43903e3398a0SMingming Cao struct kstat *stat) 43913e3398a0SMingming Cao { 43923e3398a0SMingming Cao struct inode *inode; 43933e3398a0SMingming Cao unsigned long delalloc_blocks; 43943e3398a0SMingming Cao 43953e3398a0SMingming Cao inode = dentry->d_inode; 43963e3398a0SMingming Cao generic_fillattr(inode, stat); 43973e3398a0SMingming Cao 43983e3398a0SMingming Cao /* 43993e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 44003e3398a0SMingming Cao * otherwise in the case of system crash before the real block 44013e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 44023e3398a0SMingming Cao * on-disk file blocks. 44033e3398a0SMingming Cao * We always keep i_blocks updated together with real 44043e3398a0SMingming Cao * allocation. But to not confuse with user, stat 44053e3398a0SMingming Cao * will return the blocks that include the delayed allocation 44063e3398a0SMingming Cao * blocks for this file. 44073e3398a0SMingming Cao */ 440896607551STao Ma delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb), 440996607551STao Ma EXT4_I(inode)->i_reserved_data_blocks); 44103e3398a0SMingming Cao 44113e3398a0SMingming Cao stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9; 44123e3398a0SMingming Cao return 0; 44133e3398a0SMingming Cao } 4414ac27a0ecSDave Kleikamp 4415a02908f1SMingming Cao static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4416a02908f1SMingming Cao { 441712e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 44188bb2b247SAmir Goldstein return ext4_ind_trans_blocks(inode, nrblocks, chunk); 4419ac51d837STheodore Ts'o return ext4_ext_index_trans_blocks(inode, nrblocks, chunk); 4420a02908f1SMingming Cao } 4421ac51d837STheodore Ts'o 4422a02908f1SMingming Cao /* 4423a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 4424a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 4425a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 4426a02908f1SMingming Cao * 4427a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 44284907cb7bSAnatol Pomozov * different block groups too. If they are contiguous, with flexbg, 4429a02908f1SMingming Cao * they could still across block group boundary. 4430a02908f1SMingming Cao * 4431a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 4432a02908f1SMingming Cao */ 44331f109d5aSTheodore Ts'o static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4434a02908f1SMingming Cao { 44358df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 44368df9675fSTheodore Ts'o int gdpblocks; 4437a02908f1SMingming Cao int idxblocks; 4438a02908f1SMingming Cao int ret = 0; 4439a02908f1SMingming Cao 4440a02908f1SMingming Cao /* 4441a02908f1SMingming Cao * How many index blocks need to touch to modify nrblocks? 4442a02908f1SMingming Cao * The "Chunk" flag indicating whether the nrblocks is 4443a02908f1SMingming Cao * physically contiguous on disk 4444a02908f1SMingming Cao * 4445a02908f1SMingming Cao * For Direct IO and fallocate, they calls get_block to allocate 4446a02908f1SMingming Cao * one single extent at a time, so they could set the "Chunk" flag 4447a02908f1SMingming Cao */ 4448a02908f1SMingming Cao idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk); 4449a02908f1SMingming Cao 4450a02908f1SMingming Cao ret = idxblocks; 4451a02908f1SMingming Cao 4452a02908f1SMingming Cao /* 4453a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 4454a02908f1SMingming Cao * to account 4455a02908f1SMingming Cao */ 4456a02908f1SMingming Cao groups = idxblocks; 4457a02908f1SMingming Cao if (chunk) 4458a02908f1SMingming Cao groups += 1; 4459ac27a0ecSDave Kleikamp else 4460a02908f1SMingming Cao groups += nrblocks; 4461ac27a0ecSDave Kleikamp 4462a02908f1SMingming Cao gdpblocks = groups; 44638df9675fSTheodore Ts'o if (groups > ngroups) 44648df9675fSTheodore Ts'o groups = ngroups; 4465a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 4466a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 4467a02908f1SMingming Cao 4468a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 4469a02908f1SMingming Cao ret += groups + gdpblocks; 4470a02908f1SMingming Cao 4471a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 4472a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 4473ac27a0ecSDave Kleikamp 4474ac27a0ecSDave Kleikamp return ret; 4475ac27a0ecSDave Kleikamp } 4476ac27a0ecSDave Kleikamp 4477ac27a0ecSDave Kleikamp /* 447825985edcSLucas De Marchi * Calculate the total number of credits to reserve to fit 4479f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 4480f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 4481a02908f1SMingming Cao * 4482525f4ed8SMingming Cao * This could be called via ext4_write_begin() 4483a02908f1SMingming Cao * 4484525f4ed8SMingming Cao * We need to consider the worse case, when 4485a02908f1SMingming Cao * one new block per extent. 4486a02908f1SMingming Cao */ 4487a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 4488a02908f1SMingming Cao { 4489a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 4490a02908f1SMingming Cao int ret; 4491a02908f1SMingming Cao 4492a02908f1SMingming Cao ret = ext4_meta_trans_blocks(inode, bpp, 0); 4493a02908f1SMingming Cao 4494a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 4495a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 4496a02908f1SMingming Cao ret += bpp; 4497a02908f1SMingming Cao return ret; 4498a02908f1SMingming Cao } 4499f3bd1f3fSMingming Cao 4500f3bd1f3fSMingming Cao /* 4501f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 4502f3bd1f3fSMingming Cao * 4503f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 450479e83036SEric Sandeen * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks. 4505f3bd1f3fSMingming Cao * 4506f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 4507f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 4508f3bd1f3fSMingming Cao */ 4509f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 4510f3bd1f3fSMingming Cao { 4511f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 4512f3bd1f3fSMingming Cao } 4513f3bd1f3fSMingming Cao 4514a02908f1SMingming Cao /* 4515617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 4516ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 4517ac27a0ecSDave Kleikamp */ 4518617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 4519617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 4520ac27a0ecSDave Kleikamp { 4521ac27a0ecSDave Kleikamp int err = 0; 4522ac27a0ecSDave Kleikamp 4523c64db50eSTheodore Ts'o if (IS_I_VERSION(inode)) 452425ec56b5SJean Noel Cordenner inode_inc_iversion(inode); 452525ec56b5SJean Noel Cordenner 4526ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 4527ac27a0ecSDave Kleikamp get_bh(iloc->bh); 4528ac27a0ecSDave Kleikamp 4529dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 4530830156c7SFrank Mayhar err = ext4_do_update_inode(handle, inode, iloc); 4531ac27a0ecSDave Kleikamp put_bh(iloc->bh); 4532ac27a0ecSDave Kleikamp return err; 4533ac27a0ecSDave Kleikamp } 4534ac27a0ecSDave Kleikamp 4535ac27a0ecSDave Kleikamp /* 4536ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 4537ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 4538ac27a0ecSDave Kleikamp */ 4539ac27a0ecSDave Kleikamp 4540ac27a0ecSDave Kleikamp int 4541617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 4542617ba13bSMingming Cao struct ext4_iloc *iloc) 4543ac27a0ecSDave Kleikamp { 45440390131bSFrank Mayhar int err; 45450390131bSFrank Mayhar 4546617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 4547ac27a0ecSDave Kleikamp if (!err) { 4548ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 4549617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, iloc->bh); 4550ac27a0ecSDave Kleikamp if (err) { 4551ac27a0ecSDave Kleikamp brelse(iloc->bh); 4552ac27a0ecSDave Kleikamp iloc->bh = NULL; 4553ac27a0ecSDave Kleikamp } 4554ac27a0ecSDave Kleikamp } 4555617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4556ac27a0ecSDave Kleikamp return err; 4557ac27a0ecSDave Kleikamp } 4558ac27a0ecSDave Kleikamp 4559ac27a0ecSDave Kleikamp /* 45606dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 45616dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 45626dd4ee7cSKalpak Shah */ 45631d03ec98SAneesh Kumar K.V static int ext4_expand_extra_isize(struct inode *inode, 45641d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 45651d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 45661d03ec98SAneesh Kumar K.V handle_t *handle) 45676dd4ee7cSKalpak Shah { 45686dd4ee7cSKalpak Shah struct ext4_inode *raw_inode; 45696dd4ee7cSKalpak Shah struct ext4_xattr_ibody_header *header; 45706dd4ee7cSKalpak Shah 45716dd4ee7cSKalpak Shah if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) 45726dd4ee7cSKalpak Shah return 0; 45736dd4ee7cSKalpak Shah 45746dd4ee7cSKalpak Shah raw_inode = ext4_raw_inode(&iloc); 45756dd4ee7cSKalpak Shah 45766dd4ee7cSKalpak Shah header = IHDR(inode, raw_inode); 45776dd4ee7cSKalpak Shah 45786dd4ee7cSKalpak Shah /* No extended attributes present */ 457919f5fb7aSTheodore Ts'o if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) || 45806dd4ee7cSKalpak Shah header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 45816dd4ee7cSKalpak Shah memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0, 45826dd4ee7cSKalpak Shah new_extra_isize); 45836dd4ee7cSKalpak Shah EXT4_I(inode)->i_extra_isize = new_extra_isize; 45846dd4ee7cSKalpak Shah return 0; 45856dd4ee7cSKalpak Shah } 45866dd4ee7cSKalpak Shah 45876dd4ee7cSKalpak Shah /* try to expand with EAs present */ 45886dd4ee7cSKalpak Shah return ext4_expand_extra_isize_ea(inode, new_extra_isize, 45896dd4ee7cSKalpak Shah raw_inode, handle); 45906dd4ee7cSKalpak Shah } 45916dd4ee7cSKalpak Shah 45926dd4ee7cSKalpak Shah /* 4593ac27a0ecSDave Kleikamp * What we do here is to mark the in-core inode as clean with respect to inode 4594ac27a0ecSDave Kleikamp * dirtiness (it may still be data-dirty). 4595ac27a0ecSDave Kleikamp * This means that the in-core inode may be reaped by prune_icache 4596ac27a0ecSDave Kleikamp * without having to perform any I/O. This is a very good thing, 4597ac27a0ecSDave Kleikamp * because *any* task may call prune_icache - even ones which 4598ac27a0ecSDave Kleikamp * have a transaction open against a different journal. 4599ac27a0ecSDave Kleikamp * 4600ac27a0ecSDave Kleikamp * Is this cheating? Not really. Sure, we haven't written the 4601ac27a0ecSDave Kleikamp * inode out, but prune_icache isn't a user-visible syncing function. 4602ac27a0ecSDave Kleikamp * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 4603ac27a0ecSDave Kleikamp * we start and wait on commits. 4604ac27a0ecSDave Kleikamp */ 4605617ba13bSMingming Cao int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode) 4606ac27a0ecSDave Kleikamp { 4607617ba13bSMingming Cao struct ext4_iloc iloc; 46086dd4ee7cSKalpak Shah struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 46096dd4ee7cSKalpak Shah static unsigned int mnt_count; 46106dd4ee7cSKalpak Shah int err, ret; 4611ac27a0ecSDave Kleikamp 4612ac27a0ecSDave Kleikamp might_sleep(); 46137ff9c073STheodore Ts'o trace_ext4_mark_inode_dirty(inode, _RET_IP_); 4614617ba13bSMingming Cao err = ext4_reserve_inode_write(handle, inode, &iloc); 46150390131bSFrank Mayhar if (ext4_handle_valid(handle) && 46160390131bSFrank Mayhar EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize && 461719f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) { 46186dd4ee7cSKalpak Shah /* 46196dd4ee7cSKalpak Shah * We need extra buffer credits since we may write into EA block 46206dd4ee7cSKalpak Shah * with this same handle. If journal_extend fails, then it will 46216dd4ee7cSKalpak Shah * only result in a minor loss of functionality for that inode. 46226dd4ee7cSKalpak Shah * If this is felt to be critical, then e2fsck should be run to 46236dd4ee7cSKalpak Shah * force a large enough s_min_extra_isize. 46246dd4ee7cSKalpak Shah */ 46256dd4ee7cSKalpak Shah if ((jbd2_journal_extend(handle, 46266dd4ee7cSKalpak Shah EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) { 46276dd4ee7cSKalpak Shah ret = ext4_expand_extra_isize(inode, 46286dd4ee7cSKalpak Shah sbi->s_want_extra_isize, 46296dd4ee7cSKalpak Shah iloc, handle); 46306dd4ee7cSKalpak Shah if (ret) { 463119f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, 463219f5fb7aSTheodore Ts'o EXT4_STATE_NO_EXPAND); 4633c1bddad9SAneesh Kumar K.V if (mnt_count != 4634c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count)) { 463512062dddSEric Sandeen ext4_warning(inode->i_sb, 46366dd4ee7cSKalpak Shah "Unable to expand inode %lu. Delete" 46376dd4ee7cSKalpak Shah " some EAs or run e2fsck.", 46386dd4ee7cSKalpak Shah inode->i_ino); 4639c1bddad9SAneesh Kumar K.V mnt_count = 4640c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count); 46416dd4ee7cSKalpak Shah } 46426dd4ee7cSKalpak Shah } 46436dd4ee7cSKalpak Shah } 46446dd4ee7cSKalpak Shah } 4645ac27a0ecSDave Kleikamp if (!err) 4646617ba13bSMingming Cao err = ext4_mark_iloc_dirty(handle, inode, &iloc); 4647ac27a0ecSDave Kleikamp return err; 4648ac27a0ecSDave Kleikamp } 4649ac27a0ecSDave Kleikamp 4650ac27a0ecSDave Kleikamp /* 4651617ba13bSMingming Cao * ext4_dirty_inode() is called from __mark_inode_dirty() 4652ac27a0ecSDave Kleikamp * 4653ac27a0ecSDave Kleikamp * We're really interested in the case where a file is being extended. 4654ac27a0ecSDave Kleikamp * i_size has been changed by generic_commit_write() and we thus need 4655ac27a0ecSDave Kleikamp * to include the updated inode in the current transaction. 4656ac27a0ecSDave Kleikamp * 46575dd4056dSChristoph Hellwig * Also, dquot_alloc_block() will always dirty the inode when blocks 4658ac27a0ecSDave Kleikamp * are allocated to the file. 4659ac27a0ecSDave Kleikamp * 4660ac27a0ecSDave Kleikamp * If the inode is marked synchronous, we don't honour that here - doing 4661ac27a0ecSDave Kleikamp * so would cause a commit on atime updates, which we don't bother doing. 4662ac27a0ecSDave Kleikamp * We handle synchronous inodes at the highest possible level. 4663ac27a0ecSDave Kleikamp */ 4664aa385729SChristoph Hellwig void ext4_dirty_inode(struct inode *inode, int flags) 4665ac27a0ecSDave Kleikamp { 4666ac27a0ecSDave Kleikamp handle_t *handle; 4667ac27a0ecSDave Kleikamp 4668617ba13bSMingming Cao handle = ext4_journal_start(inode, 2); 4669ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 4670ac27a0ecSDave Kleikamp goto out; 4671f3dc272fSCurt Wohlgemuth 4672617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 4673f3dc272fSCurt Wohlgemuth 4674617ba13bSMingming Cao ext4_journal_stop(handle); 4675ac27a0ecSDave Kleikamp out: 4676ac27a0ecSDave Kleikamp return; 4677ac27a0ecSDave Kleikamp } 4678ac27a0ecSDave Kleikamp 4679ac27a0ecSDave Kleikamp #if 0 4680ac27a0ecSDave Kleikamp /* 4681ac27a0ecSDave Kleikamp * Bind an inode's backing buffer_head into this transaction, to prevent 4682ac27a0ecSDave Kleikamp * it from being flushed to disk early. Unlike 4683617ba13bSMingming Cao * ext4_reserve_inode_write, this leaves behind no bh reference and 4684ac27a0ecSDave Kleikamp * returns no iloc structure, so the caller needs to repeat the iloc 4685ac27a0ecSDave Kleikamp * lookup to mark the inode dirty later. 4686ac27a0ecSDave Kleikamp */ 4687617ba13bSMingming Cao static int ext4_pin_inode(handle_t *handle, struct inode *inode) 4688ac27a0ecSDave Kleikamp { 4689617ba13bSMingming Cao struct ext4_iloc iloc; 4690ac27a0ecSDave Kleikamp 4691ac27a0ecSDave Kleikamp int err = 0; 4692ac27a0ecSDave Kleikamp if (handle) { 4693617ba13bSMingming Cao err = ext4_get_inode_loc(inode, &iloc); 4694ac27a0ecSDave Kleikamp if (!err) { 4695ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc.bh, "get_write_access"); 4696dab291afSMingming Cao err = jbd2_journal_get_write_access(handle, iloc.bh); 4697ac27a0ecSDave Kleikamp if (!err) 46980390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, 469973b50c1cSCurt Wohlgemuth NULL, 4700ac27a0ecSDave Kleikamp iloc.bh); 4701ac27a0ecSDave Kleikamp brelse(iloc.bh); 4702ac27a0ecSDave Kleikamp } 4703ac27a0ecSDave Kleikamp } 4704617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4705ac27a0ecSDave Kleikamp return err; 4706ac27a0ecSDave Kleikamp } 4707ac27a0ecSDave Kleikamp #endif 4708ac27a0ecSDave Kleikamp 4709617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val) 4710ac27a0ecSDave Kleikamp { 4711ac27a0ecSDave Kleikamp journal_t *journal; 4712ac27a0ecSDave Kleikamp handle_t *handle; 4713ac27a0ecSDave Kleikamp int err; 4714ac27a0ecSDave Kleikamp 4715ac27a0ecSDave Kleikamp /* 4716ac27a0ecSDave Kleikamp * We have to be very careful here: changing a data block's 4717ac27a0ecSDave Kleikamp * journaling status dynamically is dangerous. If we write a 4718ac27a0ecSDave Kleikamp * data block to the journal, change the status and then delete 4719ac27a0ecSDave Kleikamp * that block, we risk forgetting to revoke the old log record 4720ac27a0ecSDave Kleikamp * from the journal and so a subsequent replay can corrupt data. 4721ac27a0ecSDave Kleikamp * So, first we make sure that the journal is empty and that 4722ac27a0ecSDave Kleikamp * nobody is changing anything. 4723ac27a0ecSDave Kleikamp */ 4724ac27a0ecSDave Kleikamp 4725617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 47260390131bSFrank Mayhar if (!journal) 47270390131bSFrank Mayhar return 0; 4728d699594dSDave Hansen if (is_journal_aborted(journal)) 4729ac27a0ecSDave Kleikamp return -EROFS; 47302aff57b0SYongqiang Yang /* We have to allocate physical blocks for delalloc blocks 47312aff57b0SYongqiang Yang * before flushing journal. otherwise delalloc blocks can not 47322aff57b0SYongqiang Yang * be allocated any more. even more truncate on delalloc blocks 47332aff57b0SYongqiang Yang * could trigger BUG by flushing delalloc blocks in journal. 47342aff57b0SYongqiang Yang * There is no delalloc block in non-journal data mode. 47352aff57b0SYongqiang Yang */ 47362aff57b0SYongqiang Yang if (val && test_opt(inode->i_sb, DELALLOC)) { 47372aff57b0SYongqiang Yang err = ext4_alloc_da_blocks(inode); 47382aff57b0SYongqiang Yang if (err < 0) 47392aff57b0SYongqiang Yang return err; 47402aff57b0SYongqiang Yang } 4741ac27a0ecSDave Kleikamp 474217335dccSDmitry Monakhov /* Wait for all existing dio workers */ 474317335dccSDmitry Monakhov ext4_inode_block_unlocked_dio(inode); 474417335dccSDmitry Monakhov inode_dio_wait(inode); 474517335dccSDmitry Monakhov 4746dab291afSMingming Cao jbd2_journal_lock_updates(journal); 4747ac27a0ecSDave Kleikamp 4748ac27a0ecSDave Kleikamp /* 4749ac27a0ecSDave Kleikamp * OK, there are no updates running now, and all cached data is 4750ac27a0ecSDave Kleikamp * synced to disk. We are now in a completely consistent state 4751ac27a0ecSDave Kleikamp * which doesn't have anything in the journal, and we know that 4752ac27a0ecSDave Kleikamp * no filesystem updates are running, so it is safe to modify 4753ac27a0ecSDave Kleikamp * the inode's in-core data-journaling state flag now. 4754ac27a0ecSDave Kleikamp */ 4755ac27a0ecSDave Kleikamp 4756ac27a0ecSDave Kleikamp if (val) 475712e9b892SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 47585872ddaaSYongqiang Yang else { 47595872ddaaSYongqiang Yang jbd2_journal_flush(journal); 476012e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 47615872ddaaSYongqiang Yang } 4762617ba13bSMingming Cao ext4_set_aops(inode); 4763ac27a0ecSDave Kleikamp 4764dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 476517335dccSDmitry Monakhov ext4_inode_resume_unlocked_dio(inode); 4766ac27a0ecSDave Kleikamp 4767ac27a0ecSDave Kleikamp /* Finally we can mark the inode as dirty. */ 4768ac27a0ecSDave Kleikamp 4769617ba13bSMingming Cao handle = ext4_journal_start(inode, 1); 4770ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 4771ac27a0ecSDave Kleikamp return PTR_ERR(handle); 4772ac27a0ecSDave Kleikamp 4773617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 47740390131bSFrank Mayhar ext4_handle_sync(handle); 4775617ba13bSMingming Cao ext4_journal_stop(handle); 4776617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4777ac27a0ecSDave Kleikamp 4778ac27a0ecSDave Kleikamp return err; 4779ac27a0ecSDave Kleikamp } 47802e9ee850SAneesh Kumar K.V 47812e9ee850SAneesh Kumar K.V static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) 47822e9ee850SAneesh Kumar K.V { 47832e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 47842e9ee850SAneesh Kumar K.V } 47852e9ee850SAneesh Kumar K.V 4786c2ec175cSNick Piggin int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) 47872e9ee850SAneesh Kumar K.V { 4788c2ec175cSNick Piggin struct page *page = vmf->page; 47892e9ee850SAneesh Kumar K.V loff_t size; 47902e9ee850SAneesh Kumar K.V unsigned long len; 47919ea7df53SJan Kara int ret; 47922e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 47932e9ee850SAneesh Kumar K.V struct inode *inode = file->f_path.dentry->d_inode; 47942e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 47959ea7df53SJan Kara handle_t *handle; 47969ea7df53SJan Kara get_block_t *get_block; 47979ea7df53SJan Kara int retries = 0; 47982e9ee850SAneesh Kumar K.V 47998e8ad8a5SJan Kara sb_start_pagefault(inode->i_sb); 4800041bbb6dSTheodore Ts'o file_update_time(vma->vm_file); 48019ea7df53SJan Kara /* Delalloc case is easy... */ 48029ea7df53SJan Kara if (test_opt(inode->i_sb, DELALLOC) && 48039ea7df53SJan Kara !ext4_should_journal_data(inode) && 48049ea7df53SJan Kara !ext4_nonda_switch(inode->i_sb)) { 48059ea7df53SJan Kara do { 48069ea7df53SJan Kara ret = __block_page_mkwrite(vma, vmf, 48079ea7df53SJan Kara ext4_da_get_block_prep); 48089ea7df53SJan Kara } while (ret == -ENOSPC && 48099ea7df53SJan Kara ext4_should_retry_alloc(inode->i_sb, &retries)); 48109ea7df53SJan Kara goto out_ret; 48112e9ee850SAneesh Kumar K.V } 48120e499890SDarrick J. Wong 48130e499890SDarrick J. Wong lock_page(page); 48149ea7df53SJan Kara size = i_size_read(inode); 48159ea7df53SJan Kara /* Page got truncated from under us? */ 48169ea7df53SJan Kara if (page->mapping != mapping || page_offset(page) > size) { 48179ea7df53SJan Kara unlock_page(page); 48189ea7df53SJan Kara ret = VM_FAULT_NOPAGE; 48199ea7df53SJan Kara goto out; 48200e499890SDarrick J. Wong } 48212e9ee850SAneesh Kumar K.V 48222e9ee850SAneesh Kumar K.V if (page->index == size >> PAGE_CACHE_SHIFT) 48232e9ee850SAneesh Kumar K.V len = size & ~PAGE_CACHE_MASK; 48242e9ee850SAneesh Kumar K.V else 48252e9ee850SAneesh Kumar K.V len = PAGE_CACHE_SIZE; 4826a827eaffSAneesh Kumar K.V /* 48279ea7df53SJan Kara * Return if we have all the buffers mapped. This avoids the need to do 48289ea7df53SJan Kara * journal_start/journal_stop which can block and take a long time 4829a827eaffSAneesh Kumar K.V */ 48302e9ee850SAneesh Kumar K.V if (page_has_buffers(page)) { 48312e9ee850SAneesh Kumar K.V if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL, 4832a827eaffSAneesh Kumar K.V ext4_bh_unmapped)) { 48339ea7df53SJan Kara /* Wait so that we don't change page under IO */ 48349ea7df53SJan Kara wait_on_page_writeback(page); 48359ea7df53SJan Kara ret = VM_FAULT_LOCKED; 48369ea7df53SJan Kara goto out; 48372e9ee850SAneesh Kumar K.V } 4838a827eaffSAneesh Kumar K.V } 4839a827eaffSAneesh Kumar K.V unlock_page(page); 48409ea7df53SJan Kara /* OK, we need to fill the hole... */ 48419ea7df53SJan Kara if (ext4_should_dioread_nolock(inode)) 48429ea7df53SJan Kara get_block = ext4_get_block_write; 48439ea7df53SJan Kara else 48449ea7df53SJan Kara get_block = ext4_get_block; 48459ea7df53SJan Kara retry_alloc: 48469ea7df53SJan Kara handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode)); 48479ea7df53SJan Kara if (IS_ERR(handle)) { 4848c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 48499ea7df53SJan Kara goto out; 48509ea7df53SJan Kara } 48519ea7df53SJan Kara ret = __block_page_mkwrite(vma, vmf, get_block); 48529ea7df53SJan Kara if (!ret && ext4_should_journal_data(inode)) { 48539ea7df53SJan Kara if (walk_page_buffers(handle, page_buffers(page), 0, 48549ea7df53SJan Kara PAGE_CACHE_SIZE, NULL, do_journal_get_write_access)) { 48559ea7df53SJan Kara unlock_page(page); 48569ea7df53SJan Kara ret = VM_FAULT_SIGBUS; 4857fcbb5515SYongqiang Yang ext4_journal_stop(handle); 48589ea7df53SJan Kara goto out; 48599ea7df53SJan Kara } 48609ea7df53SJan Kara ext4_set_inode_state(inode, EXT4_STATE_JDATA); 48619ea7df53SJan Kara } 48629ea7df53SJan Kara ext4_journal_stop(handle); 48639ea7df53SJan Kara if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 48649ea7df53SJan Kara goto retry_alloc; 48659ea7df53SJan Kara out_ret: 48669ea7df53SJan Kara ret = block_page_mkwrite_return(ret); 48679ea7df53SJan Kara out: 48688e8ad8a5SJan Kara sb_end_pagefault(inode->i_sb); 48692e9ee850SAneesh Kumar K.V return ret; 48702e9ee850SAneesh Kumar K.V } 4871