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 2369f125d64STheodore Ts'o handle = ext4_journal_start(inode, ext4_blocks_for_truncate(inode)+3); 237ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 238bc965ab3STheodore Ts'o ext4_std_error(inode->i_sb, PTR_ERR(handle)); 239ac27a0ecSDave Kleikamp /* 240ac27a0ecSDave Kleikamp * If we're going to skip the normal cleanup, we still need to 241ac27a0ecSDave Kleikamp * make sure that the in-core orphan linked list is properly 242ac27a0ecSDave Kleikamp * cleaned up. 243ac27a0ecSDave Kleikamp */ 244617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 245ac27a0ecSDave Kleikamp goto no_delete; 246ac27a0ecSDave Kleikamp } 247ac27a0ecSDave Kleikamp 248ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 2490390131bSFrank Mayhar ext4_handle_sync(handle); 250ac27a0ecSDave Kleikamp inode->i_size = 0; 251bc965ab3STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 252bc965ab3STheodore Ts'o if (err) { 25312062dddSEric Sandeen ext4_warning(inode->i_sb, 254bc965ab3STheodore Ts'o "couldn't mark inode dirty (err %d)", err); 255bc965ab3STheodore Ts'o goto stop_handle; 256bc965ab3STheodore Ts'o } 257ac27a0ecSDave Kleikamp if (inode->i_blocks) 258617ba13bSMingming Cao ext4_truncate(inode); 259bc965ab3STheodore Ts'o 260bc965ab3STheodore Ts'o /* 261bc965ab3STheodore Ts'o * ext4_ext_truncate() doesn't reserve any slop when it 262bc965ab3STheodore Ts'o * restarts journal transactions; therefore there may not be 263bc965ab3STheodore Ts'o * enough credits left in the handle to remove the inode from 264bc965ab3STheodore Ts'o * the orphan list and set the dtime field. 265bc965ab3STheodore Ts'o */ 2660390131bSFrank Mayhar if (!ext4_handle_has_enough_credits(handle, 3)) { 267bc965ab3STheodore Ts'o err = ext4_journal_extend(handle, 3); 268bc965ab3STheodore Ts'o if (err > 0) 269bc965ab3STheodore Ts'o err = ext4_journal_restart(handle, 3); 270bc965ab3STheodore Ts'o if (err != 0) { 27112062dddSEric Sandeen ext4_warning(inode->i_sb, 272bc965ab3STheodore Ts'o "couldn't extend journal (err %d)", err); 273bc965ab3STheodore Ts'o stop_handle: 274bc965ab3STheodore Ts'o ext4_journal_stop(handle); 27545388219STheodore Ts'o ext4_orphan_del(NULL, inode); 276bc965ab3STheodore Ts'o goto no_delete; 277bc965ab3STheodore Ts'o } 278bc965ab3STheodore Ts'o } 279bc965ab3STheodore Ts'o 280ac27a0ecSDave Kleikamp /* 281617ba13bSMingming Cao * Kill off the orphan record which ext4_truncate created. 282ac27a0ecSDave Kleikamp * AKPM: I think this can be inside the above `if'. 283617ba13bSMingming Cao * Note that ext4_orphan_del() has to be able to cope with the 284ac27a0ecSDave Kleikamp * deletion of a non-existent orphan - this is because we don't 285617ba13bSMingming Cao * know if ext4_truncate() actually created an orphan record. 286ac27a0ecSDave Kleikamp * (Well, we could do this if we need to, but heck - it works) 287ac27a0ecSDave Kleikamp */ 288617ba13bSMingming Cao ext4_orphan_del(handle, inode); 289617ba13bSMingming Cao EXT4_I(inode)->i_dtime = get_seconds(); 290ac27a0ecSDave Kleikamp 291ac27a0ecSDave Kleikamp /* 292ac27a0ecSDave Kleikamp * One subtle ordering requirement: if anything has gone wrong 293ac27a0ecSDave Kleikamp * (transaction abort, IO errors, whatever), then we can still 294ac27a0ecSDave Kleikamp * do these next steps (the fs will already have been marked as 295ac27a0ecSDave Kleikamp * having errors), but we can't free the inode if the mark_dirty 296ac27a0ecSDave Kleikamp * fails. 297ac27a0ecSDave Kleikamp */ 298617ba13bSMingming Cao if (ext4_mark_inode_dirty(handle, inode)) 299ac27a0ecSDave Kleikamp /* If that failed, just do the required in-core inode clear. */ 3000930fcc1SAl Viro ext4_clear_inode(inode); 301ac27a0ecSDave Kleikamp else 302617ba13bSMingming Cao ext4_free_inode(handle, inode); 303617ba13bSMingming Cao ext4_journal_stop(handle); 304ac27a0ecSDave Kleikamp return; 305ac27a0ecSDave Kleikamp no_delete: 3060930fcc1SAl Viro ext4_clear_inode(inode); /* We must guarantee clearing of inode... */ 307ac27a0ecSDave Kleikamp } 308ac27a0ecSDave Kleikamp 309a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 310a9e7f447SDmitry Monakhov qsize_t *ext4_get_reserved_space(struct inode *inode) 31160e58e0fSMingming Cao { 312a9e7f447SDmitry Monakhov return &EXT4_I(inode)->i_reserved_quota; 31360e58e0fSMingming Cao } 314a9e7f447SDmitry Monakhov #endif 3159d0be502STheodore Ts'o 31612219aeaSAneesh Kumar K.V /* 31712219aeaSAneesh Kumar K.V * Calculate the number of metadata blocks need to reserve 3189d0be502STheodore Ts'o * to allocate a block located at @lblock 31912219aeaSAneesh Kumar K.V */ 32001f49d0bSTheodore Ts'o static int ext4_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock) 32112219aeaSAneesh Kumar K.V { 32212e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3239d0be502STheodore Ts'o return ext4_ext_calc_metadata_amount(inode, lblock); 32412219aeaSAneesh Kumar K.V 3258bb2b247SAmir Goldstein return ext4_ind_calc_metadata_amount(inode, lblock); 32612219aeaSAneesh Kumar K.V } 32712219aeaSAneesh Kumar K.V 3280637c6f4STheodore Ts'o /* 3290637c6f4STheodore Ts'o * Called with i_data_sem down, which is important since we can call 3300637c6f4STheodore Ts'o * ext4_discard_preallocations() from here. 3310637c6f4STheodore Ts'o */ 3325f634d06SAneesh Kumar K.V void ext4_da_update_reserve_space(struct inode *inode, 3335f634d06SAneesh Kumar K.V int used, int quota_claim) 33412219aeaSAneesh Kumar K.V { 33512219aeaSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3360637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 33712219aeaSAneesh Kumar K.V 3380637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 339d8990240SAditya Kali trace_ext4_da_update_reserve_space(inode, used, quota_claim); 3400637c6f4STheodore Ts'o if (unlikely(used > ei->i_reserved_data_blocks)) { 3410637c6f4STheodore Ts'o ext4_msg(inode->i_sb, KERN_NOTICE, "%s: ino %lu, used %d " 3421084f252STheodore Ts'o "with only %d reserved data blocks", 3430637c6f4STheodore Ts'o __func__, inode->i_ino, used, 3440637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 3450637c6f4STheodore Ts'o WARN_ON(1); 3460637c6f4STheodore Ts'o used = ei->i_reserved_data_blocks; 3476bc6e63fSAneesh Kumar K.V } 34812219aeaSAneesh Kumar K.V 3490637c6f4STheodore Ts'o /* Update per-inode reservations */ 3500637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= used; 3510637c6f4STheodore Ts'o ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks; 35257042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 35372b8ab9dSEric Sandeen used + ei->i_allocated_meta_blocks); 3540637c6f4STheodore Ts'o ei->i_allocated_meta_blocks = 0; 3550637c6f4STheodore Ts'o 3560637c6f4STheodore Ts'o if (ei->i_reserved_data_blocks == 0) { 3570637c6f4STheodore Ts'o /* 3580637c6f4STheodore Ts'o * We can release all of the reserved metadata blocks 3590637c6f4STheodore Ts'o * only when we have written all of the delayed 3600637c6f4STheodore Ts'o * allocation blocks. 3610637c6f4STheodore Ts'o */ 36257042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 36372b8ab9dSEric Sandeen ei->i_reserved_meta_blocks); 364ee5f4d9cSTheodore Ts'o ei->i_reserved_meta_blocks = 0; 3659d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 3660637c6f4STheodore Ts'o } 36712219aeaSAneesh Kumar K.V spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 36860e58e0fSMingming Cao 36972b8ab9dSEric Sandeen /* Update quota subsystem for data blocks */ 37072b8ab9dSEric Sandeen if (quota_claim) 3717b415bf6SAditya Kali dquot_claim_block(inode, EXT4_C2B(sbi, used)); 37272b8ab9dSEric Sandeen else { 3735f634d06SAneesh Kumar K.V /* 3745f634d06SAneesh Kumar K.V * We did fallocate with an offset that is already delayed 3755f634d06SAneesh Kumar K.V * allocated. So on delayed allocated writeback we should 37672b8ab9dSEric Sandeen * not re-claim the quota for fallocated blocks. 3775f634d06SAneesh Kumar K.V */ 3787b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, used)); 3795f634d06SAneesh Kumar K.V } 380d6014301SAneesh Kumar K.V 381d6014301SAneesh Kumar K.V /* 382d6014301SAneesh Kumar K.V * If we have done all the pending block allocations and if 383d6014301SAneesh Kumar K.V * there aren't any writers on the inode, we can discard the 384d6014301SAneesh Kumar K.V * inode's preallocations. 385d6014301SAneesh Kumar K.V */ 3860637c6f4STheodore Ts'o if ((ei->i_reserved_data_blocks == 0) && 3870637c6f4STheodore Ts'o (atomic_read(&inode->i_writecount) == 0)) 388d6014301SAneesh Kumar K.V ext4_discard_preallocations(inode); 38912219aeaSAneesh Kumar K.V } 39012219aeaSAneesh Kumar K.V 391e29136f8STheodore Ts'o static int __check_block_validity(struct inode *inode, const char *func, 392c398eda0STheodore Ts'o unsigned int line, 39324676da4STheodore Ts'o struct ext4_map_blocks *map) 3946fd058f7STheodore Ts'o { 39524676da4STheodore Ts'o if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk, 39624676da4STheodore Ts'o map->m_len)) { 397c398eda0STheodore Ts'o ext4_error_inode(inode, func, line, map->m_pblk, 398c398eda0STheodore Ts'o "lblock %lu mapped to illegal pblock " 39924676da4STheodore Ts'o "(length %d)", (unsigned long) map->m_lblk, 400c398eda0STheodore Ts'o map->m_len); 4016fd058f7STheodore Ts'o return -EIO; 4026fd058f7STheodore Ts'o } 4036fd058f7STheodore Ts'o return 0; 4046fd058f7STheodore Ts'o } 4056fd058f7STheodore Ts'o 406e29136f8STheodore Ts'o #define check_block_validity(inode, map) \ 407c398eda0STheodore Ts'o __check_block_validity((inode), __func__, __LINE__, (map)) 408e29136f8STheodore Ts'o 409f5ab0d1fSMingming Cao /* 4101f94533dSTheodore Ts'o * Return the number of contiguous dirty pages in a given inode 4111f94533dSTheodore Ts'o * starting at page frame idx. 41255138e0bSTheodore Ts'o */ 41355138e0bSTheodore Ts'o static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx, 41455138e0bSTheodore Ts'o unsigned int max_pages) 41555138e0bSTheodore Ts'o { 41655138e0bSTheodore Ts'o struct address_space *mapping = inode->i_mapping; 41755138e0bSTheodore Ts'o pgoff_t index; 41855138e0bSTheodore Ts'o struct pagevec pvec; 41955138e0bSTheodore Ts'o pgoff_t num = 0; 42055138e0bSTheodore Ts'o int i, nr_pages, done = 0; 42155138e0bSTheodore Ts'o 42255138e0bSTheodore Ts'o if (max_pages == 0) 42355138e0bSTheodore Ts'o return 0; 42455138e0bSTheodore Ts'o pagevec_init(&pvec, 0); 42555138e0bSTheodore Ts'o while (!done) { 42655138e0bSTheodore Ts'o index = idx; 42755138e0bSTheodore Ts'o nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 42855138e0bSTheodore Ts'o PAGECACHE_TAG_DIRTY, 42955138e0bSTheodore Ts'o (pgoff_t)PAGEVEC_SIZE); 43055138e0bSTheodore Ts'o if (nr_pages == 0) 43155138e0bSTheodore Ts'o break; 43255138e0bSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 43355138e0bSTheodore Ts'o struct page *page = pvec.pages[i]; 43455138e0bSTheodore Ts'o struct buffer_head *bh, *head; 43555138e0bSTheodore Ts'o 43655138e0bSTheodore Ts'o lock_page(page); 43755138e0bSTheodore Ts'o if (unlikely(page->mapping != mapping) || 43855138e0bSTheodore Ts'o !PageDirty(page) || 43955138e0bSTheodore Ts'o PageWriteback(page) || 44055138e0bSTheodore Ts'o page->index != idx) { 44155138e0bSTheodore Ts'o done = 1; 44255138e0bSTheodore Ts'o unlock_page(page); 44355138e0bSTheodore Ts'o break; 44455138e0bSTheodore Ts'o } 4451f94533dSTheodore Ts'o if (page_has_buffers(page)) { 4461f94533dSTheodore Ts'o bh = head = page_buffers(page); 44755138e0bSTheodore Ts'o do { 44855138e0bSTheodore Ts'o if (!buffer_delay(bh) && 4491f94533dSTheodore Ts'o !buffer_unwritten(bh)) 45055138e0bSTheodore Ts'o done = 1; 4511f94533dSTheodore Ts'o bh = bh->b_this_page; 4521f94533dSTheodore Ts'o } while (!done && (bh != head)); 45355138e0bSTheodore Ts'o } 45455138e0bSTheodore Ts'o unlock_page(page); 45555138e0bSTheodore Ts'o if (done) 45655138e0bSTheodore Ts'o break; 45755138e0bSTheodore Ts'o idx++; 45855138e0bSTheodore Ts'o num++; 459659c6009SEric Sandeen if (num >= max_pages) { 460659c6009SEric Sandeen done = 1; 46155138e0bSTheodore Ts'o break; 46255138e0bSTheodore Ts'o } 463659c6009SEric Sandeen } 46455138e0bSTheodore Ts'o pagevec_release(&pvec); 46555138e0bSTheodore Ts'o } 46655138e0bSTheodore Ts'o return num; 46755138e0bSTheodore Ts'o } 46855138e0bSTheodore Ts'o 46955138e0bSTheodore Ts'o /* 4705356f261SAditya Kali * Sets the BH_Da_Mapped bit on the buffer heads corresponding to the given map. 4715356f261SAditya Kali */ 4725356f261SAditya Kali static void set_buffers_da_mapped(struct inode *inode, 4735356f261SAditya Kali struct ext4_map_blocks *map) 4745356f261SAditya Kali { 4755356f261SAditya Kali struct address_space *mapping = inode->i_mapping; 4765356f261SAditya Kali struct pagevec pvec; 4775356f261SAditya Kali int i, nr_pages; 4785356f261SAditya Kali pgoff_t index, end; 4795356f261SAditya Kali 4805356f261SAditya Kali index = map->m_lblk >> (PAGE_CACHE_SHIFT - inode->i_blkbits); 4815356f261SAditya Kali end = (map->m_lblk + map->m_len - 1) >> 4825356f261SAditya Kali (PAGE_CACHE_SHIFT - inode->i_blkbits); 4835356f261SAditya Kali 4845356f261SAditya Kali pagevec_init(&pvec, 0); 4855356f261SAditya Kali while (index <= end) { 4865356f261SAditya Kali nr_pages = pagevec_lookup(&pvec, mapping, index, 4875356f261SAditya Kali min(end - index + 1, 4885356f261SAditya Kali (pgoff_t)PAGEVEC_SIZE)); 4895356f261SAditya Kali if (nr_pages == 0) 4905356f261SAditya Kali break; 4915356f261SAditya Kali for (i = 0; i < nr_pages; i++) { 4925356f261SAditya Kali struct page *page = pvec.pages[i]; 4935356f261SAditya Kali struct buffer_head *bh, *head; 4945356f261SAditya Kali 4955356f261SAditya Kali if (unlikely(page->mapping != mapping) || 4965356f261SAditya Kali !PageDirty(page)) 4975356f261SAditya Kali break; 4985356f261SAditya Kali 4995356f261SAditya Kali if (page_has_buffers(page)) { 5005356f261SAditya Kali bh = head = page_buffers(page); 5015356f261SAditya Kali do { 5025356f261SAditya Kali set_buffer_da_mapped(bh); 5035356f261SAditya Kali bh = bh->b_this_page; 5045356f261SAditya Kali } while (bh != head); 5055356f261SAditya Kali } 5065356f261SAditya Kali index++; 5075356f261SAditya Kali } 5085356f261SAditya Kali pagevec_release(&pvec); 5095356f261SAditya Kali } 5105356f261SAditya Kali } 5115356f261SAditya Kali 5125356f261SAditya Kali /* 513e35fd660STheodore Ts'o * The ext4_map_blocks() function tries to look up the requested blocks, 5142b2d6d01STheodore Ts'o * and returns if the blocks are already mapped. 515f5ab0d1fSMingming Cao * 516f5ab0d1fSMingming Cao * Otherwise it takes the write lock of the i_data_sem and allocate blocks 517f5ab0d1fSMingming Cao * and store the allocated blocks in the result buffer head and mark it 518f5ab0d1fSMingming Cao * mapped. 519f5ab0d1fSMingming Cao * 520e35fd660STheodore Ts'o * If file type is extents based, it will call ext4_ext_map_blocks(), 521e35fd660STheodore Ts'o * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping 522f5ab0d1fSMingming Cao * based files 523f5ab0d1fSMingming Cao * 524f5ab0d1fSMingming Cao * On success, it returns the number of blocks being mapped or allocate. 525f5ab0d1fSMingming Cao * if create==0 and the blocks are pre-allocated and uninitialized block, 526f5ab0d1fSMingming Cao * the result buffer head is unmapped. If the create ==1, it will make sure 527f5ab0d1fSMingming Cao * the buffer head is mapped. 528f5ab0d1fSMingming Cao * 529f5ab0d1fSMingming Cao * It returns 0 if plain look up failed (blocks have not been allocated), in 530df3ab170STao Ma * that case, buffer head is unmapped 531f5ab0d1fSMingming Cao * 532f5ab0d1fSMingming Cao * It returns the error in case of allocation failure. 533f5ab0d1fSMingming Cao */ 534e35fd660STheodore Ts'o int ext4_map_blocks(handle_t *handle, struct inode *inode, 535e35fd660STheodore Ts'o struct ext4_map_blocks *map, int flags) 5360e855ac8SAneesh Kumar K.V { 5370e855ac8SAneesh Kumar K.V int retval; 538f5ab0d1fSMingming Cao 539e35fd660STheodore Ts'o map->m_flags = 0; 540e35fd660STheodore Ts'o ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u," 541e35fd660STheodore Ts'o "logical block %lu\n", inode->i_ino, flags, map->m_len, 542e35fd660STheodore Ts'o (unsigned long) map->m_lblk); 5434df3d265SAneesh Kumar K.V /* 544b920c755STheodore Ts'o * Try to see if we can get the block without requesting a new 545b920c755STheodore Ts'o * file system block. 5464df3d265SAneesh Kumar K.V */ 547729f52c6SZheng Liu if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 5480e855ac8SAneesh Kumar K.V down_read((&EXT4_I(inode)->i_data_sem)); 54912e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 550a4e5d88bSDmitry Monakhov retval = ext4_ext_map_blocks(handle, inode, map, flags & 551a4e5d88bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 5524df3d265SAneesh Kumar K.V } else { 553a4e5d88bSDmitry Monakhov retval = ext4_ind_map_blocks(handle, inode, map, flags & 554a4e5d88bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 5550e855ac8SAneesh Kumar K.V } 556729f52c6SZheng Liu if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 5574df3d265SAneesh Kumar K.V up_read((&EXT4_I(inode)->i_data_sem)); 558f5ab0d1fSMingming Cao 559e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 560e29136f8STheodore Ts'o int ret = check_block_validity(inode, map); 5616fd058f7STheodore Ts'o if (ret != 0) 5626fd058f7STheodore Ts'o return ret; 5636fd058f7STheodore Ts'o } 5646fd058f7STheodore Ts'o 565f5ab0d1fSMingming Cao /* If it is only a block(s) look up */ 566c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 5674df3d265SAneesh Kumar K.V return retval; 5684df3d265SAneesh Kumar K.V 5694df3d265SAneesh Kumar K.V /* 570f5ab0d1fSMingming Cao * Returns if the blocks have already allocated 571f5ab0d1fSMingming Cao * 572f5ab0d1fSMingming Cao * Note that if blocks have been preallocated 573df3ab170STao Ma * ext4_ext_get_block() returns the create = 0 574f5ab0d1fSMingming Cao * with buffer head unmapped. 575f5ab0d1fSMingming Cao */ 576e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) 577f5ab0d1fSMingming Cao return retval; 578f5ab0d1fSMingming Cao 579f5ab0d1fSMingming Cao /* 5802a8964d6SAneesh Kumar K.V * When we call get_blocks without the create flag, the 5812a8964d6SAneesh Kumar K.V * BH_Unwritten flag could have gotten set if the blocks 5822a8964d6SAneesh Kumar K.V * requested were part of a uninitialized extent. We need to 5832a8964d6SAneesh Kumar K.V * clear this flag now that we are committed to convert all or 5842a8964d6SAneesh Kumar K.V * part of the uninitialized extent to be an initialized 5852a8964d6SAneesh Kumar K.V * extent. This is because we need to avoid the combination 5862a8964d6SAneesh Kumar K.V * of BH_Unwritten and BH_Mapped flags being simultaneously 5872a8964d6SAneesh Kumar K.V * set on the buffer_head. 5882a8964d6SAneesh Kumar K.V */ 589e35fd660STheodore Ts'o map->m_flags &= ~EXT4_MAP_UNWRITTEN; 5902a8964d6SAneesh Kumar K.V 5912a8964d6SAneesh Kumar K.V /* 592f5ab0d1fSMingming Cao * New blocks allocate and/or writing to uninitialized extent 593f5ab0d1fSMingming Cao * will possibly result in updating i_data, so we take 594f5ab0d1fSMingming Cao * the write lock of i_data_sem, and call get_blocks() 595f5ab0d1fSMingming Cao * with create == 1 flag. 5964df3d265SAneesh Kumar K.V */ 5974df3d265SAneesh Kumar K.V down_write((&EXT4_I(inode)->i_data_sem)); 598d2a17637SMingming Cao 599d2a17637SMingming Cao /* 600d2a17637SMingming Cao * if the caller is from delayed allocation writeout path 601d2a17637SMingming Cao * we have already reserved fs blocks for allocation 602d2a17637SMingming Cao * let the underlying get_block() function know to 603d2a17637SMingming Cao * avoid double accounting 604d2a17637SMingming Cao */ 605c2177057STheodore Ts'o if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 606f2321097STheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED); 6074df3d265SAneesh Kumar K.V /* 6084df3d265SAneesh Kumar K.V * We need to check for EXT4 here because migrate 6094df3d265SAneesh Kumar K.V * could have changed the inode type in between 6104df3d265SAneesh Kumar K.V */ 61112e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 612e35fd660STheodore Ts'o retval = ext4_ext_map_blocks(handle, inode, map, flags); 6130e855ac8SAneesh Kumar K.V } else { 614e35fd660STheodore Ts'o retval = ext4_ind_map_blocks(handle, inode, map, flags); 615267e4db9SAneesh Kumar K.V 616e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_NEW) { 617267e4db9SAneesh Kumar K.V /* 618267e4db9SAneesh Kumar K.V * We allocated new blocks which will result in 619267e4db9SAneesh Kumar K.V * i_data's format changing. Force the migrate 620267e4db9SAneesh Kumar K.V * to fail by clearing migrate flags 621267e4db9SAneesh Kumar K.V */ 62219f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE); 623267e4db9SAneesh Kumar K.V } 6242ac3b6e0STheodore Ts'o 625d2a17637SMingming Cao /* 6262ac3b6e0STheodore Ts'o * Update reserved blocks/metadata blocks after successful 6275f634d06SAneesh Kumar K.V * block allocation which had been deferred till now. We don't 6285f634d06SAneesh Kumar K.V * support fallocate for non extent files. So we can update 6295f634d06SAneesh Kumar K.V * reserve space here. 630d2a17637SMingming Cao */ 6315f634d06SAneesh Kumar K.V if ((retval > 0) && 6321296cc85SAneesh Kumar K.V (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)) 6335f634d06SAneesh Kumar K.V ext4_da_update_reserve_space(inode, retval, 1); 6345f634d06SAneesh Kumar K.V } 6355356f261SAditya Kali if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) { 636f2321097STheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED); 637d2a17637SMingming Cao 6385356f261SAditya Kali /* If we have successfully mapped the delayed allocated blocks, 6395356f261SAditya Kali * set the BH_Da_Mapped bit on them. Its important to do this 6405356f261SAditya Kali * under the protection of i_data_sem. 6415356f261SAditya Kali */ 6425356f261SAditya Kali if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) 6435356f261SAditya Kali set_buffers_da_mapped(inode, map); 6445356f261SAditya Kali } 6455356f261SAditya Kali 6460e855ac8SAneesh Kumar K.V up_write((&EXT4_I(inode)->i_data_sem)); 647e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 648e29136f8STheodore Ts'o int ret = check_block_validity(inode, map); 6496fd058f7STheodore Ts'o if (ret != 0) 6506fd058f7STheodore Ts'o return ret; 6516fd058f7STheodore Ts'o } 6520e855ac8SAneesh Kumar K.V return retval; 6530e855ac8SAneesh Kumar K.V } 6540e855ac8SAneesh Kumar K.V 655f3bd1f3fSMingming Cao /* Maximum number of blocks we map for direct IO at once. */ 656f3bd1f3fSMingming Cao #define DIO_MAX_BLOCKS 4096 657f3bd1f3fSMingming Cao 6582ed88685STheodore Ts'o static int _ext4_get_block(struct inode *inode, sector_t iblock, 6592ed88685STheodore Ts'o struct buffer_head *bh, int flags) 660ac27a0ecSDave Kleikamp { 6613e4fdaf8SDmitriy Monakhov handle_t *handle = ext4_journal_current_handle(); 6622ed88685STheodore Ts'o struct ext4_map_blocks map; 6637fb5409dSJan Kara int ret = 0, started = 0; 664f3bd1f3fSMingming Cao int dio_credits; 665ac27a0ecSDave Kleikamp 6662ed88685STheodore Ts'o map.m_lblk = iblock; 6672ed88685STheodore Ts'o map.m_len = bh->b_size >> inode->i_blkbits; 6682ed88685STheodore Ts'o 6692ed88685STheodore Ts'o if (flags && !handle) { 6707fb5409dSJan Kara /* Direct IO write... */ 6712ed88685STheodore Ts'o if (map.m_len > DIO_MAX_BLOCKS) 6722ed88685STheodore Ts'o map.m_len = DIO_MAX_BLOCKS; 6732ed88685STheodore Ts'o dio_credits = ext4_chunk_trans_blocks(inode, map.m_len); 674f3bd1f3fSMingming Cao handle = ext4_journal_start(inode, dio_credits); 6757fb5409dSJan Kara if (IS_ERR(handle)) { 676ac27a0ecSDave Kleikamp ret = PTR_ERR(handle); 6772ed88685STheodore Ts'o return ret; 6787fb5409dSJan Kara } 6797fb5409dSJan Kara started = 1; 680ac27a0ecSDave Kleikamp } 681ac27a0ecSDave Kleikamp 6822ed88685STheodore Ts'o ret = ext4_map_blocks(handle, inode, &map, flags); 683ac27a0ecSDave Kleikamp if (ret > 0) { 6842ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 6852ed88685STheodore Ts'o bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; 6862ed88685STheodore Ts'o bh->b_size = inode->i_sb->s_blocksize * map.m_len; 687ac27a0ecSDave Kleikamp ret = 0; 688ac27a0ecSDave Kleikamp } 6897fb5409dSJan Kara if (started) 6907fb5409dSJan Kara ext4_journal_stop(handle); 691ac27a0ecSDave Kleikamp return ret; 692ac27a0ecSDave Kleikamp } 693ac27a0ecSDave Kleikamp 6942ed88685STheodore Ts'o int ext4_get_block(struct inode *inode, sector_t iblock, 6952ed88685STheodore Ts'o struct buffer_head *bh, int create) 6962ed88685STheodore Ts'o { 6972ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh, 6982ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 6992ed88685STheodore Ts'o } 7002ed88685STheodore Ts'o 701ac27a0ecSDave Kleikamp /* 702ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 703ac27a0ecSDave Kleikamp */ 704617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 705725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *errp) 706ac27a0ecSDave Kleikamp { 7072ed88685STheodore Ts'o struct ext4_map_blocks map; 7082ed88685STheodore Ts'o struct buffer_head *bh; 709ac27a0ecSDave Kleikamp int fatal = 0, err; 710ac27a0ecSDave Kleikamp 711ac27a0ecSDave Kleikamp J_ASSERT(handle != NULL || create == 0); 712ac27a0ecSDave Kleikamp 7132ed88685STheodore Ts'o map.m_lblk = block; 7142ed88685STheodore Ts'o map.m_len = 1; 7152ed88685STheodore Ts'o err = ext4_map_blocks(handle, inode, &map, 7162ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 7172ed88685STheodore Ts'o 7182ed88685STheodore Ts'o if (err < 0) 719ac27a0ecSDave Kleikamp *errp = err; 7202ed88685STheodore Ts'o if (err <= 0) 7212ed88685STheodore Ts'o return NULL; 7222ed88685STheodore Ts'o *errp = 0; 7232ed88685STheodore Ts'o 7242ed88685STheodore Ts'o bh = sb_getblk(inode->i_sb, map.m_pblk); 725ac27a0ecSDave Kleikamp if (!bh) { 726ac27a0ecSDave Kleikamp *errp = -EIO; 7272ed88685STheodore Ts'o return NULL; 728ac27a0ecSDave Kleikamp } 7292ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 730ac27a0ecSDave Kleikamp J_ASSERT(create != 0); 731ac39849dSAneesh Kumar K.V J_ASSERT(handle != NULL); 732ac27a0ecSDave Kleikamp 733ac27a0ecSDave Kleikamp /* 734ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 735ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 736ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 737617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 738ac27a0ecSDave Kleikamp * problem. 739ac27a0ecSDave Kleikamp */ 740ac27a0ecSDave Kleikamp lock_buffer(bh); 741ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 742617ba13bSMingming Cao fatal = ext4_journal_get_create_access(handle, bh); 743ac27a0ecSDave Kleikamp if (!fatal && !buffer_uptodate(bh)) { 744ac27a0ecSDave Kleikamp memset(bh->b_data, 0, inode->i_sb->s_blocksize); 745ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 746ac27a0ecSDave Kleikamp } 747ac27a0ecSDave Kleikamp unlock_buffer(bh); 7480390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 7490390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 750ac27a0ecSDave Kleikamp if (!fatal) 751ac27a0ecSDave Kleikamp fatal = err; 752ac27a0ecSDave Kleikamp } else { 753ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "not a new buffer"); 754ac27a0ecSDave Kleikamp } 755ac27a0ecSDave Kleikamp if (fatal) { 756ac27a0ecSDave Kleikamp *errp = fatal; 757ac27a0ecSDave Kleikamp brelse(bh); 758ac27a0ecSDave Kleikamp bh = NULL; 759ac27a0ecSDave Kleikamp } 760ac27a0ecSDave Kleikamp return bh; 761ac27a0ecSDave Kleikamp } 762ac27a0ecSDave Kleikamp 763617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 764725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *err) 765ac27a0ecSDave Kleikamp { 766ac27a0ecSDave Kleikamp struct buffer_head *bh; 767ac27a0ecSDave Kleikamp 768617ba13bSMingming Cao bh = ext4_getblk(handle, inode, block, create, err); 769ac27a0ecSDave Kleikamp if (!bh) 770ac27a0ecSDave Kleikamp return bh; 771ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 772ac27a0ecSDave Kleikamp return bh; 77365299a3bSChristoph Hellwig ll_rw_block(READ | REQ_META | REQ_PRIO, 1, &bh); 774ac27a0ecSDave Kleikamp wait_on_buffer(bh); 775ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 776ac27a0ecSDave Kleikamp return bh; 777ac27a0ecSDave Kleikamp put_bh(bh); 778ac27a0ecSDave Kleikamp *err = -EIO; 779ac27a0ecSDave Kleikamp return NULL; 780ac27a0ecSDave Kleikamp } 781ac27a0ecSDave Kleikamp 782ac27a0ecSDave Kleikamp static int walk_page_buffers(handle_t *handle, 783ac27a0ecSDave Kleikamp struct buffer_head *head, 784ac27a0ecSDave Kleikamp unsigned from, 785ac27a0ecSDave Kleikamp unsigned to, 786ac27a0ecSDave Kleikamp int *partial, 787ac27a0ecSDave Kleikamp int (*fn)(handle_t *handle, 788ac27a0ecSDave Kleikamp struct buffer_head *bh)) 789ac27a0ecSDave Kleikamp { 790ac27a0ecSDave Kleikamp struct buffer_head *bh; 791ac27a0ecSDave Kleikamp unsigned block_start, block_end; 792ac27a0ecSDave Kleikamp unsigned blocksize = head->b_size; 793ac27a0ecSDave Kleikamp int err, ret = 0; 794ac27a0ecSDave Kleikamp struct buffer_head *next; 795ac27a0ecSDave Kleikamp 796ac27a0ecSDave Kleikamp for (bh = head, block_start = 0; 797ac27a0ecSDave Kleikamp ret == 0 && (bh != head || !block_start); 798de9a55b8STheodore Ts'o block_start = block_end, bh = next) { 799ac27a0ecSDave Kleikamp next = bh->b_this_page; 800ac27a0ecSDave Kleikamp block_end = block_start + blocksize; 801ac27a0ecSDave Kleikamp if (block_end <= from || block_start >= to) { 802ac27a0ecSDave Kleikamp if (partial && !buffer_uptodate(bh)) 803ac27a0ecSDave Kleikamp *partial = 1; 804ac27a0ecSDave Kleikamp continue; 805ac27a0ecSDave Kleikamp } 806ac27a0ecSDave Kleikamp err = (*fn)(handle, bh); 807ac27a0ecSDave Kleikamp if (!ret) 808ac27a0ecSDave Kleikamp ret = err; 809ac27a0ecSDave Kleikamp } 810ac27a0ecSDave Kleikamp return ret; 811ac27a0ecSDave Kleikamp } 812ac27a0ecSDave Kleikamp 813ac27a0ecSDave Kleikamp /* 814ac27a0ecSDave Kleikamp * To preserve ordering, it is essential that the hole instantiation and 815ac27a0ecSDave Kleikamp * the data write be encapsulated in a single transaction. We cannot 816617ba13bSMingming Cao * close off a transaction and start a new one between the ext4_get_block() 817dab291afSMingming Cao * and the commit_write(). So doing the jbd2_journal_start at the start of 818ac27a0ecSDave Kleikamp * prepare_write() is the right place. 819ac27a0ecSDave Kleikamp * 820617ba13bSMingming Cao * Also, this function can nest inside ext4_writepage() -> 821617ba13bSMingming Cao * block_write_full_page(). In that case, we *know* that ext4_writepage() 822ac27a0ecSDave Kleikamp * has generated enough buffer credits to do the whole page. So we won't 823ac27a0ecSDave Kleikamp * block on the journal in that case, which is good, because the caller may 824ac27a0ecSDave Kleikamp * be PF_MEMALLOC. 825ac27a0ecSDave Kleikamp * 826617ba13bSMingming Cao * By accident, ext4 can be reentered when a transaction is open via 827ac27a0ecSDave Kleikamp * quota file writes. If we were to commit the transaction while thus 828ac27a0ecSDave Kleikamp * reentered, there can be a deadlock - we would be holding a quota 829ac27a0ecSDave Kleikamp * lock, and the commit would never complete if another thread had a 830ac27a0ecSDave Kleikamp * transaction open and was blocking on the quota lock - a ranking 831ac27a0ecSDave Kleikamp * violation. 832ac27a0ecSDave Kleikamp * 833dab291afSMingming Cao * So what we do is to rely on the fact that jbd2_journal_stop/journal_start 834ac27a0ecSDave Kleikamp * will _not_ run commit under these circumstances because handle->h_ref 835ac27a0ecSDave Kleikamp * is elevated. We'll still have enough credits for the tiny quotafile 836ac27a0ecSDave Kleikamp * write. 837ac27a0ecSDave Kleikamp */ 838ac27a0ecSDave Kleikamp static int do_journal_get_write_access(handle_t *handle, 839ac27a0ecSDave Kleikamp struct buffer_head *bh) 840ac27a0ecSDave Kleikamp { 84156d35a4cSJan Kara int dirty = buffer_dirty(bh); 84256d35a4cSJan Kara int ret; 84356d35a4cSJan Kara 844ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 845ac27a0ecSDave Kleikamp return 0; 84656d35a4cSJan Kara /* 847ebdec241SChristoph Hellwig * __block_write_begin() could have dirtied some buffers. Clean 84856d35a4cSJan Kara * the dirty bit as jbd2_journal_get_write_access() could complain 84956d35a4cSJan Kara * otherwise about fs integrity issues. Setting of the dirty bit 850ebdec241SChristoph Hellwig * by __block_write_begin() isn't a real problem here as we clear 85156d35a4cSJan Kara * the bit before releasing a page lock and thus writeback cannot 85256d35a4cSJan Kara * ever write the buffer. 85356d35a4cSJan Kara */ 85456d35a4cSJan Kara if (dirty) 85556d35a4cSJan Kara clear_buffer_dirty(bh); 85656d35a4cSJan Kara ret = ext4_journal_get_write_access(handle, bh); 85756d35a4cSJan Kara if (!ret && dirty) 85856d35a4cSJan Kara ret = ext4_handle_dirty_metadata(handle, NULL, bh); 85956d35a4cSJan Kara return ret; 860ac27a0ecSDave Kleikamp } 861ac27a0ecSDave Kleikamp 862744692dcSJiaying Zhang static int ext4_get_block_write(struct inode *inode, sector_t iblock, 863744692dcSJiaying Zhang struct buffer_head *bh_result, int create); 864bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 865bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned flags, 866bfc1af65SNick Piggin struct page **pagep, void **fsdata) 867ac27a0ecSDave Kleikamp { 868bfc1af65SNick Piggin struct inode *inode = mapping->host; 8691938a150SAneesh Kumar K.V int ret, needed_blocks; 870ac27a0ecSDave Kleikamp handle_t *handle; 871ac27a0ecSDave Kleikamp int retries = 0; 872bfc1af65SNick Piggin struct page *page; 873bfc1af65SNick Piggin pgoff_t index; 874bfc1af65SNick Piggin unsigned from, to; 875bfc1af65SNick Piggin 8769bffad1eSTheodore Ts'o trace_ext4_write_begin(inode, pos, len, flags); 8771938a150SAneesh Kumar K.V /* 8781938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 8791938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 8801938a150SAneesh Kumar K.V */ 8811938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 882bfc1af65SNick Piggin index = pos >> PAGE_CACHE_SHIFT; 883bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 884bfc1af65SNick Piggin to = from + len; 885ac27a0ecSDave Kleikamp 886ac27a0ecSDave Kleikamp retry: 887617ba13bSMingming Cao handle = ext4_journal_start(inode, needed_blocks); 8887479d2b9SAndrew Morton if (IS_ERR(handle)) { 8897479d2b9SAndrew Morton ret = PTR_ERR(handle); 8907479d2b9SAndrew Morton goto out; 8917479d2b9SAndrew Morton } 892ac27a0ecSDave Kleikamp 893ebd3610bSJan Kara /* We cannot recurse into the filesystem as the transaction is already 894ebd3610bSJan Kara * started */ 895ebd3610bSJan Kara flags |= AOP_FLAG_NOFS; 896ebd3610bSJan Kara 89754566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 898cf108bcaSJan Kara if (!page) { 899cf108bcaSJan Kara ext4_journal_stop(handle); 900cf108bcaSJan Kara ret = -ENOMEM; 901cf108bcaSJan Kara goto out; 902cf108bcaSJan Kara } 903cf108bcaSJan Kara *pagep = page; 904cf108bcaSJan Kara 905744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 9066e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block_write); 907744692dcSJiaying Zhang else 9086e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block); 909bfc1af65SNick Piggin 910bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 911ac27a0ecSDave Kleikamp ret = walk_page_buffers(handle, page_buffers(page), 912ac27a0ecSDave Kleikamp from, to, NULL, do_journal_get_write_access); 913b46be050SAndrey Savochkin } 914bfc1af65SNick Piggin 915bfc1af65SNick Piggin if (ret) { 916bfc1af65SNick Piggin unlock_page(page); 917bfc1af65SNick Piggin page_cache_release(page); 918ae4d5372SAneesh Kumar K.V /* 9196e1db88dSChristoph Hellwig * __block_write_begin may have instantiated a few blocks 920ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 921ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 9221938a150SAneesh Kumar K.V * 9231938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 9241938a150SAneesh Kumar K.V * truncate finishes 925ae4d5372SAneesh Kumar K.V */ 926ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 9271938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 9281938a150SAneesh Kumar K.V 9291938a150SAneesh Kumar K.V ext4_journal_stop(handle); 9301938a150SAneesh Kumar K.V if (pos + len > inode->i_size) { 931b9a4207dSJan Kara ext4_truncate_failed_write(inode); 9321938a150SAneesh Kumar K.V /* 933ffacfa7aSJan Kara * If truncate failed early the inode might 9341938a150SAneesh Kumar K.V * still be on the orphan list; we need to 9351938a150SAneesh Kumar K.V * make sure the inode is removed from the 9361938a150SAneesh Kumar K.V * orphan list in that case. 9371938a150SAneesh Kumar K.V */ 9381938a150SAneesh Kumar K.V if (inode->i_nlink) 9391938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 9401938a150SAneesh Kumar K.V } 941bfc1af65SNick Piggin } 942bfc1af65SNick Piggin 943617ba13bSMingming Cao if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 944ac27a0ecSDave Kleikamp goto retry; 9457479d2b9SAndrew Morton out: 946ac27a0ecSDave Kleikamp return ret; 947ac27a0ecSDave Kleikamp } 948ac27a0ecSDave Kleikamp 949bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 950bfc1af65SNick Piggin static int write_end_fn(handle_t *handle, struct buffer_head *bh) 951ac27a0ecSDave Kleikamp { 952ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 953ac27a0ecSDave Kleikamp return 0; 954ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 9550390131bSFrank Mayhar return ext4_handle_dirty_metadata(handle, NULL, bh); 956ac27a0ecSDave Kleikamp } 957ac27a0ecSDave Kleikamp 958f8514083SAneesh Kumar K.V static int ext4_generic_write_end(struct file *file, 959f8514083SAneesh Kumar K.V struct address_space *mapping, 960f8514083SAneesh Kumar K.V loff_t pos, unsigned len, unsigned copied, 961f8514083SAneesh Kumar K.V struct page *page, void *fsdata) 962f8514083SAneesh Kumar K.V { 963f8514083SAneesh Kumar K.V int i_size_changed = 0; 964f8514083SAneesh Kumar K.V struct inode *inode = mapping->host; 965f8514083SAneesh Kumar K.V handle_t *handle = ext4_journal_current_handle(); 966f8514083SAneesh Kumar K.V 967f8514083SAneesh Kumar K.V copied = block_write_end(file, mapping, pos, len, copied, page, fsdata); 968f8514083SAneesh Kumar K.V 969f8514083SAneesh Kumar K.V /* 970f8514083SAneesh Kumar K.V * No need to use i_size_read() here, the i_size 971f8514083SAneesh Kumar K.V * cannot change under us because we hold i_mutex. 972f8514083SAneesh Kumar K.V * 973f8514083SAneesh Kumar K.V * But it's important to update i_size while still holding page lock: 974f8514083SAneesh Kumar K.V * page writeout could otherwise come in and zero beyond i_size. 975f8514083SAneesh Kumar K.V */ 976f8514083SAneesh Kumar K.V if (pos + copied > inode->i_size) { 977f8514083SAneesh Kumar K.V i_size_write(inode, pos + copied); 978f8514083SAneesh Kumar K.V i_size_changed = 1; 979f8514083SAneesh Kumar K.V } 980f8514083SAneesh Kumar K.V 981f8514083SAneesh Kumar K.V if (pos + copied > EXT4_I(inode)->i_disksize) { 982f8514083SAneesh Kumar K.V /* We need to mark inode dirty even if 983f8514083SAneesh Kumar K.V * new_i_size is less that inode->i_size 984f8514083SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 985f8514083SAneesh Kumar K.V */ 986f8514083SAneesh Kumar K.V ext4_update_i_disksize(inode, (pos + copied)); 987f8514083SAneesh Kumar K.V i_size_changed = 1; 988f8514083SAneesh Kumar K.V } 989f8514083SAneesh Kumar K.V unlock_page(page); 990f8514083SAneesh Kumar K.V page_cache_release(page); 991f8514083SAneesh Kumar K.V 992f8514083SAneesh Kumar K.V /* 993f8514083SAneesh Kumar K.V * Don't mark the inode dirty under page lock. First, it unnecessarily 994f8514083SAneesh Kumar K.V * makes the holding time of page lock longer. Second, it forces lock 995f8514083SAneesh Kumar K.V * ordering of page lock and transaction start for journaling 996f8514083SAneesh Kumar K.V * filesystems. 997f8514083SAneesh Kumar K.V */ 998f8514083SAneesh Kumar K.V if (i_size_changed) 999f8514083SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 1000f8514083SAneesh Kumar K.V 1001f8514083SAneesh Kumar K.V return copied; 1002f8514083SAneesh Kumar K.V } 1003f8514083SAneesh Kumar K.V 1004ac27a0ecSDave Kleikamp /* 1005ac27a0ecSDave Kleikamp * We need to pick up the new inode size which generic_commit_write gave us 1006ac27a0ecSDave Kleikamp * `file' can be NULL - eg, when called from page_symlink(). 1007ac27a0ecSDave Kleikamp * 1008617ba13bSMingming Cao * ext4 never places buffers on inode->i_mapping->private_list. metadata 1009ac27a0ecSDave Kleikamp * buffers are managed internally. 1010ac27a0ecSDave Kleikamp */ 1011bfc1af65SNick Piggin static int ext4_ordered_write_end(struct file *file, 1012bfc1af65SNick Piggin struct address_space *mapping, 1013bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1014bfc1af65SNick Piggin struct page *page, void *fsdata) 1015ac27a0ecSDave Kleikamp { 1016617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1017cf108bcaSJan Kara struct inode *inode = mapping->host; 1018ac27a0ecSDave Kleikamp int ret = 0, ret2; 1019ac27a0ecSDave Kleikamp 10209bffad1eSTheodore Ts'o trace_ext4_ordered_write_end(inode, pos, len, copied); 1021678aaf48SJan Kara ret = ext4_jbd2_file_inode(handle, inode); 1022ac27a0ecSDave Kleikamp 1023ac27a0ecSDave Kleikamp if (ret == 0) { 1024f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 1025bfc1af65SNick Piggin page, fsdata); 1026f8a87d89SRoel Kluin copied = ret2; 1027ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1028f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1029f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1030f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1031f8514083SAneesh Kumar K.V */ 1032f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1033f8a87d89SRoel Kluin if (ret2 < 0) 1034f8a87d89SRoel Kluin ret = ret2; 103509e0834fSAkira Fujita } else { 103609e0834fSAkira Fujita unlock_page(page); 103709e0834fSAkira Fujita page_cache_release(page); 1038ac27a0ecSDave Kleikamp } 103909e0834fSAkira Fujita 1040617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1041ac27a0ecSDave Kleikamp if (!ret) 1042ac27a0ecSDave Kleikamp ret = ret2; 1043bfc1af65SNick Piggin 1044f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1045b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1046f8514083SAneesh Kumar K.V /* 1047ffacfa7aSJan Kara * If truncate failed early the inode might still be 1048f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1049f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1050f8514083SAneesh Kumar K.V */ 1051f8514083SAneesh Kumar K.V if (inode->i_nlink) 1052f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1053f8514083SAneesh Kumar K.V } 1054f8514083SAneesh Kumar K.V 1055f8514083SAneesh Kumar K.V 1056bfc1af65SNick Piggin return ret ? ret : copied; 1057ac27a0ecSDave Kleikamp } 1058ac27a0ecSDave Kleikamp 1059bfc1af65SNick Piggin static int ext4_writeback_write_end(struct file *file, 1060bfc1af65SNick Piggin struct address_space *mapping, 1061bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1062bfc1af65SNick Piggin struct page *page, void *fsdata) 1063ac27a0ecSDave Kleikamp { 1064617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1065cf108bcaSJan Kara struct inode *inode = mapping->host; 1066ac27a0ecSDave Kleikamp int ret = 0, ret2; 1067ac27a0ecSDave Kleikamp 10689bffad1eSTheodore Ts'o trace_ext4_writeback_write_end(inode, pos, len, copied); 1069f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 1070bfc1af65SNick Piggin page, fsdata); 1071f8a87d89SRoel Kluin copied = ret2; 1072ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1073f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1074f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1075f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1076f8514083SAneesh Kumar K.V */ 1077f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1078f8514083SAneesh Kumar K.V 1079f8a87d89SRoel Kluin if (ret2 < 0) 1080f8a87d89SRoel Kluin ret = ret2; 1081ac27a0ecSDave Kleikamp 1082617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1083ac27a0ecSDave Kleikamp if (!ret) 1084ac27a0ecSDave Kleikamp ret = ret2; 1085bfc1af65SNick Piggin 1086f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1087b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1088f8514083SAneesh Kumar K.V /* 1089ffacfa7aSJan Kara * If truncate failed early the inode might still be 1090f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1091f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1092f8514083SAneesh Kumar K.V */ 1093f8514083SAneesh Kumar K.V if (inode->i_nlink) 1094f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1095f8514083SAneesh Kumar K.V } 1096f8514083SAneesh Kumar K.V 1097bfc1af65SNick Piggin return ret ? ret : copied; 1098ac27a0ecSDave Kleikamp } 1099ac27a0ecSDave Kleikamp 1100bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 1101bfc1af65SNick Piggin struct address_space *mapping, 1102bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1103bfc1af65SNick Piggin struct page *page, void *fsdata) 1104ac27a0ecSDave Kleikamp { 1105617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1106bfc1af65SNick Piggin struct inode *inode = mapping->host; 1107ac27a0ecSDave Kleikamp int ret = 0, ret2; 1108ac27a0ecSDave Kleikamp int partial = 0; 1109bfc1af65SNick Piggin unsigned from, to; 1110cf17fea6SAneesh Kumar K.V loff_t new_i_size; 1111ac27a0ecSDave Kleikamp 11129bffad1eSTheodore Ts'o trace_ext4_journalled_write_end(inode, pos, len, copied); 1113bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 1114bfc1af65SNick Piggin to = from + len; 1115bfc1af65SNick Piggin 1116441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1117441c8508SCurt Wohlgemuth 1118bfc1af65SNick Piggin if (copied < len) { 1119bfc1af65SNick Piggin if (!PageUptodate(page)) 1120bfc1af65SNick Piggin copied = 0; 1121bfc1af65SNick Piggin page_zero_new_buffers(page, from+copied, to); 1122bfc1af65SNick Piggin } 1123ac27a0ecSDave Kleikamp 1124ac27a0ecSDave Kleikamp ret = walk_page_buffers(handle, page_buffers(page), from, 1125bfc1af65SNick Piggin to, &partial, write_end_fn); 1126ac27a0ecSDave Kleikamp if (!partial) 1127ac27a0ecSDave Kleikamp SetPageUptodate(page); 1128cf17fea6SAneesh Kumar K.V new_i_size = pos + copied; 1129cf17fea6SAneesh Kumar K.V if (new_i_size > inode->i_size) 1130bfc1af65SNick Piggin i_size_write(inode, pos+copied); 113119f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 11322d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 1133cf17fea6SAneesh Kumar K.V if (new_i_size > EXT4_I(inode)->i_disksize) { 1134cf17fea6SAneesh Kumar K.V ext4_update_i_disksize(inode, new_i_size); 1135617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1136ac27a0ecSDave Kleikamp if (!ret) 1137ac27a0ecSDave Kleikamp ret = ret2; 1138ac27a0ecSDave Kleikamp } 1139bfc1af65SNick Piggin 1140cf108bcaSJan Kara unlock_page(page); 1141f8514083SAneesh Kumar K.V page_cache_release(page); 1142ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1143f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1144f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1145f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1146f8514083SAneesh Kumar K.V */ 1147f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1148f8514083SAneesh Kumar K.V 1149617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1150ac27a0ecSDave Kleikamp if (!ret) 1151ac27a0ecSDave Kleikamp ret = ret2; 1152f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1153b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1154f8514083SAneesh Kumar K.V /* 1155ffacfa7aSJan Kara * If truncate failed early the inode might still be 1156f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1157f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1158f8514083SAneesh Kumar K.V */ 1159f8514083SAneesh Kumar K.V if (inode->i_nlink) 1160f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1161f8514083SAneesh Kumar K.V } 1162bfc1af65SNick Piggin 1163bfc1af65SNick Piggin return ret ? ret : copied; 1164ac27a0ecSDave Kleikamp } 1165d2a17637SMingming Cao 11669d0be502STheodore Ts'o /* 11677b415bf6SAditya Kali * Reserve a single cluster located at lblock 11689d0be502STheodore Ts'o */ 116901f49d0bSTheodore Ts'o static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock) 1170d2a17637SMingming Cao { 1171030ba6bcSAneesh Kumar K.V int retries = 0; 1172d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 11730637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 11747b415bf6SAditya Kali unsigned int md_needed; 11755dd4056dSChristoph Hellwig int ret; 1176d2a17637SMingming Cao 1177d2a17637SMingming Cao /* 1178d2a17637SMingming Cao * recalculate the amount of metadata blocks to reserve 1179d2a17637SMingming Cao * in order to allocate nrblocks 1180d2a17637SMingming Cao * worse case is one extent per block 1181d2a17637SMingming Cao */ 1182030ba6bcSAneesh Kumar K.V repeat: 11830637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 11847b415bf6SAditya Kali md_needed = EXT4_NUM_B2C(sbi, 11857b415bf6SAditya Kali ext4_calc_metadata_amount(inode, lblock)); 1186f8ec9d68STheodore Ts'o trace_ext4_da_reserve_space(inode, md_needed); 11870637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 1188d2a17637SMingming Cao 118960e58e0fSMingming Cao /* 119072b8ab9dSEric Sandeen * We will charge metadata quota at writeout time; this saves 119172b8ab9dSEric Sandeen * us from metadata over-estimation, though we may go over by 119272b8ab9dSEric Sandeen * a small amount in the end. Here we just reserve for data. 119360e58e0fSMingming Cao */ 11947b415bf6SAditya Kali ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1)); 11955dd4056dSChristoph Hellwig if (ret) 11965dd4056dSChristoph Hellwig return ret; 119772b8ab9dSEric Sandeen /* 119872b8ab9dSEric Sandeen * We do still charge estimated metadata to the sb though; 119972b8ab9dSEric Sandeen * we cannot afford to run out of free blocks. 120072b8ab9dSEric Sandeen */ 1201e7d5f315STheodore Ts'o if (ext4_claim_free_clusters(sbi, md_needed + 1, 0)) { 12027b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1)); 1203030ba6bcSAneesh Kumar K.V if (ext4_should_retry_alloc(inode->i_sb, &retries)) { 1204030ba6bcSAneesh Kumar K.V yield(); 1205030ba6bcSAneesh Kumar K.V goto repeat; 1206030ba6bcSAneesh Kumar K.V } 1207d2a17637SMingming Cao return -ENOSPC; 1208d2a17637SMingming Cao } 12090637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 12109d0be502STheodore Ts'o ei->i_reserved_data_blocks++; 12110637c6f4STheodore Ts'o ei->i_reserved_meta_blocks += md_needed; 12120637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 121339bc680aSDmitry Monakhov 1214d2a17637SMingming Cao return 0; /* success */ 1215d2a17637SMingming Cao } 1216d2a17637SMingming Cao 121712219aeaSAneesh Kumar K.V static void ext4_da_release_space(struct inode *inode, int to_free) 1218d2a17637SMingming Cao { 1219d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 12200637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 1221d2a17637SMingming Cao 1222cd213226SMingming Cao if (!to_free) 1223cd213226SMingming Cao return; /* Nothing to release, exit */ 1224cd213226SMingming Cao 1225d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1226cd213226SMingming Cao 12275a58ec87SLi Zefan trace_ext4_da_release_space(inode, to_free); 12280637c6f4STheodore Ts'o if (unlikely(to_free > ei->i_reserved_data_blocks)) { 1229cd213226SMingming Cao /* 12300637c6f4STheodore Ts'o * if there aren't enough reserved blocks, then the 12310637c6f4STheodore Ts'o * counter is messed up somewhere. Since this 12320637c6f4STheodore Ts'o * function is called from invalidate page, it's 12330637c6f4STheodore Ts'o * harmless to return without any action. 1234cd213226SMingming Cao */ 12350637c6f4STheodore Ts'o ext4_msg(inode->i_sb, KERN_NOTICE, "ext4_da_release_space: " 12360637c6f4STheodore Ts'o "ino %lu, to_free %d with only %d reserved " 12371084f252STheodore Ts'o "data blocks", inode->i_ino, to_free, 12380637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 12390637c6f4STheodore Ts'o WARN_ON(1); 12400637c6f4STheodore Ts'o to_free = ei->i_reserved_data_blocks; 12410637c6f4STheodore Ts'o } 12420637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= to_free; 12430637c6f4STheodore Ts'o 12440637c6f4STheodore Ts'o if (ei->i_reserved_data_blocks == 0) { 12450637c6f4STheodore Ts'o /* 12460637c6f4STheodore Ts'o * We can release all of the reserved metadata blocks 12470637c6f4STheodore Ts'o * only when we have written all of the delayed 12480637c6f4STheodore Ts'o * allocation blocks. 12497b415bf6SAditya Kali * Note that in case of bigalloc, i_reserved_meta_blocks, 12507b415bf6SAditya Kali * i_reserved_data_blocks, etc. refer to number of clusters. 12510637c6f4STheodore Ts'o */ 125257042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 125372b8ab9dSEric Sandeen ei->i_reserved_meta_blocks); 1254ee5f4d9cSTheodore Ts'o ei->i_reserved_meta_blocks = 0; 12559d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 1256cd213226SMingming Cao } 1257cd213226SMingming Cao 125872b8ab9dSEric Sandeen /* update fs dirty data blocks counter */ 125957042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free); 1260d2a17637SMingming Cao 1261d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 126260e58e0fSMingming Cao 12637b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free)); 1264d2a17637SMingming Cao } 1265d2a17637SMingming Cao 1266d2a17637SMingming Cao static void ext4_da_page_release_reservation(struct page *page, 1267d2a17637SMingming Cao unsigned long offset) 1268d2a17637SMingming Cao { 1269d2a17637SMingming Cao int to_release = 0; 1270d2a17637SMingming Cao struct buffer_head *head, *bh; 1271d2a17637SMingming Cao unsigned int curr_off = 0; 12727b415bf6SAditya Kali struct inode *inode = page->mapping->host; 12737b415bf6SAditya Kali struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 12747b415bf6SAditya Kali int num_clusters; 1275d2a17637SMingming Cao 1276d2a17637SMingming Cao head = page_buffers(page); 1277d2a17637SMingming Cao bh = head; 1278d2a17637SMingming Cao do { 1279d2a17637SMingming Cao unsigned int next_off = curr_off + bh->b_size; 1280d2a17637SMingming Cao 1281d2a17637SMingming Cao if ((offset <= curr_off) && (buffer_delay(bh))) { 1282d2a17637SMingming Cao to_release++; 1283d2a17637SMingming Cao clear_buffer_delay(bh); 12845356f261SAditya Kali clear_buffer_da_mapped(bh); 1285d2a17637SMingming Cao } 1286d2a17637SMingming Cao curr_off = next_off; 1287d2a17637SMingming Cao } while ((bh = bh->b_this_page) != head); 12887b415bf6SAditya Kali 12897b415bf6SAditya Kali /* If we have released all the blocks belonging to a cluster, then we 12907b415bf6SAditya Kali * need to release the reserved space for that cluster. */ 12917b415bf6SAditya Kali num_clusters = EXT4_NUM_B2C(sbi, to_release); 12927b415bf6SAditya Kali while (num_clusters > 0) { 12937b415bf6SAditya Kali ext4_fsblk_t lblk; 12947b415bf6SAditya Kali lblk = (page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits)) + 12957b415bf6SAditya Kali ((num_clusters - 1) << sbi->s_cluster_bits); 12967b415bf6SAditya Kali if (sbi->s_cluster_ratio == 1 || 12977b415bf6SAditya Kali !ext4_find_delalloc_cluster(inode, lblk, 1)) 12987b415bf6SAditya Kali ext4_da_release_space(inode, 1); 12997b415bf6SAditya Kali 13007b415bf6SAditya Kali num_clusters--; 13017b415bf6SAditya Kali } 1302d2a17637SMingming Cao } 1303ac27a0ecSDave Kleikamp 1304ac27a0ecSDave Kleikamp /* 130564769240SAlex Tomas * Delayed allocation stuff 130664769240SAlex Tomas */ 130764769240SAlex Tomas 130864769240SAlex Tomas /* 130964769240SAlex Tomas * mpage_da_submit_io - walks through extent of pages and try to write 1310a1d6cc56SAneesh Kumar K.V * them with writepage() call back 131164769240SAlex Tomas * 131264769240SAlex Tomas * @mpd->inode: inode 131364769240SAlex Tomas * @mpd->first_page: first page of the extent 131464769240SAlex Tomas * @mpd->next_page: page after the last page of the extent 131564769240SAlex Tomas * 131664769240SAlex Tomas * By the time mpage_da_submit_io() is called we expect all blocks 131764769240SAlex Tomas * to be allocated. this may be wrong if allocation failed. 131864769240SAlex Tomas * 131964769240SAlex Tomas * As pages are already locked by write_cache_pages(), we can't use it 132064769240SAlex Tomas */ 13211de3e3dfSTheodore Ts'o static int mpage_da_submit_io(struct mpage_da_data *mpd, 13221de3e3dfSTheodore Ts'o struct ext4_map_blocks *map) 132364769240SAlex Tomas { 1324791b7f08SAneesh Kumar K.V struct pagevec pvec; 1325791b7f08SAneesh Kumar K.V unsigned long index, end; 1326791b7f08SAneesh Kumar K.V int ret = 0, err, nr_pages, i; 1327791b7f08SAneesh Kumar K.V struct inode *inode = mpd->inode; 1328791b7f08SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 1329cb20d518STheodore Ts'o loff_t size = i_size_read(inode); 13303ecdb3a1STheodore Ts'o unsigned int len, block_start; 13313ecdb3a1STheodore Ts'o struct buffer_head *bh, *page_bufs = NULL; 1332cb20d518STheodore Ts'o int journal_data = ext4_should_journal_data(inode); 13331de3e3dfSTheodore Ts'o sector_t pblock = 0, cur_logical = 0; 1334bd2d0210STheodore Ts'o struct ext4_io_submit io_submit; 133564769240SAlex Tomas 133664769240SAlex Tomas BUG_ON(mpd->next_page <= mpd->first_page); 1337bd2d0210STheodore Ts'o memset(&io_submit, 0, sizeof(io_submit)); 1338791b7f08SAneesh Kumar K.V /* 1339791b7f08SAneesh Kumar K.V * We need to start from the first_page to the next_page - 1 1340791b7f08SAneesh Kumar K.V * to make sure we also write the mapped dirty buffer_heads. 13418dc207c0STheodore Ts'o * If we look at mpd->b_blocknr we would only be looking 1342791b7f08SAneesh Kumar K.V * at the currently mapped buffer_heads. 1343791b7f08SAneesh Kumar K.V */ 134464769240SAlex Tomas index = mpd->first_page; 134564769240SAlex Tomas end = mpd->next_page - 1; 134664769240SAlex Tomas 1347791b7f08SAneesh Kumar K.V pagevec_init(&pvec, 0); 134864769240SAlex Tomas while (index <= end) { 1349791b7f08SAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 135064769240SAlex Tomas if (nr_pages == 0) 135164769240SAlex Tomas break; 135264769240SAlex Tomas for (i = 0; i < nr_pages; i++) { 135397498956STheodore Ts'o int commit_write = 0, skip_page = 0; 135464769240SAlex Tomas struct page *page = pvec.pages[i]; 135564769240SAlex Tomas 1356791b7f08SAneesh Kumar K.V index = page->index; 1357791b7f08SAneesh Kumar K.V if (index > end) 1358791b7f08SAneesh Kumar K.V break; 1359cb20d518STheodore Ts'o 1360cb20d518STheodore Ts'o if (index == size >> PAGE_CACHE_SHIFT) 1361cb20d518STheodore Ts'o len = size & ~PAGE_CACHE_MASK; 1362cb20d518STheodore Ts'o else 1363cb20d518STheodore Ts'o len = PAGE_CACHE_SIZE; 13641de3e3dfSTheodore Ts'o if (map) { 13651de3e3dfSTheodore Ts'o cur_logical = index << (PAGE_CACHE_SHIFT - 13661de3e3dfSTheodore Ts'o inode->i_blkbits); 13671de3e3dfSTheodore Ts'o pblock = map->m_pblk + (cur_logical - 13681de3e3dfSTheodore Ts'o map->m_lblk); 13691de3e3dfSTheodore Ts'o } 1370791b7f08SAneesh Kumar K.V index++; 1371791b7f08SAneesh Kumar K.V 1372791b7f08SAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1373791b7f08SAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1374791b7f08SAneesh Kumar K.V 137522208dedSAneesh Kumar K.V /* 1376cb20d518STheodore Ts'o * If the page does not have buffers (for 1377cb20d518STheodore Ts'o * whatever reason), try to create them using 1378a107e5a3STheodore Ts'o * __block_write_begin. If this fails, 137997498956STheodore Ts'o * skip the page and move on. 138022208dedSAneesh Kumar K.V */ 1381cb20d518STheodore Ts'o if (!page_has_buffers(page)) { 1382a107e5a3STheodore Ts'o if (__block_write_begin(page, 0, len, 1383cb20d518STheodore Ts'o noalloc_get_block_write)) { 138497498956STheodore Ts'o skip_page: 1385cb20d518STheodore Ts'o unlock_page(page); 1386cb20d518STheodore Ts'o continue; 1387cb20d518STheodore Ts'o } 1388cb20d518STheodore Ts'o commit_write = 1; 1389cb20d518STheodore Ts'o } 13903ecdb3a1STheodore Ts'o 13913ecdb3a1STheodore Ts'o bh = page_bufs = page_buffers(page); 13923ecdb3a1STheodore Ts'o block_start = 0; 13933ecdb3a1STheodore Ts'o do { 13941de3e3dfSTheodore Ts'o if (!bh) 139597498956STheodore Ts'o goto skip_page; 13961de3e3dfSTheodore Ts'o if (map && (cur_logical >= map->m_lblk) && 13971de3e3dfSTheodore Ts'o (cur_logical <= (map->m_lblk + 13981de3e3dfSTheodore Ts'o (map->m_len - 1)))) { 13991de3e3dfSTheodore Ts'o if (buffer_delay(bh)) { 14001de3e3dfSTheodore Ts'o clear_buffer_delay(bh); 14011de3e3dfSTheodore Ts'o bh->b_blocknr = pblock; 14021de3e3dfSTheodore Ts'o } 14035356f261SAditya Kali if (buffer_da_mapped(bh)) 14045356f261SAditya Kali clear_buffer_da_mapped(bh); 14051de3e3dfSTheodore Ts'o if (buffer_unwritten(bh) || 14061de3e3dfSTheodore Ts'o buffer_mapped(bh)) 14071de3e3dfSTheodore Ts'o BUG_ON(bh->b_blocknr != pblock); 14081de3e3dfSTheodore Ts'o if (map->m_flags & EXT4_MAP_UNINIT) 14091de3e3dfSTheodore Ts'o set_buffer_uninit(bh); 14101de3e3dfSTheodore Ts'o clear_buffer_unwritten(bh); 14111de3e3dfSTheodore Ts'o } 14121de3e3dfSTheodore Ts'o 141313a79a47SYongqiang Yang /* 141413a79a47SYongqiang Yang * skip page if block allocation undone and 141513a79a47SYongqiang Yang * block is dirty 141613a79a47SYongqiang Yang */ 141713a79a47SYongqiang Yang if (ext4_bh_delay_or_unwritten(NULL, bh)) 141897498956STheodore Ts'o skip_page = 1; 14193ecdb3a1STheodore Ts'o bh = bh->b_this_page; 14203ecdb3a1STheodore Ts'o block_start += bh->b_size; 14211de3e3dfSTheodore Ts'o cur_logical++; 14221de3e3dfSTheodore Ts'o pblock++; 14231de3e3dfSTheodore Ts'o } while (bh != page_bufs); 14241de3e3dfSTheodore Ts'o 142597498956STheodore Ts'o if (skip_page) 142697498956STheodore Ts'o goto skip_page; 1427cb20d518STheodore Ts'o 1428cb20d518STheodore Ts'o if (commit_write) 1429cb20d518STheodore Ts'o /* mark the buffer_heads as dirty & uptodate */ 1430cb20d518STheodore Ts'o block_commit_write(page, 0, len); 1431cb20d518STheodore Ts'o 143297498956STheodore Ts'o clear_page_dirty_for_io(page); 1433bd2d0210STheodore Ts'o /* 1434bd2d0210STheodore Ts'o * Delalloc doesn't support data journalling, 1435bd2d0210STheodore Ts'o * but eventually maybe we'll lift this 1436bd2d0210STheodore Ts'o * restriction. 1437bd2d0210STheodore Ts'o */ 1438bd2d0210STheodore Ts'o if (unlikely(journal_data && PageChecked(page))) 1439cb20d518STheodore Ts'o err = __ext4_journalled_writepage(page, len); 14401449032bSTheodore Ts'o else if (test_opt(inode->i_sb, MBLK_IO_SUBMIT)) 1441bd2d0210STheodore Ts'o err = ext4_bio_write_page(&io_submit, page, 1442bd2d0210STheodore Ts'o len, mpd->wbc); 14439dd75f1fSTheodore Ts'o else if (buffer_uninit(page_bufs)) { 14449dd75f1fSTheodore Ts'o ext4_set_bh_endio(page_bufs, inode); 14459dd75f1fSTheodore Ts'o err = block_write_full_page_endio(page, 14469dd75f1fSTheodore Ts'o noalloc_get_block_write, 14479dd75f1fSTheodore Ts'o mpd->wbc, ext4_end_io_buffer_write); 14489dd75f1fSTheodore Ts'o } else 14491449032bSTheodore Ts'o err = block_write_full_page(page, 14501449032bSTheodore Ts'o noalloc_get_block_write, mpd->wbc); 1451cb20d518STheodore Ts'o 1452cb20d518STheodore Ts'o if (!err) 1453a1d6cc56SAneesh Kumar K.V mpd->pages_written++; 145464769240SAlex Tomas /* 145564769240SAlex Tomas * In error case, we have to continue because 145664769240SAlex Tomas * remaining pages are still locked 145764769240SAlex Tomas */ 145864769240SAlex Tomas if (ret == 0) 145964769240SAlex Tomas ret = err; 146064769240SAlex Tomas } 146164769240SAlex Tomas pagevec_release(&pvec); 146264769240SAlex Tomas } 1463bd2d0210STheodore Ts'o ext4_io_submit(&io_submit); 146464769240SAlex Tomas return ret; 146564769240SAlex Tomas } 146664769240SAlex Tomas 1467c7f5938aSCurt Wohlgemuth static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd) 1468c4a0c46eSAneesh Kumar K.V { 1469c4a0c46eSAneesh Kumar K.V int nr_pages, i; 1470c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 1471c4a0c46eSAneesh Kumar K.V struct pagevec pvec; 1472c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 1473c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 1474c4a0c46eSAneesh Kumar K.V 1475c7f5938aSCurt Wohlgemuth index = mpd->first_page; 1476c7f5938aSCurt Wohlgemuth end = mpd->next_page - 1; 1477c4a0c46eSAneesh Kumar K.V while (index <= end) { 1478c4a0c46eSAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 1479c4a0c46eSAneesh Kumar K.V if (nr_pages == 0) 1480c4a0c46eSAneesh Kumar K.V break; 1481c4a0c46eSAneesh Kumar K.V for (i = 0; i < nr_pages; i++) { 1482c4a0c46eSAneesh Kumar K.V struct page *page = pvec.pages[i]; 14839b1d0998SJan Kara if (page->index > end) 1484c4a0c46eSAneesh Kumar K.V break; 1485c4a0c46eSAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1486c4a0c46eSAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1487c4a0c46eSAneesh Kumar K.V block_invalidatepage(page, 0); 1488c4a0c46eSAneesh Kumar K.V ClearPageUptodate(page); 1489c4a0c46eSAneesh Kumar K.V unlock_page(page); 1490c4a0c46eSAneesh Kumar K.V } 14919b1d0998SJan Kara index = pvec.pages[nr_pages - 1]->index + 1; 14929b1d0998SJan Kara pagevec_release(&pvec); 1493c4a0c46eSAneesh Kumar K.V } 1494c4a0c46eSAneesh Kumar K.V return; 1495c4a0c46eSAneesh Kumar K.V } 1496c4a0c46eSAneesh Kumar K.V 1497df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 1498df22291fSAneesh Kumar K.V { 1499df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 150092b97816STheodore Ts'o struct super_block *sb = inode->i_sb; 150192b97816STheodore Ts'o 150292b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld", 15035dee5437STheodore Ts'o EXT4_C2B(EXT4_SB(inode->i_sb), 15045dee5437STheodore Ts'o ext4_count_free_clusters(inode->i_sb))); 150592b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Free/Dirty block details"); 150692b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "free_blocks=%lld", 150757042651STheodore Ts'o (long long) EXT4_C2B(EXT4_SB(inode->i_sb), 150857042651STheodore Ts'o percpu_counter_sum(&sbi->s_freeclusters_counter))); 150992b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld", 15107b415bf6SAditya Kali (long long) EXT4_C2B(EXT4_SB(inode->i_sb), 15117b415bf6SAditya Kali percpu_counter_sum(&sbi->s_dirtyclusters_counter))); 151292b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Block reservation details"); 151392b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u", 1514df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_data_blocks); 151592b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_meta_blocks=%u", 1516df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_meta_blocks); 1517df22291fSAneesh Kumar K.V return; 1518df22291fSAneesh Kumar K.V } 1519df22291fSAneesh Kumar K.V 1520b920c755STheodore Ts'o /* 15215a87b7a5STheodore Ts'o * mpage_da_map_and_submit - go through given space, map them 15225a87b7a5STheodore Ts'o * if necessary, and then submit them for I/O 152364769240SAlex Tomas * 15248dc207c0STheodore Ts'o * @mpd - bh describing space 152564769240SAlex Tomas * 152664769240SAlex Tomas * The function skips space we know is already mapped to disk blocks. 152764769240SAlex Tomas * 152864769240SAlex Tomas */ 15295a87b7a5STheodore Ts'o static void mpage_da_map_and_submit(struct mpage_da_data *mpd) 153064769240SAlex Tomas { 15312ac3b6e0STheodore Ts'o int err, blks, get_blocks_flags; 15321de3e3dfSTheodore Ts'o struct ext4_map_blocks map, *mapp = NULL; 15332fa3cdfbSTheodore Ts'o sector_t next = mpd->b_blocknr; 15342fa3cdfbSTheodore Ts'o unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits; 15352fa3cdfbSTheodore Ts'o loff_t disksize = EXT4_I(mpd->inode)->i_disksize; 15362fa3cdfbSTheodore Ts'o handle_t *handle = NULL; 153764769240SAlex Tomas 153864769240SAlex Tomas /* 15395a87b7a5STheodore Ts'o * If the blocks are mapped already, or we couldn't accumulate 15405a87b7a5STheodore Ts'o * any blocks, then proceed immediately to the submission stage. 154164769240SAlex Tomas */ 15425a87b7a5STheodore Ts'o if ((mpd->b_size == 0) || 15435a87b7a5STheodore Ts'o ((mpd->b_state & (1 << BH_Mapped)) && 154429fa89d0SAneesh Kumar K.V !(mpd->b_state & (1 << BH_Delay)) && 15455a87b7a5STheodore Ts'o !(mpd->b_state & (1 << BH_Unwritten)))) 15465a87b7a5STheodore Ts'o goto submit_io; 15472fa3cdfbSTheodore Ts'o 15482fa3cdfbSTheodore Ts'o handle = ext4_journal_current_handle(); 15492fa3cdfbSTheodore Ts'o BUG_ON(!handle); 15502fa3cdfbSTheodore Ts'o 155179ffab34SAneesh Kumar K.V /* 155279e83036SEric Sandeen * Call ext4_map_blocks() to allocate any delayed allocation 15532ac3b6e0STheodore Ts'o * blocks, or to convert an uninitialized extent to be 15542ac3b6e0STheodore Ts'o * initialized (in the case where we have written into 15552ac3b6e0STheodore Ts'o * one or more preallocated blocks). 15562ac3b6e0STheodore Ts'o * 15572ac3b6e0STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to 15582ac3b6e0STheodore Ts'o * indicate that we are on the delayed allocation path. This 15592ac3b6e0STheodore Ts'o * affects functions in many different parts of the allocation 15602ac3b6e0STheodore Ts'o * call path. This flag exists primarily because we don't 156179e83036SEric Sandeen * want to change *many* call functions, so ext4_map_blocks() 1562f2321097STheodore Ts'o * will set the EXT4_STATE_DELALLOC_RESERVED flag once the 15632ac3b6e0STheodore Ts'o * inode's allocation semaphore is taken. 15642ac3b6e0STheodore Ts'o * 15652ac3b6e0STheodore Ts'o * If the blocks in questions were delalloc blocks, set 15662ac3b6e0STheodore Ts'o * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting 15672ac3b6e0STheodore Ts'o * variables are updated after the blocks have been allocated. 156879ffab34SAneesh Kumar K.V */ 15692ed88685STheodore Ts'o map.m_lblk = next; 15702ed88685STheodore Ts'o map.m_len = max_blocks; 15711296cc85SAneesh Kumar K.V get_blocks_flags = EXT4_GET_BLOCKS_CREATE; 1572744692dcSJiaying Zhang if (ext4_should_dioread_nolock(mpd->inode)) 1573744692dcSJiaying Zhang get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; 15742ac3b6e0STheodore Ts'o if (mpd->b_state & (1 << BH_Delay)) 15751296cc85SAneesh Kumar K.V get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; 15761296cc85SAneesh Kumar K.V 15772ed88685STheodore Ts'o blks = ext4_map_blocks(handle, mpd->inode, &map, get_blocks_flags); 15782fa3cdfbSTheodore Ts'o if (blks < 0) { 1579e3570639SEric Sandeen struct super_block *sb = mpd->inode->i_sb; 1580e3570639SEric Sandeen 15812fa3cdfbSTheodore Ts'o err = blks; 1582ed5bde0bSTheodore Ts'o /* 15835a87b7a5STheodore Ts'o * If get block returns EAGAIN or ENOSPC and there 158497498956STheodore Ts'o * appears to be free blocks we will just let 158597498956STheodore Ts'o * mpage_da_submit_io() unlock all of the pages. 1586c4a0c46eSAneesh Kumar K.V */ 1587c4a0c46eSAneesh Kumar K.V if (err == -EAGAIN) 15885a87b7a5STheodore Ts'o goto submit_io; 1589df22291fSAneesh Kumar K.V 15905dee5437STheodore Ts'o if (err == -ENOSPC && ext4_count_free_clusters(sb)) { 1591df22291fSAneesh Kumar K.V mpd->retval = err; 15925a87b7a5STheodore Ts'o goto submit_io; 1593df22291fSAneesh Kumar K.V } 1594df22291fSAneesh Kumar K.V 1595c4a0c46eSAneesh Kumar K.V /* 1596ed5bde0bSTheodore Ts'o * get block failure will cause us to loop in 1597ed5bde0bSTheodore Ts'o * writepages, because a_ops->writepage won't be able 1598ed5bde0bSTheodore Ts'o * to make progress. The page will be redirtied by 1599ed5bde0bSTheodore Ts'o * writepage and writepages will again try to write 1600ed5bde0bSTheodore Ts'o * the same. 1601c4a0c46eSAneesh Kumar K.V */ 1602e3570639SEric Sandeen if (!(EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) { 1603e3570639SEric Sandeen ext4_msg(sb, KERN_CRIT, 1604e3570639SEric Sandeen "delayed block allocation failed for inode %lu " 1605e3570639SEric Sandeen "at logical offset %llu with max blocks %zd " 1606e3570639SEric Sandeen "with error %d", mpd->inode->i_ino, 1607c4a0c46eSAneesh Kumar K.V (unsigned long long) next, 16088dc207c0STheodore Ts'o mpd->b_size >> mpd->inode->i_blkbits, err); 1609e3570639SEric Sandeen ext4_msg(sb, KERN_CRIT, 1610e3570639SEric Sandeen "This should not happen!! Data will be lost\n"); 1611e3570639SEric Sandeen if (err == -ENOSPC) 1612df22291fSAneesh Kumar K.V ext4_print_free_blocks(mpd->inode); 1613030ba6bcSAneesh Kumar K.V } 16142fa3cdfbSTheodore Ts'o /* invalidate all the pages */ 1615c7f5938aSCurt Wohlgemuth ext4_da_block_invalidatepages(mpd); 1616e0fd9b90SCurt Wohlgemuth 1617e0fd9b90SCurt Wohlgemuth /* Mark this page range as having been completed */ 1618e0fd9b90SCurt Wohlgemuth mpd->io_done = 1; 16195a87b7a5STheodore Ts'o return; 1620c4a0c46eSAneesh Kumar K.V } 16212fa3cdfbSTheodore Ts'o BUG_ON(blks == 0); 16222fa3cdfbSTheodore Ts'o 16231de3e3dfSTheodore Ts'o mapp = ↦ 16242ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 16252ed88685STheodore Ts'o struct block_device *bdev = mpd->inode->i_sb->s_bdev; 16262ed88685STheodore Ts'o int i; 162764769240SAlex Tomas 16282ed88685STheodore Ts'o for (i = 0; i < map.m_len; i++) 16292ed88685STheodore Ts'o unmap_underlying_metadata(bdev, map.m_pblk + i); 163064769240SAlex Tomas 16312fa3cdfbSTheodore Ts'o if (ext4_should_order_data(mpd->inode)) { 16322fa3cdfbSTheodore Ts'o err = ext4_jbd2_file_inode(handle, mpd->inode); 16338de49e67SKazuya Mio if (err) { 1634decbd919STheodore Ts'o /* Only if the journal is aborted */ 16358de49e67SKazuya Mio mpd->retval = err; 16368de49e67SKazuya Mio goto submit_io; 16378de49e67SKazuya Mio } 16382fa3cdfbSTheodore Ts'o } 16392fa3cdfbSTheodore Ts'o } 16402fa3cdfbSTheodore Ts'o 16412fa3cdfbSTheodore Ts'o /* 164203f5d8bcSJan Kara * Update on-disk size along with block allocation. 16432fa3cdfbSTheodore Ts'o */ 16442fa3cdfbSTheodore Ts'o disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits; 16452fa3cdfbSTheodore Ts'o if (disksize > i_size_read(mpd->inode)) 16462fa3cdfbSTheodore Ts'o disksize = i_size_read(mpd->inode); 16472fa3cdfbSTheodore Ts'o if (disksize > EXT4_I(mpd->inode)->i_disksize) { 16482fa3cdfbSTheodore Ts'o ext4_update_i_disksize(mpd->inode, disksize); 16495a87b7a5STheodore Ts'o err = ext4_mark_inode_dirty(handle, mpd->inode); 16505a87b7a5STheodore Ts'o if (err) 16515a87b7a5STheodore Ts'o ext4_error(mpd->inode->i_sb, 16525a87b7a5STheodore Ts'o "Failed to mark inode %lu dirty", 16535a87b7a5STheodore Ts'o mpd->inode->i_ino); 16542fa3cdfbSTheodore Ts'o } 16552fa3cdfbSTheodore Ts'o 16565a87b7a5STheodore Ts'o submit_io: 16571de3e3dfSTheodore Ts'o mpage_da_submit_io(mpd, mapp); 16585a87b7a5STheodore Ts'o mpd->io_done = 1; 165964769240SAlex Tomas } 166064769240SAlex Tomas 1661bf068ee2SAneesh Kumar K.V #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \ 1662bf068ee2SAneesh Kumar K.V (1 << BH_Delay) | (1 << BH_Unwritten)) 166364769240SAlex Tomas 166464769240SAlex Tomas /* 166564769240SAlex Tomas * mpage_add_bh_to_extent - try to add one more block to extent of blocks 166664769240SAlex Tomas * 166764769240SAlex Tomas * @mpd->lbh - extent of blocks 166864769240SAlex Tomas * @logical - logical number of the block in the file 166964769240SAlex Tomas * @bh - bh of the block (used to access block's state) 167064769240SAlex Tomas * 167164769240SAlex Tomas * the function is used to collect contig. blocks in same state 167264769240SAlex Tomas */ 167364769240SAlex Tomas static void mpage_add_bh_to_extent(struct mpage_da_data *mpd, 16748dc207c0STheodore Ts'o sector_t logical, size_t b_size, 16758dc207c0STheodore Ts'o unsigned long b_state) 167664769240SAlex Tomas { 167764769240SAlex Tomas sector_t next; 16788dc207c0STheodore Ts'o int nrblocks = mpd->b_size >> mpd->inode->i_blkbits; 167964769240SAlex Tomas 1680c445e3e0SEric Sandeen /* 1681c445e3e0SEric Sandeen * XXX Don't go larger than mballoc is willing to allocate 1682c445e3e0SEric Sandeen * This is a stopgap solution. We eventually need to fold 1683c445e3e0SEric Sandeen * mpage_da_submit_io() into this function and then call 168479e83036SEric Sandeen * ext4_map_blocks() multiple times in a loop 1685c445e3e0SEric Sandeen */ 1686c445e3e0SEric Sandeen if (nrblocks >= 8*1024*1024/mpd->inode->i_sb->s_blocksize) 1687c445e3e0SEric Sandeen goto flush_it; 1688c445e3e0SEric Sandeen 1689525f4ed8SMingming Cao /* check if thereserved journal credits might overflow */ 169012e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(mpd->inode, EXT4_INODE_EXTENTS))) { 1691525f4ed8SMingming Cao if (nrblocks >= EXT4_MAX_TRANS_DATA) { 1692525f4ed8SMingming Cao /* 1693525f4ed8SMingming Cao * With non-extent format we are limited by the journal 1694525f4ed8SMingming Cao * credit available. Total credit needed to insert 1695525f4ed8SMingming Cao * nrblocks contiguous blocks is dependent on the 1696525f4ed8SMingming Cao * nrblocks. So limit nrblocks. 1697525f4ed8SMingming Cao */ 1698525f4ed8SMingming Cao goto flush_it; 1699525f4ed8SMingming Cao } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) > 1700525f4ed8SMingming Cao EXT4_MAX_TRANS_DATA) { 1701525f4ed8SMingming Cao /* 1702525f4ed8SMingming Cao * Adding the new buffer_head would make it cross the 1703525f4ed8SMingming Cao * allowed limit for which we have journal credit 1704525f4ed8SMingming Cao * reserved. So limit the new bh->b_size 1705525f4ed8SMingming Cao */ 1706525f4ed8SMingming Cao b_size = (EXT4_MAX_TRANS_DATA - nrblocks) << 1707525f4ed8SMingming Cao mpd->inode->i_blkbits; 1708525f4ed8SMingming Cao /* we will do mpage_da_submit_io in the next loop */ 1709525f4ed8SMingming Cao } 1710525f4ed8SMingming Cao } 171164769240SAlex Tomas /* 171264769240SAlex Tomas * First block in the extent 171364769240SAlex Tomas */ 17148dc207c0STheodore Ts'o if (mpd->b_size == 0) { 17158dc207c0STheodore Ts'o mpd->b_blocknr = logical; 17168dc207c0STheodore Ts'o mpd->b_size = b_size; 17178dc207c0STheodore Ts'o mpd->b_state = b_state & BH_FLAGS; 171864769240SAlex Tomas return; 171964769240SAlex Tomas } 172064769240SAlex Tomas 17218dc207c0STheodore Ts'o next = mpd->b_blocknr + nrblocks; 172264769240SAlex Tomas /* 172364769240SAlex Tomas * Can we merge the block to our big extent? 172464769240SAlex Tomas */ 17258dc207c0STheodore Ts'o if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) { 17268dc207c0STheodore Ts'o mpd->b_size += b_size; 172764769240SAlex Tomas return; 172864769240SAlex Tomas } 172964769240SAlex Tomas 1730525f4ed8SMingming Cao flush_it: 173164769240SAlex Tomas /* 173264769240SAlex Tomas * We couldn't merge the block to our extent, so we 173364769240SAlex Tomas * need to flush current extent and start new one 173464769240SAlex Tomas */ 17355a87b7a5STheodore Ts'o mpage_da_map_and_submit(mpd); 1736a1d6cc56SAneesh Kumar K.V return; 173764769240SAlex Tomas } 173864769240SAlex Tomas 1739c364b22cSAneesh Kumar K.V static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh) 174029fa89d0SAneesh Kumar K.V { 1741c364b22cSAneesh Kumar K.V return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 174229fa89d0SAneesh Kumar K.V } 174329fa89d0SAneesh Kumar K.V 174464769240SAlex Tomas /* 17455356f261SAditya Kali * This function is grabs code from the very beginning of 17465356f261SAditya Kali * ext4_map_blocks, but assumes that the caller is from delayed write 17475356f261SAditya Kali * time. This function looks up the requested blocks and sets the 17485356f261SAditya Kali * buffer delay bit under the protection of i_data_sem. 17495356f261SAditya Kali */ 17505356f261SAditya Kali static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, 17515356f261SAditya Kali struct ext4_map_blocks *map, 17525356f261SAditya Kali struct buffer_head *bh) 17535356f261SAditya Kali { 17545356f261SAditya Kali int retval; 17555356f261SAditya Kali sector_t invalid_block = ~((sector_t) 0xffff); 17565356f261SAditya Kali 17575356f261SAditya Kali if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 17585356f261SAditya Kali invalid_block = ~0; 17595356f261SAditya Kali 17605356f261SAditya Kali map->m_flags = 0; 17615356f261SAditya Kali ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u," 17625356f261SAditya Kali "logical block %lu\n", inode->i_ino, map->m_len, 17635356f261SAditya Kali (unsigned long) map->m_lblk); 17645356f261SAditya Kali /* 17655356f261SAditya Kali * Try to see if we can get the block without requesting a new 17665356f261SAditya Kali * file system block. 17675356f261SAditya Kali */ 17685356f261SAditya Kali down_read((&EXT4_I(inode)->i_data_sem)); 17695356f261SAditya Kali if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 17705356f261SAditya Kali retval = ext4_ext_map_blocks(NULL, inode, map, 0); 17715356f261SAditya Kali else 17725356f261SAditya Kali retval = ext4_ind_map_blocks(NULL, inode, map, 0); 17735356f261SAditya Kali 17745356f261SAditya Kali if (retval == 0) { 17755356f261SAditya Kali /* 17765356f261SAditya Kali * XXX: __block_prepare_write() unmaps passed block, 17775356f261SAditya Kali * is it OK? 17785356f261SAditya Kali */ 17795356f261SAditya Kali /* If the block was allocated from previously allocated cluster, 17805356f261SAditya Kali * then we dont need to reserve it again. */ 17815356f261SAditya Kali if (!(map->m_flags & EXT4_MAP_FROM_CLUSTER)) { 17825356f261SAditya Kali retval = ext4_da_reserve_space(inode, iblock); 17835356f261SAditya Kali if (retval) 17845356f261SAditya Kali /* not enough space to reserve */ 17855356f261SAditya Kali goto out_unlock; 17865356f261SAditya Kali } 17875356f261SAditya Kali 17885356f261SAditya Kali /* Clear EXT4_MAP_FROM_CLUSTER flag since its purpose is served 17895356f261SAditya Kali * and it should not appear on the bh->b_state. 17905356f261SAditya Kali */ 17915356f261SAditya Kali map->m_flags &= ~EXT4_MAP_FROM_CLUSTER; 17925356f261SAditya Kali 17935356f261SAditya Kali map_bh(bh, inode->i_sb, invalid_block); 17945356f261SAditya Kali set_buffer_new(bh); 17955356f261SAditya Kali set_buffer_delay(bh); 17965356f261SAditya Kali } 17975356f261SAditya Kali 17985356f261SAditya Kali out_unlock: 17995356f261SAditya Kali up_read((&EXT4_I(inode)->i_data_sem)); 18005356f261SAditya Kali 18015356f261SAditya Kali return retval; 18025356f261SAditya Kali } 18035356f261SAditya Kali 18045356f261SAditya Kali /* 1805b920c755STheodore Ts'o * This is a special get_blocks_t callback which is used by 1806b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 1807b920c755STheodore Ts'o * reserve space for a single block. 180829fa89d0SAneesh Kumar K.V * 180929fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 181029fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 181129fa89d0SAneesh Kumar K.V * 181229fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 181329fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 181429fa89d0SAneesh Kumar K.V * initialized properly. 181564769240SAlex Tomas */ 181664769240SAlex Tomas static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 18172ed88685STheodore Ts'o struct buffer_head *bh, int create) 181864769240SAlex Tomas { 18192ed88685STheodore Ts'o struct ext4_map_blocks map; 182064769240SAlex Tomas int ret = 0; 182164769240SAlex Tomas 182264769240SAlex Tomas BUG_ON(create == 0); 18232ed88685STheodore Ts'o BUG_ON(bh->b_size != inode->i_sb->s_blocksize); 18242ed88685STheodore Ts'o 18252ed88685STheodore Ts'o map.m_lblk = iblock; 18262ed88685STheodore Ts'o map.m_len = 1; 182764769240SAlex Tomas 182864769240SAlex Tomas /* 182964769240SAlex Tomas * first, we need to know whether the block is allocated already 183064769240SAlex Tomas * preallocated blocks are unmapped but should treated 183164769240SAlex Tomas * the same as allocated blocks. 183264769240SAlex Tomas */ 18335356f261SAditya Kali ret = ext4_da_map_blocks(inode, iblock, &map, bh); 18345356f261SAditya Kali if (ret <= 0) 18352ed88685STheodore Ts'o return ret; 183664769240SAlex Tomas 18372ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 18382ed88685STheodore Ts'o bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; 18392ed88685STheodore Ts'o 18402ed88685STheodore Ts'o if (buffer_unwritten(bh)) { 18412ed88685STheodore Ts'o /* A delayed write to unwritten bh should be marked 18422ed88685STheodore Ts'o * new and mapped. Mapped ensures that we don't do 18432ed88685STheodore Ts'o * get_block multiple times when we write to the same 18442ed88685STheodore Ts'o * offset and new ensures that we do proper zero out 18452ed88685STheodore Ts'o * for partial write. 18462ed88685STheodore Ts'o */ 18472ed88685STheodore Ts'o set_buffer_new(bh); 1848c8205636STheodore Ts'o set_buffer_mapped(bh); 18492ed88685STheodore Ts'o } 18502ed88685STheodore Ts'o return 0; 185164769240SAlex Tomas } 185261628a3fSMingming Cao 1853b920c755STheodore Ts'o /* 1854b920c755STheodore Ts'o * This function is used as a standard get_block_t calback function 1855b920c755STheodore Ts'o * when there is no desire to allocate any blocks. It is used as a 1856ebdec241SChristoph Hellwig * callback function for block_write_begin() and block_write_full_page(). 1857206f7ab4SChristoph Hellwig * These functions should only try to map a single block at a time. 1858b920c755STheodore Ts'o * 1859b920c755STheodore Ts'o * Since this function doesn't do block allocations even if the caller 1860b920c755STheodore Ts'o * requests it by passing in create=1, it is critically important that 1861b920c755STheodore Ts'o * any caller checks to make sure that any buffer heads are returned 1862b920c755STheodore Ts'o * by this function are either all already mapped or marked for 1863206f7ab4SChristoph Hellwig * delayed allocation before calling block_write_full_page(). Otherwise, 1864206f7ab4SChristoph Hellwig * b_blocknr could be left unitialized, and the page write functions will 1865206f7ab4SChristoph Hellwig * be taken by surprise. 1866b920c755STheodore Ts'o */ 1867b920c755STheodore Ts'o static int noalloc_get_block_write(struct inode *inode, sector_t iblock, 1868f0e6c985SAneesh Kumar K.V struct buffer_head *bh_result, int create) 1869f0e6c985SAneesh Kumar K.V { 1870a2dc52b5STheodore Ts'o BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize); 18712ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh_result, 0); 187261628a3fSMingming Cao } 187361628a3fSMingming Cao 187462e086beSAneesh Kumar K.V static int bget_one(handle_t *handle, struct buffer_head *bh) 187562e086beSAneesh Kumar K.V { 187662e086beSAneesh Kumar K.V get_bh(bh); 187762e086beSAneesh Kumar K.V return 0; 187862e086beSAneesh Kumar K.V } 187962e086beSAneesh Kumar K.V 188062e086beSAneesh Kumar K.V static int bput_one(handle_t *handle, struct buffer_head *bh) 188162e086beSAneesh Kumar K.V { 188262e086beSAneesh Kumar K.V put_bh(bh); 188362e086beSAneesh Kumar K.V return 0; 188462e086beSAneesh Kumar K.V } 188562e086beSAneesh Kumar K.V 188662e086beSAneesh Kumar K.V static int __ext4_journalled_writepage(struct page *page, 188762e086beSAneesh Kumar K.V unsigned int len) 188862e086beSAneesh Kumar K.V { 188962e086beSAneesh Kumar K.V struct address_space *mapping = page->mapping; 189062e086beSAneesh Kumar K.V struct inode *inode = mapping->host; 189162e086beSAneesh Kumar K.V struct buffer_head *page_bufs; 189262e086beSAneesh Kumar K.V handle_t *handle = NULL; 189362e086beSAneesh Kumar K.V int ret = 0; 189462e086beSAneesh Kumar K.V int err; 189562e086beSAneesh Kumar K.V 1896cb20d518STheodore Ts'o ClearPageChecked(page); 189762e086beSAneesh Kumar K.V page_bufs = page_buffers(page); 189862e086beSAneesh Kumar K.V BUG_ON(!page_bufs); 189962e086beSAneesh Kumar K.V walk_page_buffers(handle, page_bufs, 0, len, NULL, bget_one); 190062e086beSAneesh Kumar K.V /* As soon as we unlock the page, it can go away, but we have 190162e086beSAneesh Kumar K.V * references to buffers so we are safe */ 190262e086beSAneesh Kumar K.V unlock_page(page); 190362e086beSAneesh Kumar K.V 190462e086beSAneesh Kumar K.V handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode)); 190562e086beSAneesh Kumar K.V if (IS_ERR(handle)) { 190662e086beSAneesh Kumar K.V ret = PTR_ERR(handle); 190762e086beSAneesh Kumar K.V goto out; 190862e086beSAneesh Kumar K.V } 190962e086beSAneesh Kumar K.V 1910441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1911441c8508SCurt Wohlgemuth 191262e086beSAneesh Kumar K.V ret = walk_page_buffers(handle, page_bufs, 0, len, NULL, 191362e086beSAneesh Kumar K.V do_journal_get_write_access); 191462e086beSAneesh Kumar K.V 191562e086beSAneesh Kumar K.V err = walk_page_buffers(handle, page_bufs, 0, len, NULL, 191662e086beSAneesh Kumar K.V write_end_fn); 191762e086beSAneesh Kumar K.V if (ret == 0) 191862e086beSAneesh Kumar K.V ret = err; 19192d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 192062e086beSAneesh Kumar K.V err = ext4_journal_stop(handle); 192162e086beSAneesh Kumar K.V if (!ret) 192262e086beSAneesh Kumar K.V ret = err; 192362e086beSAneesh Kumar K.V 192462e086beSAneesh Kumar K.V walk_page_buffers(handle, page_bufs, 0, len, NULL, bput_one); 192519f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 192662e086beSAneesh Kumar K.V out: 192762e086beSAneesh Kumar K.V return ret; 192862e086beSAneesh Kumar K.V } 192962e086beSAneesh Kumar K.V 1930744692dcSJiaying Zhang static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode); 1931744692dcSJiaying Zhang static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate); 1932744692dcSJiaying Zhang 193361628a3fSMingming Cao /* 193443ce1d23SAneesh Kumar K.V * Note that we don't need to start a transaction unless we're journaling data 193543ce1d23SAneesh Kumar K.V * because we should have holes filled from ext4_page_mkwrite(). We even don't 193643ce1d23SAneesh Kumar K.V * need to file the inode to the transaction's list in ordered mode because if 193743ce1d23SAneesh Kumar K.V * we are writing back data added by write(), the inode is already there and if 193843ce1d23SAneesh Kumar K.V * we are writing back data modified via mmap(), no one guarantees in which 193943ce1d23SAneesh Kumar K.V * transaction the data will hit the disk. In case we are journaling data, we 194043ce1d23SAneesh Kumar K.V * cannot start transaction directly because transaction start ranks above page 194143ce1d23SAneesh Kumar K.V * lock so we have to do some magic. 194243ce1d23SAneesh Kumar K.V * 1943b920c755STheodore Ts'o * This function can get called via... 1944b920c755STheodore Ts'o * - ext4_da_writepages after taking page lock (have journal handle) 1945b920c755STheodore Ts'o * - journal_submit_inode_data_buffers (no journal handle) 1946b920c755STheodore Ts'o * - shrink_page_list via pdflush (no journal handle) 1947b920c755STheodore Ts'o * - grab_page_cache when doing write_begin (have journal handle) 194843ce1d23SAneesh Kumar K.V * 194943ce1d23SAneesh Kumar K.V * We don't do any block allocation in this function. If we have page with 195043ce1d23SAneesh Kumar K.V * multiple blocks we need to write those buffer_heads that are mapped. This 195143ce1d23SAneesh Kumar K.V * is important for mmaped based write. So if we do with blocksize 1K 195243ce1d23SAneesh Kumar K.V * truncate(f, 1024); 195343ce1d23SAneesh Kumar K.V * a = mmap(f, 0, 4096); 195443ce1d23SAneesh Kumar K.V * a[0] = 'a'; 195543ce1d23SAneesh Kumar K.V * truncate(f, 4096); 195643ce1d23SAneesh Kumar K.V * we have in the page first buffer_head mapped via page_mkwrite call back 195790802ed9SPaul Bolle * but other buffer_heads would be unmapped but dirty (dirty done via the 195843ce1d23SAneesh Kumar K.V * do_wp_page). So writepage should write the first block. If we modify 195943ce1d23SAneesh Kumar K.V * the mmap area beyond 1024 we will again get a page_fault and the 196043ce1d23SAneesh Kumar K.V * page_mkwrite callback will do the block allocation and mark the 196143ce1d23SAneesh Kumar K.V * buffer_heads mapped. 196243ce1d23SAneesh Kumar K.V * 196343ce1d23SAneesh Kumar K.V * We redirty the page if we have any buffer_heads that is either delay or 196443ce1d23SAneesh Kumar K.V * unwritten in the page. 196543ce1d23SAneesh Kumar K.V * 196643ce1d23SAneesh Kumar K.V * We can get recursively called as show below. 196743ce1d23SAneesh Kumar K.V * 196843ce1d23SAneesh Kumar K.V * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> 196943ce1d23SAneesh Kumar K.V * ext4_writepage() 197043ce1d23SAneesh Kumar K.V * 197143ce1d23SAneesh Kumar K.V * But since we don't do any block allocation we should not deadlock. 197243ce1d23SAneesh Kumar K.V * Page also have the dirty flag cleared so we don't get recurive page_lock. 197361628a3fSMingming Cao */ 197443ce1d23SAneesh Kumar K.V static int ext4_writepage(struct page *page, 197564769240SAlex Tomas struct writeback_control *wbc) 197664769240SAlex Tomas { 1977a42afc5fSTheodore Ts'o int ret = 0, commit_write = 0; 197861628a3fSMingming Cao loff_t size; 1979498e5f24STheodore Ts'o unsigned int len; 1980744692dcSJiaying Zhang struct buffer_head *page_bufs = NULL; 198161628a3fSMingming Cao struct inode *inode = page->mapping->host; 198264769240SAlex Tomas 1983a9c667f8SLukas Czerner trace_ext4_writepage(page); 198461628a3fSMingming Cao size = i_size_read(inode); 198561628a3fSMingming Cao if (page->index == size >> PAGE_CACHE_SHIFT) 198661628a3fSMingming Cao len = size & ~PAGE_CACHE_MASK; 198761628a3fSMingming Cao else 198861628a3fSMingming Cao len = PAGE_CACHE_SIZE; 198961628a3fSMingming Cao 1990a42afc5fSTheodore Ts'o /* 1991a42afc5fSTheodore Ts'o * If the page does not have buffers (for whatever reason), 1992a107e5a3STheodore Ts'o * try to create them using __block_write_begin. If this 1993a42afc5fSTheodore Ts'o * fails, redirty the page and move on. 1994a42afc5fSTheodore Ts'o */ 1995b1142e8fSTheodore Ts'o if (!page_has_buffers(page)) { 1996a107e5a3STheodore Ts'o if (__block_write_begin(page, 0, len, 1997a42afc5fSTheodore Ts'o noalloc_get_block_write)) { 1998a42afc5fSTheodore Ts'o redirty_page: 1999a42afc5fSTheodore Ts'o redirty_page_for_writepage(wbc, page); 2000a42afc5fSTheodore Ts'o unlock_page(page); 2001a42afc5fSTheodore Ts'o return 0; 2002a42afc5fSTheodore Ts'o } 2003a42afc5fSTheodore Ts'o commit_write = 1; 2004a42afc5fSTheodore Ts'o } 2005f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 2006f0e6c985SAneesh Kumar K.V if (walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2007c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 200861628a3fSMingming Cao /* 2009b1142e8fSTheodore Ts'o * We don't want to do block allocation, so redirty 2010b1142e8fSTheodore Ts'o * the page and return. We may reach here when we do 2011b1142e8fSTheodore Ts'o * a journal commit via journal_submit_inode_data_buffers. 2012966dbde2SMel Gorman * We can also reach here via shrink_page_list but it 2013966dbde2SMel Gorman * should never be for direct reclaim so warn if that 2014966dbde2SMel Gorman * happens 2015f0e6c985SAneesh Kumar K.V */ 2016966dbde2SMel Gorman WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) == 2017966dbde2SMel Gorman PF_MEMALLOC); 2018a42afc5fSTheodore Ts'o goto redirty_page; 2019f0e6c985SAneesh Kumar K.V } 2020a42afc5fSTheodore Ts'o if (commit_write) 2021ed9b3e33SAneesh Kumar K.V /* now mark the buffer_heads as dirty and uptodate */ 2022b767e78aSAneesh Kumar K.V block_commit_write(page, 0, len); 202364769240SAlex Tomas 2024cb20d518STheodore Ts'o if (PageChecked(page) && ext4_should_journal_data(inode)) 202543ce1d23SAneesh Kumar K.V /* 202643ce1d23SAneesh Kumar K.V * It's mmapped pagecache. Add buffers and journal it. There 202743ce1d23SAneesh Kumar K.V * doesn't seem much point in redirtying the page here. 202843ce1d23SAneesh Kumar K.V */ 20293f0ca309SWu Fengguang return __ext4_journalled_writepage(page, len); 203043ce1d23SAneesh Kumar K.V 2031a42afc5fSTheodore Ts'o if (buffer_uninit(page_bufs)) { 2032744692dcSJiaying Zhang ext4_set_bh_endio(page_bufs, inode); 2033744692dcSJiaying Zhang ret = block_write_full_page_endio(page, noalloc_get_block_write, 2034744692dcSJiaying Zhang wbc, ext4_end_io_buffer_write); 2035744692dcSJiaying Zhang } else 2036b920c755STheodore Ts'o ret = block_write_full_page(page, noalloc_get_block_write, 2037f0e6c985SAneesh Kumar K.V wbc); 203864769240SAlex Tomas 203964769240SAlex Tomas return ret; 204064769240SAlex Tomas } 204164769240SAlex Tomas 204261628a3fSMingming Cao /* 2043525f4ed8SMingming Cao * This is called via ext4_da_writepages() to 204425985edcSLucas De Marchi * calculate the total number of credits to reserve to fit 2045525f4ed8SMingming Cao * a single extent allocation into a single transaction, 2046525f4ed8SMingming Cao * ext4_da_writpeages() will loop calling this before 2047525f4ed8SMingming Cao * the block allocation. 204861628a3fSMingming Cao */ 2049525f4ed8SMingming Cao 2050525f4ed8SMingming Cao static int ext4_da_writepages_trans_blocks(struct inode *inode) 2051525f4ed8SMingming Cao { 2052525f4ed8SMingming Cao int max_blocks = EXT4_I(inode)->i_reserved_data_blocks; 2053525f4ed8SMingming Cao 2054525f4ed8SMingming Cao /* 2055525f4ed8SMingming Cao * With non-extent format the journal credit needed to 2056525f4ed8SMingming Cao * insert nrblocks contiguous block is dependent on 2057525f4ed8SMingming Cao * number of contiguous block. So we will limit 2058525f4ed8SMingming Cao * number of contiguous block to a sane value 2059525f4ed8SMingming Cao */ 206012e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) && 2061525f4ed8SMingming Cao (max_blocks > EXT4_MAX_TRANS_DATA)) 2062525f4ed8SMingming Cao max_blocks = EXT4_MAX_TRANS_DATA; 2063525f4ed8SMingming Cao 2064525f4ed8SMingming Cao return ext4_chunk_trans_blocks(inode, max_blocks); 2065525f4ed8SMingming Cao } 206661628a3fSMingming Cao 20678e48dcfbSTheodore Ts'o /* 20688e48dcfbSTheodore Ts'o * write_cache_pages_da - walk the list of dirty pages of the given 20698eb9e5ceSTheodore Ts'o * address space and accumulate pages that need writing, and call 2070168fc022STheodore Ts'o * mpage_da_map_and_submit to map a single contiguous memory region 2071168fc022STheodore Ts'o * and then write them. 20728e48dcfbSTheodore Ts'o */ 20738e48dcfbSTheodore Ts'o static int write_cache_pages_da(struct address_space *mapping, 20748e48dcfbSTheodore Ts'o struct writeback_control *wbc, 207572f84e65SEric Sandeen struct mpage_da_data *mpd, 207672f84e65SEric Sandeen pgoff_t *done_index) 20778e48dcfbSTheodore Ts'o { 20788eb9e5ceSTheodore Ts'o struct buffer_head *bh, *head; 2079168fc022STheodore Ts'o struct inode *inode = mapping->host; 20808e48dcfbSTheodore Ts'o struct pagevec pvec; 20814f01b02cSTheodore Ts'o unsigned int nr_pages; 20824f01b02cSTheodore Ts'o sector_t logical; 20834f01b02cSTheodore Ts'o pgoff_t index, end; 20848e48dcfbSTheodore Ts'o long nr_to_write = wbc->nr_to_write; 20854f01b02cSTheodore Ts'o int i, tag, ret = 0; 20868e48dcfbSTheodore Ts'o 2087168fc022STheodore Ts'o memset(mpd, 0, sizeof(struct mpage_da_data)); 2088168fc022STheodore Ts'o mpd->wbc = wbc; 2089168fc022STheodore Ts'o mpd->inode = inode; 20908e48dcfbSTheodore Ts'o pagevec_init(&pvec, 0); 20918e48dcfbSTheodore Ts'o index = wbc->range_start >> PAGE_CACHE_SHIFT; 20928e48dcfbSTheodore Ts'o end = wbc->range_end >> PAGE_CACHE_SHIFT; 20938e48dcfbSTheodore Ts'o 20946e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 20955b41d924SEric Sandeen tag = PAGECACHE_TAG_TOWRITE; 20965b41d924SEric Sandeen else 20975b41d924SEric Sandeen tag = PAGECACHE_TAG_DIRTY; 20985b41d924SEric Sandeen 209972f84e65SEric Sandeen *done_index = index; 21004f01b02cSTheodore Ts'o while (index <= end) { 21015b41d924SEric Sandeen nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, 21028e48dcfbSTheodore Ts'o min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); 21038e48dcfbSTheodore Ts'o if (nr_pages == 0) 21044f01b02cSTheodore Ts'o return 0; 21058e48dcfbSTheodore Ts'o 21068e48dcfbSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 21078e48dcfbSTheodore Ts'o struct page *page = pvec.pages[i]; 21088e48dcfbSTheodore Ts'o 21098e48dcfbSTheodore Ts'o /* 21108e48dcfbSTheodore Ts'o * At this point, the page may be truncated or 21118e48dcfbSTheodore Ts'o * invalidated (changing page->mapping to NULL), or 21128e48dcfbSTheodore Ts'o * even swizzled back from swapper_space to tmpfs file 21138e48dcfbSTheodore Ts'o * mapping. However, page->index will not change 21148e48dcfbSTheodore Ts'o * because we have a reference on the page. 21158e48dcfbSTheodore Ts'o */ 21164f01b02cSTheodore Ts'o if (page->index > end) 21174f01b02cSTheodore Ts'o goto out; 21188e48dcfbSTheodore Ts'o 211972f84e65SEric Sandeen *done_index = page->index + 1; 212072f84e65SEric Sandeen 212178aaced3STheodore Ts'o /* 212278aaced3STheodore Ts'o * If we can't merge this page, and we have 212378aaced3STheodore Ts'o * accumulated an contiguous region, write it 212478aaced3STheodore Ts'o */ 212578aaced3STheodore Ts'o if ((mpd->next_page != page->index) && 212678aaced3STheodore Ts'o (mpd->next_page != mpd->first_page)) { 212778aaced3STheodore Ts'o mpage_da_map_and_submit(mpd); 212878aaced3STheodore Ts'o goto ret_extent_tail; 212978aaced3STheodore Ts'o } 213078aaced3STheodore Ts'o 21318e48dcfbSTheodore Ts'o lock_page(page); 21328e48dcfbSTheodore Ts'o 21338e48dcfbSTheodore Ts'o /* 21344f01b02cSTheodore Ts'o * If the page is no longer dirty, or its 21354f01b02cSTheodore Ts'o * mapping no longer corresponds to inode we 21364f01b02cSTheodore Ts'o * are writing (which means it has been 21374f01b02cSTheodore Ts'o * truncated or invalidated), or the page is 21384f01b02cSTheodore Ts'o * already under writeback and we are not 21394f01b02cSTheodore Ts'o * doing a data integrity writeback, skip the page 21408e48dcfbSTheodore Ts'o */ 21414f01b02cSTheodore Ts'o if (!PageDirty(page) || 21424f01b02cSTheodore Ts'o (PageWriteback(page) && 21434f01b02cSTheodore Ts'o (wbc->sync_mode == WB_SYNC_NONE)) || 21444f01b02cSTheodore Ts'o unlikely(page->mapping != mapping)) { 21458e48dcfbSTheodore Ts'o unlock_page(page); 21468e48dcfbSTheodore Ts'o continue; 21478e48dcfbSTheodore Ts'o } 21488e48dcfbSTheodore Ts'o 21498e48dcfbSTheodore Ts'o wait_on_page_writeback(page); 21508e48dcfbSTheodore Ts'o BUG_ON(PageWriteback(page)); 21518e48dcfbSTheodore Ts'o 2152168fc022STheodore Ts'o if (mpd->next_page != page->index) 21538eb9e5ceSTheodore Ts'o mpd->first_page = page->index; 21548eb9e5ceSTheodore Ts'o mpd->next_page = page->index + 1; 21558eb9e5ceSTheodore Ts'o logical = (sector_t) page->index << 21568eb9e5ceSTheodore Ts'o (PAGE_CACHE_SHIFT - inode->i_blkbits); 21578eb9e5ceSTheodore Ts'o 21588eb9e5ceSTheodore Ts'o if (!page_has_buffers(page)) { 21594f01b02cSTheodore Ts'o mpage_add_bh_to_extent(mpd, logical, 21604f01b02cSTheodore Ts'o PAGE_CACHE_SIZE, 21618eb9e5ceSTheodore Ts'o (1 << BH_Dirty) | (1 << BH_Uptodate)); 21624f01b02cSTheodore Ts'o if (mpd->io_done) 21634f01b02cSTheodore Ts'o goto ret_extent_tail; 21648e48dcfbSTheodore Ts'o } else { 21658eb9e5ceSTheodore Ts'o /* 21664f01b02cSTheodore Ts'o * Page with regular buffer heads, 21674f01b02cSTheodore Ts'o * just add all dirty ones 21688eb9e5ceSTheodore Ts'o */ 21698eb9e5ceSTheodore Ts'o head = page_buffers(page); 21708eb9e5ceSTheodore Ts'o bh = head; 21718eb9e5ceSTheodore Ts'o do { 21728eb9e5ceSTheodore Ts'o BUG_ON(buffer_locked(bh)); 21738eb9e5ceSTheodore Ts'o /* 21748eb9e5ceSTheodore Ts'o * We need to try to allocate 21758eb9e5ceSTheodore Ts'o * unmapped blocks in the same page. 21768eb9e5ceSTheodore Ts'o * Otherwise we won't make progress 21778eb9e5ceSTheodore Ts'o * with the page in ext4_writepage 21788eb9e5ceSTheodore Ts'o */ 21798eb9e5ceSTheodore Ts'o if (ext4_bh_delay_or_unwritten(NULL, bh)) { 21808eb9e5ceSTheodore Ts'o mpage_add_bh_to_extent(mpd, logical, 21818eb9e5ceSTheodore Ts'o bh->b_size, 21828eb9e5ceSTheodore Ts'o bh->b_state); 21834f01b02cSTheodore Ts'o if (mpd->io_done) 21844f01b02cSTheodore Ts'o goto ret_extent_tail; 21858eb9e5ceSTheodore Ts'o } else if (buffer_dirty(bh) && (buffer_mapped(bh))) { 21868eb9e5ceSTheodore Ts'o /* 21874f01b02cSTheodore Ts'o * mapped dirty buffer. We need 21884f01b02cSTheodore Ts'o * to update the b_state 21894f01b02cSTheodore Ts'o * because we look at b_state 21904f01b02cSTheodore Ts'o * in mpage_da_map_blocks. We 21914f01b02cSTheodore Ts'o * don't update b_size because 21924f01b02cSTheodore Ts'o * if we find an unmapped 21934f01b02cSTheodore Ts'o * buffer_head later we need to 21944f01b02cSTheodore Ts'o * use the b_state flag of that 21954f01b02cSTheodore Ts'o * buffer_head. 21968eb9e5ceSTheodore Ts'o */ 21978eb9e5ceSTheodore Ts'o if (mpd->b_size == 0) 21988eb9e5ceSTheodore Ts'o mpd->b_state = bh->b_state & BH_FLAGS; 21998e48dcfbSTheodore Ts'o } 22008eb9e5ceSTheodore Ts'o logical++; 22018eb9e5ceSTheodore Ts'o } while ((bh = bh->b_this_page) != head); 22028e48dcfbSTheodore Ts'o } 22038e48dcfbSTheodore Ts'o 22048e48dcfbSTheodore Ts'o if (nr_to_write > 0) { 22058e48dcfbSTheodore Ts'o nr_to_write--; 22068e48dcfbSTheodore Ts'o if (nr_to_write == 0 && 22074f01b02cSTheodore Ts'o wbc->sync_mode == WB_SYNC_NONE) 22088e48dcfbSTheodore Ts'o /* 22098e48dcfbSTheodore Ts'o * We stop writing back only if we are 22108e48dcfbSTheodore Ts'o * not doing integrity sync. In case of 22118e48dcfbSTheodore Ts'o * integrity sync we have to keep going 22128e48dcfbSTheodore Ts'o * because someone may be concurrently 22138e48dcfbSTheodore Ts'o * dirtying pages, and we might have 22148e48dcfbSTheodore Ts'o * synced a lot of newly appeared dirty 22158e48dcfbSTheodore Ts'o * pages, but have not synced all of the 22168e48dcfbSTheodore Ts'o * old dirty pages. 22178e48dcfbSTheodore Ts'o */ 22184f01b02cSTheodore Ts'o goto out; 22198e48dcfbSTheodore Ts'o } 22208e48dcfbSTheodore Ts'o } 22218e48dcfbSTheodore Ts'o pagevec_release(&pvec); 22228e48dcfbSTheodore Ts'o cond_resched(); 22238e48dcfbSTheodore Ts'o } 22244f01b02cSTheodore Ts'o return 0; 22254f01b02cSTheodore Ts'o ret_extent_tail: 22264f01b02cSTheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 22278eb9e5ceSTheodore Ts'o out: 22288eb9e5ceSTheodore Ts'o pagevec_release(&pvec); 22298eb9e5ceSTheodore Ts'o cond_resched(); 22308e48dcfbSTheodore Ts'o return ret; 22318e48dcfbSTheodore Ts'o } 22328e48dcfbSTheodore Ts'o 22338e48dcfbSTheodore Ts'o 223464769240SAlex Tomas static int ext4_da_writepages(struct address_space *mapping, 223564769240SAlex Tomas struct writeback_control *wbc) 223664769240SAlex Tomas { 223722208dedSAneesh Kumar K.V pgoff_t index; 223822208dedSAneesh Kumar K.V int range_whole = 0; 223961628a3fSMingming Cao handle_t *handle = NULL; 2240df22291fSAneesh Kumar K.V struct mpage_da_data mpd; 22415e745b04SAneesh Kumar K.V struct inode *inode = mapping->host; 2242498e5f24STheodore Ts'o int pages_written = 0; 224355138e0bSTheodore Ts'o unsigned int max_pages; 22442acf2c26SAneesh Kumar K.V int range_cyclic, cycled = 1, io_done = 0; 224555138e0bSTheodore Ts'o int needed_blocks, ret = 0; 224655138e0bSTheodore Ts'o long desired_nr_to_write, nr_to_writebump = 0; 2247de89de6eSTheodore Ts'o loff_t range_start = wbc->range_start; 22485e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 224972f84e65SEric Sandeen pgoff_t done_index = 0; 22505b41d924SEric Sandeen pgoff_t end; 22511bce63d1SShaohua Li struct blk_plug plug; 225261628a3fSMingming Cao 22539bffad1eSTheodore Ts'o trace_ext4_da_writepages(inode, wbc); 2254ba80b101STheodore Ts'o 225561628a3fSMingming Cao /* 225661628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 225761628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 225861628a3fSMingming Cao * because that could violate lock ordering on umount 225961628a3fSMingming Cao */ 2260a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 226161628a3fSMingming Cao return 0; 22622a21e37eSTheodore Ts'o 22632a21e37eSTheodore Ts'o /* 22642a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 22652a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 22662a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 22674ab2f15bSTheodore Ts'o * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because 22682a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 22692a21e37eSTheodore Ts'o * read-only, and in that case, ext4_da_writepages should 22702a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 22712a21e37eSTheodore Ts'o * the stack trace. 22722a21e37eSTheodore Ts'o */ 22734ab2f15bSTheodore Ts'o if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) 22742a21e37eSTheodore Ts'o return -EROFS; 22752a21e37eSTheodore Ts'o 227622208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 227722208dedSAneesh Kumar K.V range_whole = 1; 227861628a3fSMingming Cao 22792acf2c26SAneesh Kumar K.V range_cyclic = wbc->range_cyclic; 22802acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 228122208dedSAneesh Kumar K.V index = mapping->writeback_index; 22822acf2c26SAneesh Kumar K.V if (index) 22832acf2c26SAneesh Kumar K.V cycled = 0; 22842acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 22852acf2c26SAneesh Kumar K.V wbc->range_end = LLONG_MAX; 22862acf2c26SAneesh Kumar K.V wbc->range_cyclic = 0; 22875b41d924SEric Sandeen end = -1; 22885b41d924SEric Sandeen } else { 228922208dedSAneesh Kumar K.V index = wbc->range_start >> PAGE_CACHE_SHIFT; 22905b41d924SEric Sandeen end = wbc->range_end >> PAGE_CACHE_SHIFT; 22915b41d924SEric Sandeen } 2292a1d6cc56SAneesh Kumar K.V 229355138e0bSTheodore Ts'o /* 229455138e0bSTheodore Ts'o * This works around two forms of stupidity. The first is in 229555138e0bSTheodore Ts'o * the writeback code, which caps the maximum number of pages 229655138e0bSTheodore Ts'o * written to be 1024 pages. This is wrong on multiple 229755138e0bSTheodore Ts'o * levels; different architectues have a different page size, 229855138e0bSTheodore Ts'o * which changes the maximum amount of data which gets 229955138e0bSTheodore Ts'o * written. Secondly, 4 megabytes is way too small. XFS 230055138e0bSTheodore Ts'o * forces this value to be 16 megabytes by multiplying 230155138e0bSTheodore Ts'o * nr_to_write parameter by four, and then relies on its 230255138e0bSTheodore Ts'o * allocator to allocate larger extents to make them 230355138e0bSTheodore Ts'o * contiguous. Unfortunately this brings us to the second 230455138e0bSTheodore Ts'o * stupidity, which is that ext4's mballoc code only allocates 230555138e0bSTheodore Ts'o * at most 2048 blocks. So we force contiguous writes up to 230655138e0bSTheodore Ts'o * the number of dirty blocks in the inode, or 230755138e0bSTheodore Ts'o * sbi->max_writeback_mb_bump whichever is smaller. 230855138e0bSTheodore Ts'o */ 230955138e0bSTheodore Ts'o max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT); 2310b443e733SEric Sandeen if (!range_cyclic && range_whole) { 2311b443e733SEric Sandeen if (wbc->nr_to_write == LONG_MAX) 2312b443e733SEric Sandeen desired_nr_to_write = wbc->nr_to_write; 231355138e0bSTheodore Ts'o else 2314b443e733SEric Sandeen desired_nr_to_write = wbc->nr_to_write * 8; 2315b443e733SEric Sandeen } else 231655138e0bSTheodore Ts'o desired_nr_to_write = ext4_num_dirty_pages(inode, index, 231755138e0bSTheodore Ts'o max_pages); 231855138e0bSTheodore Ts'o if (desired_nr_to_write > max_pages) 231955138e0bSTheodore Ts'o desired_nr_to_write = max_pages; 232055138e0bSTheodore Ts'o 232155138e0bSTheodore Ts'o if (wbc->nr_to_write < desired_nr_to_write) { 232255138e0bSTheodore Ts'o nr_to_writebump = desired_nr_to_write - wbc->nr_to_write; 232355138e0bSTheodore Ts'o wbc->nr_to_write = desired_nr_to_write; 232455138e0bSTheodore Ts'o } 232555138e0bSTheodore Ts'o 23262acf2c26SAneesh Kumar K.V retry: 23276e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 23285b41d924SEric Sandeen tag_pages_for_writeback(mapping, index, end); 23295b41d924SEric Sandeen 23301bce63d1SShaohua Li blk_start_plug(&plug); 233122208dedSAneesh Kumar K.V while (!ret && wbc->nr_to_write > 0) { 2332a1d6cc56SAneesh Kumar K.V 2333a1d6cc56SAneesh Kumar K.V /* 2334a1d6cc56SAneesh Kumar K.V * we insert one extent at a time. So we need 2335a1d6cc56SAneesh Kumar K.V * credit needed for single extent allocation. 2336a1d6cc56SAneesh Kumar K.V * journalled mode is currently not supported 2337a1d6cc56SAneesh Kumar K.V * by delalloc 2338a1d6cc56SAneesh Kumar K.V */ 2339a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 2340525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 2341a1d6cc56SAneesh Kumar K.V 234261628a3fSMingming Cao /* start a new transaction*/ 234361628a3fSMingming Cao handle = ext4_journal_start(inode, needed_blocks); 234461628a3fSMingming Cao if (IS_ERR(handle)) { 234561628a3fSMingming Cao ret = PTR_ERR(handle); 23461693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2347fbe845ddSCurt Wohlgemuth "%ld pages, ino %lu; err %d", __func__, 2348a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 23493c1fcb2cSNamjae Jeon blk_finish_plug(&plug); 235061628a3fSMingming Cao goto out_writepages; 235161628a3fSMingming Cao } 2352f63e6005STheodore Ts'o 2353f63e6005STheodore Ts'o /* 23548eb9e5ceSTheodore Ts'o * Now call write_cache_pages_da() to find the next 2355f63e6005STheodore Ts'o * contiguous region of logical blocks that need 23568eb9e5ceSTheodore Ts'o * blocks to be allocated by ext4 and submit them. 2357f63e6005STheodore Ts'o */ 235872f84e65SEric Sandeen ret = write_cache_pages_da(mapping, wbc, &mpd, &done_index); 2359f63e6005STheodore Ts'o /* 2360af901ca1SAndré Goddard Rosa * If we have a contiguous extent of pages and we 2361f63e6005STheodore Ts'o * haven't done the I/O yet, map the blocks and submit 2362f63e6005STheodore Ts'o * them for I/O. 2363f63e6005STheodore Ts'o */ 2364f63e6005STheodore Ts'o if (!mpd.io_done && mpd.next_page != mpd.first_page) { 23655a87b7a5STheodore Ts'o mpage_da_map_and_submit(&mpd); 2366f63e6005STheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 2367f63e6005STheodore Ts'o } 2368b3a3ca8cSTheodore Ts'o trace_ext4_da_write_pages(inode, &mpd); 2369f63e6005STheodore Ts'o wbc->nr_to_write -= mpd.pages_written; 2370df22291fSAneesh Kumar K.V 237161628a3fSMingming Cao ext4_journal_stop(handle); 2372df22291fSAneesh Kumar K.V 23738f64b32eSEric Sandeen if ((mpd.retval == -ENOSPC) && sbi->s_journal) { 237422208dedSAneesh Kumar K.V /* commit the transaction which would 237522208dedSAneesh Kumar K.V * free blocks released in the transaction 237622208dedSAneesh Kumar K.V * and try again 237722208dedSAneesh Kumar K.V */ 2378df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 237922208dedSAneesh Kumar K.V ret = 0; 238022208dedSAneesh Kumar K.V } else if (ret == MPAGE_DA_EXTENT_TAIL) { 2381a1d6cc56SAneesh Kumar K.V /* 23828de49e67SKazuya Mio * Got one extent now try with rest of the pages. 23838de49e67SKazuya Mio * If mpd.retval is set -EIO, journal is aborted. 23848de49e67SKazuya Mio * So we don't need to write any more. 2385a1d6cc56SAneesh Kumar K.V */ 238622208dedSAneesh Kumar K.V pages_written += mpd.pages_written; 23878de49e67SKazuya Mio ret = mpd.retval; 23882acf2c26SAneesh Kumar K.V io_done = 1; 238922208dedSAneesh Kumar K.V } else if (wbc->nr_to_write) 239061628a3fSMingming Cao /* 239161628a3fSMingming Cao * There is no more writeout needed 239261628a3fSMingming Cao * or we requested for a noblocking writeout 239361628a3fSMingming Cao * and we found the device congested 239461628a3fSMingming Cao */ 239561628a3fSMingming Cao break; 239661628a3fSMingming Cao } 23971bce63d1SShaohua Li blk_finish_plug(&plug); 23982acf2c26SAneesh Kumar K.V if (!io_done && !cycled) { 23992acf2c26SAneesh Kumar K.V cycled = 1; 24002acf2c26SAneesh Kumar K.V index = 0; 24012acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 24022acf2c26SAneesh Kumar K.V wbc->range_end = mapping->writeback_index - 1; 24032acf2c26SAneesh Kumar K.V goto retry; 24042acf2c26SAneesh Kumar K.V } 240561628a3fSMingming Cao 240622208dedSAneesh Kumar K.V /* Update index */ 24072acf2c26SAneesh Kumar K.V wbc->range_cyclic = range_cyclic; 240822208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 240922208dedSAneesh Kumar K.V /* 241022208dedSAneesh Kumar K.V * set the writeback_index so that range_cyclic 241122208dedSAneesh Kumar K.V * mode will write it back later 241222208dedSAneesh Kumar K.V */ 241372f84e65SEric Sandeen mapping->writeback_index = done_index; 2414a1d6cc56SAneesh Kumar K.V 241561628a3fSMingming Cao out_writepages: 241622208dedSAneesh Kumar K.V wbc->nr_to_write -= nr_to_writebump; 2417de89de6eSTheodore Ts'o wbc->range_start = range_start; 24189bffad1eSTheodore Ts'o trace_ext4_da_writepages_result(inode, wbc, ret, pages_written); 241961628a3fSMingming Cao return ret; 242064769240SAlex Tomas } 242164769240SAlex Tomas 242279f0be8dSAneesh Kumar K.V #define FALL_BACK_TO_NONDELALLOC 1 242379f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 242479f0be8dSAneesh Kumar K.V { 242579f0be8dSAneesh Kumar K.V s64 free_blocks, dirty_blocks; 242679f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 242779f0be8dSAneesh Kumar K.V 242879f0be8dSAneesh Kumar K.V /* 242979f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 243079f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 2431179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 243279f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 243379f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 243479f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 243579f0be8dSAneesh Kumar K.V */ 243657042651STheodore Ts'o free_blocks = EXT4_C2B(sbi, 243757042651STheodore Ts'o percpu_counter_read_positive(&sbi->s_freeclusters_counter)); 243857042651STheodore Ts'o dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyclusters_counter); 243979f0be8dSAneesh Kumar K.V if (2 * free_blocks < 3 * dirty_blocks || 2440df55c99dSTheodore Ts'o free_blocks < (dirty_blocks + EXT4_FREECLUSTERS_WATERMARK)) { 244179f0be8dSAneesh Kumar K.V /* 2442c8afb446SEric Sandeen * free block count is less than 150% of dirty blocks 2443c8afb446SEric Sandeen * or free blocks is less than watermark 244479f0be8dSAneesh Kumar K.V */ 244579f0be8dSAneesh Kumar K.V return 1; 244679f0be8dSAneesh Kumar K.V } 2447c8afb446SEric Sandeen /* 2448c8afb446SEric Sandeen * Even if we don't switch but are nearing capacity, 2449c8afb446SEric Sandeen * start pushing delalloc when 1/2 of free blocks are dirty. 2450c8afb446SEric Sandeen */ 2451c8afb446SEric Sandeen if (free_blocks < 2 * dirty_blocks) 24520e175a18SCurt Wohlgemuth writeback_inodes_sb_if_idle(sb, WB_REASON_FS_FREE_SPACE); 2453c8afb446SEric Sandeen 245479f0be8dSAneesh Kumar K.V return 0; 245579f0be8dSAneesh Kumar K.V } 245679f0be8dSAneesh Kumar K.V 245764769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 245864769240SAlex Tomas loff_t pos, unsigned len, unsigned flags, 245964769240SAlex Tomas struct page **pagep, void **fsdata) 246064769240SAlex Tomas { 246172b8ab9dSEric Sandeen int ret, retries = 0; 246264769240SAlex Tomas struct page *page; 246364769240SAlex Tomas pgoff_t index; 246464769240SAlex Tomas struct inode *inode = mapping->host; 246564769240SAlex Tomas handle_t *handle; 246664769240SAlex Tomas 246764769240SAlex Tomas index = pos >> PAGE_CACHE_SHIFT; 246879f0be8dSAneesh Kumar K.V 246979f0be8dSAneesh Kumar K.V if (ext4_nonda_switch(inode->i_sb)) { 247079f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 247179f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 247279f0be8dSAneesh Kumar K.V len, flags, pagep, fsdata); 247379f0be8dSAneesh Kumar K.V } 247479f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 24759bffad1eSTheodore Ts'o trace_ext4_da_write_begin(inode, pos, len, flags); 2476d2a17637SMingming Cao retry: 247764769240SAlex Tomas /* 247864769240SAlex Tomas * With delayed allocation, we don't log the i_disksize update 247964769240SAlex Tomas * if there is delayed block allocation. But we still need 248064769240SAlex Tomas * to journalling the i_disksize update if writes to the end 248164769240SAlex Tomas * of file which has an already mapped buffer. 248264769240SAlex Tomas */ 248364769240SAlex Tomas handle = ext4_journal_start(inode, 1); 248464769240SAlex Tomas if (IS_ERR(handle)) { 248564769240SAlex Tomas ret = PTR_ERR(handle); 248664769240SAlex Tomas goto out; 248764769240SAlex Tomas } 2488ebd3610bSJan Kara /* We cannot recurse into the filesystem as the transaction is already 2489ebd3610bSJan Kara * started */ 2490ebd3610bSJan Kara flags |= AOP_FLAG_NOFS; 249164769240SAlex Tomas 249254566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 2493d5a0d4f7SEric Sandeen if (!page) { 2494d5a0d4f7SEric Sandeen ext4_journal_stop(handle); 2495d5a0d4f7SEric Sandeen ret = -ENOMEM; 2496d5a0d4f7SEric Sandeen goto out; 2497d5a0d4f7SEric Sandeen } 249864769240SAlex Tomas *pagep = page; 249964769240SAlex Tomas 25006e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep); 250164769240SAlex Tomas if (ret < 0) { 250264769240SAlex Tomas unlock_page(page); 250364769240SAlex Tomas ext4_journal_stop(handle); 250464769240SAlex Tomas page_cache_release(page); 2505ae4d5372SAneesh Kumar K.V /* 2506ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 2507ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 2508ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 2509ae4d5372SAneesh Kumar K.V */ 2510ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 2511b9a4207dSJan Kara ext4_truncate_failed_write(inode); 251264769240SAlex Tomas } 251364769240SAlex Tomas 2514d2a17637SMingming Cao if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 2515d2a17637SMingming Cao goto retry; 251664769240SAlex Tomas out: 251764769240SAlex Tomas return ret; 251864769240SAlex Tomas } 251964769240SAlex Tomas 2520632eaeabSMingming Cao /* 2521632eaeabSMingming Cao * Check if we should update i_disksize 2522632eaeabSMingming Cao * when write to the end of file but not require block allocation 2523632eaeabSMingming Cao */ 2524632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page, 2525632eaeabSMingming Cao unsigned long offset) 2526632eaeabSMingming Cao { 2527632eaeabSMingming Cao struct buffer_head *bh; 2528632eaeabSMingming Cao struct inode *inode = page->mapping->host; 2529632eaeabSMingming Cao unsigned int idx; 2530632eaeabSMingming Cao int i; 2531632eaeabSMingming Cao 2532632eaeabSMingming Cao bh = page_buffers(page); 2533632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 2534632eaeabSMingming Cao 2535632eaeabSMingming Cao for (i = 0; i < idx; i++) 2536632eaeabSMingming Cao bh = bh->b_this_page; 2537632eaeabSMingming Cao 253829fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 2539632eaeabSMingming Cao return 0; 2540632eaeabSMingming Cao return 1; 2541632eaeabSMingming Cao } 2542632eaeabSMingming Cao 254364769240SAlex Tomas static int ext4_da_write_end(struct file *file, 254464769240SAlex Tomas struct address_space *mapping, 254564769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 254664769240SAlex Tomas struct page *page, void *fsdata) 254764769240SAlex Tomas { 254864769240SAlex Tomas struct inode *inode = mapping->host; 254964769240SAlex Tomas int ret = 0, ret2; 255064769240SAlex Tomas handle_t *handle = ext4_journal_current_handle(); 255164769240SAlex Tomas loff_t new_i_size; 2552632eaeabSMingming Cao unsigned long start, end; 255379f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 255479f0be8dSAneesh Kumar K.V 255579f0be8dSAneesh Kumar K.V if (write_mode == FALL_BACK_TO_NONDELALLOC) { 25563d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 25573d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 255879f0be8dSAneesh Kumar K.V return ext4_ordered_write_end(file, mapping, pos, 255979f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 25603d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 256179f0be8dSAneesh Kumar K.V return ext4_writeback_write_end(file, mapping, pos, 256279f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 25633d2b1582SLukas Czerner default: 256479f0be8dSAneesh Kumar K.V BUG(); 256579f0be8dSAneesh Kumar K.V } 256679f0be8dSAneesh Kumar K.V } 2567632eaeabSMingming Cao 25689bffad1eSTheodore Ts'o trace_ext4_da_write_end(inode, pos, len, copied); 2569632eaeabSMingming Cao start = pos & (PAGE_CACHE_SIZE - 1); 2570632eaeabSMingming Cao end = start + copied - 1; 257164769240SAlex Tomas 257264769240SAlex Tomas /* 257364769240SAlex Tomas * generic_write_end() will run mark_inode_dirty() if i_size 257464769240SAlex Tomas * changes. So let's piggyback the i_disksize mark_inode_dirty 257564769240SAlex Tomas * into that. 257664769240SAlex Tomas */ 257764769240SAlex Tomas 257864769240SAlex Tomas new_i_size = pos + copied; 2579ea51d132SAndrea Arcangeli if (copied && new_i_size > EXT4_I(inode)->i_disksize) { 2580632eaeabSMingming Cao if (ext4_da_should_update_i_disksize(page, end)) { 2581632eaeabSMingming Cao down_write(&EXT4_I(inode)->i_data_sem); 2582632eaeabSMingming Cao if (new_i_size > EXT4_I(inode)->i_disksize) { 258364769240SAlex Tomas /* 2584632eaeabSMingming Cao * Updating i_disksize when extending file 2585632eaeabSMingming Cao * without needing block allocation 258664769240SAlex Tomas */ 258764769240SAlex Tomas if (ext4_should_order_data(inode)) 2588632eaeabSMingming Cao ret = ext4_jbd2_file_inode(handle, 2589632eaeabSMingming Cao inode); 259064769240SAlex Tomas 259164769240SAlex Tomas EXT4_I(inode)->i_disksize = new_i_size; 259264769240SAlex Tomas } 2593632eaeabSMingming Cao up_write(&EXT4_I(inode)->i_data_sem); 2594cf17fea6SAneesh Kumar K.V /* We need to mark inode dirty even if 2595cf17fea6SAneesh Kumar K.V * new_i_size is less that inode->i_size 2596cf17fea6SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 2597cf17fea6SAneesh Kumar K.V */ 2598cf17fea6SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 2599632eaeabSMingming Cao } 2600632eaeabSMingming Cao } 260164769240SAlex Tomas ret2 = generic_write_end(file, mapping, pos, len, copied, 260264769240SAlex Tomas page, fsdata); 260364769240SAlex Tomas copied = ret2; 260464769240SAlex Tomas if (ret2 < 0) 260564769240SAlex Tomas ret = ret2; 260664769240SAlex Tomas ret2 = ext4_journal_stop(handle); 260764769240SAlex Tomas if (!ret) 260864769240SAlex Tomas ret = ret2; 260964769240SAlex Tomas 261064769240SAlex Tomas return ret ? ret : copied; 261164769240SAlex Tomas } 261264769240SAlex Tomas 261364769240SAlex Tomas static void ext4_da_invalidatepage(struct page *page, unsigned long offset) 261464769240SAlex Tomas { 261564769240SAlex Tomas /* 261664769240SAlex Tomas * Drop reserved blocks 261764769240SAlex Tomas */ 261864769240SAlex Tomas BUG_ON(!PageLocked(page)); 261964769240SAlex Tomas if (!page_has_buffers(page)) 262064769240SAlex Tomas goto out; 262164769240SAlex Tomas 2622d2a17637SMingming Cao ext4_da_page_release_reservation(page, offset); 262364769240SAlex Tomas 262464769240SAlex Tomas out: 262564769240SAlex Tomas ext4_invalidatepage(page, offset); 262664769240SAlex Tomas 262764769240SAlex Tomas return; 262864769240SAlex Tomas } 262964769240SAlex Tomas 2630ccd2506bSTheodore Ts'o /* 2631ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 2632ccd2506bSTheodore Ts'o */ 2633ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 2634ccd2506bSTheodore Ts'o { 2635fb40ba0dSTheodore Ts'o trace_ext4_alloc_da_blocks(inode); 2636fb40ba0dSTheodore Ts'o 2637ccd2506bSTheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks && 2638ccd2506bSTheodore Ts'o !EXT4_I(inode)->i_reserved_meta_blocks) 2639ccd2506bSTheodore Ts'o return 0; 2640ccd2506bSTheodore Ts'o 2641ccd2506bSTheodore Ts'o /* 2642ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 2643ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 2644ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 2645ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 2646ccd2506bSTheodore Ts'o * would require replicating code paths in: 2647ccd2506bSTheodore Ts'o * 2648ccd2506bSTheodore Ts'o * ext4_da_writepages() -> 2649ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 2650ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 2651ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 2652ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 2653ccd2506bSTheodore Ts'o * 2654ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 2655ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 2656ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 2657ccd2506bSTheodore Ts'o * doing I/O at all. 2658ccd2506bSTheodore Ts'o * 2659ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 2660380cf090SWu Fengguang * the pages by calling redirty_page_for_writepage() but that 2661ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 2662ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 266325985edcSLucas De Marchi * simplifying them because we wouldn't actually intend to 2664ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 2665ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 2666ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 2667ccd2506bSTheodore Ts'o * 2668ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 2669ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 2670ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 2671ccd2506bSTheodore Ts'o */ 2672ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 2673ccd2506bSTheodore Ts'o } 267464769240SAlex Tomas 267564769240SAlex Tomas /* 2676ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 2677ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 2678ac27a0ecSDave Kleikamp * 2679ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 2680617ba13bSMingming Cao * journal. If somebody makes a swapfile on an ext4 data-journaling 2681ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 2682ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 2683ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 2684ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 2685ac27a0ecSDave Kleikamp * 2686ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 2687ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 2688ac27a0ecSDave Kleikamp */ 2689617ba13bSMingming Cao static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 2690ac27a0ecSDave Kleikamp { 2691ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 2692ac27a0ecSDave Kleikamp journal_t *journal; 2693ac27a0ecSDave Kleikamp int err; 2694ac27a0ecSDave Kleikamp 269564769240SAlex Tomas if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 269664769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) { 269764769240SAlex Tomas /* 269864769240SAlex Tomas * With delalloc we want to sync the file 269964769240SAlex Tomas * so that we can make sure we allocate 270064769240SAlex Tomas * blocks for file 270164769240SAlex Tomas */ 270264769240SAlex Tomas filemap_write_and_wait(mapping); 270364769240SAlex Tomas } 270464769240SAlex Tomas 270519f5fb7aSTheodore Ts'o if (EXT4_JOURNAL(inode) && 270619f5fb7aSTheodore Ts'o ext4_test_inode_state(inode, EXT4_STATE_JDATA)) { 2707ac27a0ecSDave Kleikamp /* 2708ac27a0ecSDave Kleikamp * This is a REALLY heavyweight approach, but the use of 2709ac27a0ecSDave Kleikamp * bmap on dirty files is expected to be extremely rare: 2710ac27a0ecSDave Kleikamp * only if we run lilo or swapon on a freshly made file 2711ac27a0ecSDave Kleikamp * do we expect this to happen. 2712ac27a0ecSDave Kleikamp * 2713ac27a0ecSDave Kleikamp * (bmap requires CAP_SYS_RAWIO so this does not 2714ac27a0ecSDave Kleikamp * represent an unprivileged user DOS attack --- we'd be 2715ac27a0ecSDave Kleikamp * in trouble if mortal users could trigger this path at 2716ac27a0ecSDave Kleikamp * will.) 2717ac27a0ecSDave Kleikamp * 2718617ba13bSMingming Cao * NB. EXT4_STATE_JDATA is not set on files other than 2719ac27a0ecSDave Kleikamp * regular files. If somebody wants to bmap a directory 2720ac27a0ecSDave Kleikamp * or symlink and gets confused because the buffer 2721ac27a0ecSDave Kleikamp * hasn't yet been flushed to disk, they deserve 2722ac27a0ecSDave Kleikamp * everything they get. 2723ac27a0ecSDave Kleikamp */ 2724ac27a0ecSDave Kleikamp 272519f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_JDATA); 2726617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 2727dab291afSMingming Cao jbd2_journal_lock_updates(journal); 2728dab291afSMingming Cao err = jbd2_journal_flush(journal); 2729dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 2730ac27a0ecSDave Kleikamp 2731ac27a0ecSDave Kleikamp if (err) 2732ac27a0ecSDave Kleikamp return 0; 2733ac27a0ecSDave Kleikamp } 2734ac27a0ecSDave Kleikamp 2735617ba13bSMingming Cao return generic_block_bmap(mapping, block, ext4_get_block); 2736ac27a0ecSDave Kleikamp } 2737ac27a0ecSDave Kleikamp 2738617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page) 2739ac27a0ecSDave Kleikamp { 27400562e0baSJiaying Zhang trace_ext4_readpage(page); 2741617ba13bSMingming Cao return mpage_readpage(page, ext4_get_block); 2742ac27a0ecSDave Kleikamp } 2743ac27a0ecSDave Kleikamp 2744ac27a0ecSDave Kleikamp static int 2745617ba13bSMingming Cao ext4_readpages(struct file *file, struct address_space *mapping, 2746ac27a0ecSDave Kleikamp struct list_head *pages, unsigned nr_pages) 2747ac27a0ecSDave Kleikamp { 2748617ba13bSMingming Cao return mpage_readpages(mapping, pages, nr_pages, ext4_get_block); 2749ac27a0ecSDave Kleikamp } 2750ac27a0ecSDave Kleikamp 2751744692dcSJiaying Zhang static void ext4_invalidatepage_free_endio(struct page *page, unsigned long offset) 2752744692dcSJiaying Zhang { 2753744692dcSJiaying Zhang struct buffer_head *head, *bh; 2754744692dcSJiaying Zhang unsigned int curr_off = 0; 2755744692dcSJiaying Zhang 2756744692dcSJiaying Zhang if (!page_has_buffers(page)) 2757744692dcSJiaying Zhang return; 2758744692dcSJiaying Zhang head = bh = page_buffers(page); 2759744692dcSJiaying Zhang do { 2760744692dcSJiaying Zhang if (offset <= curr_off && test_clear_buffer_uninit(bh) 2761744692dcSJiaying Zhang && bh->b_private) { 2762744692dcSJiaying Zhang ext4_free_io_end(bh->b_private); 2763744692dcSJiaying Zhang bh->b_private = NULL; 2764744692dcSJiaying Zhang bh->b_end_io = NULL; 2765744692dcSJiaying Zhang } 2766744692dcSJiaying Zhang curr_off = curr_off + bh->b_size; 2767744692dcSJiaying Zhang bh = bh->b_this_page; 2768744692dcSJiaying Zhang } while (bh != head); 2769744692dcSJiaying Zhang } 2770744692dcSJiaying Zhang 2771617ba13bSMingming Cao static void ext4_invalidatepage(struct page *page, unsigned long offset) 2772ac27a0ecSDave Kleikamp { 2773617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 2774ac27a0ecSDave Kleikamp 27750562e0baSJiaying Zhang trace_ext4_invalidatepage(page, offset); 27760562e0baSJiaying Zhang 2777ac27a0ecSDave Kleikamp /* 2778744692dcSJiaying Zhang * free any io_end structure allocated for buffers to be discarded 2779744692dcSJiaying Zhang */ 2780744692dcSJiaying Zhang if (ext4_should_dioread_nolock(page->mapping->host)) 2781744692dcSJiaying Zhang ext4_invalidatepage_free_endio(page, offset); 2782744692dcSJiaying Zhang /* 2783ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 2784ac27a0ecSDave Kleikamp */ 2785ac27a0ecSDave Kleikamp if (offset == 0) 2786ac27a0ecSDave Kleikamp ClearPageChecked(page); 2787ac27a0ecSDave Kleikamp 27880390131bSFrank Mayhar if (journal) 2789dab291afSMingming Cao jbd2_journal_invalidatepage(journal, page, offset); 27900390131bSFrank Mayhar else 27910390131bSFrank Mayhar block_invalidatepage(page, offset); 2792ac27a0ecSDave Kleikamp } 2793ac27a0ecSDave Kleikamp 2794617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait) 2795ac27a0ecSDave Kleikamp { 2796617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 2797ac27a0ecSDave Kleikamp 27980562e0baSJiaying Zhang trace_ext4_releasepage(page); 27990562e0baSJiaying Zhang 2800ac27a0ecSDave Kleikamp WARN_ON(PageChecked(page)); 2801ac27a0ecSDave Kleikamp if (!page_has_buffers(page)) 2802ac27a0ecSDave Kleikamp return 0; 28030390131bSFrank Mayhar if (journal) 2804dab291afSMingming Cao return jbd2_journal_try_to_free_buffers(journal, page, wait); 28050390131bSFrank Mayhar else 28060390131bSFrank Mayhar return try_to_free_buffers(page); 2807ac27a0ecSDave Kleikamp } 2808ac27a0ecSDave Kleikamp 2809ac27a0ecSDave Kleikamp /* 28102ed88685STheodore Ts'o * ext4_get_block used when preparing for a DIO write or buffer write. 28112ed88685STheodore Ts'o * We allocate an uinitialized extent if blocks haven't been allocated. 28122ed88685STheodore Ts'o * The extent will be converted to initialized after the IO is complete. 28132ed88685STheodore Ts'o */ 2814c7064ef1SJiaying Zhang static int ext4_get_block_write(struct inode *inode, sector_t iblock, 28154c0425ffSMingming Cao struct buffer_head *bh_result, int create) 28164c0425ffSMingming Cao { 2817c7064ef1SJiaying Zhang ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n", 28188d5d02e6SMingming Cao inode->i_ino, create); 28192ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh_result, 28202ed88685STheodore Ts'o EXT4_GET_BLOCKS_IO_CREATE_EXT); 28214c0425ffSMingming Cao } 28224c0425ffSMingming Cao 2823729f52c6SZheng Liu static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock, 2824729f52c6SZheng Liu struct buffer_head *bh_result, int flags) 2825729f52c6SZheng Liu { 2826729f52c6SZheng Liu handle_t *handle = ext4_journal_current_handle(); 2827729f52c6SZheng Liu struct ext4_map_blocks map; 2828729f52c6SZheng Liu int ret = 0; 2829729f52c6SZheng Liu 2830729f52c6SZheng Liu ext4_debug("ext4_get_block_write_nolock: inode %lu, flag %d\n", 2831729f52c6SZheng Liu inode->i_ino, flags); 2832729f52c6SZheng Liu 2833729f52c6SZheng Liu flags = EXT4_GET_BLOCKS_NO_LOCK; 2834729f52c6SZheng Liu 2835729f52c6SZheng Liu map.m_lblk = iblock; 2836729f52c6SZheng Liu map.m_len = bh_result->b_size >> inode->i_blkbits; 2837729f52c6SZheng Liu 2838729f52c6SZheng Liu ret = ext4_map_blocks(handle, inode, &map, flags); 2839729f52c6SZheng Liu if (ret > 0) { 2840729f52c6SZheng Liu map_bh(bh_result, inode->i_sb, map.m_pblk); 2841729f52c6SZheng Liu bh_result->b_state = (bh_result->b_state & ~EXT4_MAP_FLAGS) | 2842729f52c6SZheng Liu map.m_flags; 2843729f52c6SZheng Liu bh_result->b_size = inode->i_sb->s_blocksize * map.m_len; 2844729f52c6SZheng Liu ret = 0; 2845729f52c6SZheng Liu } 2846729f52c6SZheng Liu return ret; 2847729f52c6SZheng Liu } 2848729f52c6SZheng Liu 28494c0425ffSMingming Cao static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset, 2850552ef802SChristoph Hellwig ssize_t size, void *private, int ret, 2851552ef802SChristoph Hellwig bool is_async) 28524c0425ffSMingming Cao { 285372c5052dSChristoph Hellwig struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode; 28544c0425ffSMingming Cao ext4_io_end_t *io_end = iocb->private; 28554c0425ffSMingming Cao struct workqueue_struct *wq; 2856744692dcSJiaying Zhang unsigned long flags; 2857744692dcSJiaying Zhang struct ext4_inode_info *ei; 28584c0425ffSMingming Cao 28594b70df18SMingming /* if not async direct IO or dio with 0 bytes write, just return */ 28604b70df18SMingming if (!io_end || !size) 2861552ef802SChristoph Hellwig goto out; 28624b70df18SMingming 28638d5d02e6SMingming Cao ext_debug("ext4_end_io_dio(): io_end 0x%p " 2864ace36ad4SJoe Perches "for inode %lu, iocb 0x%p, offset %llu, size %zd\n", 28658d5d02e6SMingming Cao iocb->private, io_end->inode->i_ino, iocb, offset, 28668d5d02e6SMingming Cao size); 28678d5d02e6SMingming Cao 2868b5a7e970STheodore Ts'o iocb->private = NULL; 2869b5a7e970STheodore Ts'o 28708d5d02e6SMingming Cao /* if not aio dio with unwritten extents, just free io and return */ 2871bd2d0210STheodore Ts'o if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) { 28728d5d02e6SMingming Cao ext4_free_io_end(io_end); 28735b3ff237Sjiayingz@google.com (Jiaying Zhang) out: 28745b3ff237Sjiayingz@google.com (Jiaying Zhang) if (is_async) 28755b3ff237Sjiayingz@google.com (Jiaying Zhang) aio_complete(iocb, ret, 0); 287672c5052dSChristoph Hellwig inode_dio_done(inode); 28775b3ff237Sjiayingz@google.com (Jiaying Zhang) return; 28788d5d02e6SMingming Cao } 28798d5d02e6SMingming Cao 28804c0425ffSMingming Cao io_end->offset = offset; 28814c0425ffSMingming Cao io_end->size = size; 28825b3ff237Sjiayingz@google.com (Jiaying Zhang) if (is_async) { 28835b3ff237Sjiayingz@google.com (Jiaying Zhang) io_end->iocb = iocb; 28845b3ff237Sjiayingz@google.com (Jiaying Zhang) io_end->result = ret; 28855b3ff237Sjiayingz@google.com (Jiaying Zhang) } 28864c0425ffSMingming Cao wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq; 28874c0425ffSMingming Cao 28888d5d02e6SMingming Cao /* Add the io_end to per-inode completed aio dio list*/ 2889744692dcSJiaying Zhang ei = EXT4_I(io_end->inode); 2890744692dcSJiaying Zhang spin_lock_irqsave(&ei->i_completed_io_lock, flags); 2891744692dcSJiaying Zhang list_add_tail(&io_end->list, &ei->i_completed_io_list); 2892744692dcSJiaying Zhang spin_unlock_irqrestore(&ei->i_completed_io_lock, flags); 2893c999af2bSEric Sandeen 2894c999af2bSEric Sandeen /* queue the work to convert unwritten extents to written */ 28954c81f045STejun Heo queue_work(wq, &io_end->work); 28964c0425ffSMingming Cao } 2897c7064ef1SJiaying Zhang 2898744692dcSJiaying Zhang static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate) 2899744692dcSJiaying Zhang { 2900744692dcSJiaying Zhang ext4_io_end_t *io_end = bh->b_private; 2901744692dcSJiaying Zhang struct workqueue_struct *wq; 2902744692dcSJiaying Zhang struct inode *inode; 2903744692dcSJiaying Zhang unsigned long flags; 2904744692dcSJiaying Zhang 2905744692dcSJiaying Zhang if (!test_clear_buffer_uninit(bh) || !io_end) 2906744692dcSJiaying Zhang goto out; 2907744692dcSJiaying Zhang 2908744692dcSJiaying Zhang if (!(io_end->inode->i_sb->s_flags & MS_ACTIVE)) { 290992b97816STheodore Ts'o ext4_msg(io_end->inode->i_sb, KERN_INFO, 291092b97816STheodore Ts'o "sb umounted, discard end_io request for inode %lu", 2911744692dcSJiaying Zhang io_end->inode->i_ino); 2912744692dcSJiaying Zhang ext4_free_io_end(io_end); 2913744692dcSJiaying Zhang goto out; 2914744692dcSJiaying Zhang } 2915744692dcSJiaying Zhang 291632c80b32STao Ma /* 291732c80b32STao Ma * It may be over-defensive here to check EXT4_IO_END_UNWRITTEN now, 291832c80b32STao Ma * but being more careful is always safe for the future change. 291932c80b32STao Ma */ 2920744692dcSJiaying Zhang inode = io_end->inode; 29210edeb71dSTao Ma ext4_set_io_unwritten_flag(inode, io_end); 2922744692dcSJiaying Zhang 2923744692dcSJiaying Zhang /* Add the io_end to per-inode completed io list*/ 2924744692dcSJiaying Zhang spin_lock_irqsave(&EXT4_I(inode)->i_completed_io_lock, flags); 2925744692dcSJiaying Zhang list_add_tail(&io_end->list, &EXT4_I(inode)->i_completed_io_list); 2926744692dcSJiaying Zhang spin_unlock_irqrestore(&EXT4_I(inode)->i_completed_io_lock, flags); 2927744692dcSJiaying Zhang 2928744692dcSJiaying Zhang wq = EXT4_SB(inode->i_sb)->dio_unwritten_wq; 2929744692dcSJiaying Zhang /* queue the work to convert unwritten extents to written */ 2930744692dcSJiaying Zhang queue_work(wq, &io_end->work); 2931744692dcSJiaying Zhang out: 2932744692dcSJiaying Zhang bh->b_private = NULL; 2933744692dcSJiaying Zhang bh->b_end_io = NULL; 2934744692dcSJiaying Zhang clear_buffer_uninit(bh); 2935744692dcSJiaying Zhang end_buffer_async_write(bh, uptodate); 2936744692dcSJiaying Zhang } 2937744692dcSJiaying Zhang 2938744692dcSJiaying Zhang static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode) 2939744692dcSJiaying Zhang { 2940744692dcSJiaying Zhang ext4_io_end_t *io_end; 2941744692dcSJiaying Zhang struct page *page = bh->b_page; 2942744692dcSJiaying Zhang loff_t offset = (sector_t)page->index << PAGE_CACHE_SHIFT; 2943744692dcSJiaying Zhang size_t size = bh->b_size; 2944744692dcSJiaying Zhang 2945744692dcSJiaying Zhang retry: 2946744692dcSJiaying Zhang io_end = ext4_init_io_end(inode, GFP_ATOMIC); 2947744692dcSJiaying Zhang if (!io_end) { 29486db26ffcSAndrew Morton pr_warn_ratelimited("%s: allocation fail\n", __func__); 2949744692dcSJiaying Zhang schedule(); 2950744692dcSJiaying Zhang goto retry; 2951744692dcSJiaying Zhang } 2952744692dcSJiaying Zhang io_end->offset = offset; 2953744692dcSJiaying Zhang io_end->size = size; 2954744692dcSJiaying Zhang /* 2955744692dcSJiaying Zhang * We need to hold a reference to the page to make sure it 2956744692dcSJiaying Zhang * doesn't get evicted before ext4_end_io_work() has a chance 2957744692dcSJiaying Zhang * to convert the extent from written to unwritten. 2958744692dcSJiaying Zhang */ 2959744692dcSJiaying Zhang io_end->page = page; 2960744692dcSJiaying Zhang get_page(io_end->page); 2961744692dcSJiaying Zhang 2962744692dcSJiaying Zhang bh->b_private = io_end; 2963744692dcSJiaying Zhang bh->b_end_io = ext4_end_io_buffer_write; 2964744692dcSJiaying Zhang return 0; 2965744692dcSJiaying Zhang } 2966744692dcSJiaying Zhang 29674c0425ffSMingming Cao /* 29684c0425ffSMingming Cao * For ext4 extent files, ext4 will do direct-io write to holes, 29694c0425ffSMingming Cao * preallocated extents, and those write extend the file, no need to 29704c0425ffSMingming Cao * fall back to buffered IO. 29714c0425ffSMingming Cao * 2972b595076aSUwe Kleine-König * For holes, we fallocate those blocks, mark them as uninitialized 29734c0425ffSMingming Cao * If those blocks were preallocated, we mark sure they are splited, but 2974b595076aSUwe Kleine-König * still keep the range to write as uninitialized. 29754c0425ffSMingming Cao * 29768d5d02e6SMingming Cao * The unwrritten extents will be converted to written when DIO is completed. 29778d5d02e6SMingming Cao * For async direct IO, since the IO may still pending when return, we 297825985edcSLucas De Marchi * set up an end_io call back function, which will do the conversion 29798d5d02e6SMingming Cao * when async direct IO completed. 29804c0425ffSMingming Cao * 29814c0425ffSMingming Cao * If the O_DIRECT write will extend the file then add this inode to the 29824c0425ffSMingming Cao * orphan list. So recovery will truncate it back to the original size 29834c0425ffSMingming Cao * if the machine crashes during the write. 29844c0425ffSMingming Cao * 29854c0425ffSMingming Cao */ 29864c0425ffSMingming Cao static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb, 29874c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 29884c0425ffSMingming Cao unsigned long nr_segs) 29894c0425ffSMingming Cao { 29904c0425ffSMingming Cao struct file *file = iocb->ki_filp; 29914c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 29924c0425ffSMingming Cao ssize_t ret; 29934c0425ffSMingming Cao size_t count = iov_length(iov, nr_segs); 29944c0425ffSMingming Cao 29954c0425ffSMingming Cao loff_t final_size = offset + count; 29964c0425ffSMingming Cao if (rw == WRITE && final_size <= inode->i_size) { 2997729f52c6SZheng Liu int overwrite = 0; 2998729f52c6SZheng Liu 2999*4bd809dbSZheng Liu BUG_ON(iocb->private == NULL); 3000*4bd809dbSZheng Liu 3001*4bd809dbSZheng Liu /* If we do a overwrite dio, i_mutex locking can be released */ 3002*4bd809dbSZheng Liu overwrite = *((int *)iocb->private); 3003*4bd809dbSZheng Liu 3004*4bd809dbSZheng Liu if (overwrite) { 3005*4bd809dbSZheng Liu down_read(&EXT4_I(inode)->i_data_sem); 3006*4bd809dbSZheng Liu mutex_unlock(&inode->i_mutex); 3007*4bd809dbSZheng Liu } 3008*4bd809dbSZheng Liu 30094c0425ffSMingming Cao /* 30108d5d02e6SMingming Cao * We could direct write to holes and fallocate. 30118d5d02e6SMingming Cao * 30128d5d02e6SMingming Cao * Allocated blocks to fill the hole are marked as uninitialized 301325985edcSLucas De Marchi * to prevent parallel buffered read to expose the stale data 30144c0425ffSMingming Cao * before DIO complete the data IO. 30158d5d02e6SMingming Cao * 30168d5d02e6SMingming Cao * As to previously fallocated extents, ext4 get_block 30174c0425ffSMingming Cao * will just simply mark the buffer mapped but still 30184c0425ffSMingming Cao * keep the extents uninitialized. 30194c0425ffSMingming Cao * 30208d5d02e6SMingming Cao * for non AIO case, we will convert those unwritten extents 30218d5d02e6SMingming Cao * to written after return back from blockdev_direct_IO. 30224c0425ffSMingming Cao * 30238d5d02e6SMingming Cao * for async DIO, the conversion needs to be defered when 30248d5d02e6SMingming Cao * the IO is completed. The ext4 end_io callback function 30258d5d02e6SMingming Cao * will be called to take care of the conversion work. 30268d5d02e6SMingming Cao * Here for async case, we allocate an io_end structure to 30278d5d02e6SMingming Cao * hook to the iocb. 30284c0425ffSMingming Cao */ 30298d5d02e6SMingming Cao iocb->private = NULL; 30308d5d02e6SMingming Cao EXT4_I(inode)->cur_aio_dio = NULL; 30318d5d02e6SMingming Cao if (!is_sync_kiocb(iocb)) { 3032266991b1SJeff Moyer ext4_io_end_t *io_end = 3033266991b1SJeff Moyer ext4_init_io_end(inode, GFP_NOFS); 3034*4bd809dbSZheng Liu if (!io_end) { 3035*4bd809dbSZheng Liu ret = -ENOMEM; 3036*4bd809dbSZheng Liu goto retake_lock; 3037*4bd809dbSZheng Liu } 3038266991b1SJeff Moyer io_end->flag |= EXT4_IO_END_DIRECT; 3039266991b1SJeff Moyer iocb->private = io_end; 30408d5d02e6SMingming Cao /* 30418d5d02e6SMingming Cao * we save the io structure for current async 304279e83036SEric Sandeen * direct IO, so that later ext4_map_blocks() 30438d5d02e6SMingming Cao * could flag the io structure whether there 30448d5d02e6SMingming Cao * is a unwritten extents needs to be converted 30458d5d02e6SMingming Cao * when IO is completed. 30468d5d02e6SMingming Cao */ 30478d5d02e6SMingming Cao EXT4_I(inode)->cur_aio_dio = iocb->private; 30488d5d02e6SMingming Cao } 30498d5d02e6SMingming Cao 3050729f52c6SZheng Liu if (overwrite) 3051729f52c6SZheng Liu ret = __blockdev_direct_IO(rw, iocb, inode, 3052729f52c6SZheng Liu inode->i_sb->s_bdev, iov, 3053729f52c6SZheng Liu offset, nr_segs, 3054729f52c6SZheng Liu ext4_get_block_write_nolock, 3055729f52c6SZheng Liu ext4_end_io_dio, 3056729f52c6SZheng Liu NULL, 3057729f52c6SZheng Liu 0); 3058729f52c6SZheng Liu else 3059aacfc19cSChristoph Hellwig ret = __blockdev_direct_IO(rw, iocb, inode, 30604c0425ffSMingming Cao inode->i_sb->s_bdev, iov, 30614c0425ffSMingming Cao offset, nr_segs, 3062c7064ef1SJiaying Zhang ext4_get_block_write, 3063aacfc19cSChristoph Hellwig ext4_end_io_dio, 3064aacfc19cSChristoph Hellwig NULL, 306593ef8541SJeff Moyer DIO_LOCKING); 30668d5d02e6SMingming Cao if (iocb->private) 30678d5d02e6SMingming Cao EXT4_I(inode)->cur_aio_dio = NULL; 30688d5d02e6SMingming Cao /* 30698d5d02e6SMingming Cao * The io_end structure takes a reference to the inode, 30708d5d02e6SMingming Cao * that structure needs to be destroyed and the 30718d5d02e6SMingming Cao * reference to the inode need to be dropped, when IO is 30728d5d02e6SMingming Cao * complete, even with 0 byte write, or failed. 30738d5d02e6SMingming Cao * 30748d5d02e6SMingming Cao * In the successful AIO DIO case, the io_end structure will be 30758d5d02e6SMingming Cao * desctroyed and the reference to the inode will be dropped 30768d5d02e6SMingming Cao * after the end_io call back function is called. 30778d5d02e6SMingming Cao * 30788d5d02e6SMingming Cao * In the case there is 0 byte write, or error case, since 30798d5d02e6SMingming Cao * VFS direct IO won't invoke the end_io call back function, 30808d5d02e6SMingming Cao * we need to free the end_io structure here. 30818d5d02e6SMingming Cao */ 30828d5d02e6SMingming Cao if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) { 30838d5d02e6SMingming Cao ext4_free_io_end(iocb->private); 30848d5d02e6SMingming Cao iocb->private = NULL; 3085729f52c6SZheng Liu } else if (ret > 0 && !overwrite && ext4_test_inode_state(inode, 30865f524950SMingming EXT4_STATE_DIO_UNWRITTEN)) { 3087109f5565SMingming int err; 30888d5d02e6SMingming Cao /* 30898d5d02e6SMingming Cao * for non AIO case, since the IO is already 309025985edcSLucas De Marchi * completed, we could do the conversion right here 30918d5d02e6SMingming Cao */ 3092109f5565SMingming err = ext4_convert_unwritten_extents(inode, 30938d5d02e6SMingming Cao offset, ret); 3094109f5565SMingming if (err < 0) 3095109f5565SMingming ret = err; 309619f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN); 3097109f5565SMingming } 3098*4bd809dbSZheng Liu 3099*4bd809dbSZheng Liu retake_lock: 3100*4bd809dbSZheng Liu /* take i_mutex locking again if we do a ovewrite dio */ 3101*4bd809dbSZheng Liu if (overwrite) { 3102*4bd809dbSZheng Liu up_read(&EXT4_I(inode)->i_data_sem); 3103*4bd809dbSZheng Liu mutex_lock(&inode->i_mutex); 3104*4bd809dbSZheng Liu } 3105*4bd809dbSZheng Liu 31064c0425ffSMingming Cao return ret; 31074c0425ffSMingming Cao } 31088d5d02e6SMingming Cao 31098d5d02e6SMingming Cao /* for write the the end of file case, we fall back to old way */ 31104c0425ffSMingming Cao return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 31114c0425ffSMingming Cao } 31124c0425ffSMingming Cao 31134c0425ffSMingming Cao static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb, 31144c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 31154c0425ffSMingming Cao unsigned long nr_segs) 31164c0425ffSMingming Cao { 31174c0425ffSMingming Cao struct file *file = iocb->ki_filp; 31184c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 31190562e0baSJiaying Zhang ssize_t ret; 31204c0425ffSMingming Cao 312184ebd795STheodore Ts'o /* 312284ebd795STheodore Ts'o * If we are doing data journalling we don't support O_DIRECT 312384ebd795STheodore Ts'o */ 312484ebd795STheodore Ts'o if (ext4_should_journal_data(inode)) 312584ebd795STheodore Ts'o return 0; 312684ebd795STheodore Ts'o 31270562e0baSJiaying Zhang trace_ext4_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw); 312812e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 31290562e0baSJiaying Zhang ret = ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs); 31300562e0baSJiaying Zhang else 31310562e0baSJiaying Zhang ret = ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 31320562e0baSJiaying Zhang trace_ext4_direct_IO_exit(inode, offset, 31330562e0baSJiaying Zhang iov_length(iov, nr_segs), rw, ret); 31340562e0baSJiaying Zhang return ret; 31354c0425ffSMingming Cao } 31364c0425ffSMingming Cao 3137ac27a0ecSDave Kleikamp /* 3138617ba13bSMingming Cao * Pages can be marked dirty completely asynchronously from ext4's journalling 3139ac27a0ecSDave Kleikamp * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do 3140ac27a0ecSDave Kleikamp * much here because ->set_page_dirty is called under VFS locks. The page is 3141ac27a0ecSDave Kleikamp * not necessarily locked. 3142ac27a0ecSDave Kleikamp * 3143ac27a0ecSDave Kleikamp * We cannot just dirty the page and leave attached buffers clean, because the 3144ac27a0ecSDave Kleikamp * buffers' dirty state is "definitive". We cannot just set the buffers dirty 3145ac27a0ecSDave Kleikamp * or jbddirty because all the journalling code will explode. 3146ac27a0ecSDave Kleikamp * 3147ac27a0ecSDave Kleikamp * So what we do is to mark the page "pending dirty" and next time writepage 3148ac27a0ecSDave Kleikamp * is called, propagate that into the buffers appropriately. 3149ac27a0ecSDave Kleikamp */ 3150617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page) 3151ac27a0ecSDave Kleikamp { 3152ac27a0ecSDave Kleikamp SetPageChecked(page); 3153ac27a0ecSDave Kleikamp return __set_page_dirty_nobuffers(page); 3154ac27a0ecSDave Kleikamp } 3155ac27a0ecSDave Kleikamp 3156617ba13bSMingming Cao static const struct address_space_operations ext4_ordered_aops = { 3157617ba13bSMingming Cao .readpage = ext4_readpage, 3158617ba13bSMingming Cao .readpages = ext4_readpages, 315943ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3160bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3161bfc1af65SNick Piggin .write_end = ext4_ordered_write_end, 3162617ba13bSMingming Cao .bmap = ext4_bmap, 3163617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3164617ba13bSMingming Cao .releasepage = ext4_releasepage, 3165617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 3166ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 31678ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3168aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3169ac27a0ecSDave Kleikamp }; 3170ac27a0ecSDave Kleikamp 3171617ba13bSMingming Cao static const struct address_space_operations ext4_writeback_aops = { 3172617ba13bSMingming Cao .readpage = ext4_readpage, 3173617ba13bSMingming Cao .readpages = ext4_readpages, 317443ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3175bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3176bfc1af65SNick Piggin .write_end = ext4_writeback_write_end, 3177617ba13bSMingming Cao .bmap = ext4_bmap, 3178617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3179617ba13bSMingming Cao .releasepage = ext4_releasepage, 3180617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 3181ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 31828ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3183aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3184ac27a0ecSDave Kleikamp }; 3185ac27a0ecSDave Kleikamp 3186617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 3187617ba13bSMingming Cao .readpage = ext4_readpage, 3188617ba13bSMingming Cao .readpages = ext4_readpages, 318943ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3190bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3191bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 3192617ba13bSMingming Cao .set_page_dirty = ext4_journalled_set_page_dirty, 3193617ba13bSMingming Cao .bmap = ext4_bmap, 3194617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3195617ba13bSMingming Cao .releasepage = ext4_releasepage, 319684ebd795STheodore Ts'o .direct_IO = ext4_direct_IO, 31978ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3198aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3199ac27a0ecSDave Kleikamp }; 3200ac27a0ecSDave Kleikamp 320164769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 320264769240SAlex Tomas .readpage = ext4_readpage, 320364769240SAlex Tomas .readpages = ext4_readpages, 320443ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 320564769240SAlex Tomas .writepages = ext4_da_writepages, 320664769240SAlex Tomas .write_begin = ext4_da_write_begin, 320764769240SAlex Tomas .write_end = ext4_da_write_end, 320864769240SAlex Tomas .bmap = ext4_bmap, 320964769240SAlex Tomas .invalidatepage = ext4_da_invalidatepage, 321064769240SAlex Tomas .releasepage = ext4_releasepage, 321164769240SAlex Tomas .direct_IO = ext4_direct_IO, 321264769240SAlex Tomas .migratepage = buffer_migrate_page, 32138ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3214aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 321564769240SAlex Tomas }; 321664769240SAlex Tomas 3217617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 3218ac27a0ecSDave Kleikamp { 32193d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 32203d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 32213d2b1582SLukas Czerner if (test_opt(inode->i_sb, DELALLOC)) 3222cd1aac32SAneesh Kumar K.V inode->i_mapping->a_ops = &ext4_da_aops; 3223ac27a0ecSDave Kleikamp else 32243d2b1582SLukas Czerner inode->i_mapping->a_ops = &ext4_ordered_aops; 32253d2b1582SLukas Czerner break; 32263d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 32273d2b1582SLukas Czerner if (test_opt(inode->i_sb, DELALLOC)) 32283d2b1582SLukas Czerner inode->i_mapping->a_ops = &ext4_da_aops; 32293d2b1582SLukas Czerner else 32303d2b1582SLukas Czerner inode->i_mapping->a_ops = &ext4_writeback_aops; 32313d2b1582SLukas Czerner break; 32323d2b1582SLukas Czerner case EXT4_INODE_JOURNAL_DATA_MODE: 3233617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 32343d2b1582SLukas Czerner break; 32353d2b1582SLukas Czerner default: 32363d2b1582SLukas Czerner BUG(); 32373d2b1582SLukas Czerner } 3238ac27a0ecSDave Kleikamp } 3239ac27a0ecSDave Kleikamp 32404e96b2dbSAllison Henderson 32414e96b2dbSAllison Henderson /* 32424e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers() 32434e96b2dbSAllison Henderson * Wrapper function for ext4_discard_partial_page_buffers_no_lock. 32444e96b2dbSAllison Henderson * This function finds and locks the page containing the offset 32454e96b2dbSAllison Henderson * "from" and passes it to ext4_discard_partial_page_buffers_no_lock. 32464e96b2dbSAllison Henderson * Calling functions that already have the page locked should call 32474e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers_no_lock directly. 32484e96b2dbSAllison Henderson */ 32494e96b2dbSAllison Henderson int ext4_discard_partial_page_buffers(handle_t *handle, 32504e96b2dbSAllison Henderson struct address_space *mapping, loff_t from, 32514e96b2dbSAllison Henderson loff_t length, int flags) 32524e96b2dbSAllison Henderson { 32534e96b2dbSAllison Henderson struct inode *inode = mapping->host; 32544e96b2dbSAllison Henderson struct page *page; 32554e96b2dbSAllison Henderson int err = 0; 32564e96b2dbSAllison Henderson 32574e96b2dbSAllison Henderson page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT, 32584e96b2dbSAllison Henderson mapping_gfp_mask(mapping) & ~__GFP_FS); 32594e96b2dbSAllison Henderson if (!page) 32605129d05fSYongqiang Yang return -ENOMEM; 32614e96b2dbSAllison Henderson 32624e96b2dbSAllison Henderson err = ext4_discard_partial_page_buffers_no_lock(handle, inode, page, 32634e96b2dbSAllison Henderson from, length, flags); 32644e96b2dbSAllison Henderson 32654e96b2dbSAllison Henderson unlock_page(page); 32664e96b2dbSAllison Henderson page_cache_release(page); 32674e96b2dbSAllison Henderson return err; 32684e96b2dbSAllison Henderson } 32694e96b2dbSAllison Henderson 32704e96b2dbSAllison Henderson /* 32714e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers_no_lock() 32724e96b2dbSAllison Henderson * Zeros a page range of length 'length' starting from offset 'from'. 32734e96b2dbSAllison Henderson * Buffer heads that correspond to the block aligned regions of the 32744e96b2dbSAllison Henderson * zeroed range will be unmapped. Unblock aligned regions 32754e96b2dbSAllison Henderson * will have the corresponding buffer head mapped if needed so that 32764e96b2dbSAllison Henderson * that region of the page can be updated with the partial zero out. 32774e96b2dbSAllison Henderson * 32784e96b2dbSAllison Henderson * This function assumes that the page has already been locked. The 32794e96b2dbSAllison Henderson * The range to be discarded must be contained with in the given page. 32804e96b2dbSAllison Henderson * If the specified range exceeds the end of the page it will be shortened 32814e96b2dbSAllison Henderson * to the end of the page that corresponds to 'from'. This function is 32824e96b2dbSAllison Henderson * appropriate for updating a page and it buffer heads to be unmapped and 32834e96b2dbSAllison Henderson * zeroed for blocks that have been either released, or are going to be 32844e96b2dbSAllison Henderson * released. 32854e96b2dbSAllison Henderson * 32864e96b2dbSAllison Henderson * handle: The journal handle 32874e96b2dbSAllison Henderson * inode: The files inode 32884e96b2dbSAllison Henderson * page: A locked page that contains the offset "from" 32894e96b2dbSAllison Henderson * from: The starting byte offset (from the begining of the file) 32904e96b2dbSAllison Henderson * to begin discarding 32914e96b2dbSAllison Henderson * len: The length of bytes to discard 32924e96b2dbSAllison Henderson * flags: Optional flags that may be used: 32934e96b2dbSAllison Henderson * 32944e96b2dbSAllison Henderson * EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED 32954e96b2dbSAllison Henderson * Only zero the regions of the page whose buffer heads 32964e96b2dbSAllison Henderson * have already been unmapped. This flag is appropriate 32974e96b2dbSAllison Henderson * for updateing the contents of a page whose blocks may 32984e96b2dbSAllison Henderson * have already been released, and we only want to zero 32994e96b2dbSAllison Henderson * out the regions that correspond to those released blocks. 33004e96b2dbSAllison Henderson * 33014e96b2dbSAllison Henderson * Returns zero on sucess or negative on failure. 33024e96b2dbSAllison Henderson */ 33035f163cc7SEric Sandeen static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle, 33044e96b2dbSAllison Henderson struct inode *inode, struct page *page, loff_t from, 33054e96b2dbSAllison Henderson loff_t length, int flags) 33064e96b2dbSAllison Henderson { 33074e96b2dbSAllison Henderson ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; 33084e96b2dbSAllison Henderson unsigned int offset = from & (PAGE_CACHE_SIZE-1); 33094e96b2dbSAllison Henderson unsigned int blocksize, max, pos; 33104e96b2dbSAllison Henderson ext4_lblk_t iblock; 33114e96b2dbSAllison Henderson struct buffer_head *bh; 33124e96b2dbSAllison Henderson int err = 0; 33134e96b2dbSAllison Henderson 33144e96b2dbSAllison Henderson blocksize = inode->i_sb->s_blocksize; 33154e96b2dbSAllison Henderson max = PAGE_CACHE_SIZE - offset; 33164e96b2dbSAllison Henderson 33174e96b2dbSAllison Henderson if (index != page->index) 33184e96b2dbSAllison Henderson return -EINVAL; 33194e96b2dbSAllison Henderson 33204e96b2dbSAllison Henderson /* 33214e96b2dbSAllison Henderson * correct length if it does not fall between 33224e96b2dbSAllison Henderson * 'from' and the end of the page 33234e96b2dbSAllison Henderson */ 33244e96b2dbSAllison Henderson if (length > max || length < 0) 33254e96b2dbSAllison Henderson length = max; 33264e96b2dbSAllison Henderson 33274e96b2dbSAllison Henderson iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); 33284e96b2dbSAllison Henderson 3329093e6e36SYongqiang Yang if (!page_has_buffers(page)) 33304e96b2dbSAllison Henderson create_empty_buffers(page, blocksize, 0); 33314e96b2dbSAllison Henderson 33324e96b2dbSAllison Henderson /* Find the buffer that contains "offset" */ 33334e96b2dbSAllison Henderson bh = page_buffers(page); 33344e96b2dbSAllison Henderson pos = blocksize; 33354e96b2dbSAllison Henderson while (offset >= pos) { 33364e96b2dbSAllison Henderson bh = bh->b_this_page; 33374e96b2dbSAllison Henderson iblock++; 33384e96b2dbSAllison Henderson pos += blocksize; 33394e96b2dbSAllison Henderson } 33404e96b2dbSAllison Henderson 33414e96b2dbSAllison Henderson pos = offset; 33424e96b2dbSAllison Henderson while (pos < offset + length) { 3343e260daf2SYongqiang Yang unsigned int end_of_block, range_to_discard; 3344e260daf2SYongqiang Yang 33454e96b2dbSAllison Henderson err = 0; 33464e96b2dbSAllison Henderson 33474e96b2dbSAllison Henderson /* The length of space left to zero and unmap */ 33484e96b2dbSAllison Henderson range_to_discard = offset + length - pos; 33494e96b2dbSAllison Henderson 33504e96b2dbSAllison Henderson /* The length of space until the end of the block */ 33514e96b2dbSAllison Henderson end_of_block = blocksize - (pos & (blocksize-1)); 33524e96b2dbSAllison Henderson 33534e96b2dbSAllison Henderson /* 33544e96b2dbSAllison Henderson * Do not unmap or zero past end of block 33554e96b2dbSAllison Henderson * for this buffer head 33564e96b2dbSAllison Henderson */ 33574e96b2dbSAllison Henderson if (range_to_discard > end_of_block) 33584e96b2dbSAllison Henderson range_to_discard = end_of_block; 33594e96b2dbSAllison Henderson 33604e96b2dbSAllison Henderson 33614e96b2dbSAllison Henderson /* 33624e96b2dbSAllison Henderson * Skip this buffer head if we are only zeroing unampped 33634e96b2dbSAllison Henderson * regions of the page 33644e96b2dbSAllison Henderson */ 33654e96b2dbSAllison Henderson if (flags & EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED && 33664e96b2dbSAllison Henderson buffer_mapped(bh)) 33674e96b2dbSAllison Henderson goto next; 33684e96b2dbSAllison Henderson 33694e96b2dbSAllison Henderson /* If the range is block aligned, unmap */ 33704e96b2dbSAllison Henderson if (range_to_discard == blocksize) { 33714e96b2dbSAllison Henderson clear_buffer_dirty(bh); 33724e96b2dbSAllison Henderson bh->b_bdev = NULL; 33734e96b2dbSAllison Henderson clear_buffer_mapped(bh); 33744e96b2dbSAllison Henderson clear_buffer_req(bh); 33754e96b2dbSAllison Henderson clear_buffer_new(bh); 33764e96b2dbSAllison Henderson clear_buffer_delay(bh); 33774e96b2dbSAllison Henderson clear_buffer_unwritten(bh); 33784e96b2dbSAllison Henderson clear_buffer_uptodate(bh); 33794e96b2dbSAllison Henderson zero_user(page, pos, range_to_discard); 33804e96b2dbSAllison Henderson BUFFER_TRACE(bh, "Buffer discarded"); 33814e96b2dbSAllison Henderson goto next; 33824e96b2dbSAllison Henderson } 33834e96b2dbSAllison Henderson 33844e96b2dbSAllison Henderson /* 33854e96b2dbSAllison Henderson * If this block is not completely contained in the range 33864e96b2dbSAllison Henderson * to be discarded, then it is not going to be released. Because 33874e96b2dbSAllison Henderson * we need to keep this block, we need to make sure this part 33884e96b2dbSAllison Henderson * of the page is uptodate before we modify it by writeing 33894e96b2dbSAllison Henderson * partial zeros on it. 33904e96b2dbSAllison Henderson */ 33914e96b2dbSAllison Henderson if (!buffer_mapped(bh)) { 33924e96b2dbSAllison Henderson /* 33934e96b2dbSAllison Henderson * Buffer head must be mapped before we can read 33944e96b2dbSAllison Henderson * from the block 33954e96b2dbSAllison Henderson */ 33964e96b2dbSAllison Henderson BUFFER_TRACE(bh, "unmapped"); 33974e96b2dbSAllison Henderson ext4_get_block(inode, iblock, bh, 0); 33984e96b2dbSAllison Henderson /* unmapped? It's a hole - nothing to do */ 33994e96b2dbSAllison Henderson if (!buffer_mapped(bh)) { 34004e96b2dbSAllison Henderson BUFFER_TRACE(bh, "still unmapped"); 34014e96b2dbSAllison Henderson goto next; 34024e96b2dbSAllison Henderson } 34034e96b2dbSAllison Henderson } 34044e96b2dbSAllison Henderson 34054e96b2dbSAllison Henderson /* Ok, it's mapped. Make sure it's up-to-date */ 34064e96b2dbSAllison Henderson if (PageUptodate(page)) 34074e96b2dbSAllison Henderson set_buffer_uptodate(bh); 34084e96b2dbSAllison Henderson 34094e96b2dbSAllison Henderson if (!buffer_uptodate(bh)) { 34104e96b2dbSAllison Henderson err = -EIO; 34114e96b2dbSAllison Henderson ll_rw_block(READ, 1, &bh); 34124e96b2dbSAllison Henderson wait_on_buffer(bh); 34134e96b2dbSAllison Henderson /* Uhhuh. Read error. Complain and punt.*/ 34144e96b2dbSAllison Henderson if (!buffer_uptodate(bh)) 34154e96b2dbSAllison Henderson goto next; 34164e96b2dbSAllison Henderson } 34174e96b2dbSAllison Henderson 34184e96b2dbSAllison Henderson if (ext4_should_journal_data(inode)) { 34194e96b2dbSAllison Henderson BUFFER_TRACE(bh, "get write access"); 34204e96b2dbSAllison Henderson err = ext4_journal_get_write_access(handle, bh); 34214e96b2dbSAllison Henderson if (err) 34224e96b2dbSAllison Henderson goto next; 34234e96b2dbSAllison Henderson } 34244e96b2dbSAllison Henderson 34254e96b2dbSAllison Henderson zero_user(page, pos, range_to_discard); 34264e96b2dbSAllison Henderson 34274e96b2dbSAllison Henderson err = 0; 34284e96b2dbSAllison Henderson if (ext4_should_journal_data(inode)) { 34294e96b2dbSAllison Henderson err = ext4_handle_dirty_metadata(handle, inode, bh); 3430decbd919STheodore Ts'o } else 34314e96b2dbSAllison Henderson mark_buffer_dirty(bh); 34324e96b2dbSAllison Henderson 34334e96b2dbSAllison Henderson BUFFER_TRACE(bh, "Partial buffer zeroed"); 34344e96b2dbSAllison Henderson next: 34354e96b2dbSAllison Henderson bh = bh->b_this_page; 34364e96b2dbSAllison Henderson iblock++; 34374e96b2dbSAllison Henderson pos += range_to_discard; 34384e96b2dbSAllison Henderson } 34394e96b2dbSAllison Henderson 34404e96b2dbSAllison Henderson return err; 34414e96b2dbSAllison Henderson } 34424e96b2dbSAllison Henderson 344391ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 344491ef4cafSDuane Griffin { 344591ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 344691ef4cafSDuane Griffin return 1; 344791ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 344891ef4cafSDuane Griffin return 1; 344991ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 345091ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 345191ef4cafSDuane Griffin return 0; 345291ef4cafSDuane Griffin } 345391ef4cafSDuane Griffin 3454ac27a0ecSDave Kleikamp /* 3455a4bb6b64SAllison Henderson * ext4_punch_hole: punches a hole in a file by releaseing the blocks 3456a4bb6b64SAllison Henderson * associated with the given offset and length 3457a4bb6b64SAllison Henderson * 3458a4bb6b64SAllison Henderson * @inode: File inode 3459a4bb6b64SAllison Henderson * @offset: The offset where the hole will begin 3460a4bb6b64SAllison Henderson * @len: The length of the hole 3461a4bb6b64SAllison Henderson * 3462a4bb6b64SAllison Henderson * Returns: 0 on sucess or negative on failure 3463a4bb6b64SAllison Henderson */ 3464a4bb6b64SAllison Henderson 3465a4bb6b64SAllison Henderson int ext4_punch_hole(struct file *file, loff_t offset, loff_t length) 3466a4bb6b64SAllison Henderson { 3467a4bb6b64SAllison Henderson struct inode *inode = file->f_path.dentry->d_inode; 3468a4bb6b64SAllison Henderson if (!S_ISREG(inode->i_mode)) 346973355192SAllison Henderson return -EOPNOTSUPP; 3470a4bb6b64SAllison Henderson 3471a4bb6b64SAllison Henderson if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 3472a4bb6b64SAllison Henderson /* TODO: Add support for non extent hole punching */ 347373355192SAllison Henderson return -EOPNOTSUPP; 3474a4bb6b64SAllison Henderson } 3475a4bb6b64SAllison Henderson 3476bab08ab9STheodore Ts'o if (EXT4_SB(inode->i_sb)->s_cluster_ratio > 1) { 3477bab08ab9STheodore Ts'o /* TODO: Add support for bigalloc file systems */ 347873355192SAllison Henderson return -EOPNOTSUPP; 3479bab08ab9STheodore Ts'o } 3480bab08ab9STheodore Ts'o 3481a4bb6b64SAllison Henderson return ext4_ext_punch_hole(file, offset, length); 3482a4bb6b64SAllison Henderson } 3483a4bb6b64SAllison Henderson 3484a4bb6b64SAllison Henderson /* 3485617ba13bSMingming Cao * ext4_truncate() 3486ac27a0ecSDave Kleikamp * 3487617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 3488617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 3489ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 3490ac27a0ecSDave Kleikamp * 349142b2aa86SJustin P. Mattock * As we work through the truncate and commit bits of it to the journal there 3492ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 3493ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 3494ac27a0ecSDave Kleikamp * 3495ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 3496ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 3497ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 3498ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 3499ac27a0ecSDave Kleikamp * left-to-right works OK too). 3500ac27a0ecSDave Kleikamp * 3501ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 3502ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 3503ac27a0ecSDave Kleikamp * 3504ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 3505617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 3506ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 3507617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 3508617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 3509ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 3510617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 3511ac27a0ecSDave Kleikamp */ 3512617ba13bSMingming Cao void ext4_truncate(struct inode *inode) 3513ac27a0ecSDave Kleikamp { 35140562e0baSJiaying Zhang trace_ext4_truncate_enter(inode); 35150562e0baSJiaying Zhang 351691ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 3517ac27a0ecSDave Kleikamp return; 3518ac27a0ecSDave Kleikamp 351912e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS); 3520c8d46e41SJiaying Zhang 35215534fb5bSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 352219f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE); 35237d8f9f7dSTheodore Ts'o 3524ff9893dcSAmir Goldstein if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3525cf108bcaSJan Kara ext4_ext_truncate(inode); 3526ff9893dcSAmir Goldstein else 3527ff9893dcSAmir Goldstein ext4_ind_truncate(inode); 3528a86c6181SAlex Tomas 35290562e0baSJiaying Zhang trace_ext4_truncate_exit(inode); 3530ac27a0ecSDave Kleikamp } 3531ac27a0ecSDave Kleikamp 3532ac27a0ecSDave Kleikamp /* 3533617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 3534ac27a0ecSDave Kleikamp * underlying buffer_head on success. If 'in_mem' is true, we have all 3535ac27a0ecSDave Kleikamp * data in memory that is needed to recreate the on-disk version of this 3536ac27a0ecSDave Kleikamp * inode. 3537ac27a0ecSDave Kleikamp */ 3538617ba13bSMingming Cao static int __ext4_get_inode_loc(struct inode *inode, 3539617ba13bSMingming Cao struct ext4_iloc *iloc, int in_mem) 3540ac27a0ecSDave Kleikamp { 3541240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 3542ac27a0ecSDave Kleikamp struct buffer_head *bh; 3543240799cdSTheodore Ts'o struct super_block *sb = inode->i_sb; 3544240799cdSTheodore Ts'o ext4_fsblk_t block; 3545240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 3546ac27a0ecSDave Kleikamp 35473a06d778SAneesh Kumar K.V iloc->bh = NULL; 3548240799cdSTheodore Ts'o if (!ext4_valid_inum(sb, inode->i_ino)) 3549ac27a0ecSDave Kleikamp return -EIO; 3550ac27a0ecSDave Kleikamp 3551240799cdSTheodore Ts'o iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb); 3552240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 3553240799cdSTheodore Ts'o if (!gdp) 3554240799cdSTheodore Ts'o return -EIO; 3555240799cdSTheodore Ts'o 3556240799cdSTheodore Ts'o /* 3557240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 3558240799cdSTheodore Ts'o */ 355900d09882STao Ma inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 3560240799cdSTheodore Ts'o inode_offset = ((inode->i_ino - 1) % 3561240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 3562240799cdSTheodore Ts'o block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); 3563240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 3564240799cdSTheodore Ts'o 3565240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 3566ac27a0ecSDave Kleikamp if (!bh) { 3567c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, block, 3568c398eda0STheodore Ts'o "unable to read itable block"); 3569ac27a0ecSDave Kleikamp return -EIO; 3570ac27a0ecSDave Kleikamp } 3571ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 3572ac27a0ecSDave Kleikamp lock_buffer(bh); 35739c83a923SHidehiro Kawai 35749c83a923SHidehiro Kawai /* 35759c83a923SHidehiro Kawai * If the buffer has the write error flag, we have failed 35769c83a923SHidehiro Kawai * to write out another inode in the same block. In this 35779c83a923SHidehiro Kawai * case, we don't have to read the block because we may 35789c83a923SHidehiro Kawai * read the old inode data successfully. 35799c83a923SHidehiro Kawai */ 35809c83a923SHidehiro Kawai if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) 35819c83a923SHidehiro Kawai set_buffer_uptodate(bh); 35829c83a923SHidehiro Kawai 3583ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) { 3584ac27a0ecSDave Kleikamp /* someone brought it uptodate while we waited */ 3585ac27a0ecSDave Kleikamp unlock_buffer(bh); 3586ac27a0ecSDave Kleikamp goto has_buffer; 3587ac27a0ecSDave Kleikamp } 3588ac27a0ecSDave Kleikamp 3589ac27a0ecSDave Kleikamp /* 3590ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 3591ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 3592ac27a0ecSDave Kleikamp * block. 3593ac27a0ecSDave Kleikamp */ 3594ac27a0ecSDave Kleikamp if (in_mem) { 3595ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 3596240799cdSTheodore Ts'o int i, start; 3597ac27a0ecSDave Kleikamp 3598240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 3599ac27a0ecSDave Kleikamp 3600ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 3601240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 3602ac27a0ecSDave Kleikamp if (!bitmap_bh) 3603ac27a0ecSDave Kleikamp goto make_io; 3604ac27a0ecSDave Kleikamp 3605ac27a0ecSDave Kleikamp /* 3606ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 3607ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 3608ac27a0ecSDave Kleikamp * of one, so skip it. 3609ac27a0ecSDave Kleikamp */ 3610ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 3611ac27a0ecSDave Kleikamp brelse(bitmap_bh); 3612ac27a0ecSDave Kleikamp goto make_io; 3613ac27a0ecSDave Kleikamp } 3614240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 3615ac27a0ecSDave Kleikamp if (i == inode_offset) 3616ac27a0ecSDave Kleikamp continue; 3617617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 3618ac27a0ecSDave Kleikamp break; 3619ac27a0ecSDave Kleikamp } 3620ac27a0ecSDave Kleikamp brelse(bitmap_bh); 3621240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 3622ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 3623ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 3624ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 3625ac27a0ecSDave Kleikamp unlock_buffer(bh); 3626ac27a0ecSDave Kleikamp goto has_buffer; 3627ac27a0ecSDave Kleikamp } 3628ac27a0ecSDave Kleikamp } 3629ac27a0ecSDave Kleikamp 3630ac27a0ecSDave Kleikamp make_io: 3631ac27a0ecSDave Kleikamp /* 3632240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 3633240799cdSTheodore Ts'o * blocks from the inode table. 3634240799cdSTheodore Ts'o */ 3635240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 3636240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 3637240799cdSTheodore Ts'o unsigned num; 3638240799cdSTheodore Ts'o 3639240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 3640b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 3641240799cdSTheodore Ts'o b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1); 3642240799cdSTheodore Ts'o if (table > b) 3643240799cdSTheodore Ts'o b = table; 3644240799cdSTheodore Ts'o end = b + EXT4_SB(sb)->s_inode_readahead_blks; 3645240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 3646feb0ab32SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) 3647560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 3648240799cdSTheodore Ts'o table += num / inodes_per_block; 3649240799cdSTheodore Ts'o if (end > table) 3650240799cdSTheodore Ts'o end = table; 3651240799cdSTheodore Ts'o while (b <= end) 3652240799cdSTheodore Ts'o sb_breadahead(sb, b++); 3653240799cdSTheodore Ts'o } 3654240799cdSTheodore Ts'o 3655240799cdSTheodore Ts'o /* 3656ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 3657ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 3658ac27a0ecSDave Kleikamp * Read the block from disk. 3659ac27a0ecSDave Kleikamp */ 36600562e0baSJiaying Zhang trace_ext4_load_inode(inode); 3661ac27a0ecSDave Kleikamp get_bh(bh); 3662ac27a0ecSDave Kleikamp bh->b_end_io = end_buffer_read_sync; 366365299a3bSChristoph Hellwig submit_bh(READ | REQ_META | REQ_PRIO, bh); 3664ac27a0ecSDave Kleikamp wait_on_buffer(bh); 3665ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 3666c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, block, 3667c398eda0STheodore Ts'o "unable to read itable block"); 3668ac27a0ecSDave Kleikamp brelse(bh); 3669ac27a0ecSDave Kleikamp return -EIO; 3670ac27a0ecSDave Kleikamp } 3671ac27a0ecSDave Kleikamp } 3672ac27a0ecSDave Kleikamp has_buffer: 3673ac27a0ecSDave Kleikamp iloc->bh = bh; 3674ac27a0ecSDave Kleikamp return 0; 3675ac27a0ecSDave Kleikamp } 3676ac27a0ecSDave Kleikamp 3677617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 3678ac27a0ecSDave Kleikamp { 3679ac27a0ecSDave Kleikamp /* We have all inode data except xattrs in memory here. */ 3680617ba13bSMingming Cao return __ext4_get_inode_loc(inode, iloc, 368119f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_XATTR)); 3682ac27a0ecSDave Kleikamp } 3683ac27a0ecSDave Kleikamp 3684617ba13bSMingming Cao void ext4_set_inode_flags(struct inode *inode) 3685ac27a0ecSDave Kleikamp { 3686617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 3687ac27a0ecSDave Kleikamp 3688ac27a0ecSDave Kleikamp inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); 3689617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 3690ac27a0ecSDave Kleikamp inode->i_flags |= S_SYNC; 3691617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 3692ac27a0ecSDave Kleikamp inode->i_flags |= S_APPEND; 3693617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 3694ac27a0ecSDave Kleikamp inode->i_flags |= S_IMMUTABLE; 3695617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 3696ac27a0ecSDave Kleikamp inode->i_flags |= S_NOATIME; 3697617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 3698ac27a0ecSDave Kleikamp inode->i_flags |= S_DIRSYNC; 3699ac27a0ecSDave Kleikamp } 3700ac27a0ecSDave Kleikamp 3701ff9ddf7eSJan Kara /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */ 3702ff9ddf7eSJan Kara void ext4_get_inode_flags(struct ext4_inode_info *ei) 3703ff9ddf7eSJan Kara { 370484a8dce2SDmitry Monakhov unsigned int vfs_fl; 370584a8dce2SDmitry Monakhov unsigned long old_fl, new_fl; 3706ff9ddf7eSJan Kara 370784a8dce2SDmitry Monakhov do { 370884a8dce2SDmitry Monakhov vfs_fl = ei->vfs_inode.i_flags; 370984a8dce2SDmitry Monakhov old_fl = ei->i_flags; 371084a8dce2SDmitry Monakhov new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL| 371184a8dce2SDmitry Monakhov EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL| 371284a8dce2SDmitry Monakhov EXT4_DIRSYNC_FL); 371384a8dce2SDmitry Monakhov if (vfs_fl & S_SYNC) 371484a8dce2SDmitry Monakhov new_fl |= EXT4_SYNC_FL; 371584a8dce2SDmitry Monakhov if (vfs_fl & S_APPEND) 371684a8dce2SDmitry Monakhov new_fl |= EXT4_APPEND_FL; 371784a8dce2SDmitry Monakhov if (vfs_fl & S_IMMUTABLE) 371884a8dce2SDmitry Monakhov new_fl |= EXT4_IMMUTABLE_FL; 371984a8dce2SDmitry Monakhov if (vfs_fl & S_NOATIME) 372084a8dce2SDmitry Monakhov new_fl |= EXT4_NOATIME_FL; 372184a8dce2SDmitry Monakhov if (vfs_fl & S_DIRSYNC) 372284a8dce2SDmitry Monakhov new_fl |= EXT4_DIRSYNC_FL; 372384a8dce2SDmitry Monakhov } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl); 3724ff9ddf7eSJan Kara } 3725de9a55b8STheodore Ts'o 37260fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 37270fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 37280fc1b451SAneesh Kumar K.V { 37290fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 37308180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 37318180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 37320fc1b451SAneesh Kumar K.V 37330fc1b451SAneesh Kumar K.V if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 37340fc1b451SAneesh Kumar K.V EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) { 37350fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 37360fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 37370fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 373807a03824STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) { 37398180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 37408180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 37418180a562SAneesh Kumar K.V } else { 37420fc1b451SAneesh Kumar K.V return i_blocks; 37438180a562SAneesh Kumar K.V } 37440fc1b451SAneesh Kumar K.V } else { 37450fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 37460fc1b451SAneesh Kumar K.V } 37470fc1b451SAneesh Kumar K.V } 3748ff9ddf7eSJan Kara 37491d1fe1eeSDavid Howells struct inode *ext4_iget(struct super_block *sb, unsigned long ino) 3750ac27a0ecSDave Kleikamp { 3751617ba13bSMingming Cao struct ext4_iloc iloc; 3752617ba13bSMingming Cao struct ext4_inode *raw_inode; 37531d1fe1eeSDavid Howells struct ext4_inode_info *ei; 37541d1fe1eeSDavid Howells struct inode *inode; 3755b436b9beSJan Kara journal_t *journal = EXT4_SB(sb)->s_journal; 37561d1fe1eeSDavid Howells long ret; 3757ac27a0ecSDave Kleikamp int block; 375808cefc7aSEric W. Biederman uid_t i_uid; 375908cefc7aSEric W. Biederman gid_t i_gid; 3760ac27a0ecSDave Kleikamp 37611d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 37621d1fe1eeSDavid Howells if (!inode) 37631d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 37641d1fe1eeSDavid Howells if (!(inode->i_state & I_NEW)) 37651d1fe1eeSDavid Howells return inode; 37661d1fe1eeSDavid Howells 37671d1fe1eeSDavid Howells ei = EXT4_I(inode); 37687dc57615SPeter Huewe iloc.bh = NULL; 3769ac27a0ecSDave Kleikamp 37701d1fe1eeSDavid Howells ret = __ext4_get_inode_loc(inode, &iloc, 0); 37711d1fe1eeSDavid Howells if (ret < 0) 3772ac27a0ecSDave Kleikamp goto bad_inode; 3773617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 3774814525f4SDarrick J. Wong 3775814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 3776814525f4SDarrick J. Wong ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 3777814525f4SDarrick J. Wong if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 3778814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)) { 3779814525f4SDarrick J. Wong EXT4_ERROR_INODE(inode, "bad extra_isize (%u != %u)", 3780814525f4SDarrick J. Wong EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize, 3781814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)); 3782814525f4SDarrick J. Wong ret = -EIO; 3783814525f4SDarrick J. Wong goto bad_inode; 3784814525f4SDarrick J. Wong } 3785814525f4SDarrick J. Wong } else 3786814525f4SDarrick J. Wong ei->i_extra_isize = 0; 3787814525f4SDarrick J. Wong 3788814525f4SDarrick J. Wong /* Precompute checksum seed for inode metadata */ 3789814525f4SDarrick J. Wong if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 3790814525f4SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) { 3791814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3792814525f4SDarrick J. Wong __u32 csum; 3793814525f4SDarrick J. Wong __le32 inum = cpu_to_le32(inode->i_ino); 3794814525f4SDarrick J. Wong __le32 gen = raw_inode->i_generation; 3795814525f4SDarrick J. Wong csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, 3796814525f4SDarrick J. Wong sizeof(inum)); 3797814525f4SDarrick J. Wong ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, 3798814525f4SDarrick J. Wong sizeof(gen)); 3799814525f4SDarrick J. Wong } 3800814525f4SDarrick J. Wong 3801814525f4SDarrick J. Wong if (!ext4_inode_csum_verify(inode, raw_inode, ei)) { 3802814525f4SDarrick J. Wong EXT4_ERROR_INODE(inode, "checksum invalid"); 3803814525f4SDarrick J. Wong ret = -EIO; 3804814525f4SDarrick J. Wong goto bad_inode; 3805814525f4SDarrick J. Wong } 3806814525f4SDarrick J. Wong 3807ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 380808cefc7aSEric W. Biederman i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 380908cefc7aSEric W. Biederman i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 3810ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 381108cefc7aSEric W. Biederman i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 381208cefc7aSEric W. Biederman i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 3813ac27a0ecSDave Kleikamp } 381408cefc7aSEric W. Biederman i_uid_write(inode, i_uid); 381508cefc7aSEric W. Biederman i_gid_write(inode, i_gid); 3816bfe86848SMiklos Szeredi set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); 3817ac27a0ecSDave Kleikamp 3818353eb83cSTheodore Ts'o ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 3819ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 3820ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 3821ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 3822ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 3823ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 3824ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 3825ac27a0ecSDave Kleikamp */ 3826ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 3827ac27a0ecSDave Kleikamp if (inode->i_mode == 0 || 3828617ba13bSMingming Cao !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) { 3829ac27a0ecSDave Kleikamp /* this inode is deleted */ 38301d1fe1eeSDavid Howells ret = -ESTALE; 3831ac27a0ecSDave Kleikamp goto bad_inode; 3832ac27a0ecSDave Kleikamp } 3833ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 3834ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 3835ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 3836ac27a0ecSDave Kleikamp * the process of deleting those. */ 3837ac27a0ecSDave Kleikamp } 3838ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 38390fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 38407973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 3841a9e81742STheodore Ts'o if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) 3842a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 3843a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 3844a48380f7SAneesh Kumar K.V inode->i_size = ext4_isize(raw_inode); 3845ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 3846a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 3847a9e7f447SDmitry Monakhov ei->i_reserved_quota = 0; 3848a9e7f447SDmitry Monakhov #endif 3849ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 3850ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 3851a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 3852ac27a0ecSDave Kleikamp /* 3853ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 3854ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 3855ac27a0ecSDave Kleikamp */ 3856617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 3857ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 3858ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 3859ac27a0ecSDave Kleikamp 3860b436b9beSJan Kara /* 3861b436b9beSJan Kara * Set transaction id's of transactions that have to be committed 3862b436b9beSJan Kara * to finish f[data]sync. We set them to currently running transaction 3863b436b9beSJan Kara * as we cannot be sure that the inode or some of its metadata isn't 3864b436b9beSJan Kara * part of the transaction - the inode could have been reclaimed and 3865b436b9beSJan Kara * now it is reread from disk. 3866b436b9beSJan Kara */ 3867b436b9beSJan Kara if (journal) { 3868b436b9beSJan Kara transaction_t *transaction; 3869b436b9beSJan Kara tid_t tid; 3870b436b9beSJan Kara 3871a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 3872b436b9beSJan Kara if (journal->j_running_transaction) 3873b436b9beSJan Kara transaction = journal->j_running_transaction; 3874b436b9beSJan Kara else 3875b436b9beSJan Kara transaction = journal->j_committing_transaction; 3876b436b9beSJan Kara if (transaction) 3877b436b9beSJan Kara tid = transaction->t_tid; 3878b436b9beSJan Kara else 3879b436b9beSJan Kara tid = journal->j_commit_sequence; 3880a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 3881b436b9beSJan Kara ei->i_sync_tid = tid; 3882b436b9beSJan Kara ei->i_datasync_tid = tid; 3883b436b9beSJan Kara } 3884b436b9beSJan Kara 38850040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 3886ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 3887ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 3888617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 3889617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 3890ac27a0ecSDave Kleikamp } else { 3891ac27a0ecSDave Kleikamp __le32 *magic = (void *)raw_inode + 3892617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE + 3893ac27a0ecSDave Kleikamp ei->i_extra_isize; 3894617ba13bSMingming Cao if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) 389519f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_XATTR); 3896ac27a0ecSDave Kleikamp } 3897814525f4SDarrick J. Wong } 3898ac27a0ecSDave Kleikamp 3899ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 3900ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 3901ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 3902ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 3903ef7f3835SKalpak Shah 390425ec56b5SJean Noel Cordenner inode->i_version = le32_to_cpu(raw_inode->i_disk_version); 390525ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 390625ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 390725ec56b5SJean Noel Cordenner inode->i_version |= 390825ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 390925ec56b5SJean Noel Cordenner } 391025ec56b5SJean Noel Cordenner 3911c4b5a614STheodore Ts'o ret = 0; 3912485c26ecSTheodore Ts'o if (ei->i_file_acl && 39131032988cSTheodore Ts'o !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) { 391424676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "bad extended attribute block %llu", 391524676da4STheodore Ts'o ei->i_file_acl); 3916485c26ecSTheodore Ts'o ret = -EIO; 3917485c26ecSTheodore Ts'o goto bad_inode; 391807a03824STheodore Ts'o } else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 3919c4b5a614STheodore Ts'o if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 3920c4b5a614STheodore Ts'o (S_ISLNK(inode->i_mode) && 3921c4b5a614STheodore Ts'o !ext4_inode_is_fast_symlink(inode))) 39227a262f7cSAneesh Kumar K.V /* Validate extent which is part of inode */ 39237a262f7cSAneesh Kumar K.V ret = ext4_ext_check_inode(inode); 3924fe2c8191SThiemo Nagel } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 3925fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 3926fe2c8191SThiemo Nagel !ext4_inode_is_fast_symlink(inode))) { 3927fe2c8191SThiemo Nagel /* Validate block references which are part of inode */ 39281f7d1e77STheodore Ts'o ret = ext4_ind_check_inode(inode); 3929fe2c8191SThiemo Nagel } 3930567f3e9aSTheodore Ts'o if (ret) 39317a262f7cSAneesh Kumar K.V goto bad_inode; 39327a262f7cSAneesh Kumar K.V 3933ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 3934617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 3935617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 3936617ba13bSMingming Cao ext4_set_aops(inode); 3937ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 3938617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 3939617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 3940ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 3941e83c1397SDuane Griffin if (ext4_inode_is_fast_symlink(inode)) { 3942617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 3943e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 3944e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 3945e83c1397SDuane Griffin } else { 3946617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 3947617ba13bSMingming Cao ext4_set_aops(inode); 3948ac27a0ecSDave Kleikamp } 3949563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 3950563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 3951617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 3952ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 3953ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 3954ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 3955ac27a0ecSDave Kleikamp else 3956ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 3957ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 3958563bdd61STheodore Ts'o } else { 3959563bdd61STheodore Ts'o ret = -EIO; 396024676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode); 3961563bdd61STheodore Ts'o goto bad_inode; 3962ac27a0ecSDave Kleikamp } 3963ac27a0ecSDave Kleikamp brelse(iloc.bh); 3964617ba13bSMingming Cao ext4_set_inode_flags(inode); 39651d1fe1eeSDavid Howells unlock_new_inode(inode); 39661d1fe1eeSDavid Howells return inode; 3967ac27a0ecSDave Kleikamp 3968ac27a0ecSDave Kleikamp bad_inode: 3969567f3e9aSTheodore Ts'o brelse(iloc.bh); 39701d1fe1eeSDavid Howells iget_failed(inode); 39711d1fe1eeSDavid Howells return ERR_PTR(ret); 3972ac27a0ecSDave Kleikamp } 3973ac27a0ecSDave Kleikamp 39740fc1b451SAneesh Kumar K.V static int ext4_inode_blocks_set(handle_t *handle, 39750fc1b451SAneesh Kumar K.V struct ext4_inode *raw_inode, 39760fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 39770fc1b451SAneesh Kumar K.V { 39780fc1b451SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 39790fc1b451SAneesh Kumar K.V u64 i_blocks = inode->i_blocks; 39800fc1b451SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 39810fc1b451SAneesh Kumar K.V 39820fc1b451SAneesh Kumar K.V if (i_blocks <= ~0U) { 39830fc1b451SAneesh Kumar K.V /* 39840fc1b451SAneesh Kumar K.V * i_blocks can be represnted in a 32 bit variable 39850fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 39860fc1b451SAneesh Kumar K.V */ 39878180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 39880fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = 0; 398984a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 3990f287a1a5STheodore Ts'o return 0; 3991f287a1a5STheodore Ts'o } 3992f287a1a5STheodore Ts'o if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) 3993f287a1a5STheodore Ts'o return -EFBIG; 3994f287a1a5STheodore Ts'o 3995f287a1a5STheodore Ts'o if (i_blocks <= 0xffffffffffffULL) { 39960fc1b451SAneesh Kumar K.V /* 39970fc1b451SAneesh Kumar K.V * i_blocks can be represented in a 48 bit variable 39980fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 39990fc1b451SAneesh Kumar K.V */ 40008180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 40010fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 400284a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 40030fc1b451SAneesh Kumar K.V } else { 400484a8dce2SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE); 40058180a562SAneesh Kumar K.V /* i_block is stored in file system block size */ 40068180a562SAneesh Kumar K.V i_blocks = i_blocks >> (inode->i_blkbits - 9); 40078180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 40088180a562SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 40090fc1b451SAneesh Kumar K.V } 4010f287a1a5STheodore Ts'o return 0; 40110fc1b451SAneesh Kumar K.V } 40120fc1b451SAneesh Kumar K.V 4013ac27a0ecSDave Kleikamp /* 4014ac27a0ecSDave Kleikamp * Post the struct inode info into an on-disk inode location in the 4015ac27a0ecSDave Kleikamp * buffer-cache. This gobbles the caller's reference to the 4016ac27a0ecSDave Kleikamp * buffer_head in the inode location struct. 4017ac27a0ecSDave Kleikamp * 4018ac27a0ecSDave Kleikamp * The caller must have write access to iloc->bh. 4019ac27a0ecSDave Kleikamp */ 4020617ba13bSMingming Cao static int ext4_do_update_inode(handle_t *handle, 4021ac27a0ecSDave Kleikamp struct inode *inode, 4022830156c7SFrank Mayhar struct ext4_iloc *iloc) 4023ac27a0ecSDave Kleikamp { 4024617ba13bSMingming Cao struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 4025617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 4026ac27a0ecSDave Kleikamp struct buffer_head *bh = iloc->bh; 4027ac27a0ecSDave Kleikamp int err = 0, rc, block; 402808cefc7aSEric W. Biederman uid_t i_uid; 402908cefc7aSEric W. Biederman gid_t i_gid; 4030ac27a0ecSDave Kleikamp 4031ac27a0ecSDave Kleikamp /* For fields not not tracking in the in-memory inode, 4032ac27a0ecSDave Kleikamp * initialise them to zero for new inodes. */ 403319f5fb7aSTheodore Ts'o if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) 4034617ba13bSMingming Cao memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 4035ac27a0ecSDave Kleikamp 4036ff9ddf7eSJan Kara ext4_get_inode_flags(ei); 4037ac27a0ecSDave Kleikamp raw_inode->i_mode = cpu_to_le16(inode->i_mode); 403808cefc7aSEric W. Biederman i_uid = i_uid_read(inode); 403908cefc7aSEric W. Biederman i_gid = i_gid_read(inode); 4040ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 404108cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid)); 404208cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid)); 4043ac27a0ecSDave Kleikamp /* 4044ac27a0ecSDave Kleikamp * Fix up interoperability with old kernels. Otherwise, old inodes get 4045ac27a0ecSDave Kleikamp * re-used with the upper 16 bits of the uid/gid intact 4046ac27a0ecSDave Kleikamp */ 4047ac27a0ecSDave Kleikamp if (!ei->i_dtime) { 4048ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 404908cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_uid)); 4050ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 405108cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_gid)); 4052ac27a0ecSDave Kleikamp } else { 4053ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4054ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4055ac27a0ecSDave Kleikamp } 4056ac27a0ecSDave Kleikamp } else { 405708cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid)); 405808cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid)); 4059ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4060ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4061ac27a0ecSDave Kleikamp } 4062ac27a0ecSDave Kleikamp raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 4063ef7f3835SKalpak Shah 4064ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 4065ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 4066ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 4067ef7f3835SKalpak Shah EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 4068ef7f3835SKalpak Shah 40690fc1b451SAneesh Kumar K.V if (ext4_inode_blocks_set(handle, raw_inode, ei)) 40700fc1b451SAneesh Kumar K.V goto out_brelse; 4071ac27a0ecSDave Kleikamp raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 4072353eb83cSTheodore Ts'o raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF); 40739b8f1f01SMingming Cao if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 40749b8f1f01SMingming Cao cpu_to_le32(EXT4_OS_HURD)) 4075a1ddeb7eSBadari Pulavarty raw_inode->i_file_acl_high = 4076a1ddeb7eSBadari Pulavarty cpu_to_le16(ei->i_file_acl >> 32); 40777973c0c1SAneesh Kumar K.V raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 4078a48380f7SAneesh Kumar K.V ext4_isize_set(raw_inode, ei->i_disksize); 4079ac27a0ecSDave Kleikamp if (ei->i_disksize > 0x7fffffffULL) { 4080ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 4081617ba13bSMingming Cao if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, 4082617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE) || 4083617ba13bSMingming Cao EXT4_SB(sb)->s_es->s_rev_level == 4084617ba13bSMingming Cao cpu_to_le32(EXT4_GOOD_OLD_REV)) { 4085ac27a0ecSDave Kleikamp /* If this is the first large file 4086ac27a0ecSDave Kleikamp * created, add a flag to the superblock. 4087ac27a0ecSDave Kleikamp */ 4088617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, 4089617ba13bSMingming Cao EXT4_SB(sb)->s_sbh); 4090ac27a0ecSDave Kleikamp if (err) 4091ac27a0ecSDave Kleikamp goto out_brelse; 4092617ba13bSMingming Cao ext4_update_dynamic_rev(sb); 4093617ba13bSMingming Cao EXT4_SET_RO_COMPAT_FEATURE(sb, 4094617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE); 40950390131bSFrank Mayhar ext4_handle_sync(handle); 4096a9c47317SDarrick J. Wong err = ext4_handle_dirty_super_now(handle, sb); 4097ac27a0ecSDave Kleikamp } 4098ac27a0ecSDave Kleikamp } 4099ac27a0ecSDave Kleikamp raw_inode->i_generation = cpu_to_le32(inode->i_generation); 4100ac27a0ecSDave Kleikamp if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 4101ac27a0ecSDave Kleikamp if (old_valid_dev(inode->i_rdev)) { 4102ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 4103ac27a0ecSDave Kleikamp cpu_to_le32(old_encode_dev(inode->i_rdev)); 4104ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 0; 4105ac27a0ecSDave Kleikamp } else { 4106ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 0; 4107ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 4108ac27a0ecSDave Kleikamp cpu_to_le32(new_encode_dev(inode->i_rdev)); 4109ac27a0ecSDave Kleikamp raw_inode->i_block[2] = 0; 4110ac27a0ecSDave Kleikamp } 4111de9a55b8STheodore Ts'o } else 4112de9a55b8STheodore Ts'o for (block = 0; block < EXT4_N_BLOCKS; block++) 4113ac27a0ecSDave Kleikamp raw_inode->i_block[block] = ei->i_data[block]; 4114ac27a0ecSDave Kleikamp 411525ec56b5SJean Noel Cordenner raw_inode->i_disk_version = cpu_to_le32(inode->i_version); 411625ec56b5SJean Noel Cordenner if (ei->i_extra_isize) { 411725ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 411825ec56b5SJean Noel Cordenner raw_inode->i_version_hi = 411925ec56b5SJean Noel Cordenner cpu_to_le32(inode->i_version >> 32); 4120ac27a0ecSDave Kleikamp raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize); 412125ec56b5SJean Noel Cordenner } 412225ec56b5SJean Noel Cordenner 4123814525f4SDarrick J. Wong ext4_inode_csum_set(inode, raw_inode, ei); 4124814525f4SDarrick J. Wong 41250390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 412673b50c1cSCurt Wohlgemuth rc = ext4_handle_dirty_metadata(handle, NULL, bh); 4127ac27a0ecSDave Kleikamp if (!err) 4128ac27a0ecSDave Kleikamp err = rc; 412919f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_NEW); 4130ac27a0ecSDave Kleikamp 4131b436b9beSJan Kara ext4_update_inode_fsync_trans(handle, inode, 0); 4132ac27a0ecSDave Kleikamp out_brelse: 4133ac27a0ecSDave Kleikamp brelse(bh); 4134617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4135ac27a0ecSDave Kleikamp return err; 4136ac27a0ecSDave Kleikamp } 4137ac27a0ecSDave Kleikamp 4138ac27a0ecSDave Kleikamp /* 4139617ba13bSMingming Cao * ext4_write_inode() 4140ac27a0ecSDave Kleikamp * 4141ac27a0ecSDave Kleikamp * We are called from a few places: 4142ac27a0ecSDave Kleikamp * 4143ac27a0ecSDave Kleikamp * - Within generic_file_write() for O_SYNC files. 4144ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 4145ac27a0ecSDave Kleikamp * trasnaction to commit. 4146ac27a0ecSDave Kleikamp * 4147ac27a0ecSDave Kleikamp * - Within sys_sync(), kupdate and such. 4148ac27a0ecSDave Kleikamp * We wait on commit, if tol to. 4149ac27a0ecSDave Kleikamp * 4150ac27a0ecSDave Kleikamp * - Within prune_icache() (PF_MEMALLOC == true) 4151ac27a0ecSDave Kleikamp * Here we simply return. We can't afford to block kswapd on the 4152ac27a0ecSDave Kleikamp * journal commit. 4153ac27a0ecSDave Kleikamp * 4154ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 4155ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 4156617ba13bSMingming Cao * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for 4157ac27a0ecSDave Kleikamp * knfsd. 4158ac27a0ecSDave Kleikamp * 4159ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 4160ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 4161ac27a0ecSDave Kleikamp * which we are interested. 4162ac27a0ecSDave Kleikamp * 4163ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 4164ac27a0ecSDave Kleikamp * 4165ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 4166ac27a0ecSDave Kleikamp * stuff(); 4167ac27a0ecSDave Kleikamp * inode->i_size = expr; 4168ac27a0ecSDave Kleikamp * 4169ac27a0ecSDave Kleikamp * is in error because a kswapd-driven write_inode() could occur while 4170ac27a0ecSDave Kleikamp * `stuff()' is running, and the new i_size will be lost. Plus the inode 4171ac27a0ecSDave Kleikamp * will no longer be on the superblock's dirty inode list. 4172ac27a0ecSDave Kleikamp */ 4173a9185b41SChristoph Hellwig int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) 4174ac27a0ecSDave Kleikamp { 417591ac6f43SFrank Mayhar int err; 417691ac6f43SFrank Mayhar 4177ac27a0ecSDave Kleikamp if (current->flags & PF_MEMALLOC) 4178ac27a0ecSDave Kleikamp return 0; 4179ac27a0ecSDave Kleikamp 418091ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal) { 4181617ba13bSMingming Cao if (ext4_journal_current_handle()) { 4182b38bd33aSMingming Cao jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); 4183ac27a0ecSDave Kleikamp dump_stack(); 4184ac27a0ecSDave Kleikamp return -EIO; 4185ac27a0ecSDave Kleikamp } 4186ac27a0ecSDave Kleikamp 4187a9185b41SChristoph Hellwig if (wbc->sync_mode != WB_SYNC_ALL) 4188ac27a0ecSDave Kleikamp return 0; 4189ac27a0ecSDave Kleikamp 419091ac6f43SFrank Mayhar err = ext4_force_commit(inode->i_sb); 419191ac6f43SFrank Mayhar } else { 419291ac6f43SFrank Mayhar struct ext4_iloc iloc; 419391ac6f43SFrank Mayhar 41948b472d73SCurt Wohlgemuth err = __ext4_get_inode_loc(inode, &iloc, 0); 419591ac6f43SFrank Mayhar if (err) 419691ac6f43SFrank Mayhar return err; 4197a9185b41SChristoph Hellwig if (wbc->sync_mode == WB_SYNC_ALL) 4198830156c7SFrank Mayhar sync_dirty_buffer(iloc.bh); 4199830156c7SFrank Mayhar if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { 4200c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr, 4201c398eda0STheodore Ts'o "IO error syncing inode"); 4202830156c7SFrank Mayhar err = -EIO; 4203830156c7SFrank Mayhar } 4204fd2dd9fbSCurt Wohlgemuth brelse(iloc.bh); 420591ac6f43SFrank Mayhar } 420691ac6f43SFrank Mayhar return err; 4207ac27a0ecSDave Kleikamp } 4208ac27a0ecSDave Kleikamp 4209ac27a0ecSDave Kleikamp /* 4210617ba13bSMingming Cao * ext4_setattr() 4211ac27a0ecSDave Kleikamp * 4212ac27a0ecSDave Kleikamp * Called from notify_change. 4213ac27a0ecSDave Kleikamp * 4214ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 4215ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 4216ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 4217ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 4218ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 4219ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 4220ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 4221ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 4222ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 4223ac27a0ecSDave Kleikamp * 4224678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 4225678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 4226678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 4227678aaf48SJan Kara * This way we are sure that all the data written in the previous 4228678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 4229678aaf48SJan Kara * writeback). 4230678aaf48SJan Kara * 4231678aaf48SJan Kara * Called with inode->i_mutex down. 4232ac27a0ecSDave Kleikamp */ 4233617ba13bSMingming Cao int ext4_setattr(struct dentry *dentry, struct iattr *attr) 4234ac27a0ecSDave Kleikamp { 4235ac27a0ecSDave Kleikamp struct inode *inode = dentry->d_inode; 4236ac27a0ecSDave Kleikamp int error, rc = 0; 42373d287de3SDmitry Monakhov int orphan = 0; 4238ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 4239ac27a0ecSDave Kleikamp 4240ac27a0ecSDave Kleikamp error = inode_change_ok(inode, attr); 4241ac27a0ecSDave Kleikamp if (error) 4242ac27a0ecSDave Kleikamp return error; 4243ac27a0ecSDave Kleikamp 424412755627SDmitry Monakhov if (is_quota_modification(inode, attr)) 4245871a2931SChristoph Hellwig dquot_initialize(inode); 424608cefc7aSEric W. Biederman if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) || 424708cefc7aSEric W. Biederman (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) { 4248ac27a0ecSDave Kleikamp handle_t *handle; 4249ac27a0ecSDave Kleikamp 4250ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 4251ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 42525aca07ebSDmitry Monakhov handle = ext4_journal_start(inode, (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb)+ 4253194074acSDmitry Monakhov EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb))+3); 4254ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 4255ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 4256ac27a0ecSDave Kleikamp goto err_out; 4257ac27a0ecSDave Kleikamp } 4258b43fa828SChristoph Hellwig error = dquot_transfer(inode, attr); 4259ac27a0ecSDave Kleikamp if (error) { 4260617ba13bSMingming Cao ext4_journal_stop(handle); 4261ac27a0ecSDave Kleikamp return error; 4262ac27a0ecSDave Kleikamp } 4263ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 4264ac27a0ecSDave Kleikamp * one transaction */ 4265ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_UID) 4266ac27a0ecSDave Kleikamp inode->i_uid = attr->ia_uid; 4267ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_GID) 4268ac27a0ecSDave Kleikamp inode->i_gid = attr->ia_gid; 4269617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 4270617ba13bSMingming Cao ext4_journal_stop(handle); 4271ac27a0ecSDave Kleikamp } 4272ac27a0ecSDave Kleikamp 4273e2b46574SEric Sandeen if (attr->ia_valid & ATTR_SIZE) { 4274562c72aaSChristoph Hellwig inode_dio_wait(inode); 4275562c72aaSChristoph Hellwig 427612e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 4277e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4278e2b46574SEric Sandeen 42790c095c7fSTheodore Ts'o if (attr->ia_size > sbi->s_bitmap_maxbytes) 42800c095c7fSTheodore Ts'o return -EFBIG; 4281e2b46574SEric Sandeen } 4282e2b46574SEric Sandeen } 4283e2b46574SEric Sandeen 4284ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode) && 4285c8d46e41SJiaying Zhang attr->ia_valid & ATTR_SIZE && 4286072bd7eaSTheodore Ts'o (attr->ia_size < inode->i_size)) { 4287ac27a0ecSDave Kleikamp handle_t *handle; 4288ac27a0ecSDave Kleikamp 4289617ba13bSMingming Cao handle = ext4_journal_start(inode, 3); 4290ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 4291ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 4292ac27a0ecSDave Kleikamp goto err_out; 4293ac27a0ecSDave Kleikamp } 42943d287de3SDmitry Monakhov if (ext4_handle_valid(handle)) { 4295617ba13bSMingming Cao error = ext4_orphan_add(handle, inode); 42963d287de3SDmitry Monakhov orphan = 1; 42973d287de3SDmitry Monakhov } 4298617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 4299617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 4300ac27a0ecSDave Kleikamp if (!error) 4301ac27a0ecSDave Kleikamp error = rc; 4302617ba13bSMingming Cao ext4_journal_stop(handle); 4303678aaf48SJan Kara 4304678aaf48SJan Kara if (ext4_should_order_data(inode)) { 4305678aaf48SJan Kara error = ext4_begin_ordered_truncate(inode, 4306678aaf48SJan Kara attr->ia_size); 4307678aaf48SJan Kara if (error) { 4308678aaf48SJan Kara /* Do as much error cleanup as possible */ 4309678aaf48SJan Kara handle = ext4_journal_start(inode, 3); 4310678aaf48SJan Kara if (IS_ERR(handle)) { 4311678aaf48SJan Kara ext4_orphan_del(NULL, inode); 4312678aaf48SJan Kara goto err_out; 4313678aaf48SJan Kara } 4314678aaf48SJan Kara ext4_orphan_del(handle, inode); 43153d287de3SDmitry Monakhov orphan = 0; 4316678aaf48SJan Kara ext4_journal_stop(handle); 4317678aaf48SJan Kara goto err_out; 4318678aaf48SJan Kara } 4319678aaf48SJan Kara } 4320ac27a0ecSDave Kleikamp } 4321ac27a0ecSDave Kleikamp 4322072bd7eaSTheodore Ts'o if (attr->ia_valid & ATTR_SIZE) { 4323afcff5d8SLukas Czerner if (attr->ia_size != i_size_read(inode)) 4324072bd7eaSTheodore Ts'o truncate_setsize(inode, attr->ia_size); 4325072bd7eaSTheodore Ts'o ext4_truncate(inode); 4326072bd7eaSTheodore Ts'o } 4327ac27a0ecSDave Kleikamp 43281025774cSChristoph Hellwig if (!rc) { 43291025774cSChristoph Hellwig setattr_copy(inode, attr); 43301025774cSChristoph Hellwig mark_inode_dirty(inode); 43311025774cSChristoph Hellwig } 43321025774cSChristoph Hellwig 43331025774cSChristoph Hellwig /* 43341025774cSChristoph Hellwig * If the call to ext4_truncate failed to get a transaction handle at 43351025774cSChristoph Hellwig * all, we need to clean up the in-core orphan list manually. 43361025774cSChristoph Hellwig */ 43373d287de3SDmitry Monakhov if (orphan && inode->i_nlink) 4338617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 4339ac27a0ecSDave Kleikamp 4340ac27a0ecSDave Kleikamp if (!rc && (ia_valid & ATTR_MODE)) 4341617ba13bSMingming Cao rc = ext4_acl_chmod(inode); 4342ac27a0ecSDave Kleikamp 4343ac27a0ecSDave Kleikamp err_out: 4344617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 4345ac27a0ecSDave Kleikamp if (!error) 4346ac27a0ecSDave Kleikamp error = rc; 4347ac27a0ecSDave Kleikamp return error; 4348ac27a0ecSDave Kleikamp } 4349ac27a0ecSDave Kleikamp 43503e3398a0SMingming Cao int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry, 43513e3398a0SMingming Cao struct kstat *stat) 43523e3398a0SMingming Cao { 43533e3398a0SMingming Cao struct inode *inode; 43543e3398a0SMingming Cao unsigned long delalloc_blocks; 43553e3398a0SMingming Cao 43563e3398a0SMingming Cao inode = dentry->d_inode; 43573e3398a0SMingming Cao generic_fillattr(inode, stat); 43583e3398a0SMingming Cao 43593e3398a0SMingming Cao /* 43603e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 43613e3398a0SMingming Cao * otherwise in the case of system crash before the real block 43623e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 43633e3398a0SMingming Cao * on-disk file blocks. 43643e3398a0SMingming Cao * We always keep i_blocks updated together with real 43653e3398a0SMingming Cao * allocation. But to not confuse with user, stat 43663e3398a0SMingming Cao * will return the blocks that include the delayed allocation 43673e3398a0SMingming Cao * blocks for this file. 43683e3398a0SMingming Cao */ 436996607551STao Ma delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb), 437096607551STao Ma EXT4_I(inode)->i_reserved_data_blocks); 43713e3398a0SMingming Cao 43723e3398a0SMingming Cao stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9; 43733e3398a0SMingming Cao return 0; 43743e3398a0SMingming Cao } 4375ac27a0ecSDave Kleikamp 4376a02908f1SMingming Cao static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4377a02908f1SMingming Cao { 437812e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 43798bb2b247SAmir Goldstein return ext4_ind_trans_blocks(inode, nrblocks, chunk); 4380ac51d837STheodore Ts'o return ext4_ext_index_trans_blocks(inode, nrblocks, chunk); 4381a02908f1SMingming Cao } 4382ac51d837STheodore Ts'o 4383a02908f1SMingming Cao /* 4384a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 4385a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 4386a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 4387a02908f1SMingming Cao * 4388a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 4389af901ca1SAndré Goddard Rosa * different block groups too. If they are contiuguous, with flexbg, 4390a02908f1SMingming Cao * they could still across block group boundary. 4391a02908f1SMingming Cao * 4392a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 4393a02908f1SMingming Cao */ 43941f109d5aSTheodore Ts'o static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4395a02908f1SMingming Cao { 43968df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 43978df9675fSTheodore Ts'o int gdpblocks; 4398a02908f1SMingming Cao int idxblocks; 4399a02908f1SMingming Cao int ret = 0; 4400a02908f1SMingming Cao 4401a02908f1SMingming Cao /* 4402a02908f1SMingming Cao * How many index blocks need to touch to modify nrblocks? 4403a02908f1SMingming Cao * The "Chunk" flag indicating whether the nrblocks is 4404a02908f1SMingming Cao * physically contiguous on disk 4405a02908f1SMingming Cao * 4406a02908f1SMingming Cao * For Direct IO and fallocate, they calls get_block to allocate 4407a02908f1SMingming Cao * one single extent at a time, so they could set the "Chunk" flag 4408a02908f1SMingming Cao */ 4409a02908f1SMingming Cao idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk); 4410a02908f1SMingming Cao 4411a02908f1SMingming Cao ret = idxblocks; 4412a02908f1SMingming Cao 4413a02908f1SMingming Cao /* 4414a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 4415a02908f1SMingming Cao * to account 4416a02908f1SMingming Cao */ 4417a02908f1SMingming Cao groups = idxblocks; 4418a02908f1SMingming Cao if (chunk) 4419a02908f1SMingming Cao groups += 1; 4420ac27a0ecSDave Kleikamp else 4421a02908f1SMingming Cao groups += nrblocks; 4422ac27a0ecSDave Kleikamp 4423a02908f1SMingming Cao gdpblocks = groups; 44248df9675fSTheodore Ts'o if (groups > ngroups) 44258df9675fSTheodore Ts'o groups = ngroups; 4426a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 4427a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 4428a02908f1SMingming Cao 4429a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 4430a02908f1SMingming Cao ret += groups + gdpblocks; 4431a02908f1SMingming Cao 4432a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 4433a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 4434ac27a0ecSDave Kleikamp 4435ac27a0ecSDave Kleikamp return ret; 4436ac27a0ecSDave Kleikamp } 4437ac27a0ecSDave Kleikamp 4438ac27a0ecSDave Kleikamp /* 443925985edcSLucas De Marchi * Calculate the total number of credits to reserve to fit 4440f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 4441f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 4442a02908f1SMingming Cao * 4443525f4ed8SMingming Cao * This could be called via ext4_write_begin() 4444a02908f1SMingming Cao * 4445525f4ed8SMingming Cao * We need to consider the worse case, when 4446a02908f1SMingming Cao * one new block per extent. 4447a02908f1SMingming Cao */ 4448a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 4449a02908f1SMingming Cao { 4450a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 4451a02908f1SMingming Cao int ret; 4452a02908f1SMingming Cao 4453a02908f1SMingming Cao ret = ext4_meta_trans_blocks(inode, bpp, 0); 4454a02908f1SMingming Cao 4455a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 4456a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 4457a02908f1SMingming Cao ret += bpp; 4458a02908f1SMingming Cao return ret; 4459a02908f1SMingming Cao } 4460f3bd1f3fSMingming Cao 4461f3bd1f3fSMingming Cao /* 4462f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 4463f3bd1f3fSMingming Cao * 4464f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 446579e83036SEric Sandeen * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks. 4466f3bd1f3fSMingming Cao * 4467f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 4468f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 4469f3bd1f3fSMingming Cao */ 4470f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 4471f3bd1f3fSMingming Cao { 4472f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 4473f3bd1f3fSMingming Cao } 4474f3bd1f3fSMingming Cao 4475a02908f1SMingming Cao /* 4476617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 4477ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 4478ac27a0ecSDave Kleikamp */ 4479617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 4480617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 4481ac27a0ecSDave Kleikamp { 4482ac27a0ecSDave Kleikamp int err = 0; 4483ac27a0ecSDave Kleikamp 4484c64db50eSTheodore Ts'o if (IS_I_VERSION(inode)) 448525ec56b5SJean Noel Cordenner inode_inc_iversion(inode); 448625ec56b5SJean Noel Cordenner 4487ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 4488ac27a0ecSDave Kleikamp get_bh(iloc->bh); 4489ac27a0ecSDave Kleikamp 4490dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 4491830156c7SFrank Mayhar err = ext4_do_update_inode(handle, inode, iloc); 4492ac27a0ecSDave Kleikamp put_bh(iloc->bh); 4493ac27a0ecSDave Kleikamp return err; 4494ac27a0ecSDave Kleikamp } 4495ac27a0ecSDave Kleikamp 4496ac27a0ecSDave Kleikamp /* 4497ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 4498ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 4499ac27a0ecSDave Kleikamp */ 4500ac27a0ecSDave Kleikamp 4501ac27a0ecSDave Kleikamp int 4502617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 4503617ba13bSMingming Cao struct ext4_iloc *iloc) 4504ac27a0ecSDave Kleikamp { 45050390131bSFrank Mayhar int err; 45060390131bSFrank Mayhar 4507617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 4508ac27a0ecSDave Kleikamp if (!err) { 4509ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 4510617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, iloc->bh); 4511ac27a0ecSDave Kleikamp if (err) { 4512ac27a0ecSDave Kleikamp brelse(iloc->bh); 4513ac27a0ecSDave Kleikamp iloc->bh = NULL; 4514ac27a0ecSDave Kleikamp } 4515ac27a0ecSDave Kleikamp } 4516617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4517ac27a0ecSDave Kleikamp return err; 4518ac27a0ecSDave Kleikamp } 4519ac27a0ecSDave Kleikamp 4520ac27a0ecSDave Kleikamp /* 45216dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 45226dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 45236dd4ee7cSKalpak Shah */ 45241d03ec98SAneesh Kumar K.V static int ext4_expand_extra_isize(struct inode *inode, 45251d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 45261d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 45271d03ec98SAneesh Kumar K.V handle_t *handle) 45286dd4ee7cSKalpak Shah { 45296dd4ee7cSKalpak Shah struct ext4_inode *raw_inode; 45306dd4ee7cSKalpak Shah struct ext4_xattr_ibody_header *header; 45316dd4ee7cSKalpak Shah 45326dd4ee7cSKalpak Shah if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) 45336dd4ee7cSKalpak Shah return 0; 45346dd4ee7cSKalpak Shah 45356dd4ee7cSKalpak Shah raw_inode = ext4_raw_inode(&iloc); 45366dd4ee7cSKalpak Shah 45376dd4ee7cSKalpak Shah header = IHDR(inode, raw_inode); 45386dd4ee7cSKalpak Shah 45396dd4ee7cSKalpak Shah /* No extended attributes present */ 454019f5fb7aSTheodore Ts'o if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) || 45416dd4ee7cSKalpak Shah header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 45426dd4ee7cSKalpak Shah memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0, 45436dd4ee7cSKalpak Shah new_extra_isize); 45446dd4ee7cSKalpak Shah EXT4_I(inode)->i_extra_isize = new_extra_isize; 45456dd4ee7cSKalpak Shah return 0; 45466dd4ee7cSKalpak Shah } 45476dd4ee7cSKalpak Shah 45486dd4ee7cSKalpak Shah /* try to expand with EAs present */ 45496dd4ee7cSKalpak Shah return ext4_expand_extra_isize_ea(inode, new_extra_isize, 45506dd4ee7cSKalpak Shah raw_inode, handle); 45516dd4ee7cSKalpak Shah } 45526dd4ee7cSKalpak Shah 45536dd4ee7cSKalpak Shah /* 4554ac27a0ecSDave Kleikamp * What we do here is to mark the in-core inode as clean with respect to inode 4555ac27a0ecSDave Kleikamp * dirtiness (it may still be data-dirty). 4556ac27a0ecSDave Kleikamp * This means that the in-core inode may be reaped by prune_icache 4557ac27a0ecSDave Kleikamp * without having to perform any I/O. This is a very good thing, 4558ac27a0ecSDave Kleikamp * because *any* task may call prune_icache - even ones which 4559ac27a0ecSDave Kleikamp * have a transaction open against a different journal. 4560ac27a0ecSDave Kleikamp * 4561ac27a0ecSDave Kleikamp * Is this cheating? Not really. Sure, we haven't written the 4562ac27a0ecSDave Kleikamp * inode out, but prune_icache isn't a user-visible syncing function. 4563ac27a0ecSDave Kleikamp * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 4564ac27a0ecSDave Kleikamp * we start and wait on commits. 4565ac27a0ecSDave Kleikamp * 4566ac27a0ecSDave Kleikamp * Is this efficient/effective? Well, we're being nice to the system 4567ac27a0ecSDave Kleikamp * by cleaning up our inodes proactively so they can be reaped 4568ac27a0ecSDave Kleikamp * without I/O. But we are potentially leaving up to five seconds' 4569ac27a0ecSDave Kleikamp * worth of inodes floating about which prune_icache wants us to 4570ac27a0ecSDave Kleikamp * write out. One way to fix that would be to get prune_icache() 4571ac27a0ecSDave Kleikamp * to do a write_super() to free up some memory. It has the desired 4572ac27a0ecSDave Kleikamp * effect. 4573ac27a0ecSDave Kleikamp */ 4574617ba13bSMingming Cao int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode) 4575ac27a0ecSDave Kleikamp { 4576617ba13bSMingming Cao struct ext4_iloc iloc; 45776dd4ee7cSKalpak Shah struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 45786dd4ee7cSKalpak Shah static unsigned int mnt_count; 45796dd4ee7cSKalpak Shah int err, ret; 4580ac27a0ecSDave Kleikamp 4581ac27a0ecSDave Kleikamp might_sleep(); 45827ff9c073STheodore Ts'o trace_ext4_mark_inode_dirty(inode, _RET_IP_); 4583617ba13bSMingming Cao err = ext4_reserve_inode_write(handle, inode, &iloc); 45840390131bSFrank Mayhar if (ext4_handle_valid(handle) && 45850390131bSFrank Mayhar EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize && 458619f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) { 45876dd4ee7cSKalpak Shah /* 45886dd4ee7cSKalpak Shah * We need extra buffer credits since we may write into EA block 45896dd4ee7cSKalpak Shah * with this same handle. If journal_extend fails, then it will 45906dd4ee7cSKalpak Shah * only result in a minor loss of functionality for that inode. 45916dd4ee7cSKalpak Shah * If this is felt to be critical, then e2fsck should be run to 45926dd4ee7cSKalpak Shah * force a large enough s_min_extra_isize. 45936dd4ee7cSKalpak Shah */ 45946dd4ee7cSKalpak Shah if ((jbd2_journal_extend(handle, 45956dd4ee7cSKalpak Shah EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) { 45966dd4ee7cSKalpak Shah ret = ext4_expand_extra_isize(inode, 45976dd4ee7cSKalpak Shah sbi->s_want_extra_isize, 45986dd4ee7cSKalpak Shah iloc, handle); 45996dd4ee7cSKalpak Shah if (ret) { 460019f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, 460119f5fb7aSTheodore Ts'o EXT4_STATE_NO_EXPAND); 4602c1bddad9SAneesh Kumar K.V if (mnt_count != 4603c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count)) { 460412062dddSEric Sandeen ext4_warning(inode->i_sb, 46056dd4ee7cSKalpak Shah "Unable to expand inode %lu. Delete" 46066dd4ee7cSKalpak Shah " some EAs or run e2fsck.", 46076dd4ee7cSKalpak Shah inode->i_ino); 4608c1bddad9SAneesh Kumar K.V mnt_count = 4609c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count); 46106dd4ee7cSKalpak Shah } 46116dd4ee7cSKalpak Shah } 46126dd4ee7cSKalpak Shah } 46136dd4ee7cSKalpak Shah } 4614ac27a0ecSDave Kleikamp if (!err) 4615617ba13bSMingming Cao err = ext4_mark_iloc_dirty(handle, inode, &iloc); 4616ac27a0ecSDave Kleikamp return err; 4617ac27a0ecSDave Kleikamp } 4618ac27a0ecSDave Kleikamp 4619ac27a0ecSDave Kleikamp /* 4620617ba13bSMingming Cao * ext4_dirty_inode() is called from __mark_inode_dirty() 4621ac27a0ecSDave Kleikamp * 4622ac27a0ecSDave Kleikamp * We're really interested in the case where a file is being extended. 4623ac27a0ecSDave Kleikamp * i_size has been changed by generic_commit_write() and we thus need 4624ac27a0ecSDave Kleikamp * to include the updated inode in the current transaction. 4625ac27a0ecSDave Kleikamp * 46265dd4056dSChristoph Hellwig * Also, dquot_alloc_block() will always dirty the inode when blocks 4627ac27a0ecSDave Kleikamp * are allocated to the file. 4628ac27a0ecSDave Kleikamp * 4629ac27a0ecSDave Kleikamp * If the inode is marked synchronous, we don't honour that here - doing 4630ac27a0ecSDave Kleikamp * so would cause a commit on atime updates, which we don't bother doing. 4631ac27a0ecSDave Kleikamp * We handle synchronous inodes at the highest possible level. 4632ac27a0ecSDave Kleikamp */ 4633aa385729SChristoph Hellwig void ext4_dirty_inode(struct inode *inode, int flags) 4634ac27a0ecSDave Kleikamp { 4635ac27a0ecSDave Kleikamp handle_t *handle; 4636ac27a0ecSDave Kleikamp 4637617ba13bSMingming Cao handle = ext4_journal_start(inode, 2); 4638ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 4639ac27a0ecSDave Kleikamp goto out; 4640f3dc272fSCurt Wohlgemuth 4641617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 4642f3dc272fSCurt Wohlgemuth 4643617ba13bSMingming Cao ext4_journal_stop(handle); 4644ac27a0ecSDave Kleikamp out: 4645ac27a0ecSDave Kleikamp return; 4646ac27a0ecSDave Kleikamp } 4647ac27a0ecSDave Kleikamp 4648ac27a0ecSDave Kleikamp #if 0 4649ac27a0ecSDave Kleikamp /* 4650ac27a0ecSDave Kleikamp * Bind an inode's backing buffer_head into this transaction, to prevent 4651ac27a0ecSDave Kleikamp * it from being flushed to disk early. Unlike 4652617ba13bSMingming Cao * ext4_reserve_inode_write, this leaves behind no bh reference and 4653ac27a0ecSDave Kleikamp * returns no iloc structure, so the caller needs to repeat the iloc 4654ac27a0ecSDave Kleikamp * lookup to mark the inode dirty later. 4655ac27a0ecSDave Kleikamp */ 4656617ba13bSMingming Cao static int ext4_pin_inode(handle_t *handle, struct inode *inode) 4657ac27a0ecSDave Kleikamp { 4658617ba13bSMingming Cao struct ext4_iloc iloc; 4659ac27a0ecSDave Kleikamp 4660ac27a0ecSDave Kleikamp int err = 0; 4661ac27a0ecSDave Kleikamp if (handle) { 4662617ba13bSMingming Cao err = ext4_get_inode_loc(inode, &iloc); 4663ac27a0ecSDave Kleikamp if (!err) { 4664ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc.bh, "get_write_access"); 4665dab291afSMingming Cao err = jbd2_journal_get_write_access(handle, iloc.bh); 4666ac27a0ecSDave Kleikamp if (!err) 46670390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, 466873b50c1cSCurt Wohlgemuth NULL, 4669ac27a0ecSDave Kleikamp iloc.bh); 4670ac27a0ecSDave Kleikamp brelse(iloc.bh); 4671ac27a0ecSDave Kleikamp } 4672ac27a0ecSDave Kleikamp } 4673617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4674ac27a0ecSDave Kleikamp return err; 4675ac27a0ecSDave Kleikamp } 4676ac27a0ecSDave Kleikamp #endif 4677ac27a0ecSDave Kleikamp 4678617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val) 4679ac27a0ecSDave Kleikamp { 4680ac27a0ecSDave Kleikamp journal_t *journal; 4681ac27a0ecSDave Kleikamp handle_t *handle; 4682ac27a0ecSDave Kleikamp int err; 4683ac27a0ecSDave Kleikamp 4684ac27a0ecSDave Kleikamp /* 4685ac27a0ecSDave Kleikamp * We have to be very careful here: changing a data block's 4686ac27a0ecSDave Kleikamp * journaling status dynamically is dangerous. If we write a 4687ac27a0ecSDave Kleikamp * data block to the journal, change the status and then delete 4688ac27a0ecSDave Kleikamp * that block, we risk forgetting to revoke the old log record 4689ac27a0ecSDave Kleikamp * from the journal and so a subsequent replay can corrupt data. 4690ac27a0ecSDave Kleikamp * So, first we make sure that the journal is empty and that 4691ac27a0ecSDave Kleikamp * nobody is changing anything. 4692ac27a0ecSDave Kleikamp */ 4693ac27a0ecSDave Kleikamp 4694617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 46950390131bSFrank Mayhar if (!journal) 46960390131bSFrank Mayhar return 0; 4697d699594dSDave Hansen if (is_journal_aborted(journal)) 4698ac27a0ecSDave Kleikamp return -EROFS; 46992aff57b0SYongqiang Yang /* We have to allocate physical blocks for delalloc blocks 47002aff57b0SYongqiang Yang * before flushing journal. otherwise delalloc blocks can not 47012aff57b0SYongqiang Yang * be allocated any more. even more truncate on delalloc blocks 47022aff57b0SYongqiang Yang * could trigger BUG by flushing delalloc blocks in journal. 47032aff57b0SYongqiang Yang * There is no delalloc block in non-journal data mode. 47042aff57b0SYongqiang Yang */ 47052aff57b0SYongqiang Yang if (val && test_opt(inode->i_sb, DELALLOC)) { 47062aff57b0SYongqiang Yang err = ext4_alloc_da_blocks(inode); 47072aff57b0SYongqiang Yang if (err < 0) 47082aff57b0SYongqiang Yang return err; 47092aff57b0SYongqiang Yang } 4710ac27a0ecSDave Kleikamp 4711dab291afSMingming Cao jbd2_journal_lock_updates(journal); 4712ac27a0ecSDave Kleikamp 4713ac27a0ecSDave Kleikamp /* 4714ac27a0ecSDave Kleikamp * OK, there are no updates running now, and all cached data is 4715ac27a0ecSDave Kleikamp * synced to disk. We are now in a completely consistent state 4716ac27a0ecSDave Kleikamp * which doesn't have anything in the journal, and we know that 4717ac27a0ecSDave Kleikamp * no filesystem updates are running, so it is safe to modify 4718ac27a0ecSDave Kleikamp * the inode's in-core data-journaling state flag now. 4719ac27a0ecSDave Kleikamp */ 4720ac27a0ecSDave Kleikamp 4721ac27a0ecSDave Kleikamp if (val) 472212e9b892SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 47235872ddaaSYongqiang Yang else { 47245872ddaaSYongqiang Yang jbd2_journal_flush(journal); 472512e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 47265872ddaaSYongqiang Yang } 4727617ba13bSMingming Cao ext4_set_aops(inode); 4728ac27a0ecSDave Kleikamp 4729dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 4730ac27a0ecSDave Kleikamp 4731ac27a0ecSDave Kleikamp /* Finally we can mark the inode as dirty. */ 4732ac27a0ecSDave Kleikamp 4733617ba13bSMingming Cao handle = ext4_journal_start(inode, 1); 4734ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 4735ac27a0ecSDave Kleikamp return PTR_ERR(handle); 4736ac27a0ecSDave Kleikamp 4737617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 47380390131bSFrank Mayhar ext4_handle_sync(handle); 4739617ba13bSMingming Cao ext4_journal_stop(handle); 4740617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4741ac27a0ecSDave Kleikamp 4742ac27a0ecSDave Kleikamp return err; 4743ac27a0ecSDave Kleikamp } 47442e9ee850SAneesh Kumar K.V 47452e9ee850SAneesh Kumar K.V static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) 47462e9ee850SAneesh Kumar K.V { 47472e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 47482e9ee850SAneesh Kumar K.V } 47492e9ee850SAneesh Kumar K.V 4750c2ec175cSNick Piggin int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) 47512e9ee850SAneesh Kumar K.V { 4752c2ec175cSNick Piggin struct page *page = vmf->page; 47532e9ee850SAneesh Kumar K.V loff_t size; 47542e9ee850SAneesh Kumar K.V unsigned long len; 47559ea7df53SJan Kara int ret; 47562e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 47572e9ee850SAneesh Kumar K.V struct inode *inode = file->f_path.dentry->d_inode; 47582e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 47599ea7df53SJan Kara handle_t *handle; 47609ea7df53SJan Kara get_block_t *get_block; 47619ea7df53SJan Kara int retries = 0; 47622e9ee850SAneesh Kumar K.V 47632e9ee850SAneesh Kumar K.V /* 47649ea7df53SJan Kara * This check is racy but catches the common case. We rely on 47659ea7df53SJan Kara * __block_page_mkwrite() to do a reliable check. 47662e9ee850SAneesh Kumar K.V */ 47679ea7df53SJan Kara vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); 47689ea7df53SJan Kara /* Delalloc case is easy... */ 47699ea7df53SJan Kara if (test_opt(inode->i_sb, DELALLOC) && 47709ea7df53SJan Kara !ext4_should_journal_data(inode) && 47719ea7df53SJan Kara !ext4_nonda_switch(inode->i_sb)) { 47729ea7df53SJan Kara do { 47739ea7df53SJan Kara ret = __block_page_mkwrite(vma, vmf, 47749ea7df53SJan Kara ext4_da_get_block_prep); 47759ea7df53SJan Kara } while (ret == -ENOSPC && 47769ea7df53SJan Kara ext4_should_retry_alloc(inode->i_sb, &retries)); 47779ea7df53SJan Kara goto out_ret; 47782e9ee850SAneesh Kumar K.V } 47790e499890SDarrick J. Wong 47800e499890SDarrick J. Wong lock_page(page); 47819ea7df53SJan Kara size = i_size_read(inode); 47829ea7df53SJan Kara /* Page got truncated from under us? */ 47839ea7df53SJan Kara if (page->mapping != mapping || page_offset(page) > size) { 47849ea7df53SJan Kara unlock_page(page); 47859ea7df53SJan Kara ret = VM_FAULT_NOPAGE; 47869ea7df53SJan Kara goto out; 47870e499890SDarrick J. Wong } 47882e9ee850SAneesh Kumar K.V 47892e9ee850SAneesh Kumar K.V if (page->index == size >> PAGE_CACHE_SHIFT) 47902e9ee850SAneesh Kumar K.V len = size & ~PAGE_CACHE_MASK; 47912e9ee850SAneesh Kumar K.V else 47922e9ee850SAneesh Kumar K.V len = PAGE_CACHE_SIZE; 4793a827eaffSAneesh Kumar K.V /* 47949ea7df53SJan Kara * Return if we have all the buffers mapped. This avoids the need to do 47959ea7df53SJan Kara * journal_start/journal_stop which can block and take a long time 4796a827eaffSAneesh Kumar K.V */ 47972e9ee850SAneesh Kumar K.V if (page_has_buffers(page)) { 47982e9ee850SAneesh Kumar K.V if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL, 4799a827eaffSAneesh Kumar K.V ext4_bh_unmapped)) { 48009ea7df53SJan Kara /* Wait so that we don't change page under IO */ 48019ea7df53SJan Kara wait_on_page_writeback(page); 48029ea7df53SJan Kara ret = VM_FAULT_LOCKED; 48039ea7df53SJan Kara goto out; 48042e9ee850SAneesh Kumar K.V } 4805a827eaffSAneesh Kumar K.V } 4806a827eaffSAneesh Kumar K.V unlock_page(page); 48079ea7df53SJan Kara /* OK, we need to fill the hole... */ 48089ea7df53SJan Kara if (ext4_should_dioread_nolock(inode)) 48099ea7df53SJan Kara get_block = ext4_get_block_write; 48109ea7df53SJan Kara else 48119ea7df53SJan Kara get_block = ext4_get_block; 48129ea7df53SJan Kara retry_alloc: 48139ea7df53SJan Kara handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode)); 48149ea7df53SJan Kara if (IS_ERR(handle)) { 4815c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 48169ea7df53SJan Kara goto out; 48179ea7df53SJan Kara } 48189ea7df53SJan Kara ret = __block_page_mkwrite(vma, vmf, get_block); 48199ea7df53SJan Kara if (!ret && ext4_should_journal_data(inode)) { 48209ea7df53SJan Kara if (walk_page_buffers(handle, page_buffers(page), 0, 48219ea7df53SJan Kara PAGE_CACHE_SIZE, NULL, do_journal_get_write_access)) { 48229ea7df53SJan Kara unlock_page(page); 48239ea7df53SJan Kara ret = VM_FAULT_SIGBUS; 4824fcbb5515SYongqiang Yang ext4_journal_stop(handle); 48259ea7df53SJan Kara goto out; 48269ea7df53SJan Kara } 48279ea7df53SJan Kara ext4_set_inode_state(inode, EXT4_STATE_JDATA); 48289ea7df53SJan Kara } 48299ea7df53SJan Kara ext4_journal_stop(handle); 48309ea7df53SJan Kara if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 48319ea7df53SJan Kara goto retry_alloc; 48329ea7df53SJan Kara out_ret: 48339ea7df53SJan Kara ret = block_page_mkwrite_return(ret); 48349ea7df53SJan Kara out: 48352e9ee850SAneesh Kumar K.V return ret; 48362e9ee850SAneesh Kumar K.V } 4837