1ac27a0ecSDave Kleikamp /* 2617ba13bSMingming Cao * linux/fs/ext4/inode.c 3ac27a0ecSDave Kleikamp * 4ac27a0ecSDave Kleikamp * Copyright (C) 1992, 1993, 1994, 1995 5ac27a0ecSDave Kleikamp * Remy Card (card@masi.ibp.fr) 6ac27a0ecSDave Kleikamp * Laboratoire MASI - Institut Blaise Pascal 7ac27a0ecSDave Kleikamp * Universite Pierre et Marie Curie (Paris VI) 8ac27a0ecSDave Kleikamp * 9ac27a0ecSDave Kleikamp * from 10ac27a0ecSDave Kleikamp * 11ac27a0ecSDave Kleikamp * linux/fs/minix/inode.c 12ac27a0ecSDave Kleikamp * 13ac27a0ecSDave Kleikamp * Copyright (C) 1991, 1992 Linus Torvalds 14ac27a0ecSDave Kleikamp * 15ac27a0ecSDave Kleikamp * 64-bit file support on 64-bit platforms by Jakub Jelinek 16ac27a0ecSDave Kleikamp * (jj@sunsite.ms.mff.cuni.cz) 17ac27a0ecSDave Kleikamp * 18617ba13bSMingming Cao * Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000 19ac27a0ecSDave Kleikamp */ 20ac27a0ecSDave Kleikamp 21ac27a0ecSDave Kleikamp #include <linux/fs.h> 22ac27a0ecSDave Kleikamp #include <linux/time.h> 23dab291afSMingming Cao #include <linux/jbd2.h> 24ac27a0ecSDave Kleikamp #include <linux/highuid.h> 25ac27a0ecSDave Kleikamp #include <linux/pagemap.h> 26ac27a0ecSDave Kleikamp #include <linux/quotaops.h> 27ac27a0ecSDave Kleikamp #include <linux/string.h> 28ac27a0ecSDave Kleikamp #include <linux/buffer_head.h> 29ac27a0ecSDave Kleikamp #include <linux/writeback.h> 3064769240SAlex Tomas #include <linux/pagevec.h> 31ac27a0ecSDave Kleikamp #include <linux/mpage.h> 32e83c1397SDuane Griffin #include <linux/namei.h> 33ac27a0ecSDave Kleikamp #include <linux/uio.h> 34ac27a0ecSDave Kleikamp #include <linux/bio.h> 354c0425ffSMingming Cao #include <linux/workqueue.h> 36744692dcSJiaying Zhang #include <linux/kernel.h> 376db26ffcSAndrew Morton #include <linux/printk.h> 385a0e3ad6STejun Heo #include <linux/slab.h> 39a8901d34STheodore Ts'o #include <linux/ratelimit.h> 409bffad1eSTheodore Ts'o 413dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 42ac27a0ecSDave Kleikamp #include "xattr.h" 43ac27a0ecSDave Kleikamp #include "acl.h" 449f125d64STheodore Ts'o #include "truncate.h" 45ac27a0ecSDave Kleikamp 469bffad1eSTheodore Ts'o #include <trace/events/ext4.h> 479bffad1eSTheodore Ts'o 48a1d6cc56SAneesh Kumar K.V #define MPAGE_DA_EXTENT_TAIL 0x01 49a1d6cc56SAneesh Kumar K.V 50814525f4SDarrick J. Wong static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw, 51814525f4SDarrick J. Wong struct ext4_inode_info *ei) 52814525f4SDarrick J. Wong { 53814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 54814525f4SDarrick J. Wong __u16 csum_lo; 55814525f4SDarrick J. Wong __u16 csum_hi = 0; 56814525f4SDarrick J. Wong __u32 csum; 57814525f4SDarrick J. Wong 58814525f4SDarrick J. Wong csum_lo = raw->i_checksum_lo; 59814525f4SDarrick J. Wong raw->i_checksum_lo = 0; 60814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 61814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) { 62814525f4SDarrick J. Wong csum_hi = raw->i_checksum_hi; 63814525f4SDarrick J. Wong raw->i_checksum_hi = 0; 64814525f4SDarrick J. Wong } 65814525f4SDarrick J. Wong 66814525f4SDarrick J. Wong csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw, 67814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)); 68814525f4SDarrick J. Wong 69814525f4SDarrick J. Wong raw->i_checksum_lo = csum_lo; 70814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 71814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 72814525f4SDarrick J. Wong raw->i_checksum_hi = csum_hi; 73814525f4SDarrick J. Wong 74814525f4SDarrick J. Wong return csum; 75814525f4SDarrick J. Wong } 76814525f4SDarrick J. Wong 77814525f4SDarrick J. Wong static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw, 78814525f4SDarrick J. Wong struct ext4_inode_info *ei) 79814525f4SDarrick J. Wong { 80814525f4SDarrick J. Wong __u32 provided, calculated; 81814525f4SDarrick J. Wong 82814525f4SDarrick J. Wong if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 83814525f4SDarrick J. Wong cpu_to_le32(EXT4_OS_LINUX) || 84814525f4SDarrick J. Wong !EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, 85814525f4SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) 86814525f4SDarrick J. Wong return 1; 87814525f4SDarrick J. Wong 88814525f4SDarrick J. Wong provided = le16_to_cpu(raw->i_checksum_lo); 89814525f4SDarrick J. Wong calculated = ext4_inode_csum(inode, raw, ei); 90814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 91814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 92814525f4SDarrick J. Wong provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16; 93814525f4SDarrick J. Wong else 94814525f4SDarrick J. Wong calculated &= 0xFFFF; 95814525f4SDarrick J. Wong 96814525f4SDarrick J. Wong return provided == calculated; 97814525f4SDarrick J. Wong } 98814525f4SDarrick J. Wong 99814525f4SDarrick J. Wong static void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw, 100814525f4SDarrick J. Wong struct ext4_inode_info *ei) 101814525f4SDarrick J. Wong { 102814525f4SDarrick J. Wong __u32 csum; 103814525f4SDarrick J. Wong 104814525f4SDarrick J. Wong if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 105814525f4SDarrick J. Wong cpu_to_le32(EXT4_OS_LINUX) || 106814525f4SDarrick J. Wong !EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, 107814525f4SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) 108814525f4SDarrick J. Wong return; 109814525f4SDarrick J. Wong 110814525f4SDarrick J. Wong csum = ext4_inode_csum(inode, raw, ei); 111814525f4SDarrick J. Wong raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF); 112814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 113814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 114814525f4SDarrick J. Wong raw->i_checksum_hi = cpu_to_le16(csum >> 16); 115814525f4SDarrick J. Wong } 116814525f4SDarrick J. Wong 117678aaf48SJan Kara static inline int ext4_begin_ordered_truncate(struct inode *inode, 118678aaf48SJan Kara loff_t new_size) 119678aaf48SJan Kara { 1207ff9c073STheodore Ts'o trace_ext4_begin_ordered_truncate(inode, new_size); 1218aefcd55STheodore Ts'o /* 1228aefcd55STheodore Ts'o * If jinode is zero, then we never opened the file for 1238aefcd55STheodore Ts'o * writing, so there's no need to call 1248aefcd55STheodore Ts'o * jbd2_journal_begin_ordered_truncate() since there's no 1258aefcd55STheodore Ts'o * outstanding writes we need to flush. 1268aefcd55STheodore Ts'o */ 1278aefcd55STheodore Ts'o if (!EXT4_I(inode)->jinode) 1288aefcd55STheodore Ts'o return 0; 1298aefcd55STheodore Ts'o return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode), 1308aefcd55STheodore Ts'o EXT4_I(inode)->jinode, 131678aaf48SJan Kara new_size); 132678aaf48SJan Kara } 133678aaf48SJan Kara 13464769240SAlex Tomas static void ext4_invalidatepage(struct page *page, unsigned long offset); 135cb20d518STheodore Ts'o static int noalloc_get_block_write(struct inode *inode, sector_t iblock, 136cb20d518STheodore Ts'o struct buffer_head *bh_result, int create); 137cb20d518STheodore Ts'o static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode); 138cb20d518STheodore Ts'o static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate); 139cb20d518STheodore Ts'o static int __ext4_journalled_writepage(struct page *page, unsigned int len); 140cb20d518STheodore Ts'o static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh); 1415f163cc7SEric Sandeen static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle, 1425f163cc7SEric Sandeen struct inode *inode, struct page *page, loff_t from, 1435f163cc7SEric Sandeen loff_t length, int flags); 14464769240SAlex Tomas 145ac27a0ecSDave Kleikamp /* 146ac27a0ecSDave Kleikamp * Test whether an inode is a fast symlink. 147ac27a0ecSDave Kleikamp */ 148617ba13bSMingming Cao static int ext4_inode_is_fast_symlink(struct inode *inode) 149ac27a0ecSDave Kleikamp { 150617ba13bSMingming Cao int ea_blocks = EXT4_I(inode)->i_file_acl ? 151ac27a0ecSDave Kleikamp (inode->i_sb->s_blocksize >> 9) : 0; 152ac27a0ecSDave Kleikamp 153ac27a0ecSDave Kleikamp return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0); 154ac27a0ecSDave Kleikamp } 155ac27a0ecSDave Kleikamp 156ac27a0ecSDave Kleikamp /* 157ac27a0ecSDave Kleikamp * Restart the transaction associated with *handle. This does a commit, 158ac27a0ecSDave Kleikamp * so before we call here everything must be consistently dirtied against 159ac27a0ecSDave Kleikamp * this transaction. 160ac27a0ecSDave Kleikamp */ 161487caeefSJan Kara int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode, 162487caeefSJan Kara int nblocks) 163ac27a0ecSDave Kleikamp { 164487caeefSJan Kara int ret; 165487caeefSJan Kara 166487caeefSJan Kara /* 167e35fd660STheodore Ts'o * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this 168487caeefSJan Kara * moment, get_block can be called only for blocks inside i_size since 169487caeefSJan Kara * page cache has been already dropped and writes are blocked by 170487caeefSJan Kara * i_mutex. So we can safely drop the i_data_sem here. 171487caeefSJan Kara */ 1720390131bSFrank Mayhar BUG_ON(EXT4_JOURNAL(inode) == NULL); 173ac27a0ecSDave Kleikamp jbd_debug(2, "restarting handle %p\n", handle); 174487caeefSJan Kara up_write(&EXT4_I(inode)->i_data_sem); 1758e8eaabeSAmir Goldstein ret = ext4_journal_restart(handle, nblocks); 176487caeefSJan Kara down_write(&EXT4_I(inode)->i_data_sem); 177fa5d1113SAneesh Kumar K.V ext4_discard_preallocations(inode); 178487caeefSJan Kara 179487caeefSJan Kara return ret; 180ac27a0ecSDave Kleikamp } 181ac27a0ecSDave Kleikamp 182ac27a0ecSDave Kleikamp /* 183ac27a0ecSDave Kleikamp * Called at the last iput() if i_nlink is zero. 184ac27a0ecSDave Kleikamp */ 1850930fcc1SAl Viro void ext4_evict_inode(struct inode *inode) 186ac27a0ecSDave Kleikamp { 187ac27a0ecSDave Kleikamp handle_t *handle; 188bc965ab3STheodore Ts'o int err; 189ac27a0ecSDave Kleikamp 1907ff9c073STheodore Ts'o trace_ext4_evict_inode(inode); 1912581fdc8SJiaying Zhang 1922581fdc8SJiaying Zhang ext4_ioend_wait(inode); 1932581fdc8SJiaying Zhang 1940930fcc1SAl Viro if (inode->i_nlink) { 1952d859db3SJan Kara /* 1962d859db3SJan Kara * When journalling data dirty buffers are tracked only in the 1972d859db3SJan Kara * journal. So although mm thinks everything is clean and 1982d859db3SJan Kara * ready for reaping the inode might still have some pages to 1992d859db3SJan Kara * write in the running transaction or waiting to be 2002d859db3SJan Kara * checkpointed. Thus calling jbd2_journal_invalidatepage() 2012d859db3SJan Kara * (via truncate_inode_pages()) to discard these buffers can 2022d859db3SJan Kara * cause data loss. Also even if we did not discard these 2032d859db3SJan Kara * buffers, we would have no way to find them after the inode 2042d859db3SJan Kara * is reaped and thus user could see stale data if he tries to 2052d859db3SJan Kara * read them before the transaction is checkpointed. So be 2062d859db3SJan Kara * careful and force everything to disk here... We use 2072d859db3SJan Kara * ei->i_datasync_tid to store the newest transaction 2082d859db3SJan Kara * containing inode's data. 2092d859db3SJan Kara * 2102d859db3SJan Kara * Note that directories do not have this problem because they 2112d859db3SJan Kara * don't use page cache. 2122d859db3SJan Kara */ 2132d859db3SJan Kara if (ext4_should_journal_data(inode) && 2142d859db3SJan Kara (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode))) { 2152d859db3SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 2162d859db3SJan Kara tid_t commit_tid = EXT4_I(inode)->i_datasync_tid; 2172d859db3SJan Kara 2182d859db3SJan Kara jbd2_log_start_commit(journal, commit_tid); 2192d859db3SJan Kara jbd2_log_wait_commit(journal, commit_tid); 2202d859db3SJan Kara filemap_write_and_wait(&inode->i_data); 2212d859db3SJan Kara } 2220930fcc1SAl Viro truncate_inode_pages(&inode->i_data, 0); 2230930fcc1SAl Viro goto no_delete; 2240930fcc1SAl Viro } 2250930fcc1SAl Viro 226907f4554SChristoph Hellwig if (!is_bad_inode(inode)) 227871a2931SChristoph Hellwig dquot_initialize(inode); 228907f4554SChristoph Hellwig 229678aaf48SJan Kara if (ext4_should_order_data(inode)) 230678aaf48SJan Kara ext4_begin_ordered_truncate(inode, 0); 231ac27a0ecSDave Kleikamp truncate_inode_pages(&inode->i_data, 0); 232ac27a0ecSDave Kleikamp 233ac27a0ecSDave Kleikamp if (is_bad_inode(inode)) 234ac27a0ecSDave Kleikamp goto no_delete; 235ac27a0ecSDave Kleikamp 2368e8ad8a5SJan Kara /* 2378e8ad8a5SJan Kara * Protect us against freezing - iput() caller didn't have to have any 2388e8ad8a5SJan Kara * protection against it 2398e8ad8a5SJan Kara */ 2408e8ad8a5SJan Kara sb_start_intwrite(inode->i_sb); 2419f125d64STheodore Ts'o handle = ext4_journal_start(inode, ext4_blocks_for_truncate(inode)+3); 242ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 243bc965ab3STheodore Ts'o ext4_std_error(inode->i_sb, PTR_ERR(handle)); 244ac27a0ecSDave Kleikamp /* 245ac27a0ecSDave Kleikamp * If we're going to skip the normal cleanup, we still need to 246ac27a0ecSDave Kleikamp * make sure that the in-core orphan linked list is properly 247ac27a0ecSDave Kleikamp * cleaned up. 248ac27a0ecSDave Kleikamp */ 249617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 2508e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 251ac27a0ecSDave Kleikamp goto no_delete; 252ac27a0ecSDave Kleikamp } 253ac27a0ecSDave Kleikamp 254ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 2550390131bSFrank Mayhar ext4_handle_sync(handle); 256ac27a0ecSDave Kleikamp inode->i_size = 0; 257bc965ab3STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 258bc965ab3STheodore Ts'o if (err) { 25912062dddSEric Sandeen ext4_warning(inode->i_sb, 260bc965ab3STheodore Ts'o "couldn't mark inode dirty (err %d)", err); 261bc965ab3STheodore Ts'o goto stop_handle; 262bc965ab3STheodore Ts'o } 263ac27a0ecSDave Kleikamp if (inode->i_blocks) 264617ba13bSMingming Cao ext4_truncate(inode); 265bc965ab3STheodore Ts'o 266bc965ab3STheodore Ts'o /* 267bc965ab3STheodore Ts'o * ext4_ext_truncate() doesn't reserve any slop when it 268bc965ab3STheodore Ts'o * restarts journal transactions; therefore there may not be 269bc965ab3STheodore Ts'o * enough credits left in the handle to remove the inode from 270bc965ab3STheodore Ts'o * the orphan list and set the dtime field. 271bc965ab3STheodore Ts'o */ 2720390131bSFrank Mayhar if (!ext4_handle_has_enough_credits(handle, 3)) { 273bc965ab3STheodore Ts'o err = ext4_journal_extend(handle, 3); 274bc965ab3STheodore Ts'o if (err > 0) 275bc965ab3STheodore Ts'o err = ext4_journal_restart(handle, 3); 276bc965ab3STheodore Ts'o if (err != 0) { 27712062dddSEric Sandeen ext4_warning(inode->i_sb, 278bc965ab3STheodore Ts'o "couldn't extend journal (err %d)", err); 279bc965ab3STheodore Ts'o stop_handle: 280bc965ab3STheodore Ts'o ext4_journal_stop(handle); 28145388219STheodore Ts'o ext4_orphan_del(NULL, inode); 2828e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 283bc965ab3STheodore Ts'o goto no_delete; 284bc965ab3STheodore Ts'o } 285bc965ab3STheodore Ts'o } 286bc965ab3STheodore Ts'o 287ac27a0ecSDave Kleikamp /* 288617ba13bSMingming Cao * Kill off the orphan record which ext4_truncate created. 289ac27a0ecSDave Kleikamp * AKPM: I think this can be inside the above `if'. 290617ba13bSMingming Cao * Note that ext4_orphan_del() has to be able to cope with the 291ac27a0ecSDave Kleikamp * deletion of a non-existent orphan - this is because we don't 292617ba13bSMingming Cao * know if ext4_truncate() actually created an orphan record. 293ac27a0ecSDave Kleikamp * (Well, we could do this if we need to, but heck - it works) 294ac27a0ecSDave Kleikamp */ 295617ba13bSMingming Cao ext4_orphan_del(handle, inode); 296617ba13bSMingming Cao EXT4_I(inode)->i_dtime = get_seconds(); 297ac27a0ecSDave Kleikamp 298ac27a0ecSDave Kleikamp /* 299ac27a0ecSDave Kleikamp * One subtle ordering requirement: if anything has gone wrong 300ac27a0ecSDave Kleikamp * (transaction abort, IO errors, whatever), then we can still 301ac27a0ecSDave Kleikamp * do these next steps (the fs will already have been marked as 302ac27a0ecSDave Kleikamp * having errors), but we can't free the inode if the mark_dirty 303ac27a0ecSDave Kleikamp * fails. 304ac27a0ecSDave Kleikamp */ 305617ba13bSMingming Cao if (ext4_mark_inode_dirty(handle, inode)) 306ac27a0ecSDave Kleikamp /* If that failed, just do the required in-core inode clear. */ 3070930fcc1SAl Viro ext4_clear_inode(inode); 308ac27a0ecSDave Kleikamp else 309617ba13bSMingming Cao ext4_free_inode(handle, inode); 310617ba13bSMingming Cao ext4_journal_stop(handle); 3118e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 312ac27a0ecSDave Kleikamp return; 313ac27a0ecSDave Kleikamp no_delete: 3140930fcc1SAl Viro ext4_clear_inode(inode); /* We must guarantee clearing of inode... */ 315ac27a0ecSDave Kleikamp } 316ac27a0ecSDave Kleikamp 317a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 318a9e7f447SDmitry Monakhov qsize_t *ext4_get_reserved_space(struct inode *inode) 31960e58e0fSMingming Cao { 320a9e7f447SDmitry Monakhov return &EXT4_I(inode)->i_reserved_quota; 32160e58e0fSMingming Cao } 322a9e7f447SDmitry Monakhov #endif 3239d0be502STheodore Ts'o 32412219aeaSAneesh Kumar K.V /* 32512219aeaSAneesh Kumar K.V * Calculate the number of metadata blocks need to reserve 3269d0be502STheodore Ts'o * to allocate a block located at @lblock 32712219aeaSAneesh Kumar K.V */ 32801f49d0bSTheodore Ts'o static int ext4_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock) 32912219aeaSAneesh Kumar K.V { 33012e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3319d0be502STheodore Ts'o return ext4_ext_calc_metadata_amount(inode, lblock); 33212219aeaSAneesh Kumar K.V 3338bb2b247SAmir Goldstein return ext4_ind_calc_metadata_amount(inode, lblock); 33412219aeaSAneesh Kumar K.V } 33512219aeaSAneesh Kumar K.V 3360637c6f4STheodore Ts'o /* 3370637c6f4STheodore Ts'o * Called with i_data_sem down, which is important since we can call 3380637c6f4STheodore Ts'o * ext4_discard_preallocations() from here. 3390637c6f4STheodore Ts'o */ 3405f634d06SAneesh Kumar K.V void ext4_da_update_reserve_space(struct inode *inode, 3415f634d06SAneesh Kumar K.V int used, int quota_claim) 34212219aeaSAneesh Kumar K.V { 34312219aeaSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3440637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 34512219aeaSAneesh Kumar K.V 3460637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 347d8990240SAditya Kali trace_ext4_da_update_reserve_space(inode, used, quota_claim); 3480637c6f4STheodore Ts'o if (unlikely(used > ei->i_reserved_data_blocks)) { 3490637c6f4STheodore Ts'o ext4_msg(inode->i_sb, KERN_NOTICE, "%s: ino %lu, used %d " 3501084f252STheodore Ts'o "with only %d reserved data blocks", 3510637c6f4STheodore Ts'o __func__, inode->i_ino, used, 3520637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 3530637c6f4STheodore Ts'o WARN_ON(1); 3540637c6f4STheodore Ts'o used = ei->i_reserved_data_blocks; 3556bc6e63fSAneesh Kumar K.V } 35612219aeaSAneesh Kumar K.V 35797795d2aSBrian Foster if (unlikely(ei->i_allocated_meta_blocks > ei->i_reserved_meta_blocks)) { 35897795d2aSBrian Foster ext4_msg(inode->i_sb, KERN_NOTICE, "%s: ino %lu, allocated %d " 35997795d2aSBrian Foster "with only %d reserved metadata blocks\n", __func__, 36097795d2aSBrian Foster inode->i_ino, ei->i_allocated_meta_blocks, 36197795d2aSBrian Foster ei->i_reserved_meta_blocks); 36297795d2aSBrian Foster WARN_ON(1); 36397795d2aSBrian Foster ei->i_allocated_meta_blocks = ei->i_reserved_meta_blocks; 36497795d2aSBrian Foster } 36597795d2aSBrian Foster 3660637c6f4STheodore Ts'o /* Update per-inode reservations */ 3670637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= used; 3680637c6f4STheodore Ts'o ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks; 36957042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 37072b8ab9dSEric Sandeen used + ei->i_allocated_meta_blocks); 3710637c6f4STheodore Ts'o ei->i_allocated_meta_blocks = 0; 3720637c6f4STheodore Ts'o 3730637c6f4STheodore Ts'o if (ei->i_reserved_data_blocks == 0) { 3740637c6f4STheodore Ts'o /* 3750637c6f4STheodore Ts'o * We can release all of the reserved metadata blocks 3760637c6f4STheodore Ts'o * only when we have written all of the delayed 3770637c6f4STheodore Ts'o * allocation blocks. 3780637c6f4STheodore Ts'o */ 37957042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 38072b8ab9dSEric Sandeen ei->i_reserved_meta_blocks); 381ee5f4d9cSTheodore Ts'o ei->i_reserved_meta_blocks = 0; 3829d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 3830637c6f4STheodore Ts'o } 38412219aeaSAneesh Kumar K.V spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 38560e58e0fSMingming Cao 38672b8ab9dSEric Sandeen /* Update quota subsystem for data blocks */ 38772b8ab9dSEric Sandeen if (quota_claim) 3887b415bf6SAditya Kali dquot_claim_block(inode, EXT4_C2B(sbi, used)); 38972b8ab9dSEric Sandeen else { 3905f634d06SAneesh Kumar K.V /* 3915f634d06SAneesh Kumar K.V * We did fallocate with an offset that is already delayed 3925f634d06SAneesh Kumar K.V * allocated. So on delayed allocated writeback we should 39372b8ab9dSEric Sandeen * not re-claim the quota for fallocated blocks. 3945f634d06SAneesh Kumar K.V */ 3957b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, used)); 3965f634d06SAneesh Kumar K.V } 397d6014301SAneesh Kumar K.V 398d6014301SAneesh Kumar K.V /* 399d6014301SAneesh Kumar K.V * If we have done all the pending block allocations and if 400d6014301SAneesh Kumar K.V * there aren't any writers on the inode, we can discard the 401d6014301SAneesh Kumar K.V * inode's preallocations. 402d6014301SAneesh Kumar K.V */ 4030637c6f4STheodore Ts'o if ((ei->i_reserved_data_blocks == 0) && 4040637c6f4STheodore Ts'o (atomic_read(&inode->i_writecount) == 0)) 405d6014301SAneesh Kumar K.V ext4_discard_preallocations(inode); 40612219aeaSAneesh Kumar K.V } 40712219aeaSAneesh Kumar K.V 408e29136f8STheodore Ts'o static int __check_block_validity(struct inode *inode, const char *func, 409c398eda0STheodore Ts'o unsigned int line, 41024676da4STheodore Ts'o struct ext4_map_blocks *map) 4116fd058f7STheodore Ts'o { 41224676da4STheodore Ts'o if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk, 41324676da4STheodore Ts'o map->m_len)) { 414c398eda0STheodore Ts'o ext4_error_inode(inode, func, line, map->m_pblk, 415c398eda0STheodore Ts'o "lblock %lu mapped to illegal pblock " 41624676da4STheodore Ts'o "(length %d)", (unsigned long) map->m_lblk, 417c398eda0STheodore Ts'o map->m_len); 4186fd058f7STheodore Ts'o return -EIO; 4196fd058f7STheodore Ts'o } 4206fd058f7STheodore Ts'o return 0; 4216fd058f7STheodore Ts'o } 4226fd058f7STheodore Ts'o 423e29136f8STheodore Ts'o #define check_block_validity(inode, map) \ 424c398eda0STheodore Ts'o __check_block_validity((inode), __func__, __LINE__, (map)) 425e29136f8STheodore Ts'o 426f5ab0d1fSMingming Cao /* 4271f94533dSTheodore Ts'o * Return the number of contiguous dirty pages in a given inode 4281f94533dSTheodore Ts'o * starting at page frame idx. 42955138e0bSTheodore Ts'o */ 43055138e0bSTheodore Ts'o static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx, 43155138e0bSTheodore Ts'o unsigned int max_pages) 43255138e0bSTheodore Ts'o { 43355138e0bSTheodore Ts'o struct address_space *mapping = inode->i_mapping; 43455138e0bSTheodore Ts'o pgoff_t index; 43555138e0bSTheodore Ts'o struct pagevec pvec; 43655138e0bSTheodore Ts'o pgoff_t num = 0; 43755138e0bSTheodore Ts'o int i, nr_pages, done = 0; 43855138e0bSTheodore Ts'o 43955138e0bSTheodore Ts'o if (max_pages == 0) 44055138e0bSTheodore Ts'o return 0; 44155138e0bSTheodore Ts'o pagevec_init(&pvec, 0); 44255138e0bSTheodore Ts'o while (!done) { 44355138e0bSTheodore Ts'o index = idx; 44455138e0bSTheodore Ts'o nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 44555138e0bSTheodore Ts'o PAGECACHE_TAG_DIRTY, 44655138e0bSTheodore Ts'o (pgoff_t)PAGEVEC_SIZE); 44755138e0bSTheodore Ts'o if (nr_pages == 0) 44855138e0bSTheodore Ts'o break; 44955138e0bSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 45055138e0bSTheodore Ts'o struct page *page = pvec.pages[i]; 45155138e0bSTheodore Ts'o struct buffer_head *bh, *head; 45255138e0bSTheodore Ts'o 45355138e0bSTheodore Ts'o lock_page(page); 45455138e0bSTheodore Ts'o if (unlikely(page->mapping != mapping) || 45555138e0bSTheodore Ts'o !PageDirty(page) || 45655138e0bSTheodore Ts'o PageWriteback(page) || 45755138e0bSTheodore Ts'o page->index != idx) { 45855138e0bSTheodore Ts'o done = 1; 45955138e0bSTheodore Ts'o unlock_page(page); 46055138e0bSTheodore Ts'o break; 46155138e0bSTheodore Ts'o } 4621f94533dSTheodore Ts'o if (page_has_buffers(page)) { 4631f94533dSTheodore Ts'o bh = head = page_buffers(page); 46455138e0bSTheodore Ts'o do { 46555138e0bSTheodore Ts'o if (!buffer_delay(bh) && 4661f94533dSTheodore Ts'o !buffer_unwritten(bh)) 46755138e0bSTheodore Ts'o done = 1; 4681f94533dSTheodore Ts'o bh = bh->b_this_page; 4691f94533dSTheodore Ts'o } while (!done && (bh != head)); 47055138e0bSTheodore Ts'o } 47155138e0bSTheodore Ts'o unlock_page(page); 47255138e0bSTheodore Ts'o if (done) 47355138e0bSTheodore Ts'o break; 47455138e0bSTheodore Ts'o idx++; 47555138e0bSTheodore Ts'o num++; 476659c6009SEric Sandeen if (num >= max_pages) { 477659c6009SEric Sandeen done = 1; 47855138e0bSTheodore Ts'o break; 47955138e0bSTheodore Ts'o } 480659c6009SEric Sandeen } 48155138e0bSTheodore Ts'o pagevec_release(&pvec); 48255138e0bSTheodore Ts'o } 48355138e0bSTheodore Ts'o return num; 48455138e0bSTheodore Ts'o } 48555138e0bSTheodore Ts'o 48655138e0bSTheodore Ts'o /* 487e35fd660STheodore Ts'o * The ext4_map_blocks() function tries to look up the requested blocks, 4882b2d6d01STheodore Ts'o * and returns if the blocks are already mapped. 489f5ab0d1fSMingming Cao * 490f5ab0d1fSMingming Cao * Otherwise it takes the write lock of the i_data_sem and allocate blocks 491f5ab0d1fSMingming Cao * and store the allocated blocks in the result buffer head and mark it 492f5ab0d1fSMingming Cao * mapped. 493f5ab0d1fSMingming Cao * 494e35fd660STheodore Ts'o * If file type is extents based, it will call ext4_ext_map_blocks(), 495e35fd660STheodore Ts'o * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping 496f5ab0d1fSMingming Cao * based files 497f5ab0d1fSMingming Cao * 498f5ab0d1fSMingming Cao * On success, it returns the number of blocks being mapped or allocate. 499f5ab0d1fSMingming Cao * if create==0 and the blocks are pre-allocated and uninitialized block, 500f5ab0d1fSMingming Cao * the result buffer head is unmapped. If the create ==1, it will make sure 501f5ab0d1fSMingming Cao * the buffer head is mapped. 502f5ab0d1fSMingming Cao * 503f5ab0d1fSMingming Cao * It returns 0 if plain look up failed (blocks have not been allocated), in 504df3ab170STao Ma * that case, buffer head is unmapped 505f5ab0d1fSMingming Cao * 506f5ab0d1fSMingming Cao * It returns the error in case of allocation failure. 507f5ab0d1fSMingming Cao */ 508e35fd660STheodore Ts'o int ext4_map_blocks(handle_t *handle, struct inode *inode, 509e35fd660STheodore Ts'o struct ext4_map_blocks *map, int flags) 5100e855ac8SAneesh Kumar K.V { 5110e855ac8SAneesh Kumar K.V int retval; 512f5ab0d1fSMingming Cao 513e35fd660STheodore Ts'o map->m_flags = 0; 514e35fd660STheodore Ts'o ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u," 515e35fd660STheodore Ts'o "logical block %lu\n", inode->i_ino, flags, map->m_len, 516e35fd660STheodore Ts'o (unsigned long) map->m_lblk); 5174df3d265SAneesh Kumar K.V /* 518b920c755STheodore Ts'o * Try to see if we can get the block without requesting a new 519b920c755STheodore Ts'o * file system block. 5204df3d265SAneesh Kumar K.V */ 521729f52c6SZheng Liu if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 5220e855ac8SAneesh Kumar K.V down_read((&EXT4_I(inode)->i_data_sem)); 52312e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 524a4e5d88bSDmitry Monakhov retval = ext4_ext_map_blocks(handle, inode, map, flags & 525a4e5d88bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 5264df3d265SAneesh Kumar K.V } else { 527a4e5d88bSDmitry Monakhov retval = ext4_ind_map_blocks(handle, inode, map, flags & 528a4e5d88bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 5290e855ac8SAneesh Kumar K.V } 530729f52c6SZheng Liu if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 5314df3d265SAneesh Kumar K.V up_read((&EXT4_I(inode)->i_data_sem)); 532f5ab0d1fSMingming Cao 533e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 53451865fdaSZheng Liu int ret; 53551865fdaSZheng Liu if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) { 53651865fdaSZheng Liu /* delayed alloc may be allocated by fallocate and 53751865fdaSZheng Liu * coverted to initialized by directIO. 53851865fdaSZheng Liu * we need to handle delayed extent here. 53951865fdaSZheng Liu */ 54051865fdaSZheng Liu down_write((&EXT4_I(inode)->i_data_sem)); 54151865fdaSZheng Liu goto delayed_mapped; 54251865fdaSZheng Liu } 54351865fdaSZheng Liu ret = check_block_validity(inode, map); 5446fd058f7STheodore Ts'o if (ret != 0) 5456fd058f7STheodore Ts'o return ret; 5466fd058f7STheodore Ts'o } 5476fd058f7STheodore Ts'o 548f5ab0d1fSMingming Cao /* If it is only a block(s) look up */ 549c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 5504df3d265SAneesh Kumar K.V return retval; 5514df3d265SAneesh Kumar K.V 5524df3d265SAneesh Kumar K.V /* 553f5ab0d1fSMingming Cao * Returns if the blocks have already allocated 554f5ab0d1fSMingming Cao * 555f5ab0d1fSMingming Cao * Note that if blocks have been preallocated 556df3ab170STao Ma * ext4_ext_get_block() returns the create = 0 557f5ab0d1fSMingming Cao * with buffer head unmapped. 558f5ab0d1fSMingming Cao */ 559e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) 560f5ab0d1fSMingming Cao return retval; 561f5ab0d1fSMingming Cao 562f5ab0d1fSMingming Cao /* 5632a8964d6SAneesh Kumar K.V * When we call get_blocks without the create flag, the 5642a8964d6SAneesh Kumar K.V * BH_Unwritten flag could have gotten set if the blocks 5652a8964d6SAneesh Kumar K.V * requested were part of a uninitialized extent. We need to 5662a8964d6SAneesh Kumar K.V * clear this flag now that we are committed to convert all or 5672a8964d6SAneesh Kumar K.V * part of the uninitialized extent to be an initialized 5682a8964d6SAneesh Kumar K.V * extent. This is because we need to avoid the combination 5692a8964d6SAneesh Kumar K.V * of BH_Unwritten and BH_Mapped flags being simultaneously 5702a8964d6SAneesh Kumar K.V * set on the buffer_head. 5712a8964d6SAneesh Kumar K.V */ 572e35fd660STheodore Ts'o map->m_flags &= ~EXT4_MAP_UNWRITTEN; 5732a8964d6SAneesh Kumar K.V 5742a8964d6SAneesh Kumar K.V /* 575f5ab0d1fSMingming Cao * New blocks allocate and/or writing to uninitialized extent 576f5ab0d1fSMingming Cao * will possibly result in updating i_data, so we take 577f5ab0d1fSMingming Cao * the write lock of i_data_sem, and call get_blocks() 578f5ab0d1fSMingming Cao * with create == 1 flag. 5794df3d265SAneesh Kumar K.V */ 5804df3d265SAneesh Kumar K.V down_write((&EXT4_I(inode)->i_data_sem)); 581d2a17637SMingming Cao 582d2a17637SMingming Cao /* 583d2a17637SMingming Cao * if the caller is from delayed allocation writeout path 584d2a17637SMingming Cao * we have already reserved fs blocks for allocation 585d2a17637SMingming Cao * let the underlying get_block() function know to 586d2a17637SMingming Cao * avoid double accounting 587d2a17637SMingming Cao */ 588c2177057STheodore Ts'o if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 589f2321097STheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED); 5904df3d265SAneesh Kumar K.V /* 5914df3d265SAneesh Kumar K.V * We need to check for EXT4 here because migrate 5924df3d265SAneesh Kumar K.V * could have changed the inode type in between 5934df3d265SAneesh Kumar K.V */ 59412e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 595e35fd660STheodore Ts'o retval = ext4_ext_map_blocks(handle, inode, map, flags); 5960e855ac8SAneesh Kumar K.V } else { 597e35fd660STheodore Ts'o retval = ext4_ind_map_blocks(handle, inode, map, flags); 598267e4db9SAneesh Kumar K.V 599e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_NEW) { 600267e4db9SAneesh Kumar K.V /* 601267e4db9SAneesh Kumar K.V * We allocated new blocks which will result in 602267e4db9SAneesh Kumar K.V * i_data's format changing. Force the migrate 603267e4db9SAneesh Kumar K.V * to fail by clearing migrate flags 604267e4db9SAneesh Kumar K.V */ 60519f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE); 606267e4db9SAneesh Kumar K.V } 6072ac3b6e0STheodore Ts'o 608d2a17637SMingming Cao /* 6092ac3b6e0STheodore Ts'o * Update reserved blocks/metadata blocks after successful 6105f634d06SAneesh Kumar K.V * block allocation which had been deferred till now. We don't 6115f634d06SAneesh Kumar K.V * support fallocate for non extent files. So we can update 6125f634d06SAneesh Kumar K.V * reserve space here. 613d2a17637SMingming Cao */ 6145f634d06SAneesh Kumar K.V if ((retval > 0) && 6151296cc85SAneesh Kumar K.V (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)) 6165f634d06SAneesh Kumar K.V ext4_da_update_reserve_space(inode, retval, 1); 6175f634d06SAneesh Kumar K.V } 6185356f261SAditya Kali if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) { 619f2321097STheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED); 620d2a17637SMingming Cao 62151865fdaSZheng Liu if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 62251865fdaSZheng Liu int ret; 62351865fdaSZheng Liu delayed_mapped: 62451865fdaSZheng Liu /* delayed allocation blocks has been allocated */ 62551865fdaSZheng Liu ret = ext4_es_remove_extent(inode, map->m_lblk, 62651865fdaSZheng Liu map->m_len); 62751865fdaSZheng Liu if (ret < 0) 62851865fdaSZheng Liu retval = ret; 62951865fdaSZheng Liu } 6305356f261SAditya Kali } 6315356f261SAditya Kali 6320e855ac8SAneesh Kumar K.V up_write((&EXT4_I(inode)->i_data_sem)); 633e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 634e29136f8STheodore Ts'o int ret = check_block_validity(inode, map); 6356fd058f7STheodore Ts'o if (ret != 0) 6366fd058f7STheodore Ts'o return ret; 6376fd058f7STheodore Ts'o } 6380e855ac8SAneesh Kumar K.V return retval; 6390e855ac8SAneesh Kumar K.V } 6400e855ac8SAneesh Kumar K.V 641f3bd1f3fSMingming Cao /* Maximum number of blocks we map for direct IO at once. */ 642f3bd1f3fSMingming Cao #define DIO_MAX_BLOCKS 4096 643f3bd1f3fSMingming Cao 6442ed88685STheodore Ts'o static int _ext4_get_block(struct inode *inode, sector_t iblock, 6452ed88685STheodore Ts'o struct buffer_head *bh, int flags) 646ac27a0ecSDave Kleikamp { 6473e4fdaf8SDmitriy Monakhov handle_t *handle = ext4_journal_current_handle(); 6482ed88685STheodore Ts'o struct ext4_map_blocks map; 6497fb5409dSJan Kara int ret = 0, started = 0; 650f3bd1f3fSMingming Cao int dio_credits; 651ac27a0ecSDave Kleikamp 652*46c7f254STao Ma if (ext4_has_inline_data(inode)) 653*46c7f254STao Ma return -ERANGE; 654*46c7f254STao Ma 6552ed88685STheodore Ts'o map.m_lblk = iblock; 6562ed88685STheodore Ts'o map.m_len = bh->b_size >> inode->i_blkbits; 6572ed88685STheodore Ts'o 6588b0f165fSAnatol Pomozov if (flags && !(flags & EXT4_GET_BLOCKS_NO_LOCK) && !handle) { 6597fb5409dSJan Kara /* Direct IO write... */ 6602ed88685STheodore Ts'o if (map.m_len > DIO_MAX_BLOCKS) 6612ed88685STheodore Ts'o map.m_len = DIO_MAX_BLOCKS; 6622ed88685STheodore Ts'o dio_credits = ext4_chunk_trans_blocks(inode, map.m_len); 663f3bd1f3fSMingming Cao handle = ext4_journal_start(inode, dio_credits); 6647fb5409dSJan Kara if (IS_ERR(handle)) { 665ac27a0ecSDave Kleikamp ret = PTR_ERR(handle); 6662ed88685STheodore Ts'o return ret; 6677fb5409dSJan Kara } 6687fb5409dSJan Kara started = 1; 669ac27a0ecSDave Kleikamp } 670ac27a0ecSDave Kleikamp 6712ed88685STheodore Ts'o ret = ext4_map_blocks(handle, inode, &map, flags); 672ac27a0ecSDave Kleikamp if (ret > 0) { 6732ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 6742ed88685STheodore Ts'o bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; 6752ed88685STheodore Ts'o bh->b_size = inode->i_sb->s_blocksize * map.m_len; 676ac27a0ecSDave Kleikamp ret = 0; 677ac27a0ecSDave Kleikamp } 6787fb5409dSJan Kara if (started) 6797fb5409dSJan Kara ext4_journal_stop(handle); 680ac27a0ecSDave Kleikamp return ret; 681ac27a0ecSDave Kleikamp } 682ac27a0ecSDave Kleikamp 6832ed88685STheodore Ts'o int ext4_get_block(struct inode *inode, sector_t iblock, 6842ed88685STheodore Ts'o struct buffer_head *bh, int create) 6852ed88685STheodore Ts'o { 6862ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh, 6872ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 6882ed88685STheodore Ts'o } 6892ed88685STheodore Ts'o 690ac27a0ecSDave Kleikamp /* 691ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 692ac27a0ecSDave Kleikamp */ 693617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 694725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *errp) 695ac27a0ecSDave Kleikamp { 6962ed88685STheodore Ts'o struct ext4_map_blocks map; 6972ed88685STheodore Ts'o struct buffer_head *bh; 698ac27a0ecSDave Kleikamp int fatal = 0, err; 699ac27a0ecSDave Kleikamp 700ac27a0ecSDave Kleikamp J_ASSERT(handle != NULL || create == 0); 701ac27a0ecSDave Kleikamp 7022ed88685STheodore Ts'o map.m_lblk = block; 7032ed88685STheodore Ts'o map.m_len = 1; 7042ed88685STheodore Ts'o err = ext4_map_blocks(handle, inode, &map, 7052ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 7062ed88685STheodore Ts'o 70790b0a973SCarlos Maiolino /* ensure we send some value back into *errp */ 70890b0a973SCarlos Maiolino *errp = 0; 70990b0a973SCarlos Maiolino 7102ed88685STheodore Ts'o if (err < 0) 711ac27a0ecSDave Kleikamp *errp = err; 7122ed88685STheodore Ts'o if (err <= 0) 7132ed88685STheodore Ts'o return NULL; 7142ed88685STheodore Ts'o 7152ed88685STheodore Ts'o bh = sb_getblk(inode->i_sb, map.m_pblk); 716ac27a0ecSDave Kleikamp if (!bh) { 717ac27a0ecSDave Kleikamp *errp = -EIO; 7182ed88685STheodore Ts'o return NULL; 719ac27a0ecSDave Kleikamp } 7202ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 721ac27a0ecSDave Kleikamp J_ASSERT(create != 0); 722ac39849dSAneesh Kumar K.V J_ASSERT(handle != NULL); 723ac27a0ecSDave Kleikamp 724ac27a0ecSDave Kleikamp /* 725ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 726ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 727ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 728617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 729ac27a0ecSDave Kleikamp * problem. 730ac27a0ecSDave Kleikamp */ 731ac27a0ecSDave Kleikamp lock_buffer(bh); 732ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 733617ba13bSMingming Cao fatal = ext4_journal_get_create_access(handle, bh); 734ac27a0ecSDave Kleikamp if (!fatal && !buffer_uptodate(bh)) { 735ac27a0ecSDave Kleikamp memset(bh->b_data, 0, inode->i_sb->s_blocksize); 736ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 737ac27a0ecSDave Kleikamp } 738ac27a0ecSDave Kleikamp unlock_buffer(bh); 7390390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 7400390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 741ac27a0ecSDave Kleikamp if (!fatal) 742ac27a0ecSDave Kleikamp fatal = err; 743ac27a0ecSDave Kleikamp } else { 744ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "not a new buffer"); 745ac27a0ecSDave Kleikamp } 746ac27a0ecSDave Kleikamp if (fatal) { 747ac27a0ecSDave Kleikamp *errp = fatal; 748ac27a0ecSDave Kleikamp brelse(bh); 749ac27a0ecSDave Kleikamp bh = NULL; 750ac27a0ecSDave Kleikamp } 751ac27a0ecSDave Kleikamp return bh; 752ac27a0ecSDave Kleikamp } 753ac27a0ecSDave Kleikamp 754617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 755725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *err) 756ac27a0ecSDave Kleikamp { 757ac27a0ecSDave Kleikamp struct buffer_head *bh; 758ac27a0ecSDave Kleikamp 759617ba13bSMingming Cao bh = ext4_getblk(handle, inode, block, create, err); 760ac27a0ecSDave Kleikamp if (!bh) 761ac27a0ecSDave Kleikamp return bh; 762ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 763ac27a0ecSDave Kleikamp return bh; 76465299a3bSChristoph Hellwig ll_rw_block(READ | REQ_META | REQ_PRIO, 1, &bh); 765ac27a0ecSDave Kleikamp wait_on_buffer(bh); 766ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 767ac27a0ecSDave Kleikamp return bh; 768ac27a0ecSDave Kleikamp put_bh(bh); 769ac27a0ecSDave Kleikamp *err = -EIO; 770ac27a0ecSDave Kleikamp return NULL; 771ac27a0ecSDave Kleikamp } 772ac27a0ecSDave Kleikamp 773ac27a0ecSDave Kleikamp static int walk_page_buffers(handle_t *handle, 774ac27a0ecSDave Kleikamp struct buffer_head *head, 775ac27a0ecSDave Kleikamp unsigned from, 776ac27a0ecSDave Kleikamp unsigned to, 777ac27a0ecSDave Kleikamp int *partial, 778ac27a0ecSDave Kleikamp int (*fn)(handle_t *handle, 779ac27a0ecSDave Kleikamp struct buffer_head *bh)) 780ac27a0ecSDave Kleikamp { 781ac27a0ecSDave Kleikamp struct buffer_head *bh; 782ac27a0ecSDave Kleikamp unsigned block_start, block_end; 783ac27a0ecSDave Kleikamp unsigned blocksize = head->b_size; 784ac27a0ecSDave Kleikamp int err, ret = 0; 785ac27a0ecSDave Kleikamp struct buffer_head *next; 786ac27a0ecSDave Kleikamp 787ac27a0ecSDave Kleikamp for (bh = head, block_start = 0; 788ac27a0ecSDave Kleikamp ret == 0 && (bh != head || !block_start); 789de9a55b8STheodore Ts'o block_start = block_end, bh = next) { 790ac27a0ecSDave Kleikamp next = bh->b_this_page; 791ac27a0ecSDave Kleikamp block_end = block_start + blocksize; 792ac27a0ecSDave Kleikamp if (block_end <= from || block_start >= to) { 793ac27a0ecSDave Kleikamp if (partial && !buffer_uptodate(bh)) 794ac27a0ecSDave Kleikamp *partial = 1; 795ac27a0ecSDave Kleikamp continue; 796ac27a0ecSDave Kleikamp } 797ac27a0ecSDave Kleikamp err = (*fn)(handle, bh); 798ac27a0ecSDave Kleikamp if (!ret) 799ac27a0ecSDave Kleikamp ret = err; 800ac27a0ecSDave Kleikamp } 801ac27a0ecSDave Kleikamp return ret; 802ac27a0ecSDave Kleikamp } 803ac27a0ecSDave Kleikamp 804ac27a0ecSDave Kleikamp /* 805ac27a0ecSDave Kleikamp * To preserve ordering, it is essential that the hole instantiation and 806ac27a0ecSDave Kleikamp * the data write be encapsulated in a single transaction. We cannot 807617ba13bSMingming Cao * close off a transaction and start a new one between the ext4_get_block() 808dab291afSMingming Cao * and the commit_write(). So doing the jbd2_journal_start at the start of 809ac27a0ecSDave Kleikamp * prepare_write() is the right place. 810ac27a0ecSDave Kleikamp * 811617ba13bSMingming Cao * Also, this function can nest inside ext4_writepage() -> 812617ba13bSMingming Cao * block_write_full_page(). In that case, we *know* that ext4_writepage() 813ac27a0ecSDave Kleikamp * has generated enough buffer credits to do the whole page. So we won't 814ac27a0ecSDave Kleikamp * block on the journal in that case, which is good, because the caller may 815ac27a0ecSDave Kleikamp * be PF_MEMALLOC. 816ac27a0ecSDave Kleikamp * 817617ba13bSMingming Cao * By accident, ext4 can be reentered when a transaction is open via 818ac27a0ecSDave Kleikamp * quota file writes. If we were to commit the transaction while thus 819ac27a0ecSDave Kleikamp * reentered, there can be a deadlock - we would be holding a quota 820ac27a0ecSDave Kleikamp * lock, and the commit would never complete if another thread had a 821ac27a0ecSDave Kleikamp * transaction open and was blocking on the quota lock - a ranking 822ac27a0ecSDave Kleikamp * violation. 823ac27a0ecSDave Kleikamp * 824dab291afSMingming Cao * So what we do is to rely on the fact that jbd2_journal_stop/journal_start 825ac27a0ecSDave Kleikamp * will _not_ run commit under these circumstances because handle->h_ref 826ac27a0ecSDave Kleikamp * is elevated. We'll still have enough credits for the tiny quotafile 827ac27a0ecSDave Kleikamp * write. 828ac27a0ecSDave Kleikamp */ 829ac27a0ecSDave Kleikamp static int do_journal_get_write_access(handle_t *handle, 830ac27a0ecSDave Kleikamp struct buffer_head *bh) 831ac27a0ecSDave Kleikamp { 83256d35a4cSJan Kara int dirty = buffer_dirty(bh); 83356d35a4cSJan Kara int ret; 83456d35a4cSJan Kara 835ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 836ac27a0ecSDave Kleikamp return 0; 83756d35a4cSJan Kara /* 838ebdec241SChristoph Hellwig * __block_write_begin() could have dirtied some buffers. Clean 83956d35a4cSJan Kara * the dirty bit as jbd2_journal_get_write_access() could complain 84056d35a4cSJan Kara * otherwise about fs integrity issues. Setting of the dirty bit 841ebdec241SChristoph Hellwig * by __block_write_begin() isn't a real problem here as we clear 84256d35a4cSJan Kara * the bit before releasing a page lock and thus writeback cannot 84356d35a4cSJan Kara * ever write the buffer. 84456d35a4cSJan Kara */ 84556d35a4cSJan Kara if (dirty) 84656d35a4cSJan Kara clear_buffer_dirty(bh); 84756d35a4cSJan Kara ret = ext4_journal_get_write_access(handle, bh); 84856d35a4cSJan Kara if (!ret && dirty) 84956d35a4cSJan Kara ret = ext4_handle_dirty_metadata(handle, NULL, bh); 85056d35a4cSJan Kara return ret; 851ac27a0ecSDave Kleikamp } 852ac27a0ecSDave Kleikamp 853744692dcSJiaying Zhang static int ext4_get_block_write(struct inode *inode, sector_t iblock, 854744692dcSJiaying Zhang struct buffer_head *bh_result, int create); 8558b0f165fSAnatol Pomozov static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock, 8568b0f165fSAnatol Pomozov struct buffer_head *bh_result, int create); 857bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 858bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned flags, 859bfc1af65SNick Piggin struct page **pagep, void **fsdata) 860ac27a0ecSDave Kleikamp { 861bfc1af65SNick Piggin struct inode *inode = mapping->host; 8621938a150SAneesh Kumar K.V int ret, needed_blocks; 863ac27a0ecSDave Kleikamp handle_t *handle; 864ac27a0ecSDave Kleikamp int retries = 0; 865bfc1af65SNick Piggin struct page *page; 866bfc1af65SNick Piggin pgoff_t index; 867bfc1af65SNick Piggin unsigned from, to; 868bfc1af65SNick Piggin 8699bffad1eSTheodore Ts'o trace_ext4_write_begin(inode, pos, len, flags); 8701938a150SAneesh Kumar K.V /* 8711938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 8721938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 8731938a150SAneesh Kumar K.V */ 8741938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 875bfc1af65SNick Piggin index = pos >> PAGE_CACHE_SHIFT; 876bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 877bfc1af65SNick Piggin to = from + len; 878ac27a0ecSDave Kleikamp 879ac27a0ecSDave Kleikamp retry: 880617ba13bSMingming Cao handle = ext4_journal_start(inode, needed_blocks); 8817479d2b9SAndrew Morton if (IS_ERR(handle)) { 8827479d2b9SAndrew Morton ret = PTR_ERR(handle); 8837479d2b9SAndrew Morton goto out; 8847479d2b9SAndrew Morton } 885ac27a0ecSDave Kleikamp 886ebd3610bSJan Kara /* We cannot recurse into the filesystem as the transaction is already 887ebd3610bSJan Kara * started */ 888ebd3610bSJan Kara flags |= AOP_FLAG_NOFS; 889ebd3610bSJan Kara 89054566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 891cf108bcaSJan Kara if (!page) { 892cf108bcaSJan Kara ext4_journal_stop(handle); 893cf108bcaSJan Kara ret = -ENOMEM; 894cf108bcaSJan Kara goto out; 895cf108bcaSJan Kara } 896cf108bcaSJan Kara *pagep = page; 897cf108bcaSJan Kara 898744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 8996e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block_write); 900744692dcSJiaying Zhang else 9016e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block); 902bfc1af65SNick Piggin 903bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 904ac27a0ecSDave Kleikamp ret = walk_page_buffers(handle, page_buffers(page), 905ac27a0ecSDave Kleikamp from, to, NULL, do_journal_get_write_access); 906b46be050SAndrey Savochkin } 907bfc1af65SNick Piggin 908bfc1af65SNick Piggin if (ret) { 909bfc1af65SNick Piggin unlock_page(page); 910bfc1af65SNick Piggin page_cache_release(page); 911ae4d5372SAneesh Kumar K.V /* 9126e1db88dSChristoph Hellwig * __block_write_begin may have instantiated a few blocks 913ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 914ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 9151938a150SAneesh Kumar K.V * 9161938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 9171938a150SAneesh Kumar K.V * truncate finishes 918ae4d5372SAneesh Kumar K.V */ 919ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 9201938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 9211938a150SAneesh Kumar K.V 9221938a150SAneesh Kumar K.V ext4_journal_stop(handle); 9231938a150SAneesh Kumar K.V if (pos + len > inode->i_size) { 924b9a4207dSJan Kara ext4_truncate_failed_write(inode); 9251938a150SAneesh Kumar K.V /* 926ffacfa7aSJan Kara * If truncate failed early the inode might 9271938a150SAneesh Kumar K.V * still be on the orphan list; we need to 9281938a150SAneesh Kumar K.V * make sure the inode is removed from the 9291938a150SAneesh Kumar K.V * orphan list in that case. 9301938a150SAneesh Kumar K.V */ 9311938a150SAneesh Kumar K.V if (inode->i_nlink) 9321938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 9331938a150SAneesh Kumar K.V } 934bfc1af65SNick Piggin } 935bfc1af65SNick Piggin 936617ba13bSMingming Cao if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 937ac27a0ecSDave Kleikamp goto retry; 9387479d2b9SAndrew Morton out: 939ac27a0ecSDave Kleikamp return ret; 940ac27a0ecSDave Kleikamp } 941ac27a0ecSDave Kleikamp 942bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 943bfc1af65SNick Piggin static int write_end_fn(handle_t *handle, struct buffer_head *bh) 944ac27a0ecSDave Kleikamp { 945ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 946ac27a0ecSDave Kleikamp return 0; 947ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 9480390131bSFrank Mayhar return ext4_handle_dirty_metadata(handle, NULL, bh); 949ac27a0ecSDave Kleikamp } 950ac27a0ecSDave Kleikamp 951f8514083SAneesh Kumar K.V static int ext4_generic_write_end(struct file *file, 952f8514083SAneesh Kumar K.V struct address_space *mapping, 953f8514083SAneesh Kumar K.V loff_t pos, unsigned len, unsigned copied, 954f8514083SAneesh Kumar K.V struct page *page, void *fsdata) 955f8514083SAneesh Kumar K.V { 956f8514083SAneesh Kumar K.V int i_size_changed = 0; 957f8514083SAneesh Kumar K.V struct inode *inode = mapping->host; 958f8514083SAneesh Kumar K.V handle_t *handle = ext4_journal_current_handle(); 959f8514083SAneesh Kumar K.V 960f8514083SAneesh Kumar K.V copied = block_write_end(file, mapping, pos, len, copied, page, fsdata); 961f8514083SAneesh Kumar K.V 962f8514083SAneesh Kumar K.V /* 963f8514083SAneesh Kumar K.V * No need to use i_size_read() here, the i_size 964f8514083SAneesh Kumar K.V * cannot change under us because we hold i_mutex. 965f8514083SAneesh Kumar K.V * 966f8514083SAneesh Kumar K.V * But it's important to update i_size while still holding page lock: 967f8514083SAneesh Kumar K.V * page writeout could otherwise come in and zero beyond i_size. 968f8514083SAneesh Kumar K.V */ 969f8514083SAneesh Kumar K.V if (pos + copied > inode->i_size) { 970f8514083SAneesh Kumar K.V i_size_write(inode, pos + copied); 971f8514083SAneesh Kumar K.V i_size_changed = 1; 972f8514083SAneesh Kumar K.V } 973f8514083SAneesh Kumar K.V 974f8514083SAneesh Kumar K.V if (pos + copied > EXT4_I(inode)->i_disksize) { 975f8514083SAneesh Kumar K.V /* We need to mark inode dirty even if 976f8514083SAneesh Kumar K.V * new_i_size is less that inode->i_size 977f8514083SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 978f8514083SAneesh Kumar K.V */ 979f8514083SAneesh Kumar K.V ext4_update_i_disksize(inode, (pos + copied)); 980f8514083SAneesh Kumar K.V i_size_changed = 1; 981f8514083SAneesh Kumar K.V } 982f8514083SAneesh Kumar K.V unlock_page(page); 983f8514083SAneesh Kumar K.V page_cache_release(page); 984f8514083SAneesh Kumar K.V 985f8514083SAneesh Kumar K.V /* 986f8514083SAneesh Kumar K.V * Don't mark the inode dirty under page lock. First, it unnecessarily 987f8514083SAneesh Kumar K.V * makes the holding time of page lock longer. Second, it forces lock 988f8514083SAneesh Kumar K.V * ordering of page lock and transaction start for journaling 989f8514083SAneesh Kumar K.V * filesystems. 990f8514083SAneesh Kumar K.V */ 991f8514083SAneesh Kumar K.V if (i_size_changed) 992f8514083SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 993f8514083SAneesh Kumar K.V 994f8514083SAneesh Kumar K.V return copied; 995f8514083SAneesh Kumar K.V } 996f8514083SAneesh Kumar K.V 997ac27a0ecSDave Kleikamp /* 998ac27a0ecSDave Kleikamp * We need to pick up the new inode size which generic_commit_write gave us 999ac27a0ecSDave Kleikamp * `file' can be NULL - eg, when called from page_symlink(). 1000ac27a0ecSDave Kleikamp * 1001617ba13bSMingming Cao * ext4 never places buffers on inode->i_mapping->private_list. metadata 1002ac27a0ecSDave Kleikamp * buffers are managed internally. 1003ac27a0ecSDave Kleikamp */ 1004bfc1af65SNick Piggin static int ext4_ordered_write_end(struct file *file, 1005bfc1af65SNick Piggin struct address_space *mapping, 1006bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1007bfc1af65SNick Piggin struct page *page, void *fsdata) 1008ac27a0ecSDave Kleikamp { 1009617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1010cf108bcaSJan Kara struct inode *inode = mapping->host; 1011ac27a0ecSDave Kleikamp int ret = 0, ret2; 1012ac27a0ecSDave Kleikamp 10139bffad1eSTheodore Ts'o trace_ext4_ordered_write_end(inode, pos, len, copied); 1014678aaf48SJan Kara ret = ext4_jbd2_file_inode(handle, inode); 1015ac27a0ecSDave Kleikamp 1016ac27a0ecSDave Kleikamp if (ret == 0) { 1017f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 1018bfc1af65SNick Piggin page, fsdata); 1019f8a87d89SRoel Kluin copied = ret2; 1020ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1021f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1022f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1023f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1024f8514083SAneesh Kumar K.V */ 1025f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1026f8a87d89SRoel Kluin if (ret2 < 0) 1027f8a87d89SRoel Kluin ret = ret2; 102809e0834fSAkira Fujita } else { 102909e0834fSAkira Fujita unlock_page(page); 103009e0834fSAkira Fujita page_cache_release(page); 1031ac27a0ecSDave Kleikamp } 103209e0834fSAkira Fujita 1033617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1034ac27a0ecSDave Kleikamp if (!ret) 1035ac27a0ecSDave Kleikamp ret = ret2; 1036bfc1af65SNick Piggin 1037f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1038b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1039f8514083SAneesh Kumar K.V /* 1040ffacfa7aSJan Kara * If truncate failed early the inode might still be 1041f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1042f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1043f8514083SAneesh Kumar K.V */ 1044f8514083SAneesh Kumar K.V if (inode->i_nlink) 1045f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1046f8514083SAneesh Kumar K.V } 1047f8514083SAneesh Kumar K.V 1048f8514083SAneesh Kumar K.V 1049bfc1af65SNick Piggin return ret ? ret : copied; 1050ac27a0ecSDave Kleikamp } 1051ac27a0ecSDave Kleikamp 1052bfc1af65SNick Piggin static int ext4_writeback_write_end(struct file *file, 1053bfc1af65SNick Piggin struct address_space *mapping, 1054bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1055bfc1af65SNick Piggin struct page *page, void *fsdata) 1056ac27a0ecSDave Kleikamp { 1057617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1058cf108bcaSJan Kara struct inode *inode = mapping->host; 1059ac27a0ecSDave Kleikamp int ret = 0, ret2; 1060ac27a0ecSDave Kleikamp 10619bffad1eSTheodore Ts'o trace_ext4_writeback_write_end(inode, pos, len, copied); 1062f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 1063bfc1af65SNick Piggin page, fsdata); 1064f8a87d89SRoel Kluin copied = ret2; 1065ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1066f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1067f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1068f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1069f8514083SAneesh Kumar K.V */ 1070f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1071f8514083SAneesh Kumar K.V 1072f8a87d89SRoel Kluin if (ret2 < 0) 1073f8a87d89SRoel Kluin ret = ret2; 1074ac27a0ecSDave Kleikamp 1075617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1076ac27a0ecSDave Kleikamp if (!ret) 1077ac27a0ecSDave Kleikamp ret = ret2; 1078bfc1af65SNick Piggin 1079f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1080b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1081f8514083SAneesh Kumar K.V /* 1082ffacfa7aSJan Kara * If truncate failed early the inode might still be 1083f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1084f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1085f8514083SAneesh Kumar K.V */ 1086f8514083SAneesh Kumar K.V if (inode->i_nlink) 1087f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1088f8514083SAneesh Kumar K.V } 1089f8514083SAneesh Kumar K.V 1090bfc1af65SNick Piggin return ret ? ret : copied; 1091ac27a0ecSDave Kleikamp } 1092ac27a0ecSDave Kleikamp 1093bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 1094bfc1af65SNick Piggin struct address_space *mapping, 1095bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1096bfc1af65SNick Piggin struct page *page, void *fsdata) 1097ac27a0ecSDave Kleikamp { 1098617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1099bfc1af65SNick Piggin struct inode *inode = mapping->host; 1100ac27a0ecSDave Kleikamp int ret = 0, ret2; 1101ac27a0ecSDave Kleikamp int partial = 0; 1102bfc1af65SNick Piggin unsigned from, to; 1103cf17fea6SAneesh Kumar K.V loff_t new_i_size; 1104ac27a0ecSDave Kleikamp 11059bffad1eSTheodore Ts'o trace_ext4_journalled_write_end(inode, pos, len, copied); 1106bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 1107bfc1af65SNick Piggin to = from + len; 1108bfc1af65SNick Piggin 1109441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1110441c8508SCurt Wohlgemuth 1111bfc1af65SNick Piggin if (copied < len) { 1112bfc1af65SNick Piggin if (!PageUptodate(page)) 1113bfc1af65SNick Piggin copied = 0; 1114bfc1af65SNick Piggin page_zero_new_buffers(page, from+copied, to); 1115bfc1af65SNick Piggin } 1116ac27a0ecSDave Kleikamp 1117ac27a0ecSDave Kleikamp ret = walk_page_buffers(handle, page_buffers(page), from, 1118bfc1af65SNick Piggin to, &partial, write_end_fn); 1119ac27a0ecSDave Kleikamp if (!partial) 1120ac27a0ecSDave Kleikamp SetPageUptodate(page); 1121cf17fea6SAneesh Kumar K.V new_i_size = pos + copied; 1122cf17fea6SAneesh Kumar K.V if (new_i_size > inode->i_size) 1123bfc1af65SNick Piggin i_size_write(inode, pos+copied); 112419f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 11252d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 1126cf17fea6SAneesh Kumar K.V if (new_i_size > EXT4_I(inode)->i_disksize) { 1127cf17fea6SAneesh Kumar K.V ext4_update_i_disksize(inode, new_i_size); 1128617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1129ac27a0ecSDave Kleikamp if (!ret) 1130ac27a0ecSDave Kleikamp ret = ret2; 1131ac27a0ecSDave Kleikamp } 1132bfc1af65SNick Piggin 1133cf108bcaSJan Kara unlock_page(page); 1134f8514083SAneesh Kumar K.V page_cache_release(page); 1135ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1136f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1137f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1138f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1139f8514083SAneesh Kumar K.V */ 1140f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1141f8514083SAneesh Kumar K.V 1142617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1143ac27a0ecSDave Kleikamp if (!ret) 1144ac27a0ecSDave Kleikamp ret = ret2; 1145f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1146b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1147f8514083SAneesh Kumar K.V /* 1148ffacfa7aSJan Kara * If truncate failed early the inode might still be 1149f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1150f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1151f8514083SAneesh Kumar K.V */ 1152f8514083SAneesh Kumar K.V if (inode->i_nlink) 1153f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1154f8514083SAneesh Kumar K.V } 1155bfc1af65SNick Piggin 1156bfc1af65SNick Piggin return ret ? ret : copied; 1157ac27a0ecSDave Kleikamp } 1158d2a17637SMingming Cao 11599d0be502STheodore Ts'o /* 11607b415bf6SAditya Kali * Reserve a single cluster located at lblock 11619d0be502STheodore Ts'o */ 116201f49d0bSTheodore Ts'o static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock) 1163d2a17637SMingming Cao { 1164030ba6bcSAneesh Kumar K.V int retries = 0; 1165d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 11660637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 11677b415bf6SAditya Kali unsigned int md_needed; 11685dd4056dSChristoph Hellwig int ret; 116903179fe9STheodore Ts'o ext4_lblk_t save_last_lblock; 117003179fe9STheodore Ts'o int save_len; 1171d2a17637SMingming Cao 117260e58e0fSMingming Cao /* 117372b8ab9dSEric Sandeen * We will charge metadata quota at writeout time; this saves 117472b8ab9dSEric Sandeen * us from metadata over-estimation, though we may go over by 117572b8ab9dSEric Sandeen * a small amount in the end. Here we just reserve for data. 117660e58e0fSMingming Cao */ 11777b415bf6SAditya Kali ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1)); 11785dd4056dSChristoph Hellwig if (ret) 11795dd4056dSChristoph Hellwig return ret; 118003179fe9STheodore Ts'o 118103179fe9STheodore Ts'o /* 118203179fe9STheodore Ts'o * recalculate the amount of metadata blocks to reserve 118303179fe9STheodore Ts'o * in order to allocate nrblocks 118403179fe9STheodore Ts'o * worse case is one extent per block 118503179fe9STheodore Ts'o */ 118603179fe9STheodore Ts'o repeat: 118703179fe9STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 118803179fe9STheodore Ts'o /* 118903179fe9STheodore Ts'o * ext4_calc_metadata_amount() has side effects, which we have 119003179fe9STheodore Ts'o * to be prepared undo if we fail to claim space. 119103179fe9STheodore Ts'o */ 119203179fe9STheodore Ts'o save_len = ei->i_da_metadata_calc_len; 119303179fe9STheodore Ts'o save_last_lblock = ei->i_da_metadata_calc_last_lblock; 119403179fe9STheodore Ts'o md_needed = EXT4_NUM_B2C(sbi, 119503179fe9STheodore Ts'o ext4_calc_metadata_amount(inode, lblock)); 119603179fe9STheodore Ts'o trace_ext4_da_reserve_space(inode, md_needed); 119703179fe9STheodore Ts'o 119872b8ab9dSEric Sandeen /* 119972b8ab9dSEric Sandeen * We do still charge estimated metadata to the sb though; 120072b8ab9dSEric Sandeen * we cannot afford to run out of free blocks. 120172b8ab9dSEric Sandeen */ 1202e7d5f315STheodore Ts'o if (ext4_claim_free_clusters(sbi, md_needed + 1, 0)) { 120303179fe9STheodore Ts'o ei->i_da_metadata_calc_len = save_len; 120403179fe9STheodore Ts'o ei->i_da_metadata_calc_last_lblock = save_last_lblock; 120503179fe9STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 1206030ba6bcSAneesh Kumar K.V if (ext4_should_retry_alloc(inode->i_sb, &retries)) { 1207030ba6bcSAneesh Kumar K.V yield(); 1208030ba6bcSAneesh Kumar K.V goto repeat; 1209030ba6bcSAneesh Kumar K.V } 121003179fe9STheodore Ts'o dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1)); 1211d2a17637SMingming Cao return -ENOSPC; 1212d2a17637SMingming Cao } 12139d0be502STheodore Ts'o ei->i_reserved_data_blocks++; 12140637c6f4STheodore Ts'o ei->i_reserved_meta_blocks += md_needed; 12150637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 121639bc680aSDmitry Monakhov 1217d2a17637SMingming Cao return 0; /* success */ 1218d2a17637SMingming Cao } 1219d2a17637SMingming Cao 122012219aeaSAneesh Kumar K.V static void ext4_da_release_space(struct inode *inode, int to_free) 1221d2a17637SMingming Cao { 1222d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 12230637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 1224d2a17637SMingming Cao 1225cd213226SMingming Cao if (!to_free) 1226cd213226SMingming Cao return; /* Nothing to release, exit */ 1227cd213226SMingming Cao 1228d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1229cd213226SMingming Cao 12305a58ec87SLi Zefan trace_ext4_da_release_space(inode, to_free); 12310637c6f4STheodore Ts'o if (unlikely(to_free > ei->i_reserved_data_blocks)) { 1232cd213226SMingming Cao /* 12330637c6f4STheodore Ts'o * if there aren't enough reserved blocks, then the 12340637c6f4STheodore Ts'o * counter is messed up somewhere. Since this 12350637c6f4STheodore Ts'o * function is called from invalidate page, it's 12360637c6f4STheodore Ts'o * harmless to return without any action. 1237cd213226SMingming Cao */ 12380637c6f4STheodore Ts'o ext4_msg(inode->i_sb, KERN_NOTICE, "ext4_da_release_space: " 12390637c6f4STheodore Ts'o "ino %lu, to_free %d with only %d reserved " 12401084f252STheodore Ts'o "data blocks", inode->i_ino, to_free, 12410637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 12420637c6f4STheodore Ts'o WARN_ON(1); 12430637c6f4STheodore Ts'o to_free = ei->i_reserved_data_blocks; 12440637c6f4STheodore Ts'o } 12450637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= to_free; 12460637c6f4STheodore Ts'o 12470637c6f4STheodore Ts'o if (ei->i_reserved_data_blocks == 0) { 12480637c6f4STheodore Ts'o /* 12490637c6f4STheodore Ts'o * We can release all of the reserved metadata blocks 12500637c6f4STheodore Ts'o * only when we have written all of the delayed 12510637c6f4STheodore Ts'o * allocation blocks. 12527b415bf6SAditya Kali * Note that in case of bigalloc, i_reserved_meta_blocks, 12537b415bf6SAditya Kali * i_reserved_data_blocks, etc. refer to number of clusters. 12540637c6f4STheodore Ts'o */ 125557042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 125672b8ab9dSEric Sandeen ei->i_reserved_meta_blocks); 1257ee5f4d9cSTheodore Ts'o ei->i_reserved_meta_blocks = 0; 12589d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 1259cd213226SMingming Cao } 1260cd213226SMingming Cao 126172b8ab9dSEric Sandeen /* update fs dirty data blocks counter */ 126257042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free); 1263d2a17637SMingming Cao 1264d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 126560e58e0fSMingming Cao 12667b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free)); 1267d2a17637SMingming Cao } 1268d2a17637SMingming Cao 1269d2a17637SMingming Cao static void ext4_da_page_release_reservation(struct page *page, 1270d2a17637SMingming Cao unsigned long offset) 1271d2a17637SMingming Cao { 1272d2a17637SMingming Cao int to_release = 0; 1273d2a17637SMingming Cao struct buffer_head *head, *bh; 1274d2a17637SMingming Cao unsigned int curr_off = 0; 12757b415bf6SAditya Kali struct inode *inode = page->mapping->host; 12767b415bf6SAditya Kali struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 12777b415bf6SAditya Kali int num_clusters; 127851865fdaSZheng Liu ext4_fsblk_t lblk; 1279d2a17637SMingming Cao 1280d2a17637SMingming Cao head = page_buffers(page); 1281d2a17637SMingming Cao bh = head; 1282d2a17637SMingming Cao do { 1283d2a17637SMingming Cao unsigned int next_off = curr_off + bh->b_size; 1284d2a17637SMingming Cao 1285d2a17637SMingming Cao if ((offset <= curr_off) && (buffer_delay(bh))) { 1286d2a17637SMingming Cao to_release++; 1287d2a17637SMingming Cao clear_buffer_delay(bh); 1288d2a17637SMingming Cao } 1289d2a17637SMingming Cao curr_off = next_off; 1290d2a17637SMingming Cao } while ((bh = bh->b_this_page) != head); 12917b415bf6SAditya Kali 129251865fdaSZheng Liu if (to_release) { 129351865fdaSZheng Liu lblk = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits); 129451865fdaSZheng Liu ext4_es_remove_extent(inode, lblk, to_release); 129551865fdaSZheng Liu } 129651865fdaSZheng Liu 12977b415bf6SAditya Kali /* If we have released all the blocks belonging to a cluster, then we 12987b415bf6SAditya Kali * need to release the reserved space for that cluster. */ 12997b415bf6SAditya Kali num_clusters = EXT4_NUM_B2C(sbi, to_release); 13007b415bf6SAditya Kali while (num_clusters > 0) { 13017b415bf6SAditya Kali lblk = (page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits)) + 13027b415bf6SAditya Kali ((num_clusters - 1) << sbi->s_cluster_bits); 13037b415bf6SAditya Kali if (sbi->s_cluster_ratio == 1 || 13047d1b1fbcSZheng Liu !ext4_find_delalloc_cluster(inode, lblk)) 13057b415bf6SAditya Kali ext4_da_release_space(inode, 1); 13067b415bf6SAditya Kali 13077b415bf6SAditya Kali num_clusters--; 13087b415bf6SAditya Kali } 1309d2a17637SMingming Cao } 1310ac27a0ecSDave Kleikamp 1311ac27a0ecSDave Kleikamp /* 131264769240SAlex Tomas * Delayed allocation stuff 131364769240SAlex Tomas */ 131464769240SAlex Tomas 131564769240SAlex Tomas /* 131664769240SAlex Tomas * mpage_da_submit_io - walks through extent of pages and try to write 1317a1d6cc56SAneesh Kumar K.V * them with writepage() call back 131864769240SAlex Tomas * 131964769240SAlex Tomas * @mpd->inode: inode 132064769240SAlex Tomas * @mpd->first_page: first page of the extent 132164769240SAlex Tomas * @mpd->next_page: page after the last page of the extent 132264769240SAlex Tomas * 132364769240SAlex Tomas * By the time mpage_da_submit_io() is called we expect all blocks 132464769240SAlex Tomas * to be allocated. this may be wrong if allocation failed. 132564769240SAlex Tomas * 132664769240SAlex Tomas * As pages are already locked by write_cache_pages(), we can't use it 132764769240SAlex Tomas */ 13281de3e3dfSTheodore Ts'o static int mpage_da_submit_io(struct mpage_da_data *mpd, 13291de3e3dfSTheodore Ts'o struct ext4_map_blocks *map) 133064769240SAlex Tomas { 1331791b7f08SAneesh Kumar K.V struct pagevec pvec; 1332791b7f08SAneesh Kumar K.V unsigned long index, end; 1333791b7f08SAneesh Kumar K.V int ret = 0, err, nr_pages, i; 1334791b7f08SAneesh Kumar K.V struct inode *inode = mpd->inode; 1335791b7f08SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 1336cb20d518STheodore Ts'o loff_t size = i_size_read(inode); 13373ecdb3a1STheodore Ts'o unsigned int len, block_start; 13383ecdb3a1STheodore Ts'o struct buffer_head *bh, *page_bufs = NULL; 1339cb20d518STheodore Ts'o int journal_data = ext4_should_journal_data(inode); 13401de3e3dfSTheodore Ts'o sector_t pblock = 0, cur_logical = 0; 1341bd2d0210STheodore Ts'o struct ext4_io_submit io_submit; 134264769240SAlex Tomas 134364769240SAlex Tomas BUG_ON(mpd->next_page <= mpd->first_page); 1344bd2d0210STheodore Ts'o memset(&io_submit, 0, sizeof(io_submit)); 1345791b7f08SAneesh Kumar K.V /* 1346791b7f08SAneesh Kumar K.V * We need to start from the first_page to the next_page - 1 1347791b7f08SAneesh Kumar K.V * to make sure we also write the mapped dirty buffer_heads. 13488dc207c0STheodore Ts'o * If we look at mpd->b_blocknr we would only be looking 1349791b7f08SAneesh Kumar K.V * at the currently mapped buffer_heads. 1350791b7f08SAneesh Kumar K.V */ 135164769240SAlex Tomas index = mpd->first_page; 135264769240SAlex Tomas end = mpd->next_page - 1; 135364769240SAlex Tomas 1354791b7f08SAneesh Kumar K.V pagevec_init(&pvec, 0); 135564769240SAlex Tomas while (index <= end) { 1356791b7f08SAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 135764769240SAlex Tomas if (nr_pages == 0) 135864769240SAlex Tomas break; 135964769240SAlex Tomas for (i = 0; i < nr_pages; i++) { 136097498956STheodore Ts'o int commit_write = 0, skip_page = 0; 136164769240SAlex Tomas struct page *page = pvec.pages[i]; 136264769240SAlex Tomas 1363791b7f08SAneesh Kumar K.V index = page->index; 1364791b7f08SAneesh Kumar K.V if (index > end) 1365791b7f08SAneesh Kumar K.V break; 1366cb20d518STheodore Ts'o 1367cb20d518STheodore Ts'o if (index == size >> PAGE_CACHE_SHIFT) 1368cb20d518STheodore Ts'o len = size & ~PAGE_CACHE_MASK; 1369cb20d518STheodore Ts'o else 1370cb20d518STheodore Ts'o len = PAGE_CACHE_SIZE; 13711de3e3dfSTheodore Ts'o if (map) { 13721de3e3dfSTheodore Ts'o cur_logical = index << (PAGE_CACHE_SHIFT - 13731de3e3dfSTheodore Ts'o inode->i_blkbits); 13741de3e3dfSTheodore Ts'o pblock = map->m_pblk + (cur_logical - 13751de3e3dfSTheodore Ts'o map->m_lblk); 13761de3e3dfSTheodore Ts'o } 1377791b7f08SAneesh Kumar K.V index++; 1378791b7f08SAneesh Kumar K.V 1379791b7f08SAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1380791b7f08SAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1381791b7f08SAneesh Kumar K.V 138222208dedSAneesh Kumar K.V /* 1383cb20d518STheodore Ts'o * If the page does not have buffers (for 1384cb20d518STheodore Ts'o * whatever reason), try to create them using 1385a107e5a3STheodore Ts'o * __block_write_begin. If this fails, 138697498956STheodore Ts'o * skip the page and move on. 138722208dedSAneesh Kumar K.V */ 1388cb20d518STheodore Ts'o if (!page_has_buffers(page)) { 1389a107e5a3STheodore Ts'o if (__block_write_begin(page, 0, len, 1390cb20d518STheodore Ts'o noalloc_get_block_write)) { 139197498956STheodore Ts'o skip_page: 1392cb20d518STheodore Ts'o unlock_page(page); 1393cb20d518STheodore Ts'o continue; 1394cb20d518STheodore Ts'o } 1395cb20d518STheodore Ts'o commit_write = 1; 1396cb20d518STheodore Ts'o } 13973ecdb3a1STheodore Ts'o 13983ecdb3a1STheodore Ts'o bh = page_bufs = page_buffers(page); 13993ecdb3a1STheodore Ts'o block_start = 0; 14003ecdb3a1STheodore Ts'o do { 14011de3e3dfSTheodore Ts'o if (!bh) 140297498956STheodore Ts'o goto skip_page; 14031de3e3dfSTheodore Ts'o if (map && (cur_logical >= map->m_lblk) && 14041de3e3dfSTheodore Ts'o (cur_logical <= (map->m_lblk + 14051de3e3dfSTheodore Ts'o (map->m_len - 1)))) { 14061de3e3dfSTheodore Ts'o if (buffer_delay(bh)) { 14071de3e3dfSTheodore Ts'o clear_buffer_delay(bh); 14081de3e3dfSTheodore Ts'o bh->b_blocknr = pblock; 14091de3e3dfSTheodore Ts'o } 14101de3e3dfSTheodore Ts'o if (buffer_unwritten(bh) || 14111de3e3dfSTheodore Ts'o buffer_mapped(bh)) 14121de3e3dfSTheodore Ts'o BUG_ON(bh->b_blocknr != pblock); 14131de3e3dfSTheodore Ts'o if (map->m_flags & EXT4_MAP_UNINIT) 14141de3e3dfSTheodore Ts'o set_buffer_uninit(bh); 14151de3e3dfSTheodore Ts'o clear_buffer_unwritten(bh); 14161de3e3dfSTheodore Ts'o } 14171de3e3dfSTheodore Ts'o 141813a79a47SYongqiang Yang /* 141913a79a47SYongqiang Yang * skip page if block allocation undone and 142013a79a47SYongqiang Yang * block is dirty 142113a79a47SYongqiang Yang */ 142213a79a47SYongqiang Yang if (ext4_bh_delay_or_unwritten(NULL, bh)) 142397498956STheodore Ts'o skip_page = 1; 14243ecdb3a1STheodore Ts'o bh = bh->b_this_page; 14253ecdb3a1STheodore Ts'o block_start += bh->b_size; 14261de3e3dfSTheodore Ts'o cur_logical++; 14271de3e3dfSTheodore Ts'o pblock++; 14281de3e3dfSTheodore Ts'o } while (bh != page_bufs); 14291de3e3dfSTheodore Ts'o 143097498956STheodore Ts'o if (skip_page) 143197498956STheodore Ts'o goto skip_page; 1432cb20d518STheodore Ts'o 1433cb20d518STheodore Ts'o if (commit_write) 1434cb20d518STheodore Ts'o /* mark the buffer_heads as dirty & uptodate */ 1435cb20d518STheodore Ts'o block_commit_write(page, 0, len); 1436cb20d518STheodore Ts'o 143797498956STheodore Ts'o clear_page_dirty_for_io(page); 1438bd2d0210STheodore Ts'o /* 1439bd2d0210STheodore Ts'o * Delalloc doesn't support data journalling, 1440bd2d0210STheodore Ts'o * but eventually maybe we'll lift this 1441bd2d0210STheodore Ts'o * restriction. 1442bd2d0210STheodore Ts'o */ 1443bd2d0210STheodore Ts'o if (unlikely(journal_data && PageChecked(page))) 1444cb20d518STheodore Ts'o err = __ext4_journalled_writepage(page, len); 14451449032bSTheodore Ts'o else if (test_opt(inode->i_sb, MBLK_IO_SUBMIT)) 1446bd2d0210STheodore Ts'o err = ext4_bio_write_page(&io_submit, page, 1447bd2d0210STheodore Ts'o len, mpd->wbc); 14489dd75f1fSTheodore Ts'o else if (buffer_uninit(page_bufs)) { 14499dd75f1fSTheodore Ts'o ext4_set_bh_endio(page_bufs, inode); 14509dd75f1fSTheodore Ts'o err = block_write_full_page_endio(page, 14519dd75f1fSTheodore Ts'o noalloc_get_block_write, 14529dd75f1fSTheodore Ts'o mpd->wbc, ext4_end_io_buffer_write); 14539dd75f1fSTheodore Ts'o } else 14541449032bSTheodore Ts'o err = block_write_full_page(page, 14551449032bSTheodore Ts'o noalloc_get_block_write, mpd->wbc); 1456cb20d518STheodore Ts'o 1457cb20d518STheodore Ts'o if (!err) 1458a1d6cc56SAneesh Kumar K.V mpd->pages_written++; 145964769240SAlex Tomas /* 146064769240SAlex Tomas * In error case, we have to continue because 146164769240SAlex Tomas * remaining pages are still locked 146264769240SAlex Tomas */ 146364769240SAlex Tomas if (ret == 0) 146464769240SAlex Tomas ret = err; 146564769240SAlex Tomas } 146664769240SAlex Tomas pagevec_release(&pvec); 146764769240SAlex Tomas } 1468bd2d0210STheodore Ts'o ext4_io_submit(&io_submit); 146964769240SAlex Tomas return ret; 147064769240SAlex Tomas } 147164769240SAlex Tomas 1472c7f5938aSCurt Wohlgemuth static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd) 1473c4a0c46eSAneesh Kumar K.V { 1474c4a0c46eSAneesh Kumar K.V int nr_pages, i; 1475c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 1476c4a0c46eSAneesh Kumar K.V struct pagevec pvec; 1477c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 1478c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 147951865fdaSZheng Liu ext4_lblk_t start, last; 1480c4a0c46eSAneesh Kumar K.V 1481c7f5938aSCurt Wohlgemuth index = mpd->first_page; 1482c7f5938aSCurt Wohlgemuth end = mpd->next_page - 1; 148351865fdaSZheng Liu 148451865fdaSZheng Liu start = index << (PAGE_CACHE_SHIFT - inode->i_blkbits); 148551865fdaSZheng Liu last = end << (PAGE_CACHE_SHIFT - inode->i_blkbits); 148651865fdaSZheng Liu ext4_es_remove_extent(inode, start, last - start + 1); 148751865fdaSZheng Liu 148866bea92cSEric Sandeen pagevec_init(&pvec, 0); 1489c4a0c46eSAneesh Kumar K.V while (index <= end) { 1490c4a0c46eSAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 1491c4a0c46eSAneesh Kumar K.V if (nr_pages == 0) 1492c4a0c46eSAneesh Kumar K.V break; 1493c4a0c46eSAneesh Kumar K.V for (i = 0; i < nr_pages; i++) { 1494c4a0c46eSAneesh Kumar K.V struct page *page = pvec.pages[i]; 14959b1d0998SJan Kara if (page->index > end) 1496c4a0c46eSAneesh Kumar K.V break; 1497c4a0c46eSAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1498c4a0c46eSAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1499c4a0c46eSAneesh Kumar K.V block_invalidatepage(page, 0); 1500c4a0c46eSAneesh Kumar K.V ClearPageUptodate(page); 1501c4a0c46eSAneesh Kumar K.V unlock_page(page); 1502c4a0c46eSAneesh Kumar K.V } 15039b1d0998SJan Kara index = pvec.pages[nr_pages - 1]->index + 1; 15049b1d0998SJan Kara pagevec_release(&pvec); 1505c4a0c46eSAneesh Kumar K.V } 1506c4a0c46eSAneesh Kumar K.V return; 1507c4a0c46eSAneesh Kumar K.V } 1508c4a0c46eSAneesh Kumar K.V 1509df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 1510df22291fSAneesh Kumar K.V { 1511df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 151292b97816STheodore Ts'o struct super_block *sb = inode->i_sb; 151392b97816STheodore Ts'o 151492b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld", 15155dee5437STheodore Ts'o EXT4_C2B(EXT4_SB(inode->i_sb), 15165dee5437STheodore Ts'o ext4_count_free_clusters(inode->i_sb))); 151792b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Free/Dirty block details"); 151892b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "free_blocks=%lld", 151957042651STheodore Ts'o (long long) EXT4_C2B(EXT4_SB(inode->i_sb), 152057042651STheodore Ts'o percpu_counter_sum(&sbi->s_freeclusters_counter))); 152192b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld", 15227b415bf6SAditya Kali (long long) EXT4_C2B(EXT4_SB(inode->i_sb), 15237b415bf6SAditya Kali percpu_counter_sum(&sbi->s_dirtyclusters_counter))); 152492b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Block reservation details"); 152592b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u", 1526df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_data_blocks); 152792b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_meta_blocks=%u", 1528df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_meta_blocks); 1529df22291fSAneesh Kumar K.V return; 1530df22291fSAneesh Kumar K.V } 1531df22291fSAneesh Kumar K.V 1532b920c755STheodore Ts'o /* 15335a87b7a5STheodore Ts'o * mpage_da_map_and_submit - go through given space, map them 15345a87b7a5STheodore Ts'o * if necessary, and then submit them for I/O 153564769240SAlex Tomas * 15368dc207c0STheodore Ts'o * @mpd - bh describing space 153764769240SAlex Tomas * 153864769240SAlex Tomas * The function skips space we know is already mapped to disk blocks. 153964769240SAlex Tomas * 154064769240SAlex Tomas */ 15415a87b7a5STheodore Ts'o static void mpage_da_map_and_submit(struct mpage_da_data *mpd) 154264769240SAlex Tomas { 15432ac3b6e0STheodore Ts'o int err, blks, get_blocks_flags; 15441de3e3dfSTheodore Ts'o struct ext4_map_blocks map, *mapp = NULL; 15452fa3cdfbSTheodore Ts'o sector_t next = mpd->b_blocknr; 15462fa3cdfbSTheodore Ts'o unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits; 15472fa3cdfbSTheodore Ts'o loff_t disksize = EXT4_I(mpd->inode)->i_disksize; 15482fa3cdfbSTheodore Ts'o handle_t *handle = NULL; 154964769240SAlex Tomas 155064769240SAlex Tomas /* 15515a87b7a5STheodore Ts'o * If the blocks are mapped already, or we couldn't accumulate 15525a87b7a5STheodore Ts'o * any blocks, then proceed immediately to the submission stage. 155364769240SAlex Tomas */ 15545a87b7a5STheodore Ts'o if ((mpd->b_size == 0) || 15555a87b7a5STheodore Ts'o ((mpd->b_state & (1 << BH_Mapped)) && 155629fa89d0SAneesh Kumar K.V !(mpd->b_state & (1 << BH_Delay)) && 15575a87b7a5STheodore Ts'o !(mpd->b_state & (1 << BH_Unwritten)))) 15585a87b7a5STheodore Ts'o goto submit_io; 15592fa3cdfbSTheodore Ts'o 15602fa3cdfbSTheodore Ts'o handle = ext4_journal_current_handle(); 15612fa3cdfbSTheodore Ts'o BUG_ON(!handle); 15622fa3cdfbSTheodore Ts'o 156379ffab34SAneesh Kumar K.V /* 156479e83036SEric Sandeen * Call ext4_map_blocks() to allocate any delayed allocation 15652ac3b6e0STheodore Ts'o * blocks, or to convert an uninitialized extent to be 15662ac3b6e0STheodore Ts'o * initialized (in the case where we have written into 15672ac3b6e0STheodore Ts'o * one or more preallocated blocks). 15682ac3b6e0STheodore Ts'o * 15692ac3b6e0STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to 15702ac3b6e0STheodore Ts'o * indicate that we are on the delayed allocation path. This 15712ac3b6e0STheodore Ts'o * affects functions in many different parts of the allocation 15722ac3b6e0STheodore Ts'o * call path. This flag exists primarily because we don't 157379e83036SEric Sandeen * want to change *many* call functions, so ext4_map_blocks() 1574f2321097STheodore Ts'o * will set the EXT4_STATE_DELALLOC_RESERVED flag once the 15752ac3b6e0STheodore Ts'o * inode's allocation semaphore is taken. 15762ac3b6e0STheodore Ts'o * 15772ac3b6e0STheodore Ts'o * If the blocks in questions were delalloc blocks, set 15782ac3b6e0STheodore Ts'o * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting 15792ac3b6e0STheodore Ts'o * variables are updated after the blocks have been allocated. 158079ffab34SAneesh Kumar K.V */ 15812ed88685STheodore Ts'o map.m_lblk = next; 15822ed88685STheodore Ts'o map.m_len = max_blocks; 15831296cc85SAneesh Kumar K.V get_blocks_flags = EXT4_GET_BLOCKS_CREATE; 1584744692dcSJiaying Zhang if (ext4_should_dioread_nolock(mpd->inode)) 1585744692dcSJiaying Zhang get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; 15862ac3b6e0STheodore Ts'o if (mpd->b_state & (1 << BH_Delay)) 15871296cc85SAneesh Kumar K.V get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; 15881296cc85SAneesh Kumar K.V 15892ed88685STheodore Ts'o blks = ext4_map_blocks(handle, mpd->inode, &map, get_blocks_flags); 15902fa3cdfbSTheodore Ts'o if (blks < 0) { 1591e3570639SEric Sandeen struct super_block *sb = mpd->inode->i_sb; 1592e3570639SEric Sandeen 15932fa3cdfbSTheodore Ts'o err = blks; 1594ed5bde0bSTheodore Ts'o /* 15955a87b7a5STheodore Ts'o * If get block returns EAGAIN or ENOSPC and there 159697498956STheodore Ts'o * appears to be free blocks we will just let 159797498956STheodore Ts'o * mpage_da_submit_io() unlock all of the pages. 1598c4a0c46eSAneesh Kumar K.V */ 1599c4a0c46eSAneesh Kumar K.V if (err == -EAGAIN) 16005a87b7a5STheodore Ts'o goto submit_io; 1601df22291fSAneesh Kumar K.V 16025dee5437STheodore Ts'o if (err == -ENOSPC && ext4_count_free_clusters(sb)) { 1603df22291fSAneesh Kumar K.V mpd->retval = err; 16045a87b7a5STheodore Ts'o goto submit_io; 1605df22291fSAneesh Kumar K.V } 1606df22291fSAneesh Kumar K.V 1607c4a0c46eSAneesh Kumar K.V /* 1608ed5bde0bSTheodore Ts'o * get block failure will cause us to loop in 1609ed5bde0bSTheodore Ts'o * writepages, because a_ops->writepage won't be able 1610ed5bde0bSTheodore Ts'o * to make progress. The page will be redirtied by 1611ed5bde0bSTheodore Ts'o * writepage and writepages will again try to write 1612ed5bde0bSTheodore Ts'o * the same. 1613c4a0c46eSAneesh Kumar K.V */ 1614e3570639SEric Sandeen if (!(EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) { 1615e3570639SEric Sandeen ext4_msg(sb, KERN_CRIT, 1616e3570639SEric Sandeen "delayed block allocation failed for inode %lu " 1617e3570639SEric Sandeen "at logical offset %llu with max blocks %zd " 1618e3570639SEric Sandeen "with error %d", mpd->inode->i_ino, 1619c4a0c46eSAneesh Kumar K.V (unsigned long long) next, 16208dc207c0STheodore Ts'o mpd->b_size >> mpd->inode->i_blkbits, err); 1621e3570639SEric Sandeen ext4_msg(sb, KERN_CRIT, 1622e3570639SEric Sandeen "This should not happen!! Data will be lost\n"); 1623e3570639SEric Sandeen if (err == -ENOSPC) 1624df22291fSAneesh Kumar K.V ext4_print_free_blocks(mpd->inode); 1625030ba6bcSAneesh Kumar K.V } 16262fa3cdfbSTheodore Ts'o /* invalidate all the pages */ 1627c7f5938aSCurt Wohlgemuth ext4_da_block_invalidatepages(mpd); 1628e0fd9b90SCurt Wohlgemuth 1629e0fd9b90SCurt Wohlgemuth /* Mark this page range as having been completed */ 1630e0fd9b90SCurt Wohlgemuth mpd->io_done = 1; 16315a87b7a5STheodore Ts'o return; 1632c4a0c46eSAneesh Kumar K.V } 16332fa3cdfbSTheodore Ts'o BUG_ON(blks == 0); 16342fa3cdfbSTheodore Ts'o 16351de3e3dfSTheodore Ts'o mapp = ↦ 16362ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 16372ed88685STheodore Ts'o struct block_device *bdev = mpd->inode->i_sb->s_bdev; 16382ed88685STheodore Ts'o int i; 163964769240SAlex Tomas 16402ed88685STheodore Ts'o for (i = 0; i < map.m_len; i++) 16412ed88685STheodore Ts'o unmap_underlying_metadata(bdev, map.m_pblk + i); 16422fa3cdfbSTheodore Ts'o } 16432fa3cdfbSTheodore Ts'o 16442fa3cdfbSTheodore Ts'o /* 164503f5d8bcSJan Kara * Update on-disk size along with block allocation. 16462fa3cdfbSTheodore Ts'o */ 16472fa3cdfbSTheodore Ts'o disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits; 16482fa3cdfbSTheodore Ts'o if (disksize > i_size_read(mpd->inode)) 16492fa3cdfbSTheodore Ts'o disksize = i_size_read(mpd->inode); 16502fa3cdfbSTheodore Ts'o if (disksize > EXT4_I(mpd->inode)->i_disksize) { 16512fa3cdfbSTheodore Ts'o ext4_update_i_disksize(mpd->inode, disksize); 16525a87b7a5STheodore Ts'o err = ext4_mark_inode_dirty(handle, mpd->inode); 16535a87b7a5STheodore Ts'o if (err) 16545a87b7a5STheodore Ts'o ext4_error(mpd->inode->i_sb, 16555a87b7a5STheodore Ts'o "Failed to mark inode %lu dirty", 16565a87b7a5STheodore Ts'o mpd->inode->i_ino); 16572fa3cdfbSTheodore Ts'o } 16582fa3cdfbSTheodore Ts'o 16595a87b7a5STheodore Ts'o submit_io: 16601de3e3dfSTheodore Ts'o mpage_da_submit_io(mpd, mapp); 16615a87b7a5STheodore Ts'o mpd->io_done = 1; 166264769240SAlex Tomas } 166364769240SAlex Tomas 1664bf068ee2SAneesh Kumar K.V #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \ 1665bf068ee2SAneesh Kumar K.V (1 << BH_Delay) | (1 << BH_Unwritten)) 166664769240SAlex Tomas 166764769240SAlex Tomas /* 166864769240SAlex Tomas * mpage_add_bh_to_extent - try to add one more block to extent of blocks 166964769240SAlex Tomas * 167064769240SAlex Tomas * @mpd->lbh - extent of blocks 167164769240SAlex Tomas * @logical - logical number of the block in the file 167264769240SAlex Tomas * @bh - bh of the block (used to access block's state) 167364769240SAlex Tomas * 167464769240SAlex Tomas * the function is used to collect contig. blocks in same state 167564769240SAlex Tomas */ 167664769240SAlex Tomas static void mpage_add_bh_to_extent(struct mpage_da_data *mpd, 16778dc207c0STheodore Ts'o sector_t logical, size_t b_size, 16788dc207c0STheodore Ts'o unsigned long b_state) 167964769240SAlex Tomas { 168064769240SAlex Tomas sector_t next; 16818dc207c0STheodore Ts'o int nrblocks = mpd->b_size >> mpd->inode->i_blkbits; 168264769240SAlex Tomas 1683c445e3e0SEric Sandeen /* 1684c445e3e0SEric Sandeen * XXX Don't go larger than mballoc is willing to allocate 1685c445e3e0SEric Sandeen * This is a stopgap solution. We eventually need to fold 1686c445e3e0SEric Sandeen * mpage_da_submit_io() into this function and then call 168779e83036SEric Sandeen * ext4_map_blocks() multiple times in a loop 1688c445e3e0SEric Sandeen */ 1689c445e3e0SEric Sandeen if (nrblocks >= 8*1024*1024/mpd->inode->i_sb->s_blocksize) 1690c445e3e0SEric Sandeen goto flush_it; 1691c445e3e0SEric Sandeen 1692525f4ed8SMingming Cao /* check if thereserved journal credits might overflow */ 169312e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(mpd->inode, EXT4_INODE_EXTENTS))) { 1694525f4ed8SMingming Cao if (nrblocks >= EXT4_MAX_TRANS_DATA) { 1695525f4ed8SMingming Cao /* 1696525f4ed8SMingming Cao * With non-extent format we are limited by the journal 1697525f4ed8SMingming Cao * credit available. Total credit needed to insert 1698525f4ed8SMingming Cao * nrblocks contiguous blocks is dependent on the 1699525f4ed8SMingming Cao * nrblocks. So limit nrblocks. 1700525f4ed8SMingming Cao */ 1701525f4ed8SMingming Cao goto flush_it; 1702525f4ed8SMingming Cao } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) > 1703525f4ed8SMingming Cao EXT4_MAX_TRANS_DATA) { 1704525f4ed8SMingming Cao /* 1705525f4ed8SMingming Cao * Adding the new buffer_head would make it cross the 1706525f4ed8SMingming Cao * allowed limit for which we have journal credit 1707525f4ed8SMingming Cao * reserved. So limit the new bh->b_size 1708525f4ed8SMingming Cao */ 1709525f4ed8SMingming Cao b_size = (EXT4_MAX_TRANS_DATA - nrblocks) << 1710525f4ed8SMingming Cao mpd->inode->i_blkbits; 1711525f4ed8SMingming Cao /* we will do mpage_da_submit_io in the next loop */ 1712525f4ed8SMingming Cao } 1713525f4ed8SMingming Cao } 171464769240SAlex Tomas /* 171564769240SAlex Tomas * First block in the extent 171664769240SAlex Tomas */ 17178dc207c0STheodore Ts'o if (mpd->b_size == 0) { 17188dc207c0STheodore Ts'o mpd->b_blocknr = logical; 17198dc207c0STheodore Ts'o mpd->b_size = b_size; 17208dc207c0STheodore Ts'o mpd->b_state = b_state & BH_FLAGS; 172164769240SAlex Tomas return; 172264769240SAlex Tomas } 172364769240SAlex Tomas 17248dc207c0STheodore Ts'o next = mpd->b_blocknr + nrblocks; 172564769240SAlex Tomas /* 172664769240SAlex Tomas * Can we merge the block to our big extent? 172764769240SAlex Tomas */ 17288dc207c0STheodore Ts'o if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) { 17298dc207c0STheodore Ts'o mpd->b_size += b_size; 173064769240SAlex Tomas return; 173164769240SAlex Tomas } 173264769240SAlex Tomas 1733525f4ed8SMingming Cao flush_it: 173464769240SAlex Tomas /* 173564769240SAlex Tomas * We couldn't merge the block to our extent, so we 173664769240SAlex Tomas * need to flush current extent and start new one 173764769240SAlex Tomas */ 17385a87b7a5STheodore Ts'o mpage_da_map_and_submit(mpd); 1739a1d6cc56SAneesh Kumar K.V return; 174064769240SAlex Tomas } 174164769240SAlex Tomas 1742c364b22cSAneesh Kumar K.V static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh) 174329fa89d0SAneesh Kumar K.V { 1744c364b22cSAneesh Kumar K.V return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 174529fa89d0SAneesh Kumar K.V } 174629fa89d0SAneesh Kumar K.V 174764769240SAlex Tomas /* 17485356f261SAditya Kali * This function is grabs code from the very beginning of 17495356f261SAditya Kali * ext4_map_blocks, but assumes that the caller is from delayed write 17505356f261SAditya Kali * time. This function looks up the requested blocks and sets the 17515356f261SAditya Kali * buffer delay bit under the protection of i_data_sem. 17525356f261SAditya Kali */ 17535356f261SAditya Kali static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, 17545356f261SAditya Kali struct ext4_map_blocks *map, 17555356f261SAditya Kali struct buffer_head *bh) 17565356f261SAditya Kali { 17575356f261SAditya Kali int retval; 17585356f261SAditya Kali sector_t invalid_block = ~((sector_t) 0xffff); 17595356f261SAditya Kali 17605356f261SAditya Kali if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 17615356f261SAditya Kali invalid_block = ~0; 17625356f261SAditya Kali 17635356f261SAditya Kali map->m_flags = 0; 17645356f261SAditya Kali ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u," 17655356f261SAditya Kali "logical block %lu\n", inode->i_ino, map->m_len, 17665356f261SAditya Kali (unsigned long) map->m_lblk); 17675356f261SAditya Kali /* 17685356f261SAditya Kali * Try to see if we can get the block without requesting a new 17695356f261SAditya Kali * file system block. 17705356f261SAditya Kali */ 17715356f261SAditya Kali down_read((&EXT4_I(inode)->i_data_sem)); 17725356f261SAditya Kali if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 17735356f261SAditya Kali retval = ext4_ext_map_blocks(NULL, inode, map, 0); 17745356f261SAditya Kali else 17755356f261SAditya Kali retval = ext4_ind_map_blocks(NULL, inode, map, 0); 17765356f261SAditya Kali 17775356f261SAditya Kali if (retval == 0) { 17785356f261SAditya Kali /* 17795356f261SAditya Kali * XXX: __block_prepare_write() unmaps passed block, 17805356f261SAditya Kali * is it OK? 17815356f261SAditya Kali */ 17825356f261SAditya Kali /* If the block was allocated from previously allocated cluster, 17835356f261SAditya Kali * then we dont need to reserve it again. */ 17845356f261SAditya Kali if (!(map->m_flags & EXT4_MAP_FROM_CLUSTER)) { 17855356f261SAditya Kali retval = ext4_da_reserve_space(inode, iblock); 17865356f261SAditya Kali if (retval) 17875356f261SAditya Kali /* not enough space to reserve */ 17885356f261SAditya Kali goto out_unlock; 17895356f261SAditya Kali } 17905356f261SAditya Kali 179151865fdaSZheng Liu retval = ext4_es_insert_extent(inode, map->m_lblk, map->m_len); 179251865fdaSZheng Liu if (retval) 179351865fdaSZheng Liu goto out_unlock; 179451865fdaSZheng Liu 17955356f261SAditya Kali /* Clear EXT4_MAP_FROM_CLUSTER flag since its purpose is served 17965356f261SAditya Kali * and it should not appear on the bh->b_state. 17975356f261SAditya Kali */ 17985356f261SAditya Kali map->m_flags &= ~EXT4_MAP_FROM_CLUSTER; 17995356f261SAditya Kali 18005356f261SAditya Kali map_bh(bh, inode->i_sb, invalid_block); 18015356f261SAditya Kali set_buffer_new(bh); 18025356f261SAditya Kali set_buffer_delay(bh); 18035356f261SAditya Kali } 18045356f261SAditya Kali 18055356f261SAditya Kali out_unlock: 18065356f261SAditya Kali up_read((&EXT4_I(inode)->i_data_sem)); 18075356f261SAditya Kali 18085356f261SAditya Kali return retval; 18095356f261SAditya Kali } 18105356f261SAditya Kali 18115356f261SAditya Kali /* 1812b920c755STheodore Ts'o * This is a special get_blocks_t callback which is used by 1813b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 1814b920c755STheodore Ts'o * reserve space for a single block. 181529fa89d0SAneesh Kumar K.V * 181629fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 181729fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 181829fa89d0SAneesh Kumar K.V * 181929fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 182029fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 182129fa89d0SAneesh Kumar K.V * initialized properly. 182264769240SAlex Tomas */ 182364769240SAlex Tomas static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 18242ed88685STheodore Ts'o struct buffer_head *bh, int create) 182564769240SAlex Tomas { 18262ed88685STheodore Ts'o struct ext4_map_blocks map; 182764769240SAlex Tomas int ret = 0; 182864769240SAlex Tomas 182964769240SAlex Tomas BUG_ON(create == 0); 18302ed88685STheodore Ts'o BUG_ON(bh->b_size != inode->i_sb->s_blocksize); 18312ed88685STheodore Ts'o 18322ed88685STheodore Ts'o map.m_lblk = iblock; 18332ed88685STheodore Ts'o map.m_len = 1; 183464769240SAlex Tomas 183564769240SAlex Tomas /* 183664769240SAlex Tomas * first, we need to know whether the block is allocated already 183764769240SAlex Tomas * preallocated blocks are unmapped but should treated 183864769240SAlex Tomas * the same as allocated blocks. 183964769240SAlex Tomas */ 18405356f261SAditya Kali ret = ext4_da_map_blocks(inode, iblock, &map, bh); 18415356f261SAditya Kali if (ret <= 0) 18422ed88685STheodore Ts'o return ret; 184364769240SAlex Tomas 18442ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 18452ed88685STheodore Ts'o bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; 18462ed88685STheodore Ts'o 18472ed88685STheodore Ts'o if (buffer_unwritten(bh)) { 18482ed88685STheodore Ts'o /* A delayed write to unwritten bh should be marked 18492ed88685STheodore Ts'o * new and mapped. Mapped ensures that we don't do 18502ed88685STheodore Ts'o * get_block multiple times when we write to the same 18512ed88685STheodore Ts'o * offset and new ensures that we do proper zero out 18522ed88685STheodore Ts'o * for partial write. 18532ed88685STheodore Ts'o */ 18542ed88685STheodore Ts'o set_buffer_new(bh); 1855c8205636STheodore Ts'o set_buffer_mapped(bh); 18562ed88685STheodore Ts'o } 18572ed88685STheodore Ts'o return 0; 185864769240SAlex Tomas } 185961628a3fSMingming Cao 1860b920c755STheodore Ts'o /* 1861b920c755STheodore Ts'o * This function is used as a standard get_block_t calback function 1862b920c755STheodore Ts'o * when there is no desire to allocate any blocks. It is used as a 1863ebdec241SChristoph Hellwig * callback function for block_write_begin() and block_write_full_page(). 1864206f7ab4SChristoph Hellwig * These functions should only try to map a single block at a time. 1865b920c755STheodore Ts'o * 1866b920c755STheodore Ts'o * Since this function doesn't do block allocations even if the caller 1867b920c755STheodore Ts'o * requests it by passing in create=1, it is critically important that 1868b920c755STheodore Ts'o * any caller checks to make sure that any buffer heads are returned 1869b920c755STheodore Ts'o * by this function are either all already mapped or marked for 1870206f7ab4SChristoph Hellwig * delayed allocation before calling block_write_full_page(). Otherwise, 1871206f7ab4SChristoph Hellwig * b_blocknr could be left unitialized, and the page write functions will 1872206f7ab4SChristoph Hellwig * be taken by surprise. 1873b920c755STheodore Ts'o */ 1874b920c755STheodore Ts'o static int noalloc_get_block_write(struct inode *inode, sector_t iblock, 1875f0e6c985SAneesh Kumar K.V struct buffer_head *bh_result, int create) 1876f0e6c985SAneesh Kumar K.V { 1877a2dc52b5STheodore Ts'o BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize); 18782ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh_result, 0); 187961628a3fSMingming Cao } 188061628a3fSMingming Cao 188162e086beSAneesh Kumar K.V static int bget_one(handle_t *handle, struct buffer_head *bh) 188262e086beSAneesh Kumar K.V { 188362e086beSAneesh Kumar K.V get_bh(bh); 188462e086beSAneesh Kumar K.V return 0; 188562e086beSAneesh Kumar K.V } 188662e086beSAneesh Kumar K.V 188762e086beSAneesh Kumar K.V static int bput_one(handle_t *handle, struct buffer_head *bh) 188862e086beSAneesh Kumar K.V { 188962e086beSAneesh Kumar K.V put_bh(bh); 189062e086beSAneesh Kumar K.V return 0; 189162e086beSAneesh Kumar K.V } 189262e086beSAneesh Kumar K.V 189362e086beSAneesh Kumar K.V static int __ext4_journalled_writepage(struct page *page, 189462e086beSAneesh Kumar K.V unsigned int len) 189562e086beSAneesh Kumar K.V { 189662e086beSAneesh Kumar K.V struct address_space *mapping = page->mapping; 189762e086beSAneesh Kumar K.V struct inode *inode = mapping->host; 189862e086beSAneesh Kumar K.V struct buffer_head *page_bufs; 189962e086beSAneesh Kumar K.V handle_t *handle = NULL; 190062e086beSAneesh Kumar K.V int ret = 0; 190162e086beSAneesh Kumar K.V int err; 190262e086beSAneesh Kumar K.V 1903cb20d518STheodore Ts'o ClearPageChecked(page); 190462e086beSAneesh Kumar K.V page_bufs = page_buffers(page); 190562e086beSAneesh Kumar K.V BUG_ON(!page_bufs); 190662e086beSAneesh Kumar K.V walk_page_buffers(handle, page_bufs, 0, len, NULL, bget_one); 190762e086beSAneesh Kumar K.V /* As soon as we unlock the page, it can go away, but we have 190862e086beSAneesh Kumar K.V * references to buffers so we are safe */ 190962e086beSAneesh Kumar K.V unlock_page(page); 191062e086beSAneesh Kumar K.V 191162e086beSAneesh Kumar K.V handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode)); 191262e086beSAneesh Kumar K.V if (IS_ERR(handle)) { 191362e086beSAneesh Kumar K.V ret = PTR_ERR(handle); 191462e086beSAneesh Kumar K.V goto out; 191562e086beSAneesh Kumar K.V } 191662e086beSAneesh Kumar K.V 1917441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1918441c8508SCurt Wohlgemuth 191962e086beSAneesh Kumar K.V ret = walk_page_buffers(handle, page_bufs, 0, len, NULL, 192062e086beSAneesh Kumar K.V do_journal_get_write_access); 192162e086beSAneesh Kumar K.V 192262e086beSAneesh Kumar K.V err = walk_page_buffers(handle, page_bufs, 0, len, NULL, 192362e086beSAneesh Kumar K.V write_end_fn); 192462e086beSAneesh Kumar K.V if (ret == 0) 192562e086beSAneesh Kumar K.V ret = err; 19262d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 192762e086beSAneesh Kumar K.V err = ext4_journal_stop(handle); 192862e086beSAneesh Kumar K.V if (!ret) 192962e086beSAneesh Kumar K.V ret = err; 193062e086beSAneesh Kumar K.V 193162e086beSAneesh Kumar K.V walk_page_buffers(handle, page_bufs, 0, len, NULL, bput_one); 193219f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 193362e086beSAneesh Kumar K.V out: 193462e086beSAneesh Kumar K.V return ret; 193562e086beSAneesh Kumar K.V } 193662e086beSAneesh Kumar K.V 193761628a3fSMingming Cao /* 193843ce1d23SAneesh Kumar K.V * Note that we don't need to start a transaction unless we're journaling data 193943ce1d23SAneesh Kumar K.V * because we should have holes filled from ext4_page_mkwrite(). We even don't 194043ce1d23SAneesh Kumar K.V * need to file the inode to the transaction's list in ordered mode because if 194143ce1d23SAneesh Kumar K.V * we are writing back data added by write(), the inode is already there and if 194243ce1d23SAneesh Kumar K.V * we are writing back data modified via mmap(), no one guarantees in which 194343ce1d23SAneesh Kumar K.V * transaction the data will hit the disk. In case we are journaling data, we 194443ce1d23SAneesh Kumar K.V * cannot start transaction directly because transaction start ranks above page 194543ce1d23SAneesh Kumar K.V * lock so we have to do some magic. 194643ce1d23SAneesh Kumar K.V * 1947b920c755STheodore Ts'o * This function can get called via... 1948b920c755STheodore Ts'o * - ext4_da_writepages after taking page lock (have journal handle) 1949b920c755STheodore Ts'o * - journal_submit_inode_data_buffers (no journal handle) 1950f6463b0dSArtem Bityutskiy * - shrink_page_list via the kswapd/direct reclaim (no journal handle) 1951b920c755STheodore Ts'o * - grab_page_cache when doing write_begin (have journal handle) 195243ce1d23SAneesh Kumar K.V * 195343ce1d23SAneesh Kumar K.V * We don't do any block allocation in this function. If we have page with 195443ce1d23SAneesh Kumar K.V * multiple blocks we need to write those buffer_heads that are mapped. This 195543ce1d23SAneesh Kumar K.V * is important for mmaped based write. So if we do with blocksize 1K 195643ce1d23SAneesh Kumar K.V * truncate(f, 1024); 195743ce1d23SAneesh Kumar K.V * a = mmap(f, 0, 4096); 195843ce1d23SAneesh Kumar K.V * a[0] = 'a'; 195943ce1d23SAneesh Kumar K.V * truncate(f, 4096); 196043ce1d23SAneesh Kumar K.V * we have in the page first buffer_head mapped via page_mkwrite call back 196190802ed9SPaul Bolle * but other buffer_heads would be unmapped but dirty (dirty done via the 196243ce1d23SAneesh Kumar K.V * do_wp_page). So writepage should write the first block. If we modify 196343ce1d23SAneesh Kumar K.V * the mmap area beyond 1024 we will again get a page_fault and the 196443ce1d23SAneesh Kumar K.V * page_mkwrite callback will do the block allocation and mark the 196543ce1d23SAneesh Kumar K.V * buffer_heads mapped. 196643ce1d23SAneesh Kumar K.V * 196743ce1d23SAneesh Kumar K.V * We redirty the page if we have any buffer_heads that is either delay or 196843ce1d23SAneesh Kumar K.V * unwritten in the page. 196943ce1d23SAneesh Kumar K.V * 197043ce1d23SAneesh Kumar K.V * We can get recursively called as show below. 197143ce1d23SAneesh Kumar K.V * 197243ce1d23SAneesh Kumar K.V * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> 197343ce1d23SAneesh Kumar K.V * ext4_writepage() 197443ce1d23SAneesh Kumar K.V * 197543ce1d23SAneesh Kumar K.V * But since we don't do any block allocation we should not deadlock. 197643ce1d23SAneesh Kumar K.V * Page also have the dirty flag cleared so we don't get recurive page_lock. 197761628a3fSMingming Cao */ 197843ce1d23SAneesh Kumar K.V static int ext4_writepage(struct page *page, 197964769240SAlex Tomas struct writeback_control *wbc) 198064769240SAlex Tomas { 1981a42afc5fSTheodore Ts'o int ret = 0, commit_write = 0; 198261628a3fSMingming Cao loff_t size; 1983498e5f24STheodore Ts'o unsigned int len; 1984744692dcSJiaying Zhang struct buffer_head *page_bufs = NULL; 198561628a3fSMingming Cao struct inode *inode = page->mapping->host; 198664769240SAlex Tomas 1987a9c667f8SLukas Czerner trace_ext4_writepage(page); 198861628a3fSMingming Cao size = i_size_read(inode); 198961628a3fSMingming Cao if (page->index == size >> PAGE_CACHE_SHIFT) 199061628a3fSMingming Cao len = size & ~PAGE_CACHE_MASK; 199161628a3fSMingming Cao else 199261628a3fSMingming Cao len = PAGE_CACHE_SIZE; 199361628a3fSMingming Cao 1994a42afc5fSTheodore Ts'o /* 1995a42afc5fSTheodore Ts'o * If the page does not have buffers (for whatever reason), 1996a107e5a3STheodore Ts'o * try to create them using __block_write_begin. If this 1997a42afc5fSTheodore Ts'o * fails, redirty the page and move on. 1998a42afc5fSTheodore Ts'o */ 1999b1142e8fSTheodore Ts'o if (!page_has_buffers(page)) { 2000a107e5a3STheodore Ts'o if (__block_write_begin(page, 0, len, 2001a42afc5fSTheodore Ts'o noalloc_get_block_write)) { 2002a42afc5fSTheodore Ts'o redirty_page: 2003a42afc5fSTheodore Ts'o redirty_page_for_writepage(wbc, page); 2004a42afc5fSTheodore Ts'o unlock_page(page); 2005a42afc5fSTheodore Ts'o return 0; 2006a42afc5fSTheodore Ts'o } 2007a42afc5fSTheodore Ts'o commit_write = 1; 2008a42afc5fSTheodore Ts'o } 2009f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 2010f0e6c985SAneesh Kumar K.V if (walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2011c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 201261628a3fSMingming Cao /* 2013b1142e8fSTheodore Ts'o * We don't want to do block allocation, so redirty 2014b1142e8fSTheodore Ts'o * the page and return. We may reach here when we do 2015b1142e8fSTheodore Ts'o * a journal commit via journal_submit_inode_data_buffers. 2016966dbde2SMel Gorman * We can also reach here via shrink_page_list but it 2017966dbde2SMel Gorman * should never be for direct reclaim so warn if that 2018966dbde2SMel Gorman * happens 2019f0e6c985SAneesh Kumar K.V */ 2020966dbde2SMel Gorman WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) == 2021966dbde2SMel Gorman PF_MEMALLOC); 2022a42afc5fSTheodore Ts'o goto redirty_page; 2023f0e6c985SAneesh Kumar K.V } 2024a42afc5fSTheodore Ts'o if (commit_write) 2025ed9b3e33SAneesh Kumar K.V /* now mark the buffer_heads as dirty and uptodate */ 2026b767e78aSAneesh Kumar K.V block_commit_write(page, 0, len); 202764769240SAlex Tomas 2028cb20d518STheodore Ts'o if (PageChecked(page) && ext4_should_journal_data(inode)) 202943ce1d23SAneesh Kumar K.V /* 203043ce1d23SAneesh Kumar K.V * It's mmapped pagecache. Add buffers and journal it. There 203143ce1d23SAneesh Kumar K.V * doesn't seem much point in redirtying the page here. 203243ce1d23SAneesh Kumar K.V */ 20333f0ca309SWu Fengguang return __ext4_journalled_writepage(page, len); 203443ce1d23SAneesh Kumar K.V 2035a42afc5fSTheodore Ts'o if (buffer_uninit(page_bufs)) { 2036744692dcSJiaying Zhang ext4_set_bh_endio(page_bufs, inode); 2037744692dcSJiaying Zhang ret = block_write_full_page_endio(page, noalloc_get_block_write, 2038744692dcSJiaying Zhang wbc, ext4_end_io_buffer_write); 2039744692dcSJiaying Zhang } else 2040b920c755STheodore Ts'o ret = block_write_full_page(page, noalloc_get_block_write, 2041f0e6c985SAneesh Kumar K.V wbc); 204264769240SAlex Tomas 204364769240SAlex Tomas return ret; 204464769240SAlex Tomas } 204564769240SAlex Tomas 204661628a3fSMingming Cao /* 2047525f4ed8SMingming Cao * This is called via ext4_da_writepages() to 204825985edcSLucas De Marchi * calculate the total number of credits to reserve to fit 2049525f4ed8SMingming Cao * a single extent allocation into a single transaction, 2050525f4ed8SMingming Cao * ext4_da_writpeages() will loop calling this before 2051525f4ed8SMingming Cao * the block allocation. 205261628a3fSMingming Cao */ 2053525f4ed8SMingming Cao 2054525f4ed8SMingming Cao static int ext4_da_writepages_trans_blocks(struct inode *inode) 2055525f4ed8SMingming Cao { 2056525f4ed8SMingming Cao int max_blocks = EXT4_I(inode)->i_reserved_data_blocks; 2057525f4ed8SMingming Cao 2058525f4ed8SMingming Cao /* 2059525f4ed8SMingming Cao * With non-extent format the journal credit needed to 2060525f4ed8SMingming Cao * insert nrblocks contiguous block is dependent on 2061525f4ed8SMingming Cao * number of contiguous block. So we will limit 2062525f4ed8SMingming Cao * number of contiguous block to a sane value 2063525f4ed8SMingming Cao */ 206412e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) && 2065525f4ed8SMingming Cao (max_blocks > EXT4_MAX_TRANS_DATA)) 2066525f4ed8SMingming Cao max_blocks = EXT4_MAX_TRANS_DATA; 2067525f4ed8SMingming Cao 2068525f4ed8SMingming Cao return ext4_chunk_trans_blocks(inode, max_blocks); 2069525f4ed8SMingming Cao } 207061628a3fSMingming Cao 20718e48dcfbSTheodore Ts'o /* 20728e48dcfbSTheodore Ts'o * write_cache_pages_da - walk the list of dirty pages of the given 20738eb9e5ceSTheodore Ts'o * address space and accumulate pages that need writing, and call 2074168fc022STheodore Ts'o * mpage_da_map_and_submit to map a single contiguous memory region 2075168fc022STheodore Ts'o * and then write them. 20768e48dcfbSTheodore Ts'o */ 20778e48dcfbSTheodore Ts'o static int write_cache_pages_da(struct address_space *mapping, 20788e48dcfbSTheodore Ts'o struct writeback_control *wbc, 207972f84e65SEric Sandeen struct mpage_da_data *mpd, 208072f84e65SEric Sandeen pgoff_t *done_index) 20818e48dcfbSTheodore Ts'o { 20828eb9e5ceSTheodore Ts'o struct buffer_head *bh, *head; 2083168fc022STheodore Ts'o struct inode *inode = mapping->host; 20848e48dcfbSTheodore Ts'o struct pagevec pvec; 20854f01b02cSTheodore Ts'o unsigned int nr_pages; 20864f01b02cSTheodore Ts'o sector_t logical; 20874f01b02cSTheodore Ts'o pgoff_t index, end; 20888e48dcfbSTheodore Ts'o long nr_to_write = wbc->nr_to_write; 20894f01b02cSTheodore Ts'o int i, tag, ret = 0; 20908e48dcfbSTheodore Ts'o 2091168fc022STheodore Ts'o memset(mpd, 0, sizeof(struct mpage_da_data)); 2092168fc022STheodore Ts'o mpd->wbc = wbc; 2093168fc022STheodore Ts'o mpd->inode = inode; 20948e48dcfbSTheodore Ts'o pagevec_init(&pvec, 0); 20958e48dcfbSTheodore Ts'o index = wbc->range_start >> PAGE_CACHE_SHIFT; 20968e48dcfbSTheodore Ts'o end = wbc->range_end >> PAGE_CACHE_SHIFT; 20978e48dcfbSTheodore Ts'o 20986e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 20995b41d924SEric Sandeen tag = PAGECACHE_TAG_TOWRITE; 21005b41d924SEric Sandeen else 21015b41d924SEric Sandeen tag = PAGECACHE_TAG_DIRTY; 21025b41d924SEric Sandeen 210372f84e65SEric Sandeen *done_index = index; 21044f01b02cSTheodore Ts'o while (index <= end) { 21055b41d924SEric Sandeen nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, 21068e48dcfbSTheodore Ts'o min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); 21078e48dcfbSTheodore Ts'o if (nr_pages == 0) 21084f01b02cSTheodore Ts'o return 0; 21098e48dcfbSTheodore Ts'o 21108e48dcfbSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 21118e48dcfbSTheodore Ts'o struct page *page = pvec.pages[i]; 21128e48dcfbSTheodore Ts'o 21138e48dcfbSTheodore Ts'o /* 21148e48dcfbSTheodore Ts'o * At this point, the page may be truncated or 21158e48dcfbSTheodore Ts'o * invalidated (changing page->mapping to NULL), or 21168e48dcfbSTheodore Ts'o * even swizzled back from swapper_space to tmpfs file 21178e48dcfbSTheodore Ts'o * mapping. However, page->index will not change 21188e48dcfbSTheodore Ts'o * because we have a reference on the page. 21198e48dcfbSTheodore Ts'o */ 21204f01b02cSTheodore Ts'o if (page->index > end) 21214f01b02cSTheodore Ts'o goto out; 21228e48dcfbSTheodore Ts'o 212372f84e65SEric Sandeen *done_index = page->index + 1; 212472f84e65SEric Sandeen 212578aaced3STheodore Ts'o /* 212678aaced3STheodore Ts'o * If we can't merge this page, and we have 212778aaced3STheodore Ts'o * accumulated an contiguous region, write it 212878aaced3STheodore Ts'o */ 212978aaced3STheodore Ts'o if ((mpd->next_page != page->index) && 213078aaced3STheodore Ts'o (mpd->next_page != mpd->first_page)) { 213178aaced3STheodore Ts'o mpage_da_map_and_submit(mpd); 213278aaced3STheodore Ts'o goto ret_extent_tail; 213378aaced3STheodore Ts'o } 213478aaced3STheodore Ts'o 21358e48dcfbSTheodore Ts'o lock_page(page); 21368e48dcfbSTheodore Ts'o 21378e48dcfbSTheodore Ts'o /* 21384f01b02cSTheodore Ts'o * If the page is no longer dirty, or its 21394f01b02cSTheodore Ts'o * mapping no longer corresponds to inode we 21404f01b02cSTheodore Ts'o * are writing (which means it has been 21414f01b02cSTheodore Ts'o * truncated or invalidated), or the page is 21424f01b02cSTheodore Ts'o * already under writeback and we are not 21434f01b02cSTheodore Ts'o * doing a data integrity writeback, skip the page 21448e48dcfbSTheodore Ts'o */ 21454f01b02cSTheodore Ts'o if (!PageDirty(page) || 21464f01b02cSTheodore Ts'o (PageWriteback(page) && 21474f01b02cSTheodore Ts'o (wbc->sync_mode == WB_SYNC_NONE)) || 21484f01b02cSTheodore Ts'o unlikely(page->mapping != mapping)) { 21498e48dcfbSTheodore Ts'o unlock_page(page); 21508e48dcfbSTheodore Ts'o continue; 21518e48dcfbSTheodore Ts'o } 21528e48dcfbSTheodore Ts'o 21538e48dcfbSTheodore Ts'o wait_on_page_writeback(page); 21548e48dcfbSTheodore Ts'o BUG_ON(PageWriteback(page)); 21558e48dcfbSTheodore Ts'o 2156168fc022STheodore Ts'o if (mpd->next_page != page->index) 21578eb9e5ceSTheodore Ts'o mpd->first_page = page->index; 21588eb9e5ceSTheodore Ts'o mpd->next_page = page->index + 1; 21598eb9e5ceSTheodore Ts'o logical = (sector_t) page->index << 21608eb9e5ceSTheodore Ts'o (PAGE_CACHE_SHIFT - inode->i_blkbits); 21618eb9e5ceSTheodore Ts'o 21628eb9e5ceSTheodore Ts'o if (!page_has_buffers(page)) { 21634f01b02cSTheodore Ts'o mpage_add_bh_to_extent(mpd, logical, 21644f01b02cSTheodore Ts'o PAGE_CACHE_SIZE, 21658eb9e5ceSTheodore Ts'o (1 << BH_Dirty) | (1 << BH_Uptodate)); 21664f01b02cSTheodore Ts'o if (mpd->io_done) 21674f01b02cSTheodore Ts'o goto ret_extent_tail; 21688e48dcfbSTheodore Ts'o } else { 21698eb9e5ceSTheodore Ts'o /* 21704f01b02cSTheodore Ts'o * Page with regular buffer heads, 21714f01b02cSTheodore Ts'o * just add all dirty ones 21728eb9e5ceSTheodore Ts'o */ 21738eb9e5ceSTheodore Ts'o head = page_buffers(page); 21748eb9e5ceSTheodore Ts'o bh = head; 21758eb9e5ceSTheodore Ts'o do { 21768eb9e5ceSTheodore Ts'o BUG_ON(buffer_locked(bh)); 21778eb9e5ceSTheodore Ts'o /* 21788eb9e5ceSTheodore Ts'o * We need to try to allocate 21798eb9e5ceSTheodore Ts'o * unmapped blocks in the same page. 21808eb9e5ceSTheodore Ts'o * Otherwise we won't make progress 21818eb9e5ceSTheodore Ts'o * with the page in ext4_writepage 21828eb9e5ceSTheodore Ts'o */ 21838eb9e5ceSTheodore Ts'o if (ext4_bh_delay_or_unwritten(NULL, bh)) { 21848eb9e5ceSTheodore Ts'o mpage_add_bh_to_extent(mpd, logical, 21858eb9e5ceSTheodore Ts'o bh->b_size, 21868eb9e5ceSTheodore Ts'o bh->b_state); 21874f01b02cSTheodore Ts'o if (mpd->io_done) 21884f01b02cSTheodore Ts'o goto ret_extent_tail; 21898eb9e5ceSTheodore Ts'o } else if (buffer_dirty(bh) && (buffer_mapped(bh))) { 21908eb9e5ceSTheodore Ts'o /* 21914f01b02cSTheodore Ts'o * mapped dirty buffer. We need 21924f01b02cSTheodore Ts'o * to update the b_state 21934f01b02cSTheodore Ts'o * because we look at b_state 21944f01b02cSTheodore Ts'o * in mpage_da_map_blocks. We 21954f01b02cSTheodore Ts'o * don't update b_size because 21964f01b02cSTheodore Ts'o * if we find an unmapped 21974f01b02cSTheodore Ts'o * buffer_head later we need to 21984f01b02cSTheodore Ts'o * use the b_state flag of that 21994f01b02cSTheodore Ts'o * buffer_head. 22008eb9e5ceSTheodore Ts'o */ 22018eb9e5ceSTheodore Ts'o if (mpd->b_size == 0) 22028eb9e5ceSTheodore Ts'o mpd->b_state = bh->b_state & BH_FLAGS; 22038e48dcfbSTheodore Ts'o } 22048eb9e5ceSTheodore Ts'o logical++; 22058eb9e5ceSTheodore Ts'o } while ((bh = bh->b_this_page) != head); 22068e48dcfbSTheodore Ts'o } 22078e48dcfbSTheodore Ts'o 22088e48dcfbSTheodore Ts'o if (nr_to_write > 0) { 22098e48dcfbSTheodore Ts'o nr_to_write--; 22108e48dcfbSTheodore Ts'o if (nr_to_write == 0 && 22114f01b02cSTheodore Ts'o wbc->sync_mode == WB_SYNC_NONE) 22128e48dcfbSTheodore Ts'o /* 22138e48dcfbSTheodore Ts'o * We stop writing back only if we are 22148e48dcfbSTheodore Ts'o * not doing integrity sync. In case of 22158e48dcfbSTheodore Ts'o * integrity sync we have to keep going 22168e48dcfbSTheodore Ts'o * because someone may be concurrently 22178e48dcfbSTheodore Ts'o * dirtying pages, and we might have 22188e48dcfbSTheodore Ts'o * synced a lot of newly appeared dirty 22198e48dcfbSTheodore Ts'o * pages, but have not synced all of the 22208e48dcfbSTheodore Ts'o * old dirty pages. 22218e48dcfbSTheodore Ts'o */ 22224f01b02cSTheodore Ts'o goto out; 22238e48dcfbSTheodore Ts'o } 22248e48dcfbSTheodore Ts'o } 22258e48dcfbSTheodore Ts'o pagevec_release(&pvec); 22268e48dcfbSTheodore Ts'o cond_resched(); 22278e48dcfbSTheodore Ts'o } 22284f01b02cSTheodore Ts'o return 0; 22294f01b02cSTheodore Ts'o ret_extent_tail: 22304f01b02cSTheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 22318eb9e5ceSTheodore Ts'o out: 22328eb9e5ceSTheodore Ts'o pagevec_release(&pvec); 22338eb9e5ceSTheodore Ts'o cond_resched(); 22348e48dcfbSTheodore Ts'o return ret; 22358e48dcfbSTheodore Ts'o } 22368e48dcfbSTheodore Ts'o 22378e48dcfbSTheodore Ts'o 223864769240SAlex Tomas static int ext4_da_writepages(struct address_space *mapping, 223964769240SAlex Tomas struct writeback_control *wbc) 224064769240SAlex Tomas { 224122208dedSAneesh Kumar K.V pgoff_t index; 224222208dedSAneesh Kumar K.V int range_whole = 0; 224361628a3fSMingming Cao handle_t *handle = NULL; 2244df22291fSAneesh Kumar K.V struct mpage_da_data mpd; 22455e745b04SAneesh Kumar K.V struct inode *inode = mapping->host; 2246498e5f24STheodore Ts'o int pages_written = 0; 224755138e0bSTheodore Ts'o unsigned int max_pages; 22482acf2c26SAneesh Kumar K.V int range_cyclic, cycled = 1, io_done = 0; 224955138e0bSTheodore Ts'o int needed_blocks, ret = 0; 225055138e0bSTheodore Ts'o long desired_nr_to_write, nr_to_writebump = 0; 2251de89de6eSTheodore Ts'o loff_t range_start = wbc->range_start; 22525e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 225372f84e65SEric Sandeen pgoff_t done_index = 0; 22545b41d924SEric Sandeen pgoff_t end; 22551bce63d1SShaohua Li struct blk_plug plug; 225661628a3fSMingming Cao 22579bffad1eSTheodore Ts'o trace_ext4_da_writepages(inode, wbc); 2258ba80b101STheodore Ts'o 225961628a3fSMingming Cao /* 226061628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 226161628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 226261628a3fSMingming Cao * because that could violate lock ordering on umount 226361628a3fSMingming Cao */ 2264a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 226561628a3fSMingming Cao return 0; 22662a21e37eSTheodore Ts'o 22672a21e37eSTheodore Ts'o /* 22682a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 22692a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 22702a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 22714ab2f15bSTheodore Ts'o * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because 22722a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 22732a21e37eSTheodore Ts'o * read-only, and in that case, ext4_da_writepages should 22742a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 22752a21e37eSTheodore Ts'o * the stack trace. 22762a21e37eSTheodore Ts'o */ 22774ab2f15bSTheodore Ts'o if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) 22782a21e37eSTheodore Ts'o return -EROFS; 22792a21e37eSTheodore Ts'o 228022208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 228122208dedSAneesh Kumar K.V range_whole = 1; 228261628a3fSMingming Cao 22832acf2c26SAneesh Kumar K.V range_cyclic = wbc->range_cyclic; 22842acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 228522208dedSAneesh Kumar K.V index = mapping->writeback_index; 22862acf2c26SAneesh Kumar K.V if (index) 22872acf2c26SAneesh Kumar K.V cycled = 0; 22882acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 22892acf2c26SAneesh Kumar K.V wbc->range_end = LLONG_MAX; 22902acf2c26SAneesh Kumar K.V wbc->range_cyclic = 0; 22915b41d924SEric Sandeen end = -1; 22925b41d924SEric Sandeen } else { 229322208dedSAneesh Kumar K.V index = wbc->range_start >> PAGE_CACHE_SHIFT; 22945b41d924SEric Sandeen end = wbc->range_end >> PAGE_CACHE_SHIFT; 22955b41d924SEric Sandeen } 2296a1d6cc56SAneesh Kumar K.V 229755138e0bSTheodore Ts'o /* 229855138e0bSTheodore Ts'o * This works around two forms of stupidity. The first is in 229955138e0bSTheodore Ts'o * the writeback code, which caps the maximum number of pages 230055138e0bSTheodore Ts'o * written to be 1024 pages. This is wrong on multiple 230155138e0bSTheodore Ts'o * levels; different architectues have a different page size, 230255138e0bSTheodore Ts'o * which changes the maximum amount of data which gets 230355138e0bSTheodore Ts'o * written. Secondly, 4 megabytes is way too small. XFS 230455138e0bSTheodore Ts'o * forces this value to be 16 megabytes by multiplying 230555138e0bSTheodore Ts'o * nr_to_write parameter by four, and then relies on its 230655138e0bSTheodore Ts'o * allocator to allocate larger extents to make them 230755138e0bSTheodore Ts'o * contiguous. Unfortunately this brings us to the second 230855138e0bSTheodore Ts'o * stupidity, which is that ext4's mballoc code only allocates 230955138e0bSTheodore Ts'o * at most 2048 blocks. So we force contiguous writes up to 231055138e0bSTheodore Ts'o * the number of dirty blocks in the inode, or 231155138e0bSTheodore Ts'o * sbi->max_writeback_mb_bump whichever is smaller. 231255138e0bSTheodore Ts'o */ 231355138e0bSTheodore Ts'o max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT); 2314b443e733SEric Sandeen if (!range_cyclic && range_whole) { 2315b443e733SEric Sandeen if (wbc->nr_to_write == LONG_MAX) 2316b443e733SEric Sandeen desired_nr_to_write = wbc->nr_to_write; 231755138e0bSTheodore Ts'o else 2318b443e733SEric Sandeen desired_nr_to_write = wbc->nr_to_write * 8; 2319b443e733SEric Sandeen } else 232055138e0bSTheodore Ts'o desired_nr_to_write = ext4_num_dirty_pages(inode, index, 232155138e0bSTheodore Ts'o max_pages); 232255138e0bSTheodore Ts'o if (desired_nr_to_write > max_pages) 232355138e0bSTheodore Ts'o desired_nr_to_write = max_pages; 232455138e0bSTheodore Ts'o 232555138e0bSTheodore Ts'o if (wbc->nr_to_write < desired_nr_to_write) { 232655138e0bSTheodore Ts'o nr_to_writebump = desired_nr_to_write - wbc->nr_to_write; 232755138e0bSTheodore Ts'o wbc->nr_to_write = desired_nr_to_write; 232855138e0bSTheodore Ts'o } 232955138e0bSTheodore Ts'o 23302acf2c26SAneesh Kumar K.V retry: 23316e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 23325b41d924SEric Sandeen tag_pages_for_writeback(mapping, index, end); 23335b41d924SEric Sandeen 23341bce63d1SShaohua Li blk_start_plug(&plug); 233522208dedSAneesh Kumar K.V while (!ret && wbc->nr_to_write > 0) { 2336a1d6cc56SAneesh Kumar K.V 2337a1d6cc56SAneesh Kumar K.V /* 2338a1d6cc56SAneesh Kumar K.V * we insert one extent at a time. So we need 2339a1d6cc56SAneesh Kumar K.V * credit needed for single extent allocation. 2340a1d6cc56SAneesh Kumar K.V * journalled mode is currently not supported 2341a1d6cc56SAneesh Kumar K.V * by delalloc 2342a1d6cc56SAneesh Kumar K.V */ 2343a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 2344525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 2345a1d6cc56SAneesh Kumar K.V 234661628a3fSMingming Cao /* start a new transaction*/ 234761628a3fSMingming Cao handle = ext4_journal_start(inode, needed_blocks); 234861628a3fSMingming Cao if (IS_ERR(handle)) { 234961628a3fSMingming Cao ret = PTR_ERR(handle); 23501693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2351fbe845ddSCurt Wohlgemuth "%ld pages, ino %lu; err %d", __func__, 2352a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 23533c1fcb2cSNamjae Jeon blk_finish_plug(&plug); 235461628a3fSMingming Cao goto out_writepages; 235561628a3fSMingming Cao } 2356f63e6005STheodore Ts'o 2357f63e6005STheodore Ts'o /* 23588eb9e5ceSTheodore Ts'o * Now call write_cache_pages_da() to find the next 2359f63e6005STheodore Ts'o * contiguous region of logical blocks that need 23608eb9e5ceSTheodore Ts'o * blocks to be allocated by ext4 and submit them. 2361f63e6005STheodore Ts'o */ 236272f84e65SEric Sandeen ret = write_cache_pages_da(mapping, wbc, &mpd, &done_index); 2363f63e6005STheodore Ts'o /* 2364af901ca1SAndré Goddard Rosa * If we have a contiguous extent of pages and we 2365f63e6005STheodore Ts'o * haven't done the I/O yet, map the blocks and submit 2366f63e6005STheodore Ts'o * them for I/O. 2367f63e6005STheodore Ts'o */ 2368f63e6005STheodore Ts'o if (!mpd.io_done && mpd.next_page != mpd.first_page) { 23695a87b7a5STheodore Ts'o mpage_da_map_and_submit(&mpd); 2370f63e6005STheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 2371f63e6005STheodore Ts'o } 2372b3a3ca8cSTheodore Ts'o trace_ext4_da_write_pages(inode, &mpd); 2373f63e6005STheodore Ts'o wbc->nr_to_write -= mpd.pages_written; 2374df22291fSAneesh Kumar K.V 237561628a3fSMingming Cao ext4_journal_stop(handle); 2376df22291fSAneesh Kumar K.V 23778f64b32eSEric Sandeen if ((mpd.retval == -ENOSPC) && sbi->s_journal) { 237822208dedSAneesh Kumar K.V /* commit the transaction which would 237922208dedSAneesh Kumar K.V * free blocks released in the transaction 238022208dedSAneesh Kumar K.V * and try again 238122208dedSAneesh Kumar K.V */ 2382df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 238322208dedSAneesh Kumar K.V ret = 0; 238422208dedSAneesh Kumar K.V } else if (ret == MPAGE_DA_EXTENT_TAIL) { 2385a1d6cc56SAneesh Kumar K.V /* 23868de49e67SKazuya Mio * Got one extent now try with rest of the pages. 23878de49e67SKazuya Mio * If mpd.retval is set -EIO, journal is aborted. 23888de49e67SKazuya Mio * So we don't need to write any more. 2389a1d6cc56SAneesh Kumar K.V */ 239022208dedSAneesh Kumar K.V pages_written += mpd.pages_written; 23918de49e67SKazuya Mio ret = mpd.retval; 23922acf2c26SAneesh Kumar K.V io_done = 1; 239322208dedSAneesh Kumar K.V } else if (wbc->nr_to_write) 239461628a3fSMingming Cao /* 239561628a3fSMingming Cao * There is no more writeout needed 239661628a3fSMingming Cao * or we requested for a noblocking writeout 239761628a3fSMingming Cao * and we found the device congested 239861628a3fSMingming Cao */ 239961628a3fSMingming Cao break; 240061628a3fSMingming Cao } 24011bce63d1SShaohua Li blk_finish_plug(&plug); 24022acf2c26SAneesh Kumar K.V if (!io_done && !cycled) { 24032acf2c26SAneesh Kumar K.V cycled = 1; 24042acf2c26SAneesh Kumar K.V index = 0; 24052acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 24062acf2c26SAneesh Kumar K.V wbc->range_end = mapping->writeback_index - 1; 24072acf2c26SAneesh Kumar K.V goto retry; 24082acf2c26SAneesh Kumar K.V } 240961628a3fSMingming Cao 241022208dedSAneesh Kumar K.V /* Update index */ 24112acf2c26SAneesh Kumar K.V wbc->range_cyclic = range_cyclic; 241222208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 241322208dedSAneesh Kumar K.V /* 241422208dedSAneesh Kumar K.V * set the writeback_index so that range_cyclic 241522208dedSAneesh Kumar K.V * mode will write it back later 241622208dedSAneesh Kumar K.V */ 241772f84e65SEric Sandeen mapping->writeback_index = done_index; 2418a1d6cc56SAneesh Kumar K.V 241961628a3fSMingming Cao out_writepages: 242022208dedSAneesh Kumar K.V wbc->nr_to_write -= nr_to_writebump; 2421de89de6eSTheodore Ts'o wbc->range_start = range_start; 24229bffad1eSTheodore Ts'o trace_ext4_da_writepages_result(inode, wbc, ret, pages_written); 242361628a3fSMingming Cao return ret; 242464769240SAlex Tomas } 242564769240SAlex Tomas 242679f0be8dSAneesh Kumar K.V #define FALL_BACK_TO_NONDELALLOC 1 242779f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 242879f0be8dSAneesh Kumar K.V { 242979f0be8dSAneesh Kumar K.V s64 free_blocks, dirty_blocks; 243079f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 243179f0be8dSAneesh Kumar K.V 243279f0be8dSAneesh Kumar K.V /* 243379f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 243479f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 2435179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 243679f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 243779f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 243879f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 243979f0be8dSAneesh Kumar K.V */ 244057042651STheodore Ts'o free_blocks = EXT4_C2B(sbi, 244157042651STheodore Ts'o percpu_counter_read_positive(&sbi->s_freeclusters_counter)); 244257042651STheodore Ts'o dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyclusters_counter); 244300d4e736STheodore Ts'o /* 244400d4e736STheodore Ts'o * Start pushing delalloc when 1/2 of free blocks are dirty. 244500d4e736STheodore Ts'o */ 244600d4e736STheodore Ts'o if (dirty_blocks && (free_blocks < 2 * dirty_blocks) && 244700d4e736STheodore Ts'o !writeback_in_progress(sb->s_bdi) && 244800d4e736STheodore Ts'o down_read_trylock(&sb->s_umount)) { 244900d4e736STheodore Ts'o writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE); 245000d4e736STheodore Ts'o up_read(&sb->s_umount); 245100d4e736STheodore Ts'o } 245200d4e736STheodore Ts'o 245379f0be8dSAneesh Kumar K.V if (2 * free_blocks < 3 * dirty_blocks || 2454df55c99dSTheodore Ts'o free_blocks < (dirty_blocks + EXT4_FREECLUSTERS_WATERMARK)) { 245579f0be8dSAneesh Kumar K.V /* 2456c8afb446SEric Sandeen * free block count is less than 150% of dirty blocks 2457c8afb446SEric Sandeen * or free blocks is less than watermark 245879f0be8dSAneesh Kumar K.V */ 245979f0be8dSAneesh Kumar K.V return 1; 246079f0be8dSAneesh Kumar K.V } 246179f0be8dSAneesh Kumar K.V return 0; 246279f0be8dSAneesh Kumar K.V } 246379f0be8dSAneesh Kumar K.V 246464769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 246564769240SAlex Tomas loff_t pos, unsigned len, unsigned flags, 246664769240SAlex Tomas struct page **pagep, void **fsdata) 246764769240SAlex Tomas { 246872b8ab9dSEric Sandeen int ret, retries = 0; 246964769240SAlex Tomas struct page *page; 247064769240SAlex Tomas pgoff_t index; 247164769240SAlex Tomas struct inode *inode = mapping->host; 247264769240SAlex Tomas handle_t *handle; 247364769240SAlex Tomas 247464769240SAlex Tomas index = pos >> PAGE_CACHE_SHIFT; 247579f0be8dSAneesh Kumar K.V 247679f0be8dSAneesh Kumar K.V if (ext4_nonda_switch(inode->i_sb)) { 247779f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 247879f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 247979f0be8dSAneesh Kumar K.V len, flags, pagep, fsdata); 248079f0be8dSAneesh Kumar K.V } 248179f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 24829bffad1eSTheodore Ts'o trace_ext4_da_write_begin(inode, pos, len, flags); 2483d2a17637SMingming Cao retry: 248464769240SAlex Tomas /* 248564769240SAlex Tomas * With delayed allocation, we don't log the i_disksize update 248664769240SAlex Tomas * if there is delayed block allocation. But we still need 248764769240SAlex Tomas * to journalling the i_disksize update if writes to the end 248864769240SAlex Tomas * of file which has an already mapped buffer. 248964769240SAlex Tomas */ 249064769240SAlex Tomas handle = ext4_journal_start(inode, 1); 249164769240SAlex Tomas if (IS_ERR(handle)) { 249264769240SAlex Tomas ret = PTR_ERR(handle); 249364769240SAlex Tomas goto out; 249464769240SAlex Tomas } 2495ebd3610bSJan Kara /* We cannot recurse into the filesystem as the transaction is already 2496ebd3610bSJan Kara * started */ 2497ebd3610bSJan Kara flags |= AOP_FLAG_NOFS; 249864769240SAlex Tomas 249954566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 2500d5a0d4f7SEric Sandeen if (!page) { 2501d5a0d4f7SEric Sandeen ext4_journal_stop(handle); 2502d5a0d4f7SEric Sandeen ret = -ENOMEM; 2503d5a0d4f7SEric Sandeen goto out; 2504d5a0d4f7SEric Sandeen } 250564769240SAlex Tomas *pagep = page; 250664769240SAlex Tomas 25076e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep); 250864769240SAlex Tomas if (ret < 0) { 250964769240SAlex Tomas unlock_page(page); 251064769240SAlex Tomas ext4_journal_stop(handle); 251164769240SAlex Tomas page_cache_release(page); 2512ae4d5372SAneesh Kumar K.V /* 2513ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 2514ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 2515ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 2516ae4d5372SAneesh Kumar K.V */ 2517ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 2518b9a4207dSJan Kara ext4_truncate_failed_write(inode); 251964769240SAlex Tomas } 252064769240SAlex Tomas 2521d2a17637SMingming Cao if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 2522d2a17637SMingming Cao goto retry; 252364769240SAlex Tomas out: 252464769240SAlex Tomas return ret; 252564769240SAlex Tomas } 252664769240SAlex Tomas 2527632eaeabSMingming Cao /* 2528632eaeabSMingming Cao * Check if we should update i_disksize 2529632eaeabSMingming Cao * when write to the end of file but not require block allocation 2530632eaeabSMingming Cao */ 2531632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page, 2532632eaeabSMingming Cao unsigned long offset) 2533632eaeabSMingming Cao { 2534632eaeabSMingming Cao struct buffer_head *bh; 2535632eaeabSMingming Cao struct inode *inode = page->mapping->host; 2536632eaeabSMingming Cao unsigned int idx; 2537632eaeabSMingming Cao int i; 2538632eaeabSMingming Cao 2539632eaeabSMingming Cao bh = page_buffers(page); 2540632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 2541632eaeabSMingming Cao 2542632eaeabSMingming Cao for (i = 0; i < idx; i++) 2543632eaeabSMingming Cao bh = bh->b_this_page; 2544632eaeabSMingming Cao 254529fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 2546632eaeabSMingming Cao return 0; 2547632eaeabSMingming Cao return 1; 2548632eaeabSMingming Cao } 2549632eaeabSMingming Cao 255064769240SAlex Tomas static int ext4_da_write_end(struct file *file, 255164769240SAlex Tomas struct address_space *mapping, 255264769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 255364769240SAlex Tomas struct page *page, void *fsdata) 255464769240SAlex Tomas { 255564769240SAlex Tomas struct inode *inode = mapping->host; 255664769240SAlex Tomas int ret = 0, ret2; 255764769240SAlex Tomas handle_t *handle = ext4_journal_current_handle(); 255864769240SAlex Tomas loff_t new_i_size; 2559632eaeabSMingming Cao unsigned long start, end; 256079f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 256179f0be8dSAneesh Kumar K.V 256279f0be8dSAneesh Kumar K.V if (write_mode == FALL_BACK_TO_NONDELALLOC) { 25633d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 25643d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 256579f0be8dSAneesh Kumar K.V return ext4_ordered_write_end(file, mapping, pos, 256679f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 25673d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 256879f0be8dSAneesh Kumar K.V return ext4_writeback_write_end(file, mapping, pos, 256979f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 25703d2b1582SLukas Czerner default: 257179f0be8dSAneesh Kumar K.V BUG(); 257279f0be8dSAneesh Kumar K.V } 257379f0be8dSAneesh Kumar K.V } 2574632eaeabSMingming Cao 25759bffad1eSTheodore Ts'o trace_ext4_da_write_end(inode, pos, len, copied); 2576632eaeabSMingming Cao start = pos & (PAGE_CACHE_SIZE - 1); 2577632eaeabSMingming Cao end = start + copied - 1; 257864769240SAlex Tomas 257964769240SAlex Tomas /* 258064769240SAlex Tomas * generic_write_end() will run mark_inode_dirty() if i_size 258164769240SAlex Tomas * changes. So let's piggyback the i_disksize mark_inode_dirty 258264769240SAlex Tomas * into that. 258364769240SAlex Tomas */ 258464769240SAlex Tomas 258564769240SAlex Tomas new_i_size = pos + copied; 2586ea51d132SAndrea Arcangeli if (copied && new_i_size > EXT4_I(inode)->i_disksize) { 2587632eaeabSMingming Cao if (ext4_da_should_update_i_disksize(page, end)) { 2588632eaeabSMingming Cao down_write(&EXT4_I(inode)->i_data_sem); 2589f3b59291STheodore Ts'o if (new_i_size > EXT4_I(inode)->i_disksize) 259064769240SAlex Tomas EXT4_I(inode)->i_disksize = new_i_size; 2591632eaeabSMingming Cao up_write(&EXT4_I(inode)->i_data_sem); 2592cf17fea6SAneesh Kumar K.V /* We need to mark inode dirty even if 2593cf17fea6SAneesh Kumar K.V * new_i_size is less that inode->i_size 2594cf17fea6SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 2595cf17fea6SAneesh Kumar K.V */ 2596cf17fea6SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 2597632eaeabSMingming Cao } 2598632eaeabSMingming Cao } 259964769240SAlex Tomas ret2 = generic_write_end(file, mapping, pos, len, copied, 260064769240SAlex Tomas page, fsdata); 260164769240SAlex Tomas copied = ret2; 260264769240SAlex Tomas if (ret2 < 0) 260364769240SAlex Tomas ret = ret2; 260464769240SAlex Tomas ret2 = ext4_journal_stop(handle); 260564769240SAlex Tomas if (!ret) 260664769240SAlex Tomas ret = ret2; 260764769240SAlex Tomas 260864769240SAlex Tomas return ret ? ret : copied; 260964769240SAlex Tomas } 261064769240SAlex Tomas 261164769240SAlex Tomas static void ext4_da_invalidatepage(struct page *page, unsigned long offset) 261264769240SAlex Tomas { 261364769240SAlex Tomas /* 261464769240SAlex Tomas * Drop reserved blocks 261564769240SAlex Tomas */ 261664769240SAlex Tomas BUG_ON(!PageLocked(page)); 261764769240SAlex Tomas if (!page_has_buffers(page)) 261864769240SAlex Tomas goto out; 261964769240SAlex Tomas 2620d2a17637SMingming Cao ext4_da_page_release_reservation(page, offset); 262164769240SAlex Tomas 262264769240SAlex Tomas out: 262364769240SAlex Tomas ext4_invalidatepage(page, offset); 262464769240SAlex Tomas 262564769240SAlex Tomas return; 262664769240SAlex Tomas } 262764769240SAlex Tomas 2628ccd2506bSTheodore Ts'o /* 2629ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 2630ccd2506bSTheodore Ts'o */ 2631ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 2632ccd2506bSTheodore Ts'o { 2633fb40ba0dSTheodore Ts'o trace_ext4_alloc_da_blocks(inode); 2634fb40ba0dSTheodore Ts'o 2635ccd2506bSTheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks && 2636ccd2506bSTheodore Ts'o !EXT4_I(inode)->i_reserved_meta_blocks) 2637ccd2506bSTheodore Ts'o return 0; 2638ccd2506bSTheodore Ts'o 2639ccd2506bSTheodore Ts'o /* 2640ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 2641ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 2642ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 2643ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 2644ccd2506bSTheodore Ts'o * would require replicating code paths in: 2645ccd2506bSTheodore Ts'o * 2646ccd2506bSTheodore Ts'o * ext4_da_writepages() -> 2647ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 2648ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 2649ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 2650ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 2651ccd2506bSTheodore Ts'o * 2652ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 2653ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 2654ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 2655ccd2506bSTheodore Ts'o * doing I/O at all. 2656ccd2506bSTheodore Ts'o * 2657ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 2658380cf090SWu Fengguang * the pages by calling redirty_page_for_writepage() but that 2659ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 2660ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 266125985edcSLucas De Marchi * simplifying them because we wouldn't actually intend to 2662ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 2663ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 2664ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 2665ccd2506bSTheodore Ts'o * 2666ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 2667ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 2668ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 2669ccd2506bSTheodore Ts'o */ 2670ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 2671ccd2506bSTheodore Ts'o } 267264769240SAlex Tomas 267364769240SAlex Tomas /* 2674ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 2675ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 2676ac27a0ecSDave Kleikamp * 2677ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 2678617ba13bSMingming Cao * journal. If somebody makes a swapfile on an ext4 data-journaling 2679ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 2680ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 2681ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 2682ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 2683ac27a0ecSDave Kleikamp * 2684ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 2685ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 2686ac27a0ecSDave Kleikamp */ 2687617ba13bSMingming Cao static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 2688ac27a0ecSDave Kleikamp { 2689ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 2690ac27a0ecSDave Kleikamp journal_t *journal; 2691ac27a0ecSDave Kleikamp int err; 2692ac27a0ecSDave Kleikamp 2693*46c7f254STao Ma /* 2694*46c7f254STao Ma * We can get here for an inline file via the FIBMAP ioctl 2695*46c7f254STao Ma */ 2696*46c7f254STao Ma if (ext4_has_inline_data(inode)) 2697*46c7f254STao Ma return 0; 2698*46c7f254STao Ma 269964769240SAlex Tomas if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 270064769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) { 270164769240SAlex Tomas /* 270264769240SAlex Tomas * With delalloc we want to sync the file 270364769240SAlex Tomas * so that we can make sure we allocate 270464769240SAlex Tomas * blocks for file 270564769240SAlex Tomas */ 270664769240SAlex Tomas filemap_write_and_wait(mapping); 270764769240SAlex Tomas } 270864769240SAlex Tomas 270919f5fb7aSTheodore Ts'o if (EXT4_JOURNAL(inode) && 271019f5fb7aSTheodore Ts'o ext4_test_inode_state(inode, EXT4_STATE_JDATA)) { 2711ac27a0ecSDave Kleikamp /* 2712ac27a0ecSDave Kleikamp * This is a REALLY heavyweight approach, but the use of 2713ac27a0ecSDave Kleikamp * bmap on dirty files is expected to be extremely rare: 2714ac27a0ecSDave Kleikamp * only if we run lilo or swapon on a freshly made file 2715ac27a0ecSDave Kleikamp * do we expect this to happen. 2716ac27a0ecSDave Kleikamp * 2717ac27a0ecSDave Kleikamp * (bmap requires CAP_SYS_RAWIO so this does not 2718ac27a0ecSDave Kleikamp * represent an unprivileged user DOS attack --- we'd be 2719ac27a0ecSDave Kleikamp * in trouble if mortal users could trigger this path at 2720ac27a0ecSDave Kleikamp * will.) 2721ac27a0ecSDave Kleikamp * 2722617ba13bSMingming Cao * NB. EXT4_STATE_JDATA is not set on files other than 2723ac27a0ecSDave Kleikamp * regular files. If somebody wants to bmap a directory 2724ac27a0ecSDave Kleikamp * or symlink and gets confused because the buffer 2725ac27a0ecSDave Kleikamp * hasn't yet been flushed to disk, they deserve 2726ac27a0ecSDave Kleikamp * everything they get. 2727ac27a0ecSDave Kleikamp */ 2728ac27a0ecSDave Kleikamp 272919f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_JDATA); 2730617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 2731dab291afSMingming Cao jbd2_journal_lock_updates(journal); 2732dab291afSMingming Cao err = jbd2_journal_flush(journal); 2733dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 2734ac27a0ecSDave Kleikamp 2735ac27a0ecSDave Kleikamp if (err) 2736ac27a0ecSDave Kleikamp return 0; 2737ac27a0ecSDave Kleikamp } 2738ac27a0ecSDave Kleikamp 2739617ba13bSMingming Cao return generic_block_bmap(mapping, block, ext4_get_block); 2740ac27a0ecSDave Kleikamp } 2741ac27a0ecSDave Kleikamp 2742617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page) 2743ac27a0ecSDave Kleikamp { 2744*46c7f254STao Ma int ret = -EAGAIN; 2745*46c7f254STao Ma struct inode *inode = page->mapping->host; 2746*46c7f254STao Ma 27470562e0baSJiaying Zhang trace_ext4_readpage(page); 2748*46c7f254STao Ma 2749*46c7f254STao Ma if (ext4_has_inline_data(inode)) 2750*46c7f254STao Ma ret = ext4_readpage_inline(inode, page); 2751*46c7f254STao Ma 2752*46c7f254STao Ma if (ret == -EAGAIN) 2753617ba13bSMingming Cao return mpage_readpage(page, ext4_get_block); 2754*46c7f254STao Ma 2755*46c7f254STao Ma return ret; 2756ac27a0ecSDave Kleikamp } 2757ac27a0ecSDave Kleikamp 2758ac27a0ecSDave Kleikamp static int 2759617ba13bSMingming Cao ext4_readpages(struct file *file, struct address_space *mapping, 2760ac27a0ecSDave Kleikamp struct list_head *pages, unsigned nr_pages) 2761ac27a0ecSDave Kleikamp { 2762*46c7f254STao Ma struct inode *inode = mapping->host; 2763*46c7f254STao Ma 2764*46c7f254STao Ma /* If the file has inline data, no need to do readpages. */ 2765*46c7f254STao Ma if (ext4_has_inline_data(inode)) 2766*46c7f254STao Ma return 0; 2767*46c7f254STao Ma 2768617ba13bSMingming Cao return mpage_readpages(mapping, pages, nr_pages, ext4_get_block); 2769ac27a0ecSDave Kleikamp } 2770ac27a0ecSDave Kleikamp 2771744692dcSJiaying Zhang static void ext4_invalidatepage_free_endio(struct page *page, unsigned long offset) 2772744692dcSJiaying Zhang { 2773744692dcSJiaying Zhang struct buffer_head *head, *bh; 2774744692dcSJiaying Zhang unsigned int curr_off = 0; 2775744692dcSJiaying Zhang 2776744692dcSJiaying Zhang if (!page_has_buffers(page)) 2777744692dcSJiaying Zhang return; 2778744692dcSJiaying Zhang head = bh = page_buffers(page); 2779744692dcSJiaying Zhang do { 2780744692dcSJiaying Zhang if (offset <= curr_off && test_clear_buffer_uninit(bh) 2781744692dcSJiaying Zhang && bh->b_private) { 2782744692dcSJiaying Zhang ext4_free_io_end(bh->b_private); 2783744692dcSJiaying Zhang bh->b_private = NULL; 2784744692dcSJiaying Zhang bh->b_end_io = NULL; 2785744692dcSJiaying Zhang } 2786744692dcSJiaying Zhang curr_off = curr_off + bh->b_size; 2787744692dcSJiaying Zhang bh = bh->b_this_page; 2788744692dcSJiaying Zhang } while (bh != head); 2789744692dcSJiaying Zhang } 2790744692dcSJiaying Zhang 2791617ba13bSMingming Cao static void ext4_invalidatepage(struct page *page, unsigned long offset) 2792ac27a0ecSDave Kleikamp { 2793617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 2794ac27a0ecSDave Kleikamp 27950562e0baSJiaying Zhang trace_ext4_invalidatepage(page, offset); 27960562e0baSJiaying Zhang 2797ac27a0ecSDave Kleikamp /* 2798744692dcSJiaying Zhang * free any io_end structure allocated for buffers to be discarded 2799744692dcSJiaying Zhang */ 2800744692dcSJiaying Zhang if (ext4_should_dioread_nolock(page->mapping->host)) 2801744692dcSJiaying Zhang ext4_invalidatepage_free_endio(page, offset); 2802744692dcSJiaying Zhang /* 2803ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 2804ac27a0ecSDave Kleikamp */ 2805ac27a0ecSDave Kleikamp if (offset == 0) 2806ac27a0ecSDave Kleikamp ClearPageChecked(page); 2807ac27a0ecSDave Kleikamp 28080390131bSFrank Mayhar if (journal) 2809dab291afSMingming Cao jbd2_journal_invalidatepage(journal, page, offset); 28100390131bSFrank Mayhar else 28110390131bSFrank Mayhar block_invalidatepage(page, offset); 2812ac27a0ecSDave Kleikamp } 2813ac27a0ecSDave Kleikamp 2814617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait) 2815ac27a0ecSDave Kleikamp { 2816617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 2817ac27a0ecSDave Kleikamp 28180562e0baSJiaying Zhang trace_ext4_releasepage(page); 28190562e0baSJiaying Zhang 2820ac27a0ecSDave Kleikamp WARN_ON(PageChecked(page)); 2821ac27a0ecSDave Kleikamp if (!page_has_buffers(page)) 2822ac27a0ecSDave Kleikamp return 0; 28230390131bSFrank Mayhar if (journal) 2824dab291afSMingming Cao return jbd2_journal_try_to_free_buffers(journal, page, wait); 28250390131bSFrank Mayhar else 28260390131bSFrank Mayhar return try_to_free_buffers(page); 2827ac27a0ecSDave Kleikamp } 2828ac27a0ecSDave Kleikamp 2829ac27a0ecSDave Kleikamp /* 28302ed88685STheodore Ts'o * ext4_get_block used when preparing for a DIO write or buffer write. 28312ed88685STheodore Ts'o * We allocate an uinitialized extent if blocks haven't been allocated. 28322ed88685STheodore Ts'o * The extent will be converted to initialized after the IO is complete. 28332ed88685STheodore Ts'o */ 2834c7064ef1SJiaying Zhang static int ext4_get_block_write(struct inode *inode, sector_t iblock, 28354c0425ffSMingming Cao struct buffer_head *bh_result, int create) 28364c0425ffSMingming Cao { 2837c7064ef1SJiaying Zhang ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n", 28388d5d02e6SMingming Cao inode->i_ino, create); 28392ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh_result, 28402ed88685STheodore Ts'o EXT4_GET_BLOCKS_IO_CREATE_EXT); 28414c0425ffSMingming Cao } 28424c0425ffSMingming Cao 2843729f52c6SZheng Liu static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock, 28448b0f165fSAnatol Pomozov struct buffer_head *bh_result, int create) 2845729f52c6SZheng Liu { 28468b0f165fSAnatol Pomozov ext4_debug("ext4_get_block_write_nolock: inode %lu, create flag %d\n", 28478b0f165fSAnatol Pomozov inode->i_ino, create); 28488b0f165fSAnatol Pomozov return _ext4_get_block(inode, iblock, bh_result, 28498b0f165fSAnatol Pomozov EXT4_GET_BLOCKS_NO_LOCK); 2850729f52c6SZheng Liu } 2851729f52c6SZheng Liu 28524c0425ffSMingming Cao static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset, 2853552ef802SChristoph Hellwig ssize_t size, void *private, int ret, 2854552ef802SChristoph Hellwig bool is_async) 28554c0425ffSMingming Cao { 285672c5052dSChristoph Hellwig struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode; 28574c0425ffSMingming Cao ext4_io_end_t *io_end = iocb->private; 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 288728a535f9SDmitry Monakhov ext4_add_complete_io(io_end); 28884c0425ffSMingming Cao } 2889c7064ef1SJiaying Zhang 2890744692dcSJiaying Zhang static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate) 2891744692dcSJiaying Zhang { 2892744692dcSJiaying Zhang ext4_io_end_t *io_end = bh->b_private; 2893744692dcSJiaying Zhang struct inode *inode; 2894744692dcSJiaying Zhang 2895744692dcSJiaying Zhang if (!test_clear_buffer_uninit(bh) || !io_end) 2896744692dcSJiaying Zhang goto out; 2897744692dcSJiaying Zhang 2898744692dcSJiaying Zhang if (!(io_end->inode->i_sb->s_flags & MS_ACTIVE)) { 289992b97816STheodore Ts'o ext4_msg(io_end->inode->i_sb, KERN_INFO, 290092b97816STheodore Ts'o "sb umounted, discard end_io request for inode %lu", 2901744692dcSJiaying Zhang io_end->inode->i_ino); 2902744692dcSJiaying Zhang ext4_free_io_end(io_end); 2903744692dcSJiaying Zhang goto out; 2904744692dcSJiaying Zhang } 2905744692dcSJiaying Zhang 290632c80b32STao Ma /* 290732c80b32STao Ma * It may be over-defensive here to check EXT4_IO_END_UNWRITTEN now, 290832c80b32STao Ma * but being more careful is always safe for the future change. 290932c80b32STao Ma */ 2910744692dcSJiaying Zhang inode = io_end->inode; 29110edeb71dSTao Ma ext4_set_io_unwritten_flag(inode, io_end); 291228a535f9SDmitry Monakhov ext4_add_complete_io(io_end); 2913744692dcSJiaying Zhang out: 2914744692dcSJiaying Zhang bh->b_private = NULL; 2915744692dcSJiaying Zhang bh->b_end_io = NULL; 2916744692dcSJiaying Zhang clear_buffer_uninit(bh); 2917744692dcSJiaying Zhang end_buffer_async_write(bh, uptodate); 2918744692dcSJiaying Zhang } 2919744692dcSJiaying Zhang 2920744692dcSJiaying Zhang static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode) 2921744692dcSJiaying Zhang { 2922744692dcSJiaying Zhang ext4_io_end_t *io_end; 2923744692dcSJiaying Zhang struct page *page = bh->b_page; 2924744692dcSJiaying Zhang loff_t offset = (sector_t)page->index << PAGE_CACHE_SHIFT; 2925744692dcSJiaying Zhang size_t size = bh->b_size; 2926744692dcSJiaying Zhang 2927744692dcSJiaying Zhang retry: 2928744692dcSJiaying Zhang io_end = ext4_init_io_end(inode, GFP_ATOMIC); 2929744692dcSJiaying Zhang if (!io_end) { 29306db26ffcSAndrew Morton pr_warn_ratelimited("%s: allocation fail\n", __func__); 2931744692dcSJiaying Zhang schedule(); 2932744692dcSJiaying Zhang goto retry; 2933744692dcSJiaying Zhang } 2934744692dcSJiaying Zhang io_end->offset = offset; 2935744692dcSJiaying Zhang io_end->size = size; 2936744692dcSJiaying Zhang /* 2937744692dcSJiaying Zhang * We need to hold a reference to the page to make sure it 2938744692dcSJiaying Zhang * doesn't get evicted before ext4_end_io_work() has a chance 2939744692dcSJiaying Zhang * to convert the extent from written to unwritten. 2940744692dcSJiaying Zhang */ 2941744692dcSJiaying Zhang io_end->page = page; 2942744692dcSJiaying Zhang get_page(io_end->page); 2943744692dcSJiaying Zhang 2944744692dcSJiaying Zhang bh->b_private = io_end; 2945744692dcSJiaying Zhang bh->b_end_io = ext4_end_io_buffer_write; 2946744692dcSJiaying Zhang return 0; 2947744692dcSJiaying Zhang } 2948744692dcSJiaying Zhang 29494c0425ffSMingming Cao /* 29504c0425ffSMingming Cao * For ext4 extent files, ext4 will do direct-io write to holes, 29514c0425ffSMingming Cao * preallocated extents, and those write extend the file, no need to 29524c0425ffSMingming Cao * fall back to buffered IO. 29534c0425ffSMingming Cao * 2954b595076aSUwe Kleine-König * For holes, we fallocate those blocks, mark them as uninitialized 295569c499d1STheodore Ts'o * If those blocks were preallocated, we mark sure they are split, but 2956b595076aSUwe Kleine-König * still keep the range to write as uninitialized. 29574c0425ffSMingming Cao * 295869c499d1STheodore Ts'o * The unwritten extents will be converted to written when DIO is completed. 29598d5d02e6SMingming Cao * For async direct IO, since the IO may still pending when return, we 296025985edcSLucas De Marchi * set up an end_io call back function, which will do the conversion 29618d5d02e6SMingming Cao * when async direct IO completed. 29624c0425ffSMingming Cao * 29634c0425ffSMingming Cao * If the O_DIRECT write will extend the file then add this inode to the 29644c0425ffSMingming Cao * orphan list. So recovery will truncate it back to the original size 29654c0425ffSMingming Cao * if the machine crashes during the write. 29664c0425ffSMingming Cao * 29674c0425ffSMingming Cao */ 29684c0425ffSMingming Cao static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb, 29694c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 29704c0425ffSMingming Cao unsigned long nr_segs) 29714c0425ffSMingming Cao { 29724c0425ffSMingming Cao struct file *file = iocb->ki_filp; 29734c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 29744c0425ffSMingming Cao ssize_t ret; 29754c0425ffSMingming Cao size_t count = iov_length(iov, nr_segs); 2976729f52c6SZheng Liu int overwrite = 0; 29778b0f165fSAnatol Pomozov get_block_t *get_block_func = NULL; 29788b0f165fSAnatol Pomozov int dio_flags = 0; 297969c499d1STheodore Ts'o loff_t final_size = offset + count; 298069c499d1STheodore Ts'o 298169c499d1STheodore Ts'o /* Use the old path for reads and writes beyond i_size. */ 298269c499d1STheodore Ts'o if (rw != WRITE || final_size > inode->i_size) 298369c499d1STheodore Ts'o return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 2984729f52c6SZheng Liu 29854bd809dbSZheng Liu BUG_ON(iocb->private == NULL); 29864bd809dbSZheng Liu 29874bd809dbSZheng Liu /* If we do a overwrite dio, i_mutex locking can be released */ 29884bd809dbSZheng Liu overwrite = *((int *)iocb->private); 29894bd809dbSZheng Liu 29904bd809dbSZheng Liu if (overwrite) { 29911f555cfaSDmitry Monakhov atomic_inc(&inode->i_dio_count); 29924bd809dbSZheng Liu down_read(&EXT4_I(inode)->i_data_sem); 29934bd809dbSZheng Liu mutex_unlock(&inode->i_mutex); 29944bd809dbSZheng Liu } 29954bd809dbSZheng Liu 29964c0425ffSMingming Cao /* 29978d5d02e6SMingming Cao * We could direct write to holes and fallocate. 29988d5d02e6SMingming Cao * 299969c499d1STheodore Ts'o * Allocated blocks to fill the hole are marked as 300069c499d1STheodore Ts'o * uninitialized to prevent parallel buffered read to expose 300169c499d1STheodore Ts'o * the stale data before DIO complete the data IO. 30028d5d02e6SMingming Cao * 300369c499d1STheodore Ts'o * As to previously fallocated extents, ext4 get_block will 300469c499d1STheodore Ts'o * just simply mark the buffer mapped but still keep the 300569c499d1STheodore Ts'o * extents uninitialized. 30064c0425ffSMingming Cao * 300769c499d1STheodore Ts'o * For non AIO case, we will convert those unwritten extents 30088d5d02e6SMingming Cao * to written after return back from blockdev_direct_IO. 30094c0425ffSMingming Cao * 301069c499d1STheodore Ts'o * For async DIO, the conversion needs to be deferred when the 301169c499d1STheodore Ts'o * IO is completed. The ext4 end_io callback function will be 301269c499d1STheodore Ts'o * called to take care of the conversion work. Here for async 301369c499d1STheodore Ts'o * case, we allocate an io_end structure to hook to the iocb. 30144c0425ffSMingming Cao */ 30158d5d02e6SMingming Cao iocb->private = NULL; 3016f45ee3a1SDmitry Monakhov ext4_inode_aio_set(inode, NULL); 30178d5d02e6SMingming Cao if (!is_sync_kiocb(iocb)) { 301869c499d1STheodore Ts'o ext4_io_end_t *io_end = ext4_init_io_end(inode, GFP_NOFS); 30194bd809dbSZheng Liu if (!io_end) { 30204bd809dbSZheng Liu ret = -ENOMEM; 30214bd809dbSZheng Liu goto retake_lock; 30224bd809dbSZheng Liu } 3023266991b1SJeff Moyer io_end->flag |= EXT4_IO_END_DIRECT; 3024266991b1SJeff Moyer iocb->private = io_end; 30258d5d02e6SMingming Cao /* 302669c499d1STheodore Ts'o * we save the io structure for current async direct 302769c499d1STheodore Ts'o * IO, so that later ext4_map_blocks() could flag the 302869c499d1STheodore Ts'o * io structure whether there is a unwritten extents 302969c499d1STheodore Ts'o * needs to be converted when IO is completed. 30308d5d02e6SMingming Cao */ 3031f45ee3a1SDmitry Monakhov ext4_inode_aio_set(inode, io_end); 30328d5d02e6SMingming Cao } 30338d5d02e6SMingming Cao 30348b0f165fSAnatol Pomozov if (overwrite) { 30358b0f165fSAnatol Pomozov get_block_func = ext4_get_block_write_nolock; 30368b0f165fSAnatol Pomozov } else { 30378b0f165fSAnatol Pomozov get_block_func = ext4_get_block_write; 30388b0f165fSAnatol Pomozov dio_flags = DIO_LOCKING; 30398b0f165fSAnatol Pomozov } 3040729f52c6SZheng Liu ret = __blockdev_direct_IO(rw, iocb, inode, 3041729f52c6SZheng Liu inode->i_sb->s_bdev, iov, 3042729f52c6SZheng Liu offset, nr_segs, 30438b0f165fSAnatol Pomozov get_block_func, 3044729f52c6SZheng Liu ext4_end_io_dio, 3045729f52c6SZheng Liu NULL, 30468b0f165fSAnatol Pomozov dio_flags); 30478b0f165fSAnatol Pomozov 30488d5d02e6SMingming Cao if (iocb->private) 3049f45ee3a1SDmitry Monakhov ext4_inode_aio_set(inode, NULL); 30508d5d02e6SMingming Cao /* 305169c499d1STheodore Ts'o * The io_end structure takes a reference to the inode, that 305269c499d1STheodore Ts'o * structure needs to be destroyed and the reference to the 305369c499d1STheodore Ts'o * inode need to be dropped, when IO is complete, even with 0 305469c499d1STheodore Ts'o * byte write, or failed. 30558d5d02e6SMingming Cao * 305669c499d1STheodore Ts'o * In the successful AIO DIO case, the io_end structure will 305769c499d1STheodore Ts'o * be destroyed and the reference to the inode will be dropped 30588d5d02e6SMingming Cao * after the end_io call back function is called. 30598d5d02e6SMingming Cao * 306069c499d1STheodore Ts'o * In the case there is 0 byte write, or error case, since VFS 306169c499d1STheodore Ts'o * direct IO won't invoke the end_io call back function, we 306269c499d1STheodore Ts'o * need to free the end_io structure here. 30638d5d02e6SMingming Cao */ 30648d5d02e6SMingming Cao if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) { 30658d5d02e6SMingming Cao ext4_free_io_end(iocb->private); 30668d5d02e6SMingming Cao iocb->private = NULL; 3067729f52c6SZheng Liu } else if (ret > 0 && !overwrite && ext4_test_inode_state(inode, 30685f524950SMingming EXT4_STATE_DIO_UNWRITTEN)) { 3069109f5565SMingming int err; 30708d5d02e6SMingming Cao /* 30718d5d02e6SMingming Cao * for non AIO case, since the IO is already 307225985edcSLucas De Marchi * completed, we could do the conversion right here 30738d5d02e6SMingming Cao */ 3074109f5565SMingming err = ext4_convert_unwritten_extents(inode, 30758d5d02e6SMingming Cao offset, ret); 3076109f5565SMingming if (err < 0) 3077109f5565SMingming ret = err; 307819f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN); 3079109f5565SMingming } 30804bd809dbSZheng Liu 30814bd809dbSZheng Liu retake_lock: 30824bd809dbSZheng Liu /* take i_mutex locking again if we do a ovewrite dio */ 30834bd809dbSZheng Liu if (overwrite) { 30841f555cfaSDmitry Monakhov inode_dio_done(inode); 30854bd809dbSZheng Liu up_read(&EXT4_I(inode)->i_data_sem); 30864bd809dbSZheng Liu mutex_lock(&inode->i_mutex); 30874bd809dbSZheng Liu } 30884bd809dbSZheng Liu 30894c0425ffSMingming Cao return ret; 30904c0425ffSMingming Cao } 30918d5d02e6SMingming Cao 30924c0425ffSMingming Cao static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb, 30934c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 30944c0425ffSMingming Cao unsigned long nr_segs) 30954c0425ffSMingming Cao { 30964c0425ffSMingming Cao struct file *file = iocb->ki_filp; 30974c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 30980562e0baSJiaying Zhang ssize_t ret; 30994c0425ffSMingming Cao 310084ebd795STheodore Ts'o /* 310184ebd795STheodore Ts'o * If we are doing data journalling we don't support O_DIRECT 310284ebd795STheodore Ts'o */ 310384ebd795STheodore Ts'o if (ext4_should_journal_data(inode)) 310484ebd795STheodore Ts'o return 0; 310584ebd795STheodore Ts'o 3106*46c7f254STao Ma /* Let buffer I/O handle the inline data case. */ 3107*46c7f254STao Ma if (ext4_has_inline_data(inode)) 3108*46c7f254STao Ma return 0; 3109*46c7f254STao Ma 31100562e0baSJiaying Zhang trace_ext4_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw); 311112e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 31120562e0baSJiaying Zhang ret = ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs); 31130562e0baSJiaying Zhang else 31140562e0baSJiaying Zhang ret = ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 31150562e0baSJiaying Zhang trace_ext4_direct_IO_exit(inode, offset, 31160562e0baSJiaying Zhang iov_length(iov, nr_segs), rw, ret); 31170562e0baSJiaying Zhang return ret; 31184c0425ffSMingming Cao } 31194c0425ffSMingming Cao 3120ac27a0ecSDave Kleikamp /* 3121617ba13bSMingming Cao * Pages can be marked dirty completely asynchronously from ext4's journalling 3122ac27a0ecSDave Kleikamp * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do 3123ac27a0ecSDave Kleikamp * much here because ->set_page_dirty is called under VFS locks. The page is 3124ac27a0ecSDave Kleikamp * not necessarily locked. 3125ac27a0ecSDave Kleikamp * 3126ac27a0ecSDave Kleikamp * We cannot just dirty the page and leave attached buffers clean, because the 3127ac27a0ecSDave Kleikamp * buffers' dirty state is "definitive". We cannot just set the buffers dirty 3128ac27a0ecSDave Kleikamp * or jbddirty because all the journalling code will explode. 3129ac27a0ecSDave Kleikamp * 3130ac27a0ecSDave Kleikamp * So what we do is to mark the page "pending dirty" and next time writepage 3131ac27a0ecSDave Kleikamp * is called, propagate that into the buffers appropriately. 3132ac27a0ecSDave Kleikamp */ 3133617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page) 3134ac27a0ecSDave Kleikamp { 3135ac27a0ecSDave Kleikamp SetPageChecked(page); 3136ac27a0ecSDave Kleikamp return __set_page_dirty_nobuffers(page); 3137ac27a0ecSDave Kleikamp } 3138ac27a0ecSDave Kleikamp 3139617ba13bSMingming Cao static const struct address_space_operations ext4_ordered_aops = { 3140617ba13bSMingming Cao .readpage = ext4_readpage, 3141617ba13bSMingming Cao .readpages = ext4_readpages, 314243ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3143bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3144bfc1af65SNick Piggin .write_end = ext4_ordered_write_end, 3145617ba13bSMingming Cao .bmap = ext4_bmap, 3146617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3147617ba13bSMingming Cao .releasepage = ext4_releasepage, 3148617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 3149ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 31508ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3151aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3152ac27a0ecSDave Kleikamp }; 3153ac27a0ecSDave Kleikamp 3154617ba13bSMingming Cao static const struct address_space_operations ext4_writeback_aops = { 3155617ba13bSMingming Cao .readpage = ext4_readpage, 3156617ba13bSMingming Cao .readpages = ext4_readpages, 315743ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3158bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3159bfc1af65SNick Piggin .write_end = ext4_writeback_write_end, 3160617ba13bSMingming Cao .bmap = ext4_bmap, 3161617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3162617ba13bSMingming Cao .releasepage = ext4_releasepage, 3163617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 3164ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 31658ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3166aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3167ac27a0ecSDave Kleikamp }; 3168ac27a0ecSDave Kleikamp 3169617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 3170617ba13bSMingming Cao .readpage = ext4_readpage, 3171617ba13bSMingming Cao .readpages = ext4_readpages, 317243ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3173bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3174bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 3175617ba13bSMingming Cao .set_page_dirty = ext4_journalled_set_page_dirty, 3176617ba13bSMingming Cao .bmap = ext4_bmap, 3177617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3178617ba13bSMingming Cao .releasepage = ext4_releasepage, 317984ebd795STheodore Ts'o .direct_IO = ext4_direct_IO, 31808ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3181aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3182ac27a0ecSDave Kleikamp }; 3183ac27a0ecSDave Kleikamp 318464769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 318564769240SAlex Tomas .readpage = ext4_readpage, 318664769240SAlex Tomas .readpages = ext4_readpages, 318743ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 318864769240SAlex Tomas .writepages = ext4_da_writepages, 318964769240SAlex Tomas .write_begin = ext4_da_write_begin, 319064769240SAlex Tomas .write_end = ext4_da_write_end, 319164769240SAlex Tomas .bmap = ext4_bmap, 319264769240SAlex Tomas .invalidatepage = ext4_da_invalidatepage, 319364769240SAlex Tomas .releasepage = ext4_releasepage, 319464769240SAlex Tomas .direct_IO = ext4_direct_IO, 319564769240SAlex Tomas .migratepage = buffer_migrate_page, 31968ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3197aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 319864769240SAlex Tomas }; 319964769240SAlex Tomas 3200617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 3201ac27a0ecSDave Kleikamp { 32023d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 32033d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 32043d2b1582SLukas Czerner if (test_opt(inode->i_sb, DELALLOC)) 3205cd1aac32SAneesh Kumar K.V inode->i_mapping->a_ops = &ext4_da_aops; 3206ac27a0ecSDave Kleikamp else 32073d2b1582SLukas Czerner inode->i_mapping->a_ops = &ext4_ordered_aops; 32083d2b1582SLukas Czerner break; 32093d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 32103d2b1582SLukas Czerner if (test_opt(inode->i_sb, DELALLOC)) 32113d2b1582SLukas Czerner inode->i_mapping->a_ops = &ext4_da_aops; 32123d2b1582SLukas Czerner else 32133d2b1582SLukas Czerner inode->i_mapping->a_ops = &ext4_writeback_aops; 32143d2b1582SLukas Czerner break; 32153d2b1582SLukas Czerner case EXT4_INODE_JOURNAL_DATA_MODE: 3216617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 32173d2b1582SLukas Czerner break; 32183d2b1582SLukas Czerner default: 32193d2b1582SLukas Czerner BUG(); 32203d2b1582SLukas Czerner } 3221ac27a0ecSDave Kleikamp } 3222ac27a0ecSDave Kleikamp 32234e96b2dbSAllison Henderson 32244e96b2dbSAllison Henderson /* 32254e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers() 32264e96b2dbSAllison Henderson * Wrapper function for ext4_discard_partial_page_buffers_no_lock. 32274e96b2dbSAllison Henderson * This function finds and locks the page containing the offset 32284e96b2dbSAllison Henderson * "from" and passes it to ext4_discard_partial_page_buffers_no_lock. 32294e96b2dbSAllison Henderson * Calling functions that already have the page locked should call 32304e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers_no_lock directly. 32314e96b2dbSAllison Henderson */ 32324e96b2dbSAllison Henderson int ext4_discard_partial_page_buffers(handle_t *handle, 32334e96b2dbSAllison Henderson struct address_space *mapping, loff_t from, 32344e96b2dbSAllison Henderson loff_t length, int flags) 32354e96b2dbSAllison Henderson { 32364e96b2dbSAllison Henderson struct inode *inode = mapping->host; 32374e96b2dbSAllison Henderson struct page *page; 32384e96b2dbSAllison Henderson int err = 0; 32394e96b2dbSAllison Henderson 32404e96b2dbSAllison Henderson page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT, 32414e96b2dbSAllison Henderson mapping_gfp_mask(mapping) & ~__GFP_FS); 32424e96b2dbSAllison Henderson if (!page) 32435129d05fSYongqiang Yang return -ENOMEM; 32444e96b2dbSAllison Henderson 32454e96b2dbSAllison Henderson err = ext4_discard_partial_page_buffers_no_lock(handle, inode, page, 32464e96b2dbSAllison Henderson from, length, flags); 32474e96b2dbSAllison Henderson 32484e96b2dbSAllison Henderson unlock_page(page); 32494e96b2dbSAllison Henderson page_cache_release(page); 32504e96b2dbSAllison Henderson return err; 32514e96b2dbSAllison Henderson } 32524e96b2dbSAllison Henderson 32534e96b2dbSAllison Henderson /* 32544e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers_no_lock() 32554e96b2dbSAllison Henderson * Zeros a page range of length 'length' starting from offset 'from'. 32564e96b2dbSAllison Henderson * Buffer heads that correspond to the block aligned regions of the 32574e96b2dbSAllison Henderson * zeroed range will be unmapped. Unblock aligned regions 32584e96b2dbSAllison Henderson * will have the corresponding buffer head mapped if needed so that 32594e96b2dbSAllison Henderson * that region of the page can be updated with the partial zero out. 32604e96b2dbSAllison Henderson * 32614e96b2dbSAllison Henderson * This function assumes that the page has already been locked. The 32624e96b2dbSAllison Henderson * The range to be discarded must be contained with in the given page. 32634e96b2dbSAllison Henderson * If the specified range exceeds the end of the page it will be shortened 32644e96b2dbSAllison Henderson * to the end of the page that corresponds to 'from'. This function is 32654e96b2dbSAllison Henderson * appropriate for updating a page and it buffer heads to be unmapped and 32664e96b2dbSAllison Henderson * zeroed for blocks that have been either released, or are going to be 32674e96b2dbSAllison Henderson * released. 32684e96b2dbSAllison Henderson * 32694e96b2dbSAllison Henderson * handle: The journal handle 32704e96b2dbSAllison Henderson * inode: The files inode 32714e96b2dbSAllison Henderson * page: A locked page that contains the offset "from" 32724907cb7bSAnatol Pomozov * from: The starting byte offset (from the beginning of the file) 32734e96b2dbSAllison Henderson * to begin discarding 32744e96b2dbSAllison Henderson * len: The length of bytes to discard 32754e96b2dbSAllison Henderson * flags: Optional flags that may be used: 32764e96b2dbSAllison Henderson * 32774e96b2dbSAllison Henderson * EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED 32784e96b2dbSAllison Henderson * Only zero the regions of the page whose buffer heads 32794e96b2dbSAllison Henderson * have already been unmapped. This flag is appropriate 32804907cb7bSAnatol Pomozov * for updating the contents of a page whose blocks may 32814e96b2dbSAllison Henderson * have already been released, and we only want to zero 32824e96b2dbSAllison Henderson * out the regions that correspond to those released blocks. 32834e96b2dbSAllison Henderson * 32844907cb7bSAnatol Pomozov * Returns zero on success or negative on failure. 32854e96b2dbSAllison Henderson */ 32865f163cc7SEric Sandeen static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle, 32874e96b2dbSAllison Henderson struct inode *inode, struct page *page, loff_t from, 32884e96b2dbSAllison Henderson loff_t length, int flags) 32894e96b2dbSAllison Henderson { 32904e96b2dbSAllison Henderson ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; 32914e96b2dbSAllison Henderson unsigned int offset = from & (PAGE_CACHE_SIZE-1); 32924e96b2dbSAllison Henderson unsigned int blocksize, max, pos; 32934e96b2dbSAllison Henderson ext4_lblk_t iblock; 32944e96b2dbSAllison Henderson struct buffer_head *bh; 32954e96b2dbSAllison Henderson int err = 0; 32964e96b2dbSAllison Henderson 32974e96b2dbSAllison Henderson blocksize = inode->i_sb->s_blocksize; 32984e96b2dbSAllison Henderson max = PAGE_CACHE_SIZE - offset; 32994e96b2dbSAllison Henderson 33004e96b2dbSAllison Henderson if (index != page->index) 33014e96b2dbSAllison Henderson return -EINVAL; 33024e96b2dbSAllison Henderson 33034e96b2dbSAllison Henderson /* 33044e96b2dbSAllison Henderson * correct length if it does not fall between 33054e96b2dbSAllison Henderson * 'from' and the end of the page 33064e96b2dbSAllison Henderson */ 33074e96b2dbSAllison Henderson if (length > max || length < 0) 33084e96b2dbSAllison Henderson length = max; 33094e96b2dbSAllison Henderson 33104e96b2dbSAllison Henderson iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); 33114e96b2dbSAllison Henderson 3312093e6e36SYongqiang Yang if (!page_has_buffers(page)) 33134e96b2dbSAllison Henderson create_empty_buffers(page, blocksize, 0); 33144e96b2dbSAllison Henderson 33154e96b2dbSAllison Henderson /* Find the buffer that contains "offset" */ 33164e96b2dbSAllison Henderson bh = page_buffers(page); 33174e96b2dbSAllison Henderson pos = blocksize; 33184e96b2dbSAllison Henderson while (offset >= pos) { 33194e96b2dbSAllison Henderson bh = bh->b_this_page; 33204e96b2dbSAllison Henderson iblock++; 33214e96b2dbSAllison Henderson pos += blocksize; 33224e96b2dbSAllison Henderson } 33234e96b2dbSAllison Henderson 33244e96b2dbSAllison Henderson pos = offset; 33254e96b2dbSAllison Henderson while (pos < offset + length) { 3326e260daf2SYongqiang Yang unsigned int end_of_block, range_to_discard; 3327e260daf2SYongqiang Yang 33284e96b2dbSAllison Henderson err = 0; 33294e96b2dbSAllison Henderson 33304e96b2dbSAllison Henderson /* The length of space left to zero and unmap */ 33314e96b2dbSAllison Henderson range_to_discard = offset + length - pos; 33324e96b2dbSAllison Henderson 33334e96b2dbSAllison Henderson /* The length of space until the end of the block */ 33344e96b2dbSAllison Henderson end_of_block = blocksize - (pos & (blocksize-1)); 33354e96b2dbSAllison Henderson 33364e96b2dbSAllison Henderson /* 33374e96b2dbSAllison Henderson * Do not unmap or zero past end of block 33384e96b2dbSAllison Henderson * for this buffer head 33394e96b2dbSAllison Henderson */ 33404e96b2dbSAllison Henderson if (range_to_discard > end_of_block) 33414e96b2dbSAllison Henderson range_to_discard = end_of_block; 33424e96b2dbSAllison Henderson 33434e96b2dbSAllison Henderson 33444e96b2dbSAllison Henderson /* 33454e96b2dbSAllison Henderson * Skip this buffer head if we are only zeroing unampped 33464e96b2dbSAllison Henderson * regions of the page 33474e96b2dbSAllison Henderson */ 33484e96b2dbSAllison Henderson if (flags & EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED && 33494e96b2dbSAllison Henderson buffer_mapped(bh)) 33504e96b2dbSAllison Henderson goto next; 33514e96b2dbSAllison Henderson 33524e96b2dbSAllison Henderson /* If the range is block aligned, unmap */ 33534e96b2dbSAllison Henderson if (range_to_discard == blocksize) { 33544e96b2dbSAllison Henderson clear_buffer_dirty(bh); 33554e96b2dbSAllison Henderson bh->b_bdev = NULL; 33564e96b2dbSAllison Henderson clear_buffer_mapped(bh); 33574e96b2dbSAllison Henderson clear_buffer_req(bh); 33584e96b2dbSAllison Henderson clear_buffer_new(bh); 33594e96b2dbSAllison Henderson clear_buffer_delay(bh); 33604e96b2dbSAllison Henderson clear_buffer_unwritten(bh); 33614e96b2dbSAllison Henderson clear_buffer_uptodate(bh); 33624e96b2dbSAllison Henderson zero_user(page, pos, range_to_discard); 33634e96b2dbSAllison Henderson BUFFER_TRACE(bh, "Buffer discarded"); 33644e96b2dbSAllison Henderson goto next; 33654e96b2dbSAllison Henderson } 33664e96b2dbSAllison Henderson 33674e96b2dbSAllison Henderson /* 33684e96b2dbSAllison Henderson * If this block is not completely contained in the range 33694e96b2dbSAllison Henderson * to be discarded, then it is not going to be released. Because 33704e96b2dbSAllison Henderson * we need to keep this block, we need to make sure this part 33714e96b2dbSAllison Henderson * of the page is uptodate before we modify it by writeing 33724e96b2dbSAllison Henderson * partial zeros on it. 33734e96b2dbSAllison Henderson */ 33744e96b2dbSAllison Henderson if (!buffer_mapped(bh)) { 33754e96b2dbSAllison Henderson /* 33764e96b2dbSAllison Henderson * Buffer head must be mapped before we can read 33774e96b2dbSAllison Henderson * from the block 33784e96b2dbSAllison Henderson */ 33794e96b2dbSAllison Henderson BUFFER_TRACE(bh, "unmapped"); 33804e96b2dbSAllison Henderson ext4_get_block(inode, iblock, bh, 0); 33814e96b2dbSAllison Henderson /* unmapped? It's a hole - nothing to do */ 33824e96b2dbSAllison Henderson if (!buffer_mapped(bh)) { 33834e96b2dbSAllison Henderson BUFFER_TRACE(bh, "still unmapped"); 33844e96b2dbSAllison Henderson goto next; 33854e96b2dbSAllison Henderson } 33864e96b2dbSAllison Henderson } 33874e96b2dbSAllison Henderson 33884e96b2dbSAllison Henderson /* Ok, it's mapped. Make sure it's up-to-date */ 33894e96b2dbSAllison Henderson if (PageUptodate(page)) 33904e96b2dbSAllison Henderson set_buffer_uptodate(bh); 33914e96b2dbSAllison Henderson 33924e96b2dbSAllison Henderson if (!buffer_uptodate(bh)) { 33934e96b2dbSAllison Henderson err = -EIO; 33944e96b2dbSAllison Henderson ll_rw_block(READ, 1, &bh); 33954e96b2dbSAllison Henderson wait_on_buffer(bh); 33964e96b2dbSAllison Henderson /* Uhhuh. Read error. Complain and punt.*/ 33974e96b2dbSAllison Henderson if (!buffer_uptodate(bh)) 33984e96b2dbSAllison Henderson goto next; 33994e96b2dbSAllison Henderson } 34004e96b2dbSAllison Henderson 34014e96b2dbSAllison Henderson if (ext4_should_journal_data(inode)) { 34024e96b2dbSAllison Henderson BUFFER_TRACE(bh, "get write access"); 34034e96b2dbSAllison Henderson err = ext4_journal_get_write_access(handle, bh); 34044e96b2dbSAllison Henderson if (err) 34054e96b2dbSAllison Henderson goto next; 34064e96b2dbSAllison Henderson } 34074e96b2dbSAllison Henderson 34084e96b2dbSAllison Henderson zero_user(page, pos, range_to_discard); 34094e96b2dbSAllison Henderson 34104e96b2dbSAllison Henderson err = 0; 34114e96b2dbSAllison Henderson if (ext4_should_journal_data(inode)) { 34124e96b2dbSAllison Henderson err = ext4_handle_dirty_metadata(handle, inode, bh); 3413decbd919STheodore Ts'o } else 34144e96b2dbSAllison Henderson mark_buffer_dirty(bh); 34154e96b2dbSAllison Henderson 34164e96b2dbSAllison Henderson BUFFER_TRACE(bh, "Partial buffer zeroed"); 34174e96b2dbSAllison Henderson next: 34184e96b2dbSAllison Henderson bh = bh->b_this_page; 34194e96b2dbSAllison Henderson iblock++; 34204e96b2dbSAllison Henderson pos += range_to_discard; 34214e96b2dbSAllison Henderson } 34224e96b2dbSAllison Henderson 34234e96b2dbSAllison Henderson return err; 34244e96b2dbSAllison Henderson } 34254e96b2dbSAllison Henderson 342691ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 342791ef4cafSDuane Griffin { 342891ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 342991ef4cafSDuane Griffin return 1; 343091ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 343191ef4cafSDuane Griffin return 1; 343291ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 343391ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 343491ef4cafSDuane Griffin return 0; 343591ef4cafSDuane Griffin } 343691ef4cafSDuane Griffin 3437ac27a0ecSDave Kleikamp /* 3438a4bb6b64SAllison Henderson * ext4_punch_hole: punches a hole in a file by releaseing the blocks 3439a4bb6b64SAllison Henderson * associated with the given offset and length 3440a4bb6b64SAllison Henderson * 3441a4bb6b64SAllison Henderson * @inode: File inode 3442a4bb6b64SAllison Henderson * @offset: The offset where the hole will begin 3443a4bb6b64SAllison Henderson * @len: The length of the hole 3444a4bb6b64SAllison Henderson * 34454907cb7bSAnatol Pomozov * Returns: 0 on success or negative on failure 3446a4bb6b64SAllison Henderson */ 3447a4bb6b64SAllison Henderson 3448a4bb6b64SAllison Henderson int ext4_punch_hole(struct file *file, loff_t offset, loff_t length) 3449a4bb6b64SAllison Henderson { 3450a4bb6b64SAllison Henderson struct inode *inode = file->f_path.dentry->d_inode; 3451a4bb6b64SAllison Henderson if (!S_ISREG(inode->i_mode)) 345273355192SAllison Henderson return -EOPNOTSUPP; 3453a4bb6b64SAllison Henderson 3454a4bb6b64SAllison Henderson if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 3455a4bb6b64SAllison Henderson /* TODO: Add support for non extent hole punching */ 345673355192SAllison Henderson return -EOPNOTSUPP; 3457a4bb6b64SAllison Henderson } 3458a4bb6b64SAllison Henderson 3459bab08ab9STheodore Ts'o if (EXT4_SB(inode->i_sb)->s_cluster_ratio > 1) { 3460bab08ab9STheodore Ts'o /* TODO: Add support for bigalloc file systems */ 346173355192SAllison Henderson return -EOPNOTSUPP; 3462bab08ab9STheodore Ts'o } 3463bab08ab9STheodore Ts'o 3464a4bb6b64SAllison Henderson return ext4_ext_punch_hole(file, offset, length); 3465a4bb6b64SAllison Henderson } 3466a4bb6b64SAllison Henderson 3467a4bb6b64SAllison Henderson /* 3468617ba13bSMingming Cao * ext4_truncate() 3469ac27a0ecSDave Kleikamp * 3470617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 3471617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 3472ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 3473ac27a0ecSDave Kleikamp * 347442b2aa86SJustin P. Mattock * As we work through the truncate and commit bits of it to the journal there 3475ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 3476ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 3477ac27a0ecSDave Kleikamp * 3478ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 3479ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 3480ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 3481ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 3482ac27a0ecSDave Kleikamp * left-to-right works OK too). 3483ac27a0ecSDave Kleikamp * 3484ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 3485ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 3486ac27a0ecSDave Kleikamp * 3487ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 3488617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 3489ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 3490617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 3491617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 3492ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 3493617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 3494ac27a0ecSDave Kleikamp */ 3495617ba13bSMingming Cao void ext4_truncate(struct inode *inode) 3496ac27a0ecSDave Kleikamp { 34970562e0baSJiaying Zhang trace_ext4_truncate_enter(inode); 34980562e0baSJiaying Zhang 349991ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 3500ac27a0ecSDave Kleikamp return; 3501ac27a0ecSDave Kleikamp 350212e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS); 3503c8d46e41SJiaying Zhang 35045534fb5bSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 350519f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE); 35067d8f9f7dSTheodore Ts'o 3507ff9893dcSAmir Goldstein if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3508cf108bcaSJan Kara ext4_ext_truncate(inode); 3509ff9893dcSAmir Goldstein else 3510ff9893dcSAmir Goldstein ext4_ind_truncate(inode); 3511a86c6181SAlex Tomas 35120562e0baSJiaying Zhang trace_ext4_truncate_exit(inode); 3513ac27a0ecSDave Kleikamp } 3514ac27a0ecSDave Kleikamp 3515ac27a0ecSDave Kleikamp /* 3516617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 3517ac27a0ecSDave Kleikamp * underlying buffer_head on success. If 'in_mem' is true, we have all 3518ac27a0ecSDave Kleikamp * data in memory that is needed to recreate the on-disk version of this 3519ac27a0ecSDave Kleikamp * inode. 3520ac27a0ecSDave Kleikamp */ 3521617ba13bSMingming Cao static int __ext4_get_inode_loc(struct inode *inode, 3522617ba13bSMingming Cao struct ext4_iloc *iloc, int in_mem) 3523ac27a0ecSDave Kleikamp { 3524240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 3525ac27a0ecSDave Kleikamp struct buffer_head *bh; 3526240799cdSTheodore Ts'o struct super_block *sb = inode->i_sb; 3527240799cdSTheodore Ts'o ext4_fsblk_t block; 3528240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 3529ac27a0ecSDave Kleikamp 35303a06d778SAneesh Kumar K.V iloc->bh = NULL; 3531240799cdSTheodore Ts'o if (!ext4_valid_inum(sb, inode->i_ino)) 3532ac27a0ecSDave Kleikamp return -EIO; 3533ac27a0ecSDave Kleikamp 3534240799cdSTheodore Ts'o iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb); 3535240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 3536240799cdSTheodore Ts'o if (!gdp) 3537240799cdSTheodore Ts'o return -EIO; 3538240799cdSTheodore Ts'o 3539240799cdSTheodore Ts'o /* 3540240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 3541240799cdSTheodore Ts'o */ 354200d09882STao Ma inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 3543240799cdSTheodore Ts'o inode_offset = ((inode->i_ino - 1) % 3544240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 3545240799cdSTheodore Ts'o block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); 3546240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 3547240799cdSTheodore Ts'o 3548240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 3549ac27a0ecSDave Kleikamp if (!bh) { 3550c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, block, 3551c398eda0STheodore Ts'o "unable to read itable block"); 3552ac27a0ecSDave Kleikamp return -EIO; 3553ac27a0ecSDave Kleikamp } 3554ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 3555ac27a0ecSDave Kleikamp lock_buffer(bh); 35569c83a923SHidehiro Kawai 35579c83a923SHidehiro Kawai /* 35589c83a923SHidehiro Kawai * If the buffer has the write error flag, we have failed 35599c83a923SHidehiro Kawai * to write out another inode in the same block. In this 35609c83a923SHidehiro Kawai * case, we don't have to read the block because we may 35619c83a923SHidehiro Kawai * read the old inode data successfully. 35629c83a923SHidehiro Kawai */ 35639c83a923SHidehiro Kawai if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) 35649c83a923SHidehiro Kawai set_buffer_uptodate(bh); 35659c83a923SHidehiro Kawai 3566ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) { 3567ac27a0ecSDave Kleikamp /* someone brought it uptodate while we waited */ 3568ac27a0ecSDave Kleikamp unlock_buffer(bh); 3569ac27a0ecSDave Kleikamp goto has_buffer; 3570ac27a0ecSDave Kleikamp } 3571ac27a0ecSDave Kleikamp 3572ac27a0ecSDave Kleikamp /* 3573ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 3574ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 3575ac27a0ecSDave Kleikamp * block. 3576ac27a0ecSDave Kleikamp */ 3577ac27a0ecSDave Kleikamp if (in_mem) { 3578ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 3579240799cdSTheodore Ts'o int i, start; 3580ac27a0ecSDave Kleikamp 3581240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 3582ac27a0ecSDave Kleikamp 3583ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 3584240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 3585ac27a0ecSDave Kleikamp if (!bitmap_bh) 3586ac27a0ecSDave Kleikamp goto make_io; 3587ac27a0ecSDave Kleikamp 3588ac27a0ecSDave Kleikamp /* 3589ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 3590ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 3591ac27a0ecSDave Kleikamp * of one, so skip it. 3592ac27a0ecSDave Kleikamp */ 3593ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 3594ac27a0ecSDave Kleikamp brelse(bitmap_bh); 3595ac27a0ecSDave Kleikamp goto make_io; 3596ac27a0ecSDave Kleikamp } 3597240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 3598ac27a0ecSDave Kleikamp if (i == inode_offset) 3599ac27a0ecSDave Kleikamp continue; 3600617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 3601ac27a0ecSDave Kleikamp break; 3602ac27a0ecSDave Kleikamp } 3603ac27a0ecSDave Kleikamp brelse(bitmap_bh); 3604240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 3605ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 3606ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 3607ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 3608ac27a0ecSDave Kleikamp unlock_buffer(bh); 3609ac27a0ecSDave Kleikamp goto has_buffer; 3610ac27a0ecSDave Kleikamp } 3611ac27a0ecSDave Kleikamp } 3612ac27a0ecSDave Kleikamp 3613ac27a0ecSDave Kleikamp make_io: 3614ac27a0ecSDave Kleikamp /* 3615240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 3616240799cdSTheodore Ts'o * blocks from the inode table. 3617240799cdSTheodore Ts'o */ 3618240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 3619240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 3620240799cdSTheodore Ts'o unsigned num; 3621240799cdSTheodore Ts'o 3622240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 3623b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 3624240799cdSTheodore Ts'o b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1); 3625240799cdSTheodore Ts'o if (table > b) 3626240799cdSTheodore Ts'o b = table; 3627240799cdSTheodore Ts'o end = b + EXT4_SB(sb)->s_inode_readahead_blks; 3628240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 3629feb0ab32SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) 3630560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 3631240799cdSTheodore Ts'o table += num / inodes_per_block; 3632240799cdSTheodore Ts'o if (end > table) 3633240799cdSTheodore Ts'o end = table; 3634240799cdSTheodore Ts'o while (b <= end) 3635240799cdSTheodore Ts'o sb_breadahead(sb, b++); 3636240799cdSTheodore Ts'o } 3637240799cdSTheodore Ts'o 3638240799cdSTheodore Ts'o /* 3639ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 3640ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 3641ac27a0ecSDave Kleikamp * Read the block from disk. 3642ac27a0ecSDave Kleikamp */ 36430562e0baSJiaying Zhang trace_ext4_load_inode(inode); 3644ac27a0ecSDave Kleikamp get_bh(bh); 3645ac27a0ecSDave Kleikamp bh->b_end_io = end_buffer_read_sync; 364665299a3bSChristoph Hellwig submit_bh(READ | REQ_META | REQ_PRIO, bh); 3647ac27a0ecSDave Kleikamp wait_on_buffer(bh); 3648ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 3649c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, block, 3650c398eda0STheodore Ts'o "unable to read itable block"); 3651ac27a0ecSDave Kleikamp brelse(bh); 3652ac27a0ecSDave Kleikamp return -EIO; 3653ac27a0ecSDave Kleikamp } 3654ac27a0ecSDave Kleikamp } 3655ac27a0ecSDave Kleikamp has_buffer: 3656ac27a0ecSDave Kleikamp iloc->bh = bh; 3657ac27a0ecSDave Kleikamp return 0; 3658ac27a0ecSDave Kleikamp } 3659ac27a0ecSDave Kleikamp 3660617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 3661ac27a0ecSDave Kleikamp { 3662ac27a0ecSDave Kleikamp /* We have all inode data except xattrs in memory here. */ 3663617ba13bSMingming Cao return __ext4_get_inode_loc(inode, iloc, 366419f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_XATTR)); 3665ac27a0ecSDave Kleikamp } 3666ac27a0ecSDave Kleikamp 3667617ba13bSMingming Cao void ext4_set_inode_flags(struct inode *inode) 3668ac27a0ecSDave Kleikamp { 3669617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 3670ac27a0ecSDave Kleikamp 3671ac27a0ecSDave Kleikamp inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); 3672617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 3673ac27a0ecSDave Kleikamp inode->i_flags |= S_SYNC; 3674617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 3675ac27a0ecSDave Kleikamp inode->i_flags |= S_APPEND; 3676617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 3677ac27a0ecSDave Kleikamp inode->i_flags |= S_IMMUTABLE; 3678617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 3679ac27a0ecSDave Kleikamp inode->i_flags |= S_NOATIME; 3680617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 3681ac27a0ecSDave Kleikamp inode->i_flags |= S_DIRSYNC; 3682ac27a0ecSDave Kleikamp } 3683ac27a0ecSDave Kleikamp 3684ff9ddf7eSJan Kara /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */ 3685ff9ddf7eSJan Kara void ext4_get_inode_flags(struct ext4_inode_info *ei) 3686ff9ddf7eSJan Kara { 368784a8dce2SDmitry Monakhov unsigned int vfs_fl; 368884a8dce2SDmitry Monakhov unsigned long old_fl, new_fl; 3689ff9ddf7eSJan Kara 369084a8dce2SDmitry Monakhov do { 369184a8dce2SDmitry Monakhov vfs_fl = ei->vfs_inode.i_flags; 369284a8dce2SDmitry Monakhov old_fl = ei->i_flags; 369384a8dce2SDmitry Monakhov new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL| 369484a8dce2SDmitry Monakhov EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL| 369584a8dce2SDmitry Monakhov EXT4_DIRSYNC_FL); 369684a8dce2SDmitry Monakhov if (vfs_fl & S_SYNC) 369784a8dce2SDmitry Monakhov new_fl |= EXT4_SYNC_FL; 369884a8dce2SDmitry Monakhov if (vfs_fl & S_APPEND) 369984a8dce2SDmitry Monakhov new_fl |= EXT4_APPEND_FL; 370084a8dce2SDmitry Monakhov if (vfs_fl & S_IMMUTABLE) 370184a8dce2SDmitry Monakhov new_fl |= EXT4_IMMUTABLE_FL; 370284a8dce2SDmitry Monakhov if (vfs_fl & S_NOATIME) 370384a8dce2SDmitry Monakhov new_fl |= EXT4_NOATIME_FL; 370484a8dce2SDmitry Monakhov if (vfs_fl & S_DIRSYNC) 370584a8dce2SDmitry Monakhov new_fl |= EXT4_DIRSYNC_FL; 370684a8dce2SDmitry Monakhov } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl); 3707ff9ddf7eSJan Kara } 3708de9a55b8STheodore Ts'o 37090fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 37100fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 37110fc1b451SAneesh Kumar K.V { 37120fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 37138180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 37148180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 37150fc1b451SAneesh Kumar K.V 37160fc1b451SAneesh Kumar K.V if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 37170fc1b451SAneesh Kumar K.V EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) { 37180fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 37190fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 37200fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 372107a03824STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) { 37228180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 37238180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 37248180a562SAneesh Kumar K.V } else { 37250fc1b451SAneesh Kumar K.V return i_blocks; 37268180a562SAneesh Kumar K.V } 37270fc1b451SAneesh Kumar K.V } else { 37280fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 37290fc1b451SAneesh Kumar K.V } 37300fc1b451SAneesh Kumar K.V } 3731ff9ddf7eSJan Kara 3732152a7b0aSTao Ma static inline void ext4_iget_extra_inode(struct inode *inode, 3733152a7b0aSTao Ma struct ext4_inode *raw_inode, 3734152a7b0aSTao Ma struct ext4_inode_info *ei) 3735152a7b0aSTao Ma { 3736152a7b0aSTao Ma __le32 *magic = (void *)raw_inode + 3737152a7b0aSTao Ma EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize; 373867cf5b09STao Ma if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) { 3739152a7b0aSTao Ma ext4_set_inode_state(inode, EXT4_STATE_XATTR); 374067cf5b09STao Ma ext4_find_inline_data_nolock(inode); 374167cf5b09STao Ma } 3742152a7b0aSTao Ma } 3743152a7b0aSTao Ma 37441d1fe1eeSDavid Howells struct inode *ext4_iget(struct super_block *sb, unsigned long ino) 3745ac27a0ecSDave Kleikamp { 3746617ba13bSMingming Cao struct ext4_iloc iloc; 3747617ba13bSMingming Cao struct ext4_inode *raw_inode; 37481d1fe1eeSDavid Howells struct ext4_inode_info *ei; 37491d1fe1eeSDavid Howells struct inode *inode; 3750b436b9beSJan Kara journal_t *journal = EXT4_SB(sb)->s_journal; 37511d1fe1eeSDavid Howells long ret; 3752ac27a0ecSDave Kleikamp int block; 375308cefc7aSEric W. Biederman uid_t i_uid; 375408cefc7aSEric W. Biederman gid_t i_gid; 3755ac27a0ecSDave Kleikamp 37561d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 37571d1fe1eeSDavid Howells if (!inode) 37581d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 37591d1fe1eeSDavid Howells if (!(inode->i_state & I_NEW)) 37601d1fe1eeSDavid Howells return inode; 37611d1fe1eeSDavid Howells 37621d1fe1eeSDavid Howells ei = EXT4_I(inode); 37637dc57615SPeter Huewe iloc.bh = NULL; 3764ac27a0ecSDave Kleikamp 37651d1fe1eeSDavid Howells ret = __ext4_get_inode_loc(inode, &iloc, 0); 37661d1fe1eeSDavid Howells if (ret < 0) 3767ac27a0ecSDave Kleikamp goto bad_inode; 3768617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 3769814525f4SDarrick J. Wong 3770814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 3771814525f4SDarrick J. Wong ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 3772814525f4SDarrick J. Wong if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 3773814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)) { 3774814525f4SDarrick J. Wong EXT4_ERROR_INODE(inode, "bad extra_isize (%u != %u)", 3775814525f4SDarrick J. Wong EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize, 3776814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)); 3777814525f4SDarrick J. Wong ret = -EIO; 3778814525f4SDarrick J. Wong goto bad_inode; 3779814525f4SDarrick J. Wong } 3780814525f4SDarrick J. Wong } else 3781814525f4SDarrick J. Wong ei->i_extra_isize = 0; 3782814525f4SDarrick J. Wong 3783814525f4SDarrick J. Wong /* Precompute checksum seed for inode metadata */ 3784814525f4SDarrick J. Wong if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 3785814525f4SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) { 3786814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3787814525f4SDarrick J. Wong __u32 csum; 3788814525f4SDarrick J. Wong __le32 inum = cpu_to_le32(inode->i_ino); 3789814525f4SDarrick J. Wong __le32 gen = raw_inode->i_generation; 3790814525f4SDarrick J. Wong csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, 3791814525f4SDarrick J. Wong sizeof(inum)); 3792814525f4SDarrick J. Wong ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, 3793814525f4SDarrick J. Wong sizeof(gen)); 3794814525f4SDarrick J. Wong } 3795814525f4SDarrick J. Wong 3796814525f4SDarrick J. Wong if (!ext4_inode_csum_verify(inode, raw_inode, ei)) { 3797814525f4SDarrick J. Wong EXT4_ERROR_INODE(inode, "checksum invalid"); 3798814525f4SDarrick J. Wong ret = -EIO; 3799814525f4SDarrick J. Wong goto bad_inode; 3800814525f4SDarrick J. Wong } 3801814525f4SDarrick J. Wong 3802ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 380308cefc7aSEric W. Biederman i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 380408cefc7aSEric W. Biederman i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 3805ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 380608cefc7aSEric W. Biederman i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 380708cefc7aSEric W. Biederman i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 3808ac27a0ecSDave Kleikamp } 380908cefc7aSEric W. Biederman i_uid_write(inode, i_uid); 381008cefc7aSEric W. Biederman i_gid_write(inode, i_gid); 3811bfe86848SMiklos Szeredi set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); 3812ac27a0ecSDave Kleikamp 3813353eb83cSTheodore Ts'o ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 381467cf5b09STao Ma ei->i_inline_off = 0; 3815ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 3816ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 3817ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 3818ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 3819ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 3820ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 3821ac27a0ecSDave Kleikamp */ 3822ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 3823ac27a0ecSDave Kleikamp if (inode->i_mode == 0 || 3824617ba13bSMingming Cao !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) { 3825ac27a0ecSDave Kleikamp /* this inode is deleted */ 38261d1fe1eeSDavid Howells ret = -ESTALE; 3827ac27a0ecSDave Kleikamp goto bad_inode; 3828ac27a0ecSDave Kleikamp } 3829ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 3830ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 3831ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 3832ac27a0ecSDave Kleikamp * the process of deleting those. */ 3833ac27a0ecSDave Kleikamp } 3834ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 38350fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 38367973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 3837a9e81742STheodore Ts'o if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) 3838a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 3839a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 3840a48380f7SAneesh Kumar K.V inode->i_size = ext4_isize(raw_inode); 3841ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 3842a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 3843a9e7f447SDmitry Monakhov ei->i_reserved_quota = 0; 3844a9e7f447SDmitry Monakhov #endif 3845ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 3846ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 3847a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 3848ac27a0ecSDave Kleikamp /* 3849ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 3850ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 3851ac27a0ecSDave Kleikamp */ 3852617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 3853ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 3854ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 3855ac27a0ecSDave Kleikamp 3856b436b9beSJan Kara /* 3857b436b9beSJan Kara * Set transaction id's of transactions that have to be committed 3858b436b9beSJan Kara * to finish f[data]sync. We set them to currently running transaction 3859b436b9beSJan Kara * as we cannot be sure that the inode or some of its metadata isn't 3860b436b9beSJan Kara * part of the transaction - the inode could have been reclaimed and 3861b436b9beSJan Kara * now it is reread from disk. 3862b436b9beSJan Kara */ 3863b436b9beSJan Kara if (journal) { 3864b436b9beSJan Kara transaction_t *transaction; 3865b436b9beSJan Kara tid_t tid; 3866b436b9beSJan Kara 3867a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 3868b436b9beSJan Kara if (journal->j_running_transaction) 3869b436b9beSJan Kara transaction = journal->j_running_transaction; 3870b436b9beSJan Kara else 3871b436b9beSJan Kara transaction = journal->j_committing_transaction; 3872b436b9beSJan Kara if (transaction) 3873b436b9beSJan Kara tid = transaction->t_tid; 3874b436b9beSJan Kara else 3875b436b9beSJan Kara tid = journal->j_commit_sequence; 3876a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 3877b436b9beSJan Kara ei->i_sync_tid = tid; 3878b436b9beSJan Kara ei->i_datasync_tid = tid; 3879b436b9beSJan Kara } 3880b436b9beSJan Kara 38810040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 3882ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 3883ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 3884617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 3885617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 3886ac27a0ecSDave Kleikamp } else { 3887152a7b0aSTao Ma ext4_iget_extra_inode(inode, raw_inode, ei); 3888ac27a0ecSDave Kleikamp } 3889814525f4SDarrick J. Wong } 3890ac27a0ecSDave Kleikamp 3891ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 3892ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 3893ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 3894ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 3895ef7f3835SKalpak Shah 389625ec56b5SJean Noel Cordenner inode->i_version = le32_to_cpu(raw_inode->i_disk_version); 389725ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 389825ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 389925ec56b5SJean Noel Cordenner inode->i_version |= 390025ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 390125ec56b5SJean Noel Cordenner } 390225ec56b5SJean Noel Cordenner 3903c4b5a614STheodore Ts'o ret = 0; 3904485c26ecSTheodore Ts'o if (ei->i_file_acl && 39051032988cSTheodore Ts'o !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) { 390624676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "bad extended attribute block %llu", 390724676da4STheodore Ts'o ei->i_file_acl); 3908485c26ecSTheodore Ts'o ret = -EIO; 3909485c26ecSTheodore Ts'o goto bad_inode; 391007a03824STheodore Ts'o } else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 3911c4b5a614STheodore Ts'o if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 3912c4b5a614STheodore Ts'o (S_ISLNK(inode->i_mode) && 3913c4b5a614STheodore Ts'o !ext4_inode_is_fast_symlink(inode))) 39147a262f7cSAneesh Kumar K.V /* Validate extent which is part of inode */ 39157a262f7cSAneesh Kumar K.V ret = ext4_ext_check_inode(inode); 3916fe2c8191SThiemo Nagel } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 3917fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 3918fe2c8191SThiemo Nagel !ext4_inode_is_fast_symlink(inode))) { 3919fe2c8191SThiemo Nagel /* Validate block references which are part of inode */ 39201f7d1e77STheodore Ts'o ret = ext4_ind_check_inode(inode); 3921fe2c8191SThiemo Nagel } 3922567f3e9aSTheodore Ts'o if (ret) 39237a262f7cSAneesh Kumar K.V goto bad_inode; 39247a262f7cSAneesh Kumar K.V 3925ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 3926617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 3927617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 3928617ba13bSMingming Cao ext4_set_aops(inode); 3929ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 3930617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 3931617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 3932ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 3933e83c1397SDuane Griffin if (ext4_inode_is_fast_symlink(inode)) { 3934617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 3935e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 3936e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 3937e83c1397SDuane Griffin } else { 3938617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 3939617ba13bSMingming Cao ext4_set_aops(inode); 3940ac27a0ecSDave Kleikamp } 3941563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 3942563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 3943617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 3944ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 3945ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 3946ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 3947ac27a0ecSDave Kleikamp else 3948ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 3949ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 3950563bdd61STheodore Ts'o } else { 3951563bdd61STheodore Ts'o ret = -EIO; 395224676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode); 3953563bdd61STheodore Ts'o goto bad_inode; 3954ac27a0ecSDave Kleikamp } 3955ac27a0ecSDave Kleikamp brelse(iloc.bh); 3956617ba13bSMingming Cao ext4_set_inode_flags(inode); 39571d1fe1eeSDavid Howells unlock_new_inode(inode); 39581d1fe1eeSDavid Howells return inode; 3959ac27a0ecSDave Kleikamp 3960ac27a0ecSDave Kleikamp bad_inode: 3961567f3e9aSTheodore Ts'o brelse(iloc.bh); 39621d1fe1eeSDavid Howells iget_failed(inode); 39631d1fe1eeSDavid Howells return ERR_PTR(ret); 3964ac27a0ecSDave Kleikamp } 3965ac27a0ecSDave Kleikamp 39660fc1b451SAneesh Kumar K.V static int ext4_inode_blocks_set(handle_t *handle, 39670fc1b451SAneesh Kumar K.V struct ext4_inode *raw_inode, 39680fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 39690fc1b451SAneesh Kumar K.V { 39700fc1b451SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 39710fc1b451SAneesh Kumar K.V u64 i_blocks = inode->i_blocks; 39720fc1b451SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 39730fc1b451SAneesh Kumar K.V 39740fc1b451SAneesh Kumar K.V if (i_blocks <= ~0U) { 39750fc1b451SAneesh Kumar K.V /* 39764907cb7bSAnatol Pomozov * i_blocks can be represented in a 32 bit variable 39770fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 39780fc1b451SAneesh Kumar K.V */ 39798180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 39800fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = 0; 398184a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 3982f287a1a5STheodore Ts'o return 0; 3983f287a1a5STheodore Ts'o } 3984f287a1a5STheodore Ts'o if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) 3985f287a1a5STheodore Ts'o return -EFBIG; 3986f287a1a5STheodore Ts'o 3987f287a1a5STheodore Ts'o if (i_blocks <= 0xffffffffffffULL) { 39880fc1b451SAneesh Kumar K.V /* 39890fc1b451SAneesh Kumar K.V * i_blocks can be represented in a 48 bit variable 39900fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 39910fc1b451SAneesh Kumar K.V */ 39928180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 39930fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 399484a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 39950fc1b451SAneesh Kumar K.V } else { 399684a8dce2SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE); 39978180a562SAneesh Kumar K.V /* i_block is stored in file system block size */ 39988180a562SAneesh Kumar K.V i_blocks = i_blocks >> (inode->i_blkbits - 9); 39998180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 40008180a562SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 40010fc1b451SAneesh Kumar K.V } 4002f287a1a5STheodore Ts'o return 0; 40030fc1b451SAneesh Kumar K.V } 40040fc1b451SAneesh Kumar K.V 4005ac27a0ecSDave Kleikamp /* 4006ac27a0ecSDave Kleikamp * Post the struct inode info into an on-disk inode location in the 4007ac27a0ecSDave Kleikamp * buffer-cache. This gobbles the caller's reference to the 4008ac27a0ecSDave Kleikamp * buffer_head in the inode location struct. 4009ac27a0ecSDave Kleikamp * 4010ac27a0ecSDave Kleikamp * The caller must have write access to iloc->bh. 4011ac27a0ecSDave Kleikamp */ 4012617ba13bSMingming Cao static int ext4_do_update_inode(handle_t *handle, 4013ac27a0ecSDave Kleikamp struct inode *inode, 4014830156c7SFrank Mayhar struct ext4_iloc *iloc) 4015ac27a0ecSDave Kleikamp { 4016617ba13bSMingming Cao struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 4017617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 4018ac27a0ecSDave Kleikamp struct buffer_head *bh = iloc->bh; 4019ac27a0ecSDave Kleikamp int err = 0, rc, block; 4020b71fc079SJan Kara int need_datasync = 0; 402108cefc7aSEric W. Biederman uid_t i_uid; 402208cefc7aSEric W. Biederman gid_t i_gid; 4023ac27a0ecSDave Kleikamp 4024ac27a0ecSDave Kleikamp /* For fields not not tracking in the in-memory inode, 4025ac27a0ecSDave Kleikamp * initialise them to zero for new inodes. */ 402619f5fb7aSTheodore Ts'o if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) 4027617ba13bSMingming Cao memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 4028ac27a0ecSDave Kleikamp 4029ff9ddf7eSJan Kara ext4_get_inode_flags(ei); 4030ac27a0ecSDave Kleikamp raw_inode->i_mode = cpu_to_le16(inode->i_mode); 403108cefc7aSEric W. Biederman i_uid = i_uid_read(inode); 403208cefc7aSEric W. Biederman i_gid = i_gid_read(inode); 4033ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 403408cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid)); 403508cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid)); 4036ac27a0ecSDave Kleikamp /* 4037ac27a0ecSDave Kleikamp * Fix up interoperability with old kernels. Otherwise, old inodes get 4038ac27a0ecSDave Kleikamp * re-used with the upper 16 bits of the uid/gid intact 4039ac27a0ecSDave Kleikamp */ 4040ac27a0ecSDave Kleikamp if (!ei->i_dtime) { 4041ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 404208cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_uid)); 4043ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 404408cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_gid)); 4045ac27a0ecSDave Kleikamp } else { 4046ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4047ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4048ac27a0ecSDave Kleikamp } 4049ac27a0ecSDave Kleikamp } else { 405008cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid)); 405108cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid)); 4052ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4053ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4054ac27a0ecSDave Kleikamp } 4055ac27a0ecSDave Kleikamp raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 4056ef7f3835SKalpak Shah 4057ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 4058ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 4059ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 4060ef7f3835SKalpak Shah EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 4061ef7f3835SKalpak Shah 40620fc1b451SAneesh Kumar K.V if (ext4_inode_blocks_set(handle, raw_inode, ei)) 40630fc1b451SAneesh Kumar K.V goto out_brelse; 4064ac27a0ecSDave Kleikamp raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 4065353eb83cSTheodore Ts'o raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF); 40669b8f1f01SMingming Cao if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 40679b8f1f01SMingming Cao cpu_to_le32(EXT4_OS_HURD)) 4068a1ddeb7eSBadari Pulavarty raw_inode->i_file_acl_high = 4069a1ddeb7eSBadari Pulavarty cpu_to_le16(ei->i_file_acl >> 32); 40707973c0c1SAneesh Kumar K.V raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 4071b71fc079SJan Kara if (ei->i_disksize != ext4_isize(raw_inode)) { 4072a48380f7SAneesh Kumar K.V ext4_isize_set(raw_inode, ei->i_disksize); 4073b71fc079SJan Kara need_datasync = 1; 4074b71fc079SJan Kara } 4075ac27a0ecSDave Kleikamp if (ei->i_disksize > 0x7fffffffULL) { 4076ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 4077617ba13bSMingming Cao if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, 4078617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE) || 4079617ba13bSMingming Cao EXT4_SB(sb)->s_es->s_rev_level == 4080617ba13bSMingming Cao cpu_to_le32(EXT4_GOOD_OLD_REV)) { 4081ac27a0ecSDave Kleikamp /* If this is the first large file 4082ac27a0ecSDave Kleikamp * created, add a flag to the superblock. 4083ac27a0ecSDave Kleikamp */ 4084617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, 4085617ba13bSMingming Cao EXT4_SB(sb)->s_sbh); 4086ac27a0ecSDave Kleikamp if (err) 4087ac27a0ecSDave Kleikamp goto out_brelse; 4088617ba13bSMingming Cao ext4_update_dynamic_rev(sb); 4089617ba13bSMingming Cao EXT4_SET_RO_COMPAT_FEATURE(sb, 4090617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE); 40910390131bSFrank Mayhar ext4_handle_sync(handle); 4092b50924c2SArtem Bityutskiy err = ext4_handle_dirty_super(handle, sb); 4093ac27a0ecSDave Kleikamp } 4094ac27a0ecSDave Kleikamp } 4095ac27a0ecSDave Kleikamp raw_inode->i_generation = cpu_to_le32(inode->i_generation); 4096ac27a0ecSDave Kleikamp if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 4097ac27a0ecSDave Kleikamp if (old_valid_dev(inode->i_rdev)) { 4098ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 4099ac27a0ecSDave Kleikamp cpu_to_le32(old_encode_dev(inode->i_rdev)); 4100ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 0; 4101ac27a0ecSDave Kleikamp } else { 4102ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 0; 4103ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 4104ac27a0ecSDave Kleikamp cpu_to_le32(new_encode_dev(inode->i_rdev)); 4105ac27a0ecSDave Kleikamp raw_inode->i_block[2] = 0; 4106ac27a0ecSDave Kleikamp } 4107de9a55b8STheodore Ts'o } else 4108de9a55b8STheodore Ts'o for (block = 0; block < EXT4_N_BLOCKS; block++) 4109ac27a0ecSDave Kleikamp raw_inode->i_block[block] = ei->i_data[block]; 4110ac27a0ecSDave Kleikamp 411125ec56b5SJean Noel Cordenner raw_inode->i_disk_version = cpu_to_le32(inode->i_version); 411225ec56b5SJean Noel Cordenner if (ei->i_extra_isize) { 411325ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 411425ec56b5SJean Noel Cordenner raw_inode->i_version_hi = 411525ec56b5SJean Noel Cordenner cpu_to_le32(inode->i_version >> 32); 4116ac27a0ecSDave Kleikamp raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize); 411725ec56b5SJean Noel Cordenner } 411825ec56b5SJean Noel Cordenner 4119814525f4SDarrick J. Wong ext4_inode_csum_set(inode, raw_inode, ei); 4120814525f4SDarrick J. Wong 41210390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 412273b50c1cSCurt Wohlgemuth rc = ext4_handle_dirty_metadata(handle, NULL, bh); 4123ac27a0ecSDave Kleikamp if (!err) 4124ac27a0ecSDave Kleikamp err = rc; 412519f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_NEW); 4126ac27a0ecSDave Kleikamp 4127b71fc079SJan Kara ext4_update_inode_fsync_trans(handle, inode, need_datasync); 4128ac27a0ecSDave Kleikamp out_brelse: 4129ac27a0ecSDave Kleikamp brelse(bh); 4130617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4131ac27a0ecSDave Kleikamp return err; 4132ac27a0ecSDave Kleikamp } 4133ac27a0ecSDave Kleikamp 4134ac27a0ecSDave Kleikamp /* 4135617ba13bSMingming Cao * ext4_write_inode() 4136ac27a0ecSDave Kleikamp * 4137ac27a0ecSDave Kleikamp * We are called from a few places: 4138ac27a0ecSDave Kleikamp * 4139ac27a0ecSDave Kleikamp * - Within generic_file_write() for O_SYNC files. 4140ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 41414907cb7bSAnatol Pomozov * transaction to commit. 4142ac27a0ecSDave Kleikamp * 4143ac27a0ecSDave Kleikamp * - Within sys_sync(), kupdate and such. 4144ac27a0ecSDave Kleikamp * We wait on commit, if tol to. 4145ac27a0ecSDave Kleikamp * 4146ac27a0ecSDave Kleikamp * - Within prune_icache() (PF_MEMALLOC == true) 4147ac27a0ecSDave Kleikamp * Here we simply return. We can't afford to block kswapd on the 4148ac27a0ecSDave Kleikamp * journal commit. 4149ac27a0ecSDave Kleikamp * 4150ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 4151ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 4152617ba13bSMingming Cao * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for 4153ac27a0ecSDave Kleikamp * knfsd. 4154ac27a0ecSDave Kleikamp * 4155ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 4156ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 4157ac27a0ecSDave Kleikamp * which we are interested. 4158ac27a0ecSDave Kleikamp * 4159ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 4160ac27a0ecSDave Kleikamp * 4161ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 4162ac27a0ecSDave Kleikamp * stuff(); 4163ac27a0ecSDave Kleikamp * inode->i_size = expr; 4164ac27a0ecSDave Kleikamp * 4165ac27a0ecSDave Kleikamp * is in error because a kswapd-driven write_inode() could occur while 4166ac27a0ecSDave Kleikamp * `stuff()' is running, and the new i_size will be lost. Plus the inode 4167ac27a0ecSDave Kleikamp * will no longer be on the superblock's dirty inode list. 4168ac27a0ecSDave Kleikamp */ 4169a9185b41SChristoph Hellwig int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) 4170ac27a0ecSDave Kleikamp { 417191ac6f43SFrank Mayhar int err; 417291ac6f43SFrank Mayhar 4173ac27a0ecSDave Kleikamp if (current->flags & PF_MEMALLOC) 4174ac27a0ecSDave Kleikamp return 0; 4175ac27a0ecSDave Kleikamp 417691ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal) { 4177617ba13bSMingming Cao if (ext4_journal_current_handle()) { 4178b38bd33aSMingming Cao jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); 4179ac27a0ecSDave Kleikamp dump_stack(); 4180ac27a0ecSDave Kleikamp return -EIO; 4181ac27a0ecSDave Kleikamp } 4182ac27a0ecSDave Kleikamp 4183a9185b41SChristoph Hellwig if (wbc->sync_mode != WB_SYNC_ALL) 4184ac27a0ecSDave Kleikamp return 0; 4185ac27a0ecSDave Kleikamp 418691ac6f43SFrank Mayhar err = ext4_force_commit(inode->i_sb); 418791ac6f43SFrank Mayhar } else { 418891ac6f43SFrank Mayhar struct ext4_iloc iloc; 418991ac6f43SFrank Mayhar 41908b472d73SCurt Wohlgemuth err = __ext4_get_inode_loc(inode, &iloc, 0); 419191ac6f43SFrank Mayhar if (err) 419291ac6f43SFrank Mayhar return err; 4193a9185b41SChristoph Hellwig if (wbc->sync_mode == WB_SYNC_ALL) 4194830156c7SFrank Mayhar sync_dirty_buffer(iloc.bh); 4195830156c7SFrank Mayhar if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { 4196c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr, 4197c398eda0STheodore Ts'o "IO error syncing inode"); 4198830156c7SFrank Mayhar err = -EIO; 4199830156c7SFrank Mayhar } 4200fd2dd9fbSCurt Wohlgemuth brelse(iloc.bh); 420191ac6f43SFrank Mayhar } 420291ac6f43SFrank Mayhar return err; 4203ac27a0ecSDave Kleikamp } 4204ac27a0ecSDave Kleikamp 4205ac27a0ecSDave Kleikamp /* 4206617ba13bSMingming Cao * ext4_setattr() 4207ac27a0ecSDave Kleikamp * 4208ac27a0ecSDave Kleikamp * Called from notify_change. 4209ac27a0ecSDave Kleikamp * 4210ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 4211ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 4212ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 4213ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 4214ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 4215ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 4216ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 4217ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 4218ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 4219ac27a0ecSDave Kleikamp * 4220678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 4221678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 4222678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 4223678aaf48SJan Kara * This way we are sure that all the data written in the previous 4224678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 4225678aaf48SJan Kara * writeback). 4226678aaf48SJan Kara * 4227678aaf48SJan Kara * Called with inode->i_mutex down. 4228ac27a0ecSDave Kleikamp */ 4229617ba13bSMingming Cao int ext4_setattr(struct dentry *dentry, struct iattr *attr) 4230ac27a0ecSDave Kleikamp { 4231ac27a0ecSDave Kleikamp struct inode *inode = dentry->d_inode; 4232ac27a0ecSDave Kleikamp int error, rc = 0; 42333d287de3SDmitry Monakhov int orphan = 0; 4234ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 4235ac27a0ecSDave Kleikamp 4236ac27a0ecSDave Kleikamp error = inode_change_ok(inode, attr); 4237ac27a0ecSDave Kleikamp if (error) 4238ac27a0ecSDave Kleikamp return error; 4239ac27a0ecSDave Kleikamp 424012755627SDmitry Monakhov if (is_quota_modification(inode, attr)) 4241871a2931SChristoph Hellwig dquot_initialize(inode); 424208cefc7aSEric W. Biederman if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) || 424308cefc7aSEric W. Biederman (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) { 4244ac27a0ecSDave Kleikamp handle_t *handle; 4245ac27a0ecSDave Kleikamp 4246ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 4247ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 42485aca07ebSDmitry Monakhov handle = ext4_journal_start(inode, (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb)+ 4249194074acSDmitry Monakhov EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb))+3); 4250ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 4251ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 4252ac27a0ecSDave Kleikamp goto err_out; 4253ac27a0ecSDave Kleikamp } 4254b43fa828SChristoph Hellwig error = dquot_transfer(inode, attr); 4255ac27a0ecSDave Kleikamp if (error) { 4256617ba13bSMingming Cao ext4_journal_stop(handle); 4257ac27a0ecSDave Kleikamp return error; 4258ac27a0ecSDave Kleikamp } 4259ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 4260ac27a0ecSDave Kleikamp * one transaction */ 4261ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_UID) 4262ac27a0ecSDave Kleikamp inode->i_uid = attr->ia_uid; 4263ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_GID) 4264ac27a0ecSDave Kleikamp inode->i_gid = attr->ia_gid; 4265617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 4266617ba13bSMingming Cao ext4_journal_stop(handle); 4267ac27a0ecSDave Kleikamp } 4268ac27a0ecSDave Kleikamp 4269e2b46574SEric Sandeen if (attr->ia_valid & ATTR_SIZE) { 4270562c72aaSChristoph Hellwig 427112e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 4272e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4273e2b46574SEric Sandeen 42740c095c7fSTheodore Ts'o if (attr->ia_size > sbi->s_bitmap_maxbytes) 42750c095c7fSTheodore Ts'o return -EFBIG; 4276e2b46574SEric Sandeen } 4277e2b46574SEric Sandeen } 4278e2b46574SEric Sandeen 4279ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode) && 4280c8d46e41SJiaying Zhang attr->ia_valid & ATTR_SIZE && 4281072bd7eaSTheodore Ts'o (attr->ia_size < inode->i_size)) { 4282ac27a0ecSDave Kleikamp handle_t *handle; 4283ac27a0ecSDave Kleikamp 4284617ba13bSMingming Cao handle = ext4_journal_start(inode, 3); 4285ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 4286ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 4287ac27a0ecSDave Kleikamp goto err_out; 4288ac27a0ecSDave Kleikamp } 42893d287de3SDmitry Monakhov if (ext4_handle_valid(handle)) { 4290617ba13bSMingming Cao error = ext4_orphan_add(handle, inode); 42913d287de3SDmitry Monakhov orphan = 1; 42923d287de3SDmitry Monakhov } 4293617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 4294617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 4295ac27a0ecSDave Kleikamp if (!error) 4296ac27a0ecSDave Kleikamp error = rc; 4297617ba13bSMingming Cao ext4_journal_stop(handle); 4298678aaf48SJan Kara 4299678aaf48SJan Kara if (ext4_should_order_data(inode)) { 4300678aaf48SJan Kara error = ext4_begin_ordered_truncate(inode, 4301678aaf48SJan Kara attr->ia_size); 4302678aaf48SJan Kara if (error) { 4303678aaf48SJan Kara /* Do as much error cleanup as possible */ 4304678aaf48SJan Kara handle = ext4_journal_start(inode, 3); 4305678aaf48SJan Kara if (IS_ERR(handle)) { 4306678aaf48SJan Kara ext4_orphan_del(NULL, inode); 4307678aaf48SJan Kara goto err_out; 4308678aaf48SJan Kara } 4309678aaf48SJan Kara ext4_orphan_del(handle, inode); 43103d287de3SDmitry Monakhov orphan = 0; 4311678aaf48SJan Kara ext4_journal_stop(handle); 4312678aaf48SJan Kara goto err_out; 4313678aaf48SJan Kara } 4314678aaf48SJan Kara } 4315ac27a0ecSDave Kleikamp } 4316ac27a0ecSDave Kleikamp 4317072bd7eaSTheodore Ts'o if (attr->ia_valid & ATTR_SIZE) { 43181c9114f9SDmitry Monakhov if (attr->ia_size != i_size_read(inode)) { 4319072bd7eaSTheodore Ts'o truncate_setsize(inode, attr->ia_size); 43201b65007eSDmitry Monakhov /* Inode size will be reduced, wait for dio in flight. 43211b65007eSDmitry Monakhov * Temporarily disable dioread_nolock to prevent 43221b65007eSDmitry Monakhov * livelock. */ 43231b65007eSDmitry Monakhov if (orphan) { 43241b65007eSDmitry Monakhov ext4_inode_block_unlocked_dio(inode); 43251c9114f9SDmitry Monakhov inode_dio_wait(inode); 43261b65007eSDmitry Monakhov ext4_inode_resume_unlocked_dio(inode); 43271b65007eSDmitry Monakhov } 43281c9114f9SDmitry Monakhov } 4329072bd7eaSTheodore Ts'o ext4_truncate(inode); 4330072bd7eaSTheodore Ts'o } 4331ac27a0ecSDave Kleikamp 43321025774cSChristoph Hellwig if (!rc) { 43331025774cSChristoph Hellwig setattr_copy(inode, attr); 43341025774cSChristoph Hellwig mark_inode_dirty(inode); 43351025774cSChristoph Hellwig } 43361025774cSChristoph Hellwig 43371025774cSChristoph Hellwig /* 43381025774cSChristoph Hellwig * If the call to ext4_truncate failed to get a transaction handle at 43391025774cSChristoph Hellwig * all, we need to clean up the in-core orphan list manually. 43401025774cSChristoph Hellwig */ 43413d287de3SDmitry Monakhov if (orphan && inode->i_nlink) 4342617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 4343ac27a0ecSDave Kleikamp 4344ac27a0ecSDave Kleikamp if (!rc && (ia_valid & ATTR_MODE)) 4345617ba13bSMingming Cao rc = ext4_acl_chmod(inode); 4346ac27a0ecSDave Kleikamp 4347ac27a0ecSDave Kleikamp err_out: 4348617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 4349ac27a0ecSDave Kleikamp if (!error) 4350ac27a0ecSDave Kleikamp error = rc; 4351ac27a0ecSDave Kleikamp return error; 4352ac27a0ecSDave Kleikamp } 4353ac27a0ecSDave Kleikamp 43543e3398a0SMingming Cao int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry, 43553e3398a0SMingming Cao struct kstat *stat) 43563e3398a0SMingming Cao { 43573e3398a0SMingming Cao struct inode *inode; 43583e3398a0SMingming Cao unsigned long delalloc_blocks; 43593e3398a0SMingming Cao 43603e3398a0SMingming Cao inode = dentry->d_inode; 43613e3398a0SMingming Cao generic_fillattr(inode, stat); 43623e3398a0SMingming Cao 43633e3398a0SMingming Cao /* 43643e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 43653e3398a0SMingming Cao * otherwise in the case of system crash before the real block 43663e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 43673e3398a0SMingming Cao * on-disk file blocks. 43683e3398a0SMingming Cao * We always keep i_blocks updated together with real 43693e3398a0SMingming Cao * allocation. But to not confuse with user, stat 43703e3398a0SMingming Cao * will return the blocks that include the delayed allocation 43713e3398a0SMingming Cao * blocks for this file. 43723e3398a0SMingming Cao */ 437396607551STao Ma delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb), 437496607551STao Ma EXT4_I(inode)->i_reserved_data_blocks); 43753e3398a0SMingming Cao 43763e3398a0SMingming Cao stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9; 43773e3398a0SMingming Cao return 0; 43783e3398a0SMingming Cao } 4379ac27a0ecSDave Kleikamp 4380a02908f1SMingming Cao static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4381a02908f1SMingming Cao { 438212e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 43838bb2b247SAmir Goldstein return ext4_ind_trans_blocks(inode, nrblocks, chunk); 4384ac51d837STheodore Ts'o return ext4_ext_index_trans_blocks(inode, nrblocks, chunk); 4385a02908f1SMingming Cao } 4386ac51d837STheodore Ts'o 4387a02908f1SMingming Cao /* 4388a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 4389a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 4390a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 4391a02908f1SMingming Cao * 4392a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 43934907cb7bSAnatol Pomozov * different block groups too. If they are contiguous, with flexbg, 4394a02908f1SMingming Cao * they could still across block group boundary. 4395a02908f1SMingming Cao * 4396a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 4397a02908f1SMingming Cao */ 43981f109d5aSTheodore Ts'o static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4399a02908f1SMingming Cao { 44008df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 44018df9675fSTheodore Ts'o int gdpblocks; 4402a02908f1SMingming Cao int idxblocks; 4403a02908f1SMingming Cao int ret = 0; 4404a02908f1SMingming Cao 4405a02908f1SMingming Cao /* 4406a02908f1SMingming Cao * How many index blocks need to touch to modify nrblocks? 4407a02908f1SMingming Cao * The "Chunk" flag indicating whether the nrblocks is 4408a02908f1SMingming Cao * physically contiguous on disk 4409a02908f1SMingming Cao * 4410a02908f1SMingming Cao * For Direct IO and fallocate, they calls get_block to allocate 4411a02908f1SMingming Cao * one single extent at a time, so they could set the "Chunk" flag 4412a02908f1SMingming Cao */ 4413a02908f1SMingming Cao idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk); 4414a02908f1SMingming Cao 4415a02908f1SMingming Cao ret = idxblocks; 4416a02908f1SMingming Cao 4417a02908f1SMingming Cao /* 4418a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 4419a02908f1SMingming Cao * to account 4420a02908f1SMingming Cao */ 4421a02908f1SMingming Cao groups = idxblocks; 4422a02908f1SMingming Cao if (chunk) 4423a02908f1SMingming Cao groups += 1; 4424ac27a0ecSDave Kleikamp else 4425a02908f1SMingming Cao groups += nrblocks; 4426ac27a0ecSDave Kleikamp 4427a02908f1SMingming Cao gdpblocks = groups; 44288df9675fSTheodore Ts'o if (groups > ngroups) 44298df9675fSTheodore Ts'o groups = ngroups; 4430a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 4431a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 4432a02908f1SMingming Cao 4433a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 4434a02908f1SMingming Cao ret += groups + gdpblocks; 4435a02908f1SMingming Cao 4436a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 4437a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 4438ac27a0ecSDave Kleikamp 4439ac27a0ecSDave Kleikamp return ret; 4440ac27a0ecSDave Kleikamp } 4441ac27a0ecSDave Kleikamp 4442ac27a0ecSDave Kleikamp /* 444325985edcSLucas De Marchi * Calculate the total number of credits to reserve to fit 4444f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 4445f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 4446a02908f1SMingming Cao * 4447525f4ed8SMingming Cao * This could be called via ext4_write_begin() 4448a02908f1SMingming Cao * 4449525f4ed8SMingming Cao * We need to consider the worse case, when 4450a02908f1SMingming Cao * one new block per extent. 4451a02908f1SMingming Cao */ 4452a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 4453a02908f1SMingming Cao { 4454a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 4455a02908f1SMingming Cao int ret; 4456a02908f1SMingming Cao 4457a02908f1SMingming Cao ret = ext4_meta_trans_blocks(inode, bpp, 0); 4458a02908f1SMingming Cao 4459a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 4460a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 4461a02908f1SMingming Cao ret += bpp; 4462a02908f1SMingming Cao return ret; 4463a02908f1SMingming Cao } 4464f3bd1f3fSMingming Cao 4465f3bd1f3fSMingming Cao /* 4466f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 4467f3bd1f3fSMingming Cao * 4468f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 446979e83036SEric Sandeen * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks. 4470f3bd1f3fSMingming Cao * 4471f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 4472f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 4473f3bd1f3fSMingming Cao */ 4474f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 4475f3bd1f3fSMingming Cao { 4476f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 4477f3bd1f3fSMingming Cao } 4478f3bd1f3fSMingming Cao 4479a02908f1SMingming Cao /* 4480617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 4481ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 4482ac27a0ecSDave Kleikamp */ 4483617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 4484617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 4485ac27a0ecSDave Kleikamp { 4486ac27a0ecSDave Kleikamp int err = 0; 4487ac27a0ecSDave Kleikamp 4488c64db50eSTheodore Ts'o if (IS_I_VERSION(inode)) 448925ec56b5SJean Noel Cordenner inode_inc_iversion(inode); 449025ec56b5SJean Noel Cordenner 4491ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 4492ac27a0ecSDave Kleikamp get_bh(iloc->bh); 4493ac27a0ecSDave Kleikamp 4494dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 4495830156c7SFrank Mayhar err = ext4_do_update_inode(handle, inode, iloc); 4496ac27a0ecSDave Kleikamp put_bh(iloc->bh); 4497ac27a0ecSDave Kleikamp return err; 4498ac27a0ecSDave Kleikamp } 4499ac27a0ecSDave Kleikamp 4500ac27a0ecSDave Kleikamp /* 4501ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 4502ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 4503ac27a0ecSDave Kleikamp */ 4504ac27a0ecSDave Kleikamp 4505ac27a0ecSDave Kleikamp int 4506617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 4507617ba13bSMingming Cao struct ext4_iloc *iloc) 4508ac27a0ecSDave Kleikamp { 45090390131bSFrank Mayhar int err; 45100390131bSFrank Mayhar 4511617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 4512ac27a0ecSDave Kleikamp if (!err) { 4513ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 4514617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, iloc->bh); 4515ac27a0ecSDave Kleikamp if (err) { 4516ac27a0ecSDave Kleikamp brelse(iloc->bh); 4517ac27a0ecSDave Kleikamp iloc->bh = NULL; 4518ac27a0ecSDave Kleikamp } 4519ac27a0ecSDave Kleikamp } 4520617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4521ac27a0ecSDave Kleikamp return err; 4522ac27a0ecSDave Kleikamp } 4523ac27a0ecSDave Kleikamp 4524ac27a0ecSDave Kleikamp /* 45256dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 45266dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 45276dd4ee7cSKalpak Shah */ 45281d03ec98SAneesh Kumar K.V static int ext4_expand_extra_isize(struct inode *inode, 45291d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 45301d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 45311d03ec98SAneesh Kumar K.V handle_t *handle) 45326dd4ee7cSKalpak Shah { 45336dd4ee7cSKalpak Shah struct ext4_inode *raw_inode; 45346dd4ee7cSKalpak Shah struct ext4_xattr_ibody_header *header; 45356dd4ee7cSKalpak Shah 45366dd4ee7cSKalpak Shah if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) 45376dd4ee7cSKalpak Shah return 0; 45386dd4ee7cSKalpak Shah 45396dd4ee7cSKalpak Shah raw_inode = ext4_raw_inode(&iloc); 45406dd4ee7cSKalpak Shah 45416dd4ee7cSKalpak Shah header = IHDR(inode, raw_inode); 45426dd4ee7cSKalpak Shah 45436dd4ee7cSKalpak Shah /* No extended attributes present */ 454419f5fb7aSTheodore Ts'o if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) || 45456dd4ee7cSKalpak Shah header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 45466dd4ee7cSKalpak Shah memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0, 45476dd4ee7cSKalpak Shah new_extra_isize); 45486dd4ee7cSKalpak Shah EXT4_I(inode)->i_extra_isize = new_extra_isize; 45496dd4ee7cSKalpak Shah return 0; 45506dd4ee7cSKalpak Shah } 45516dd4ee7cSKalpak Shah 45526dd4ee7cSKalpak Shah /* try to expand with EAs present */ 45536dd4ee7cSKalpak Shah return ext4_expand_extra_isize_ea(inode, new_extra_isize, 45546dd4ee7cSKalpak Shah raw_inode, handle); 45556dd4ee7cSKalpak Shah } 45566dd4ee7cSKalpak Shah 45576dd4ee7cSKalpak Shah /* 4558ac27a0ecSDave Kleikamp * What we do here is to mark the in-core inode as clean with respect to inode 4559ac27a0ecSDave Kleikamp * dirtiness (it may still be data-dirty). 4560ac27a0ecSDave Kleikamp * This means that the in-core inode may be reaped by prune_icache 4561ac27a0ecSDave Kleikamp * without having to perform any I/O. This is a very good thing, 4562ac27a0ecSDave Kleikamp * because *any* task may call prune_icache - even ones which 4563ac27a0ecSDave Kleikamp * have a transaction open against a different journal. 4564ac27a0ecSDave Kleikamp * 4565ac27a0ecSDave Kleikamp * Is this cheating? Not really. Sure, we haven't written the 4566ac27a0ecSDave Kleikamp * inode out, but prune_icache isn't a user-visible syncing function. 4567ac27a0ecSDave Kleikamp * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 4568ac27a0ecSDave Kleikamp * we start and wait on commits. 4569ac27a0ecSDave Kleikamp */ 4570617ba13bSMingming Cao int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode) 4571ac27a0ecSDave Kleikamp { 4572617ba13bSMingming Cao struct ext4_iloc iloc; 45736dd4ee7cSKalpak Shah struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 45746dd4ee7cSKalpak Shah static unsigned int mnt_count; 45756dd4ee7cSKalpak Shah int err, ret; 4576ac27a0ecSDave Kleikamp 4577ac27a0ecSDave Kleikamp might_sleep(); 45787ff9c073STheodore Ts'o trace_ext4_mark_inode_dirty(inode, _RET_IP_); 4579617ba13bSMingming Cao err = ext4_reserve_inode_write(handle, inode, &iloc); 45800390131bSFrank Mayhar if (ext4_handle_valid(handle) && 45810390131bSFrank Mayhar EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize && 458219f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) { 45836dd4ee7cSKalpak Shah /* 45846dd4ee7cSKalpak Shah * We need extra buffer credits since we may write into EA block 45856dd4ee7cSKalpak Shah * with this same handle. If journal_extend fails, then it will 45866dd4ee7cSKalpak Shah * only result in a minor loss of functionality for that inode. 45876dd4ee7cSKalpak Shah * If this is felt to be critical, then e2fsck should be run to 45886dd4ee7cSKalpak Shah * force a large enough s_min_extra_isize. 45896dd4ee7cSKalpak Shah */ 45906dd4ee7cSKalpak Shah if ((jbd2_journal_extend(handle, 45916dd4ee7cSKalpak Shah EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) { 45926dd4ee7cSKalpak Shah ret = ext4_expand_extra_isize(inode, 45936dd4ee7cSKalpak Shah sbi->s_want_extra_isize, 45946dd4ee7cSKalpak Shah iloc, handle); 45956dd4ee7cSKalpak Shah if (ret) { 459619f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, 459719f5fb7aSTheodore Ts'o EXT4_STATE_NO_EXPAND); 4598c1bddad9SAneesh Kumar K.V if (mnt_count != 4599c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count)) { 460012062dddSEric Sandeen ext4_warning(inode->i_sb, 46016dd4ee7cSKalpak Shah "Unable to expand inode %lu. Delete" 46026dd4ee7cSKalpak Shah " some EAs or run e2fsck.", 46036dd4ee7cSKalpak Shah inode->i_ino); 4604c1bddad9SAneesh Kumar K.V mnt_count = 4605c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count); 46066dd4ee7cSKalpak Shah } 46076dd4ee7cSKalpak Shah } 46086dd4ee7cSKalpak Shah } 46096dd4ee7cSKalpak Shah } 4610ac27a0ecSDave Kleikamp if (!err) 4611617ba13bSMingming Cao err = ext4_mark_iloc_dirty(handle, inode, &iloc); 4612ac27a0ecSDave Kleikamp return err; 4613ac27a0ecSDave Kleikamp } 4614ac27a0ecSDave Kleikamp 4615ac27a0ecSDave Kleikamp /* 4616617ba13bSMingming Cao * ext4_dirty_inode() is called from __mark_inode_dirty() 4617ac27a0ecSDave Kleikamp * 4618ac27a0ecSDave Kleikamp * We're really interested in the case where a file is being extended. 4619ac27a0ecSDave Kleikamp * i_size has been changed by generic_commit_write() and we thus need 4620ac27a0ecSDave Kleikamp * to include the updated inode in the current transaction. 4621ac27a0ecSDave Kleikamp * 46225dd4056dSChristoph Hellwig * Also, dquot_alloc_block() will always dirty the inode when blocks 4623ac27a0ecSDave Kleikamp * are allocated to the file. 4624ac27a0ecSDave Kleikamp * 4625ac27a0ecSDave Kleikamp * If the inode is marked synchronous, we don't honour that here - doing 4626ac27a0ecSDave Kleikamp * so would cause a commit on atime updates, which we don't bother doing. 4627ac27a0ecSDave Kleikamp * We handle synchronous inodes at the highest possible level. 4628ac27a0ecSDave Kleikamp */ 4629aa385729SChristoph Hellwig void ext4_dirty_inode(struct inode *inode, int flags) 4630ac27a0ecSDave Kleikamp { 4631ac27a0ecSDave Kleikamp handle_t *handle; 4632ac27a0ecSDave Kleikamp 4633617ba13bSMingming Cao handle = ext4_journal_start(inode, 2); 4634ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 4635ac27a0ecSDave Kleikamp goto out; 4636f3dc272fSCurt Wohlgemuth 4637617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 4638f3dc272fSCurt Wohlgemuth 4639617ba13bSMingming Cao ext4_journal_stop(handle); 4640ac27a0ecSDave Kleikamp out: 4641ac27a0ecSDave Kleikamp return; 4642ac27a0ecSDave Kleikamp } 4643ac27a0ecSDave Kleikamp 4644ac27a0ecSDave Kleikamp #if 0 4645ac27a0ecSDave Kleikamp /* 4646ac27a0ecSDave Kleikamp * Bind an inode's backing buffer_head into this transaction, to prevent 4647ac27a0ecSDave Kleikamp * it from being flushed to disk early. Unlike 4648617ba13bSMingming Cao * ext4_reserve_inode_write, this leaves behind no bh reference and 4649ac27a0ecSDave Kleikamp * returns no iloc structure, so the caller needs to repeat the iloc 4650ac27a0ecSDave Kleikamp * lookup to mark the inode dirty later. 4651ac27a0ecSDave Kleikamp */ 4652617ba13bSMingming Cao static int ext4_pin_inode(handle_t *handle, struct inode *inode) 4653ac27a0ecSDave Kleikamp { 4654617ba13bSMingming Cao struct ext4_iloc iloc; 4655ac27a0ecSDave Kleikamp 4656ac27a0ecSDave Kleikamp int err = 0; 4657ac27a0ecSDave Kleikamp if (handle) { 4658617ba13bSMingming Cao err = ext4_get_inode_loc(inode, &iloc); 4659ac27a0ecSDave Kleikamp if (!err) { 4660ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc.bh, "get_write_access"); 4661dab291afSMingming Cao err = jbd2_journal_get_write_access(handle, iloc.bh); 4662ac27a0ecSDave Kleikamp if (!err) 46630390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, 466473b50c1cSCurt Wohlgemuth NULL, 4665ac27a0ecSDave Kleikamp iloc.bh); 4666ac27a0ecSDave Kleikamp brelse(iloc.bh); 4667ac27a0ecSDave Kleikamp } 4668ac27a0ecSDave Kleikamp } 4669617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4670ac27a0ecSDave Kleikamp return err; 4671ac27a0ecSDave Kleikamp } 4672ac27a0ecSDave Kleikamp #endif 4673ac27a0ecSDave Kleikamp 4674617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val) 4675ac27a0ecSDave Kleikamp { 4676ac27a0ecSDave Kleikamp journal_t *journal; 4677ac27a0ecSDave Kleikamp handle_t *handle; 4678ac27a0ecSDave Kleikamp int err; 4679ac27a0ecSDave Kleikamp 4680ac27a0ecSDave Kleikamp /* 4681ac27a0ecSDave Kleikamp * We have to be very careful here: changing a data block's 4682ac27a0ecSDave Kleikamp * journaling status dynamically is dangerous. If we write a 4683ac27a0ecSDave Kleikamp * data block to the journal, change the status and then delete 4684ac27a0ecSDave Kleikamp * that block, we risk forgetting to revoke the old log record 4685ac27a0ecSDave Kleikamp * from the journal and so a subsequent replay can corrupt data. 4686ac27a0ecSDave Kleikamp * So, first we make sure that the journal is empty and that 4687ac27a0ecSDave Kleikamp * nobody is changing anything. 4688ac27a0ecSDave Kleikamp */ 4689ac27a0ecSDave Kleikamp 4690617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 46910390131bSFrank Mayhar if (!journal) 46920390131bSFrank Mayhar return 0; 4693d699594dSDave Hansen if (is_journal_aborted(journal)) 4694ac27a0ecSDave Kleikamp return -EROFS; 46952aff57b0SYongqiang Yang /* We have to allocate physical blocks for delalloc blocks 46962aff57b0SYongqiang Yang * before flushing journal. otherwise delalloc blocks can not 46972aff57b0SYongqiang Yang * be allocated any more. even more truncate on delalloc blocks 46982aff57b0SYongqiang Yang * could trigger BUG by flushing delalloc blocks in journal. 46992aff57b0SYongqiang Yang * There is no delalloc block in non-journal data mode. 47002aff57b0SYongqiang Yang */ 47012aff57b0SYongqiang Yang if (val && test_opt(inode->i_sb, DELALLOC)) { 47022aff57b0SYongqiang Yang err = ext4_alloc_da_blocks(inode); 47032aff57b0SYongqiang Yang if (err < 0) 47042aff57b0SYongqiang Yang return err; 47052aff57b0SYongqiang Yang } 4706ac27a0ecSDave Kleikamp 470717335dccSDmitry Monakhov /* Wait for all existing dio workers */ 470817335dccSDmitry Monakhov ext4_inode_block_unlocked_dio(inode); 470917335dccSDmitry Monakhov inode_dio_wait(inode); 471017335dccSDmitry Monakhov 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); 473017335dccSDmitry Monakhov ext4_inode_resume_unlocked_dio(inode); 4731ac27a0ecSDave Kleikamp 4732ac27a0ecSDave Kleikamp /* Finally we can mark the inode as dirty. */ 4733ac27a0ecSDave Kleikamp 4734617ba13bSMingming Cao handle = ext4_journal_start(inode, 1); 4735ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 4736ac27a0ecSDave Kleikamp return PTR_ERR(handle); 4737ac27a0ecSDave Kleikamp 4738617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 47390390131bSFrank Mayhar ext4_handle_sync(handle); 4740617ba13bSMingming Cao ext4_journal_stop(handle); 4741617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4742ac27a0ecSDave Kleikamp 4743ac27a0ecSDave Kleikamp return err; 4744ac27a0ecSDave Kleikamp } 47452e9ee850SAneesh Kumar K.V 47462e9ee850SAneesh Kumar K.V static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) 47472e9ee850SAneesh Kumar K.V { 47482e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 47492e9ee850SAneesh Kumar K.V } 47502e9ee850SAneesh Kumar K.V 4751c2ec175cSNick Piggin int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) 47522e9ee850SAneesh Kumar K.V { 4753c2ec175cSNick Piggin struct page *page = vmf->page; 47542e9ee850SAneesh Kumar K.V loff_t size; 47552e9ee850SAneesh Kumar K.V unsigned long len; 47569ea7df53SJan Kara int ret; 47572e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 47582e9ee850SAneesh Kumar K.V struct inode *inode = file->f_path.dentry->d_inode; 47592e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 47609ea7df53SJan Kara handle_t *handle; 47619ea7df53SJan Kara get_block_t *get_block; 47629ea7df53SJan Kara int retries = 0; 47632e9ee850SAneesh Kumar K.V 47648e8ad8a5SJan Kara sb_start_pagefault(inode->i_sb); 4765041bbb6dSTheodore Ts'o file_update_time(vma->vm_file); 47669ea7df53SJan Kara /* Delalloc case is easy... */ 47679ea7df53SJan Kara if (test_opt(inode->i_sb, DELALLOC) && 47689ea7df53SJan Kara !ext4_should_journal_data(inode) && 47699ea7df53SJan Kara !ext4_nonda_switch(inode->i_sb)) { 47709ea7df53SJan Kara do { 47719ea7df53SJan Kara ret = __block_page_mkwrite(vma, vmf, 47729ea7df53SJan Kara ext4_da_get_block_prep); 47739ea7df53SJan Kara } while (ret == -ENOSPC && 47749ea7df53SJan Kara ext4_should_retry_alloc(inode->i_sb, &retries)); 47759ea7df53SJan Kara goto out_ret; 47762e9ee850SAneesh Kumar K.V } 47770e499890SDarrick J. Wong 47780e499890SDarrick J. Wong lock_page(page); 47799ea7df53SJan Kara size = i_size_read(inode); 47809ea7df53SJan Kara /* Page got truncated from under us? */ 47819ea7df53SJan Kara if (page->mapping != mapping || page_offset(page) > size) { 47829ea7df53SJan Kara unlock_page(page); 47839ea7df53SJan Kara ret = VM_FAULT_NOPAGE; 47849ea7df53SJan Kara goto out; 47850e499890SDarrick J. Wong } 47862e9ee850SAneesh Kumar K.V 47872e9ee850SAneesh Kumar K.V if (page->index == size >> PAGE_CACHE_SHIFT) 47882e9ee850SAneesh Kumar K.V len = size & ~PAGE_CACHE_MASK; 47892e9ee850SAneesh Kumar K.V else 47902e9ee850SAneesh Kumar K.V len = PAGE_CACHE_SIZE; 4791a827eaffSAneesh Kumar K.V /* 47929ea7df53SJan Kara * Return if we have all the buffers mapped. This avoids the need to do 47939ea7df53SJan Kara * journal_start/journal_stop which can block and take a long time 4794a827eaffSAneesh Kumar K.V */ 47952e9ee850SAneesh Kumar K.V if (page_has_buffers(page)) { 47962e9ee850SAneesh Kumar K.V if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL, 4797a827eaffSAneesh Kumar K.V ext4_bh_unmapped)) { 47989ea7df53SJan Kara /* Wait so that we don't change page under IO */ 47999ea7df53SJan Kara wait_on_page_writeback(page); 48009ea7df53SJan Kara ret = VM_FAULT_LOCKED; 48019ea7df53SJan Kara goto out; 48022e9ee850SAneesh Kumar K.V } 4803a827eaffSAneesh Kumar K.V } 4804a827eaffSAneesh Kumar K.V unlock_page(page); 48059ea7df53SJan Kara /* OK, we need to fill the hole... */ 48069ea7df53SJan Kara if (ext4_should_dioread_nolock(inode)) 48079ea7df53SJan Kara get_block = ext4_get_block_write; 48089ea7df53SJan Kara else 48099ea7df53SJan Kara get_block = ext4_get_block; 48109ea7df53SJan Kara retry_alloc: 48119ea7df53SJan Kara handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode)); 48129ea7df53SJan Kara if (IS_ERR(handle)) { 4813c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 48149ea7df53SJan Kara goto out; 48159ea7df53SJan Kara } 48169ea7df53SJan Kara ret = __block_page_mkwrite(vma, vmf, get_block); 48179ea7df53SJan Kara if (!ret && ext4_should_journal_data(inode)) { 48189ea7df53SJan Kara if (walk_page_buffers(handle, page_buffers(page), 0, 48199ea7df53SJan Kara PAGE_CACHE_SIZE, NULL, do_journal_get_write_access)) { 48209ea7df53SJan Kara unlock_page(page); 48219ea7df53SJan Kara ret = VM_FAULT_SIGBUS; 4822fcbb5515SYongqiang Yang ext4_journal_stop(handle); 48239ea7df53SJan Kara goto out; 48249ea7df53SJan Kara } 48259ea7df53SJan Kara ext4_set_inode_state(inode, EXT4_STATE_JDATA); 48269ea7df53SJan Kara } 48279ea7df53SJan Kara ext4_journal_stop(handle); 48289ea7df53SJan Kara if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 48299ea7df53SJan Kara goto retry_alloc; 48309ea7df53SJan Kara out_ret: 48319ea7df53SJan Kara ret = block_page_mkwrite_return(ret); 48329ea7df53SJan Kara out: 48338e8ad8a5SJan Kara sb_end_pagefault(inode->i_sb); 48342e9ee850SAneesh Kumar K.V return ret; 48352e9ee850SAneesh Kumar K.V } 4836