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/module.h> 22ac27a0ecSDave Kleikamp #include <linux/fs.h> 23ac27a0ecSDave Kleikamp #include <linux/time.h> 24dab291afSMingming Cao #include <linux/jbd2.h> 25ac27a0ecSDave Kleikamp #include <linux/highuid.h> 26ac27a0ecSDave Kleikamp #include <linux/pagemap.h> 27ac27a0ecSDave Kleikamp #include <linux/quotaops.h> 28ac27a0ecSDave Kleikamp #include <linux/string.h> 29ac27a0ecSDave Kleikamp #include <linux/buffer_head.h> 30ac27a0ecSDave Kleikamp #include <linux/writeback.h> 3164769240SAlex Tomas #include <linux/pagevec.h> 32ac27a0ecSDave Kleikamp #include <linux/mpage.h> 33e83c1397SDuane Griffin #include <linux/namei.h> 34ac27a0ecSDave Kleikamp #include <linux/uio.h> 35ac27a0ecSDave Kleikamp #include <linux/bio.h> 364c0425ffSMingming Cao #include <linux/workqueue.h> 37744692dcSJiaying Zhang #include <linux/kernel.h> 386db26ffcSAndrew Morton #include <linux/printk.h> 395a0e3ad6STejun Heo #include <linux/slab.h> 40a8901d34STheodore Ts'o #include <linux/ratelimit.h> 419bffad1eSTheodore Ts'o 423dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 43ac27a0ecSDave Kleikamp #include "xattr.h" 44ac27a0ecSDave Kleikamp #include "acl.h" 459f125d64STheodore Ts'o #include "truncate.h" 46ac27a0ecSDave Kleikamp 479bffad1eSTheodore Ts'o #include <trace/events/ext4.h> 489bffad1eSTheodore Ts'o 49a1d6cc56SAneesh Kumar K.V #define MPAGE_DA_EXTENT_TAIL 0x01 50a1d6cc56SAneesh Kumar K.V 51678aaf48SJan Kara static inline int ext4_begin_ordered_truncate(struct inode *inode, 52678aaf48SJan Kara loff_t new_size) 53678aaf48SJan Kara { 547ff9c073STheodore Ts'o trace_ext4_begin_ordered_truncate(inode, new_size); 558aefcd55STheodore Ts'o /* 568aefcd55STheodore Ts'o * If jinode is zero, then we never opened the file for 578aefcd55STheodore Ts'o * writing, so there's no need to call 588aefcd55STheodore Ts'o * jbd2_journal_begin_ordered_truncate() since there's no 598aefcd55STheodore Ts'o * outstanding writes we need to flush. 608aefcd55STheodore Ts'o */ 618aefcd55STheodore Ts'o if (!EXT4_I(inode)->jinode) 628aefcd55STheodore Ts'o return 0; 638aefcd55STheodore Ts'o return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode), 648aefcd55STheodore Ts'o EXT4_I(inode)->jinode, 65678aaf48SJan Kara new_size); 66678aaf48SJan Kara } 67678aaf48SJan Kara 6864769240SAlex Tomas static void ext4_invalidatepage(struct page *page, unsigned long offset); 69cb20d518STheodore Ts'o static int noalloc_get_block_write(struct inode *inode, sector_t iblock, 70cb20d518STheodore Ts'o struct buffer_head *bh_result, int create); 71cb20d518STheodore Ts'o static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode); 72cb20d518STheodore Ts'o static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate); 73cb20d518STheodore Ts'o static int __ext4_journalled_writepage(struct page *page, unsigned int len); 74cb20d518STheodore Ts'o static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh); 7564769240SAlex Tomas 76ac27a0ecSDave Kleikamp /* 77ac27a0ecSDave Kleikamp * Test whether an inode is a fast symlink. 78ac27a0ecSDave Kleikamp */ 79617ba13bSMingming Cao static int ext4_inode_is_fast_symlink(struct inode *inode) 80ac27a0ecSDave Kleikamp { 81617ba13bSMingming Cao int ea_blocks = EXT4_I(inode)->i_file_acl ? 82ac27a0ecSDave Kleikamp (inode->i_sb->s_blocksize >> 9) : 0; 83ac27a0ecSDave Kleikamp 84ac27a0ecSDave Kleikamp return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0); 85ac27a0ecSDave Kleikamp } 86ac27a0ecSDave Kleikamp 87ac27a0ecSDave Kleikamp /* 88ac27a0ecSDave Kleikamp * Restart the transaction associated with *handle. This does a commit, 89ac27a0ecSDave Kleikamp * so before we call here everything must be consistently dirtied against 90ac27a0ecSDave Kleikamp * this transaction. 91ac27a0ecSDave Kleikamp */ 92487caeefSJan Kara int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode, 93487caeefSJan Kara int nblocks) 94ac27a0ecSDave Kleikamp { 95487caeefSJan Kara int ret; 96487caeefSJan Kara 97487caeefSJan Kara /* 98e35fd660STheodore Ts'o * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this 99487caeefSJan Kara * moment, get_block can be called only for blocks inside i_size since 100487caeefSJan Kara * page cache has been already dropped and writes are blocked by 101487caeefSJan Kara * i_mutex. So we can safely drop the i_data_sem here. 102487caeefSJan Kara */ 1030390131bSFrank Mayhar BUG_ON(EXT4_JOURNAL(inode) == NULL); 104ac27a0ecSDave Kleikamp jbd_debug(2, "restarting handle %p\n", handle); 105487caeefSJan Kara up_write(&EXT4_I(inode)->i_data_sem); 1068e8eaabeSAmir Goldstein ret = ext4_journal_restart(handle, nblocks); 107487caeefSJan Kara down_write(&EXT4_I(inode)->i_data_sem); 108fa5d1113SAneesh Kumar K.V ext4_discard_preallocations(inode); 109487caeefSJan Kara 110487caeefSJan Kara return ret; 111ac27a0ecSDave Kleikamp } 112ac27a0ecSDave Kleikamp 113ac27a0ecSDave Kleikamp /* 114ac27a0ecSDave Kleikamp * Called at the last iput() if i_nlink is zero. 115ac27a0ecSDave Kleikamp */ 1160930fcc1SAl Viro void ext4_evict_inode(struct inode *inode) 117ac27a0ecSDave Kleikamp { 118ac27a0ecSDave Kleikamp handle_t *handle; 119bc965ab3STheodore Ts'o int err; 120ac27a0ecSDave Kleikamp 1217ff9c073STheodore Ts'o trace_ext4_evict_inode(inode); 1222581fdc8SJiaying Zhang 1232581fdc8SJiaying Zhang ext4_ioend_wait(inode); 1242581fdc8SJiaying Zhang 1250930fcc1SAl Viro if (inode->i_nlink) { 1262d859db3SJan Kara /* 1272d859db3SJan Kara * When journalling data dirty buffers are tracked only in the 1282d859db3SJan Kara * journal. So although mm thinks everything is clean and 1292d859db3SJan Kara * ready for reaping the inode might still have some pages to 1302d859db3SJan Kara * write in the running transaction or waiting to be 1312d859db3SJan Kara * checkpointed. Thus calling jbd2_journal_invalidatepage() 1322d859db3SJan Kara * (via truncate_inode_pages()) to discard these buffers can 1332d859db3SJan Kara * cause data loss. Also even if we did not discard these 1342d859db3SJan Kara * buffers, we would have no way to find them after the inode 1352d859db3SJan Kara * is reaped and thus user could see stale data if he tries to 1362d859db3SJan Kara * read them before the transaction is checkpointed. So be 1372d859db3SJan Kara * careful and force everything to disk here... We use 1382d859db3SJan Kara * ei->i_datasync_tid to store the newest transaction 1392d859db3SJan Kara * containing inode's data. 1402d859db3SJan Kara * 1412d859db3SJan Kara * Note that directories do not have this problem because they 1422d859db3SJan Kara * don't use page cache. 1432d859db3SJan Kara */ 1442d859db3SJan Kara if (ext4_should_journal_data(inode) && 1452d859db3SJan Kara (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode))) { 1462d859db3SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 1472d859db3SJan Kara tid_t commit_tid = EXT4_I(inode)->i_datasync_tid; 1482d859db3SJan Kara 1492d859db3SJan Kara jbd2_log_start_commit(journal, commit_tid); 1502d859db3SJan Kara jbd2_log_wait_commit(journal, commit_tid); 1512d859db3SJan Kara filemap_write_and_wait(&inode->i_data); 1522d859db3SJan Kara } 1530930fcc1SAl Viro truncate_inode_pages(&inode->i_data, 0); 1540930fcc1SAl Viro goto no_delete; 1550930fcc1SAl Viro } 1560930fcc1SAl Viro 157907f4554SChristoph Hellwig if (!is_bad_inode(inode)) 158871a2931SChristoph Hellwig dquot_initialize(inode); 159907f4554SChristoph Hellwig 160678aaf48SJan Kara if (ext4_should_order_data(inode)) 161678aaf48SJan Kara ext4_begin_ordered_truncate(inode, 0); 162ac27a0ecSDave Kleikamp truncate_inode_pages(&inode->i_data, 0); 163ac27a0ecSDave Kleikamp 164ac27a0ecSDave Kleikamp if (is_bad_inode(inode)) 165ac27a0ecSDave Kleikamp goto no_delete; 166ac27a0ecSDave Kleikamp 1679f125d64STheodore Ts'o handle = ext4_journal_start(inode, ext4_blocks_for_truncate(inode)+3); 168ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 169bc965ab3STheodore Ts'o ext4_std_error(inode->i_sb, PTR_ERR(handle)); 170ac27a0ecSDave Kleikamp /* 171ac27a0ecSDave Kleikamp * If we're going to skip the normal cleanup, we still need to 172ac27a0ecSDave Kleikamp * make sure that the in-core orphan linked list is properly 173ac27a0ecSDave Kleikamp * cleaned up. 174ac27a0ecSDave Kleikamp */ 175617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 176ac27a0ecSDave Kleikamp goto no_delete; 177ac27a0ecSDave Kleikamp } 178ac27a0ecSDave Kleikamp 179ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 1800390131bSFrank Mayhar ext4_handle_sync(handle); 181ac27a0ecSDave Kleikamp inode->i_size = 0; 182bc965ab3STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 183bc965ab3STheodore Ts'o if (err) { 18412062dddSEric Sandeen ext4_warning(inode->i_sb, 185bc965ab3STheodore Ts'o "couldn't mark inode dirty (err %d)", err); 186bc965ab3STheodore Ts'o goto stop_handle; 187bc965ab3STheodore Ts'o } 188ac27a0ecSDave Kleikamp if (inode->i_blocks) 189617ba13bSMingming Cao ext4_truncate(inode); 190bc965ab3STheodore Ts'o 191bc965ab3STheodore Ts'o /* 192bc965ab3STheodore Ts'o * ext4_ext_truncate() doesn't reserve any slop when it 193bc965ab3STheodore Ts'o * restarts journal transactions; therefore there may not be 194bc965ab3STheodore Ts'o * enough credits left in the handle to remove the inode from 195bc965ab3STheodore Ts'o * the orphan list and set the dtime field. 196bc965ab3STheodore Ts'o */ 1970390131bSFrank Mayhar if (!ext4_handle_has_enough_credits(handle, 3)) { 198bc965ab3STheodore Ts'o err = ext4_journal_extend(handle, 3); 199bc965ab3STheodore Ts'o if (err > 0) 200bc965ab3STheodore Ts'o err = ext4_journal_restart(handle, 3); 201bc965ab3STheodore Ts'o if (err != 0) { 20212062dddSEric Sandeen ext4_warning(inode->i_sb, 203bc965ab3STheodore Ts'o "couldn't extend journal (err %d)", err); 204bc965ab3STheodore Ts'o stop_handle: 205bc965ab3STheodore Ts'o ext4_journal_stop(handle); 20645388219STheodore Ts'o ext4_orphan_del(NULL, inode); 207bc965ab3STheodore Ts'o goto no_delete; 208bc965ab3STheodore Ts'o } 209bc965ab3STheodore Ts'o } 210bc965ab3STheodore Ts'o 211ac27a0ecSDave Kleikamp /* 212617ba13bSMingming Cao * Kill off the orphan record which ext4_truncate created. 213ac27a0ecSDave Kleikamp * AKPM: I think this can be inside the above `if'. 214617ba13bSMingming Cao * Note that ext4_orphan_del() has to be able to cope with the 215ac27a0ecSDave Kleikamp * deletion of a non-existent orphan - this is because we don't 216617ba13bSMingming Cao * know if ext4_truncate() actually created an orphan record. 217ac27a0ecSDave Kleikamp * (Well, we could do this if we need to, but heck - it works) 218ac27a0ecSDave Kleikamp */ 219617ba13bSMingming Cao ext4_orphan_del(handle, inode); 220617ba13bSMingming Cao EXT4_I(inode)->i_dtime = get_seconds(); 221ac27a0ecSDave Kleikamp 222ac27a0ecSDave Kleikamp /* 223ac27a0ecSDave Kleikamp * One subtle ordering requirement: if anything has gone wrong 224ac27a0ecSDave Kleikamp * (transaction abort, IO errors, whatever), then we can still 225ac27a0ecSDave Kleikamp * do these next steps (the fs will already have been marked as 226ac27a0ecSDave Kleikamp * having errors), but we can't free the inode if the mark_dirty 227ac27a0ecSDave Kleikamp * fails. 228ac27a0ecSDave Kleikamp */ 229617ba13bSMingming Cao if (ext4_mark_inode_dirty(handle, inode)) 230ac27a0ecSDave Kleikamp /* If that failed, just do the required in-core inode clear. */ 2310930fcc1SAl Viro ext4_clear_inode(inode); 232ac27a0ecSDave Kleikamp else 233617ba13bSMingming Cao ext4_free_inode(handle, inode); 234617ba13bSMingming Cao ext4_journal_stop(handle); 235ac27a0ecSDave Kleikamp return; 236ac27a0ecSDave Kleikamp no_delete: 2370930fcc1SAl Viro ext4_clear_inode(inode); /* We must guarantee clearing of inode... */ 238ac27a0ecSDave Kleikamp } 239ac27a0ecSDave Kleikamp 240a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 241a9e7f447SDmitry Monakhov qsize_t *ext4_get_reserved_space(struct inode *inode) 24260e58e0fSMingming Cao { 243a9e7f447SDmitry Monakhov return &EXT4_I(inode)->i_reserved_quota; 24460e58e0fSMingming Cao } 245a9e7f447SDmitry Monakhov #endif 2469d0be502STheodore Ts'o 24712219aeaSAneesh Kumar K.V /* 24812219aeaSAneesh Kumar K.V * Calculate the number of metadata blocks need to reserve 2499d0be502STheodore Ts'o * to allocate a block located at @lblock 25012219aeaSAneesh Kumar K.V */ 25101f49d0bSTheodore Ts'o static int ext4_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock) 25212219aeaSAneesh Kumar K.V { 25312e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 2549d0be502STheodore Ts'o return ext4_ext_calc_metadata_amount(inode, lblock); 25512219aeaSAneesh Kumar K.V 2568bb2b247SAmir Goldstein return ext4_ind_calc_metadata_amount(inode, lblock); 25712219aeaSAneesh Kumar K.V } 25812219aeaSAneesh Kumar K.V 2590637c6f4STheodore Ts'o /* 2600637c6f4STheodore Ts'o * Called with i_data_sem down, which is important since we can call 2610637c6f4STheodore Ts'o * ext4_discard_preallocations() from here. 2620637c6f4STheodore Ts'o */ 2635f634d06SAneesh Kumar K.V void ext4_da_update_reserve_space(struct inode *inode, 2645f634d06SAneesh Kumar K.V int used, int quota_claim) 26512219aeaSAneesh Kumar K.V { 26612219aeaSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 2670637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 26812219aeaSAneesh Kumar K.V 2690637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 270d8990240SAditya Kali trace_ext4_da_update_reserve_space(inode, used, quota_claim); 2710637c6f4STheodore Ts'o if (unlikely(used > ei->i_reserved_data_blocks)) { 2720637c6f4STheodore Ts'o ext4_msg(inode->i_sb, KERN_NOTICE, "%s: ino %lu, used %d " 2730637c6f4STheodore Ts'o "with only %d reserved data blocks\n", 2740637c6f4STheodore Ts'o __func__, inode->i_ino, used, 2750637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 2760637c6f4STheodore Ts'o WARN_ON(1); 2770637c6f4STheodore Ts'o used = ei->i_reserved_data_blocks; 2786bc6e63fSAneesh Kumar K.V } 27912219aeaSAneesh Kumar K.V 2800637c6f4STheodore Ts'o /* Update per-inode reservations */ 2810637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= used; 2820637c6f4STheodore Ts'o ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks; 28357042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 28472b8ab9dSEric Sandeen used + ei->i_allocated_meta_blocks); 2850637c6f4STheodore Ts'o ei->i_allocated_meta_blocks = 0; 2860637c6f4STheodore Ts'o 2870637c6f4STheodore Ts'o if (ei->i_reserved_data_blocks == 0) { 2880637c6f4STheodore Ts'o /* 2890637c6f4STheodore Ts'o * We can release all of the reserved metadata blocks 2900637c6f4STheodore Ts'o * only when we have written all of the delayed 2910637c6f4STheodore Ts'o * allocation blocks. 2920637c6f4STheodore Ts'o */ 29357042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 29472b8ab9dSEric Sandeen ei->i_reserved_meta_blocks); 295ee5f4d9cSTheodore Ts'o ei->i_reserved_meta_blocks = 0; 2969d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 2970637c6f4STheodore Ts'o } 29812219aeaSAneesh Kumar K.V spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 29960e58e0fSMingming Cao 30072b8ab9dSEric Sandeen /* Update quota subsystem for data blocks */ 30172b8ab9dSEric Sandeen if (quota_claim) 3027b415bf6SAditya Kali dquot_claim_block(inode, EXT4_C2B(sbi, used)); 30372b8ab9dSEric Sandeen else { 3045f634d06SAneesh Kumar K.V /* 3055f634d06SAneesh Kumar K.V * We did fallocate with an offset that is already delayed 3065f634d06SAneesh Kumar K.V * allocated. So on delayed allocated writeback we should 30772b8ab9dSEric Sandeen * not re-claim the quota for fallocated blocks. 3085f634d06SAneesh Kumar K.V */ 3097b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, used)); 3105f634d06SAneesh Kumar K.V } 311d6014301SAneesh Kumar K.V 312d6014301SAneesh Kumar K.V /* 313d6014301SAneesh Kumar K.V * If we have done all the pending block allocations and if 314d6014301SAneesh Kumar K.V * there aren't any writers on the inode, we can discard the 315d6014301SAneesh Kumar K.V * inode's preallocations. 316d6014301SAneesh Kumar K.V */ 3170637c6f4STheodore Ts'o if ((ei->i_reserved_data_blocks == 0) && 3180637c6f4STheodore Ts'o (atomic_read(&inode->i_writecount) == 0)) 319d6014301SAneesh Kumar K.V ext4_discard_preallocations(inode); 32012219aeaSAneesh Kumar K.V } 32112219aeaSAneesh Kumar K.V 322e29136f8STheodore Ts'o static int __check_block_validity(struct inode *inode, const char *func, 323c398eda0STheodore Ts'o unsigned int line, 32424676da4STheodore Ts'o struct ext4_map_blocks *map) 3256fd058f7STheodore Ts'o { 32624676da4STheodore Ts'o if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk, 32724676da4STheodore Ts'o map->m_len)) { 328c398eda0STheodore Ts'o ext4_error_inode(inode, func, line, map->m_pblk, 329c398eda0STheodore Ts'o "lblock %lu mapped to illegal pblock " 33024676da4STheodore Ts'o "(length %d)", (unsigned long) map->m_lblk, 331c398eda0STheodore Ts'o map->m_len); 3326fd058f7STheodore Ts'o return -EIO; 3336fd058f7STheodore Ts'o } 3346fd058f7STheodore Ts'o return 0; 3356fd058f7STheodore Ts'o } 3366fd058f7STheodore Ts'o 337e29136f8STheodore Ts'o #define check_block_validity(inode, map) \ 338c398eda0STheodore Ts'o __check_block_validity((inode), __func__, __LINE__, (map)) 339e29136f8STheodore Ts'o 340f5ab0d1fSMingming Cao /* 3411f94533dSTheodore Ts'o * Return the number of contiguous dirty pages in a given inode 3421f94533dSTheodore Ts'o * starting at page frame idx. 34355138e0bSTheodore Ts'o */ 34455138e0bSTheodore Ts'o static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx, 34555138e0bSTheodore Ts'o unsigned int max_pages) 34655138e0bSTheodore Ts'o { 34755138e0bSTheodore Ts'o struct address_space *mapping = inode->i_mapping; 34855138e0bSTheodore Ts'o pgoff_t index; 34955138e0bSTheodore Ts'o struct pagevec pvec; 35055138e0bSTheodore Ts'o pgoff_t num = 0; 35155138e0bSTheodore Ts'o int i, nr_pages, done = 0; 35255138e0bSTheodore Ts'o 35355138e0bSTheodore Ts'o if (max_pages == 0) 35455138e0bSTheodore Ts'o return 0; 35555138e0bSTheodore Ts'o pagevec_init(&pvec, 0); 35655138e0bSTheodore Ts'o while (!done) { 35755138e0bSTheodore Ts'o index = idx; 35855138e0bSTheodore Ts'o nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 35955138e0bSTheodore Ts'o PAGECACHE_TAG_DIRTY, 36055138e0bSTheodore Ts'o (pgoff_t)PAGEVEC_SIZE); 36155138e0bSTheodore Ts'o if (nr_pages == 0) 36255138e0bSTheodore Ts'o break; 36355138e0bSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 36455138e0bSTheodore Ts'o struct page *page = pvec.pages[i]; 36555138e0bSTheodore Ts'o struct buffer_head *bh, *head; 36655138e0bSTheodore Ts'o 36755138e0bSTheodore Ts'o lock_page(page); 36855138e0bSTheodore Ts'o if (unlikely(page->mapping != mapping) || 36955138e0bSTheodore Ts'o !PageDirty(page) || 37055138e0bSTheodore Ts'o PageWriteback(page) || 37155138e0bSTheodore Ts'o page->index != idx) { 37255138e0bSTheodore Ts'o done = 1; 37355138e0bSTheodore Ts'o unlock_page(page); 37455138e0bSTheodore Ts'o break; 37555138e0bSTheodore Ts'o } 3761f94533dSTheodore Ts'o if (page_has_buffers(page)) { 3771f94533dSTheodore Ts'o bh = head = page_buffers(page); 37855138e0bSTheodore Ts'o do { 37955138e0bSTheodore Ts'o if (!buffer_delay(bh) && 3801f94533dSTheodore Ts'o !buffer_unwritten(bh)) 38155138e0bSTheodore Ts'o done = 1; 3821f94533dSTheodore Ts'o bh = bh->b_this_page; 3831f94533dSTheodore Ts'o } while (!done && (bh != head)); 38455138e0bSTheodore Ts'o } 38555138e0bSTheodore Ts'o unlock_page(page); 38655138e0bSTheodore Ts'o if (done) 38755138e0bSTheodore Ts'o break; 38855138e0bSTheodore Ts'o idx++; 38955138e0bSTheodore Ts'o num++; 390659c6009SEric Sandeen if (num >= max_pages) { 391659c6009SEric Sandeen done = 1; 39255138e0bSTheodore Ts'o break; 39355138e0bSTheodore Ts'o } 394659c6009SEric Sandeen } 39555138e0bSTheodore Ts'o pagevec_release(&pvec); 39655138e0bSTheodore Ts'o } 39755138e0bSTheodore Ts'o return num; 39855138e0bSTheodore Ts'o } 39955138e0bSTheodore Ts'o 40055138e0bSTheodore Ts'o /* 4015356f261SAditya Kali * Sets the BH_Da_Mapped bit on the buffer heads corresponding to the given map. 4025356f261SAditya Kali */ 4035356f261SAditya Kali static void set_buffers_da_mapped(struct inode *inode, 4045356f261SAditya Kali struct ext4_map_blocks *map) 4055356f261SAditya Kali { 4065356f261SAditya Kali struct address_space *mapping = inode->i_mapping; 4075356f261SAditya Kali struct pagevec pvec; 4085356f261SAditya Kali int i, nr_pages; 4095356f261SAditya Kali pgoff_t index, end; 4105356f261SAditya Kali 4115356f261SAditya Kali index = map->m_lblk >> (PAGE_CACHE_SHIFT - inode->i_blkbits); 4125356f261SAditya Kali end = (map->m_lblk + map->m_len - 1) >> 4135356f261SAditya Kali (PAGE_CACHE_SHIFT - inode->i_blkbits); 4145356f261SAditya Kali 4155356f261SAditya Kali pagevec_init(&pvec, 0); 4165356f261SAditya Kali while (index <= end) { 4175356f261SAditya Kali nr_pages = pagevec_lookup(&pvec, mapping, index, 4185356f261SAditya Kali min(end - index + 1, 4195356f261SAditya Kali (pgoff_t)PAGEVEC_SIZE)); 4205356f261SAditya Kali if (nr_pages == 0) 4215356f261SAditya Kali break; 4225356f261SAditya Kali for (i = 0; i < nr_pages; i++) { 4235356f261SAditya Kali struct page *page = pvec.pages[i]; 4245356f261SAditya Kali struct buffer_head *bh, *head; 4255356f261SAditya Kali 4265356f261SAditya Kali if (unlikely(page->mapping != mapping) || 4275356f261SAditya Kali !PageDirty(page)) 4285356f261SAditya Kali break; 4295356f261SAditya Kali 4305356f261SAditya Kali if (page_has_buffers(page)) { 4315356f261SAditya Kali bh = head = page_buffers(page); 4325356f261SAditya Kali do { 4335356f261SAditya Kali set_buffer_da_mapped(bh); 4345356f261SAditya Kali bh = bh->b_this_page; 4355356f261SAditya Kali } while (bh != head); 4365356f261SAditya Kali } 4375356f261SAditya Kali index++; 4385356f261SAditya Kali } 4395356f261SAditya Kali pagevec_release(&pvec); 4405356f261SAditya Kali } 4415356f261SAditya Kali } 4425356f261SAditya Kali 4435356f261SAditya Kali /* 444e35fd660STheodore Ts'o * The ext4_map_blocks() function tries to look up the requested blocks, 4452b2d6d01STheodore Ts'o * and returns if the blocks are already mapped. 446f5ab0d1fSMingming Cao * 447f5ab0d1fSMingming Cao * Otherwise it takes the write lock of the i_data_sem and allocate blocks 448f5ab0d1fSMingming Cao * and store the allocated blocks in the result buffer head and mark it 449f5ab0d1fSMingming Cao * mapped. 450f5ab0d1fSMingming Cao * 451e35fd660STheodore Ts'o * If file type is extents based, it will call ext4_ext_map_blocks(), 452e35fd660STheodore Ts'o * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping 453f5ab0d1fSMingming Cao * based files 454f5ab0d1fSMingming Cao * 455f5ab0d1fSMingming Cao * On success, it returns the number of blocks being mapped or allocate. 456f5ab0d1fSMingming Cao * if create==0 and the blocks are pre-allocated and uninitialized block, 457f5ab0d1fSMingming Cao * the result buffer head is unmapped. If the create ==1, it will make sure 458f5ab0d1fSMingming Cao * the buffer head is mapped. 459f5ab0d1fSMingming Cao * 460f5ab0d1fSMingming Cao * It returns 0 if plain look up failed (blocks have not been allocated), in 461df3ab170STao Ma * that case, buffer head is unmapped 462f5ab0d1fSMingming Cao * 463f5ab0d1fSMingming Cao * It returns the error in case of allocation failure. 464f5ab0d1fSMingming Cao */ 465e35fd660STheodore Ts'o int ext4_map_blocks(handle_t *handle, struct inode *inode, 466e35fd660STheodore Ts'o struct ext4_map_blocks *map, int flags) 4670e855ac8SAneesh Kumar K.V { 4680e855ac8SAneesh Kumar K.V int retval; 469f5ab0d1fSMingming Cao 470e35fd660STheodore Ts'o map->m_flags = 0; 471e35fd660STheodore Ts'o ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u," 472e35fd660STheodore Ts'o "logical block %lu\n", inode->i_ino, flags, map->m_len, 473e35fd660STheodore Ts'o (unsigned long) map->m_lblk); 4744df3d265SAneesh Kumar K.V /* 475b920c755STheodore Ts'o * Try to see if we can get the block without requesting a new 476b920c755STheodore Ts'o * file system block. 4774df3d265SAneesh Kumar K.V */ 4780e855ac8SAneesh Kumar K.V down_read((&EXT4_I(inode)->i_data_sem)); 47912e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 480e35fd660STheodore Ts'o retval = ext4_ext_map_blocks(handle, inode, map, 0); 4814df3d265SAneesh Kumar K.V } else { 482e35fd660STheodore Ts'o retval = ext4_ind_map_blocks(handle, inode, map, 0); 4830e855ac8SAneesh Kumar K.V } 4844df3d265SAneesh Kumar K.V up_read((&EXT4_I(inode)->i_data_sem)); 485f5ab0d1fSMingming Cao 486e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 487e29136f8STheodore Ts'o int ret = check_block_validity(inode, map); 4886fd058f7STheodore Ts'o if (ret != 0) 4896fd058f7STheodore Ts'o return ret; 4906fd058f7STheodore Ts'o } 4916fd058f7STheodore Ts'o 492f5ab0d1fSMingming Cao /* If it is only a block(s) look up */ 493c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 4944df3d265SAneesh Kumar K.V return retval; 4954df3d265SAneesh Kumar K.V 4964df3d265SAneesh Kumar K.V /* 497f5ab0d1fSMingming Cao * Returns if the blocks have already allocated 498f5ab0d1fSMingming Cao * 499f5ab0d1fSMingming Cao * Note that if blocks have been preallocated 500df3ab170STao Ma * ext4_ext_get_block() returns the create = 0 501f5ab0d1fSMingming Cao * with buffer head unmapped. 502f5ab0d1fSMingming Cao */ 503e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) 504f5ab0d1fSMingming Cao return retval; 505f5ab0d1fSMingming Cao 506f5ab0d1fSMingming Cao /* 5072a8964d6SAneesh Kumar K.V * When we call get_blocks without the create flag, the 5082a8964d6SAneesh Kumar K.V * BH_Unwritten flag could have gotten set if the blocks 5092a8964d6SAneesh Kumar K.V * requested were part of a uninitialized extent. We need to 5102a8964d6SAneesh Kumar K.V * clear this flag now that we are committed to convert all or 5112a8964d6SAneesh Kumar K.V * part of the uninitialized extent to be an initialized 5122a8964d6SAneesh Kumar K.V * extent. This is because we need to avoid the combination 5132a8964d6SAneesh Kumar K.V * of BH_Unwritten and BH_Mapped flags being simultaneously 5142a8964d6SAneesh Kumar K.V * set on the buffer_head. 5152a8964d6SAneesh Kumar K.V */ 516e35fd660STheodore Ts'o map->m_flags &= ~EXT4_MAP_UNWRITTEN; 5172a8964d6SAneesh Kumar K.V 5182a8964d6SAneesh Kumar K.V /* 519f5ab0d1fSMingming Cao * New blocks allocate and/or writing to uninitialized extent 520f5ab0d1fSMingming Cao * will possibly result in updating i_data, so we take 521f5ab0d1fSMingming Cao * the write lock of i_data_sem, and call get_blocks() 522f5ab0d1fSMingming Cao * with create == 1 flag. 5234df3d265SAneesh Kumar K.V */ 5244df3d265SAneesh Kumar K.V down_write((&EXT4_I(inode)->i_data_sem)); 525d2a17637SMingming Cao 526d2a17637SMingming Cao /* 527d2a17637SMingming Cao * if the caller is from delayed allocation writeout path 528d2a17637SMingming Cao * we have already reserved fs blocks for allocation 529d2a17637SMingming Cao * let the underlying get_block() function know to 530d2a17637SMingming Cao * avoid double accounting 531d2a17637SMingming Cao */ 532c2177057STheodore Ts'o if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 533f2321097STheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED); 5344df3d265SAneesh Kumar K.V /* 5354df3d265SAneesh Kumar K.V * We need to check for EXT4 here because migrate 5364df3d265SAneesh Kumar K.V * could have changed the inode type in between 5374df3d265SAneesh Kumar K.V */ 53812e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 539e35fd660STheodore Ts'o retval = ext4_ext_map_blocks(handle, inode, map, flags); 5400e855ac8SAneesh Kumar K.V } else { 541e35fd660STheodore Ts'o retval = ext4_ind_map_blocks(handle, inode, map, flags); 542267e4db9SAneesh Kumar K.V 543e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_NEW) { 544267e4db9SAneesh Kumar K.V /* 545267e4db9SAneesh Kumar K.V * We allocated new blocks which will result in 546267e4db9SAneesh Kumar K.V * i_data's format changing. Force the migrate 547267e4db9SAneesh Kumar K.V * to fail by clearing migrate flags 548267e4db9SAneesh Kumar K.V */ 54919f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE); 550267e4db9SAneesh Kumar K.V } 5512ac3b6e0STheodore Ts'o 552d2a17637SMingming Cao /* 5532ac3b6e0STheodore Ts'o * Update reserved blocks/metadata blocks after successful 5545f634d06SAneesh Kumar K.V * block allocation which had been deferred till now. We don't 5555f634d06SAneesh Kumar K.V * support fallocate for non extent files. So we can update 5565f634d06SAneesh Kumar K.V * reserve space here. 557d2a17637SMingming Cao */ 5585f634d06SAneesh Kumar K.V if ((retval > 0) && 5591296cc85SAneesh Kumar K.V (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)) 5605f634d06SAneesh Kumar K.V ext4_da_update_reserve_space(inode, retval, 1); 5615f634d06SAneesh Kumar K.V } 5625356f261SAditya Kali if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) { 563f2321097STheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED); 564d2a17637SMingming Cao 5655356f261SAditya Kali /* If we have successfully mapped the delayed allocated blocks, 5665356f261SAditya Kali * set the BH_Da_Mapped bit on them. Its important to do this 5675356f261SAditya Kali * under the protection of i_data_sem. 5685356f261SAditya Kali */ 5695356f261SAditya Kali if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) 5705356f261SAditya Kali set_buffers_da_mapped(inode, map); 5715356f261SAditya Kali } 5725356f261SAditya Kali 5730e855ac8SAneesh Kumar K.V up_write((&EXT4_I(inode)->i_data_sem)); 574e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 575e29136f8STheodore Ts'o int ret = check_block_validity(inode, map); 5766fd058f7STheodore Ts'o if (ret != 0) 5776fd058f7STheodore Ts'o return ret; 5786fd058f7STheodore Ts'o } 5790e855ac8SAneesh Kumar K.V return retval; 5800e855ac8SAneesh Kumar K.V } 5810e855ac8SAneesh Kumar K.V 582f3bd1f3fSMingming Cao /* Maximum number of blocks we map for direct IO at once. */ 583f3bd1f3fSMingming Cao #define DIO_MAX_BLOCKS 4096 584f3bd1f3fSMingming Cao 5852ed88685STheodore Ts'o static int _ext4_get_block(struct inode *inode, sector_t iblock, 5862ed88685STheodore Ts'o struct buffer_head *bh, int flags) 587ac27a0ecSDave Kleikamp { 5883e4fdaf8SDmitriy Monakhov handle_t *handle = ext4_journal_current_handle(); 5892ed88685STheodore Ts'o struct ext4_map_blocks map; 5907fb5409dSJan Kara int ret = 0, started = 0; 591f3bd1f3fSMingming Cao int dio_credits; 592ac27a0ecSDave Kleikamp 5932ed88685STheodore Ts'o map.m_lblk = iblock; 5942ed88685STheodore Ts'o map.m_len = bh->b_size >> inode->i_blkbits; 5952ed88685STheodore Ts'o 5962ed88685STheodore Ts'o if (flags && !handle) { 5977fb5409dSJan Kara /* Direct IO write... */ 5982ed88685STheodore Ts'o if (map.m_len > DIO_MAX_BLOCKS) 5992ed88685STheodore Ts'o map.m_len = DIO_MAX_BLOCKS; 6002ed88685STheodore Ts'o dio_credits = ext4_chunk_trans_blocks(inode, map.m_len); 601f3bd1f3fSMingming Cao handle = ext4_journal_start(inode, dio_credits); 6027fb5409dSJan Kara if (IS_ERR(handle)) { 603ac27a0ecSDave Kleikamp ret = PTR_ERR(handle); 6042ed88685STheodore Ts'o return ret; 6057fb5409dSJan Kara } 6067fb5409dSJan Kara started = 1; 607ac27a0ecSDave Kleikamp } 608ac27a0ecSDave Kleikamp 6092ed88685STheodore Ts'o ret = ext4_map_blocks(handle, inode, &map, flags); 610ac27a0ecSDave Kleikamp if (ret > 0) { 6112ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 6122ed88685STheodore Ts'o bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; 6132ed88685STheodore Ts'o bh->b_size = inode->i_sb->s_blocksize * map.m_len; 614ac27a0ecSDave Kleikamp ret = 0; 615ac27a0ecSDave Kleikamp } 6167fb5409dSJan Kara if (started) 6177fb5409dSJan Kara ext4_journal_stop(handle); 618ac27a0ecSDave Kleikamp return ret; 619ac27a0ecSDave Kleikamp } 620ac27a0ecSDave Kleikamp 6212ed88685STheodore Ts'o int ext4_get_block(struct inode *inode, sector_t iblock, 6222ed88685STheodore Ts'o struct buffer_head *bh, int create) 6232ed88685STheodore Ts'o { 6242ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh, 6252ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 6262ed88685STheodore Ts'o } 6272ed88685STheodore Ts'o 628ac27a0ecSDave Kleikamp /* 629ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 630ac27a0ecSDave Kleikamp */ 631617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 632725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *errp) 633ac27a0ecSDave Kleikamp { 6342ed88685STheodore Ts'o struct ext4_map_blocks map; 6352ed88685STheodore Ts'o struct buffer_head *bh; 636ac27a0ecSDave Kleikamp int fatal = 0, err; 637ac27a0ecSDave Kleikamp 638ac27a0ecSDave Kleikamp J_ASSERT(handle != NULL || create == 0); 639ac27a0ecSDave Kleikamp 6402ed88685STheodore Ts'o map.m_lblk = block; 6412ed88685STheodore Ts'o map.m_len = 1; 6422ed88685STheodore Ts'o err = ext4_map_blocks(handle, inode, &map, 6432ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 6442ed88685STheodore Ts'o 6452ed88685STheodore Ts'o if (err < 0) 646ac27a0ecSDave Kleikamp *errp = err; 6472ed88685STheodore Ts'o if (err <= 0) 6482ed88685STheodore Ts'o return NULL; 6492ed88685STheodore Ts'o *errp = 0; 6502ed88685STheodore Ts'o 6512ed88685STheodore Ts'o bh = sb_getblk(inode->i_sb, map.m_pblk); 652ac27a0ecSDave Kleikamp if (!bh) { 653ac27a0ecSDave Kleikamp *errp = -EIO; 6542ed88685STheodore Ts'o return NULL; 655ac27a0ecSDave Kleikamp } 6562ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 657ac27a0ecSDave Kleikamp J_ASSERT(create != 0); 658ac39849dSAneesh Kumar K.V J_ASSERT(handle != NULL); 659ac27a0ecSDave Kleikamp 660ac27a0ecSDave Kleikamp /* 661ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 662ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 663ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 664617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 665ac27a0ecSDave Kleikamp * problem. 666ac27a0ecSDave Kleikamp */ 667ac27a0ecSDave Kleikamp lock_buffer(bh); 668ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 669617ba13bSMingming Cao fatal = ext4_journal_get_create_access(handle, bh); 670ac27a0ecSDave Kleikamp if (!fatal && !buffer_uptodate(bh)) { 671ac27a0ecSDave Kleikamp memset(bh->b_data, 0, inode->i_sb->s_blocksize); 672ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 673ac27a0ecSDave Kleikamp } 674ac27a0ecSDave Kleikamp unlock_buffer(bh); 6750390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 6760390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 677ac27a0ecSDave Kleikamp if (!fatal) 678ac27a0ecSDave Kleikamp fatal = err; 679ac27a0ecSDave Kleikamp } else { 680ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "not a new buffer"); 681ac27a0ecSDave Kleikamp } 682ac27a0ecSDave Kleikamp if (fatal) { 683ac27a0ecSDave Kleikamp *errp = fatal; 684ac27a0ecSDave Kleikamp brelse(bh); 685ac27a0ecSDave Kleikamp bh = NULL; 686ac27a0ecSDave Kleikamp } 687ac27a0ecSDave Kleikamp return bh; 688ac27a0ecSDave Kleikamp } 689ac27a0ecSDave Kleikamp 690617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 691725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *err) 692ac27a0ecSDave Kleikamp { 693ac27a0ecSDave Kleikamp struct buffer_head *bh; 694ac27a0ecSDave Kleikamp 695617ba13bSMingming Cao bh = ext4_getblk(handle, inode, block, create, err); 696ac27a0ecSDave Kleikamp if (!bh) 697ac27a0ecSDave Kleikamp return bh; 698ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 699ac27a0ecSDave Kleikamp return bh; 700ac27a0ecSDave Kleikamp ll_rw_block(READ_META, 1, &bh); 701ac27a0ecSDave Kleikamp wait_on_buffer(bh); 702ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 703ac27a0ecSDave Kleikamp return bh; 704ac27a0ecSDave Kleikamp put_bh(bh); 705ac27a0ecSDave Kleikamp *err = -EIO; 706ac27a0ecSDave Kleikamp return NULL; 707ac27a0ecSDave Kleikamp } 708ac27a0ecSDave Kleikamp 709ac27a0ecSDave Kleikamp static int walk_page_buffers(handle_t *handle, 710ac27a0ecSDave Kleikamp struct buffer_head *head, 711ac27a0ecSDave Kleikamp unsigned from, 712ac27a0ecSDave Kleikamp unsigned to, 713ac27a0ecSDave Kleikamp int *partial, 714ac27a0ecSDave Kleikamp int (*fn)(handle_t *handle, 715ac27a0ecSDave Kleikamp struct buffer_head *bh)) 716ac27a0ecSDave Kleikamp { 717ac27a0ecSDave Kleikamp struct buffer_head *bh; 718ac27a0ecSDave Kleikamp unsigned block_start, block_end; 719ac27a0ecSDave Kleikamp unsigned blocksize = head->b_size; 720ac27a0ecSDave Kleikamp int err, ret = 0; 721ac27a0ecSDave Kleikamp struct buffer_head *next; 722ac27a0ecSDave Kleikamp 723ac27a0ecSDave Kleikamp for (bh = head, block_start = 0; 724ac27a0ecSDave Kleikamp ret == 0 && (bh != head || !block_start); 725de9a55b8STheodore Ts'o block_start = block_end, bh = next) { 726ac27a0ecSDave Kleikamp next = bh->b_this_page; 727ac27a0ecSDave Kleikamp block_end = block_start + blocksize; 728ac27a0ecSDave Kleikamp if (block_end <= from || block_start >= to) { 729ac27a0ecSDave Kleikamp if (partial && !buffer_uptodate(bh)) 730ac27a0ecSDave Kleikamp *partial = 1; 731ac27a0ecSDave Kleikamp continue; 732ac27a0ecSDave Kleikamp } 733ac27a0ecSDave Kleikamp err = (*fn)(handle, bh); 734ac27a0ecSDave Kleikamp if (!ret) 735ac27a0ecSDave Kleikamp ret = err; 736ac27a0ecSDave Kleikamp } 737ac27a0ecSDave Kleikamp return ret; 738ac27a0ecSDave Kleikamp } 739ac27a0ecSDave Kleikamp 740ac27a0ecSDave Kleikamp /* 741ac27a0ecSDave Kleikamp * To preserve ordering, it is essential that the hole instantiation and 742ac27a0ecSDave Kleikamp * the data write be encapsulated in a single transaction. We cannot 743617ba13bSMingming Cao * close off a transaction and start a new one between the ext4_get_block() 744dab291afSMingming Cao * and the commit_write(). So doing the jbd2_journal_start at the start of 745ac27a0ecSDave Kleikamp * prepare_write() is the right place. 746ac27a0ecSDave Kleikamp * 747617ba13bSMingming Cao * Also, this function can nest inside ext4_writepage() -> 748617ba13bSMingming Cao * block_write_full_page(). In that case, we *know* that ext4_writepage() 749ac27a0ecSDave Kleikamp * has generated enough buffer credits to do the whole page. So we won't 750ac27a0ecSDave Kleikamp * block on the journal in that case, which is good, because the caller may 751ac27a0ecSDave Kleikamp * be PF_MEMALLOC. 752ac27a0ecSDave Kleikamp * 753617ba13bSMingming Cao * By accident, ext4 can be reentered when a transaction is open via 754ac27a0ecSDave Kleikamp * quota file writes. If we were to commit the transaction while thus 755ac27a0ecSDave Kleikamp * reentered, there can be a deadlock - we would be holding a quota 756ac27a0ecSDave Kleikamp * lock, and the commit would never complete if another thread had a 757ac27a0ecSDave Kleikamp * transaction open and was blocking on the quota lock - a ranking 758ac27a0ecSDave Kleikamp * violation. 759ac27a0ecSDave Kleikamp * 760dab291afSMingming Cao * So what we do is to rely on the fact that jbd2_journal_stop/journal_start 761ac27a0ecSDave Kleikamp * will _not_ run commit under these circumstances because handle->h_ref 762ac27a0ecSDave Kleikamp * is elevated. We'll still have enough credits for the tiny quotafile 763ac27a0ecSDave Kleikamp * write. 764ac27a0ecSDave Kleikamp */ 765ac27a0ecSDave Kleikamp static int do_journal_get_write_access(handle_t *handle, 766ac27a0ecSDave Kleikamp struct buffer_head *bh) 767ac27a0ecSDave Kleikamp { 76856d35a4cSJan Kara int dirty = buffer_dirty(bh); 76956d35a4cSJan Kara int ret; 77056d35a4cSJan Kara 771ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 772ac27a0ecSDave Kleikamp return 0; 77356d35a4cSJan Kara /* 774ebdec241SChristoph Hellwig * __block_write_begin() could have dirtied some buffers. Clean 77556d35a4cSJan Kara * the dirty bit as jbd2_journal_get_write_access() could complain 77656d35a4cSJan Kara * otherwise about fs integrity issues. Setting of the dirty bit 777ebdec241SChristoph Hellwig * by __block_write_begin() isn't a real problem here as we clear 77856d35a4cSJan Kara * the bit before releasing a page lock and thus writeback cannot 77956d35a4cSJan Kara * ever write the buffer. 78056d35a4cSJan Kara */ 78156d35a4cSJan Kara if (dirty) 78256d35a4cSJan Kara clear_buffer_dirty(bh); 78356d35a4cSJan Kara ret = ext4_journal_get_write_access(handle, bh); 78456d35a4cSJan Kara if (!ret && dirty) 78556d35a4cSJan Kara ret = ext4_handle_dirty_metadata(handle, NULL, bh); 78656d35a4cSJan Kara return ret; 787ac27a0ecSDave Kleikamp } 788ac27a0ecSDave Kleikamp 789744692dcSJiaying Zhang static int ext4_get_block_write(struct inode *inode, sector_t iblock, 790744692dcSJiaying Zhang struct buffer_head *bh_result, int create); 791bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 792bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned flags, 793bfc1af65SNick Piggin struct page **pagep, void **fsdata) 794ac27a0ecSDave Kleikamp { 795bfc1af65SNick Piggin struct inode *inode = mapping->host; 7961938a150SAneesh Kumar K.V int ret, needed_blocks; 797ac27a0ecSDave Kleikamp handle_t *handle; 798ac27a0ecSDave Kleikamp int retries = 0; 799bfc1af65SNick Piggin struct page *page; 800bfc1af65SNick Piggin pgoff_t index; 801bfc1af65SNick Piggin unsigned from, to; 802bfc1af65SNick Piggin 8039bffad1eSTheodore Ts'o trace_ext4_write_begin(inode, pos, len, flags); 8041938a150SAneesh Kumar K.V /* 8051938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 8061938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 8071938a150SAneesh Kumar K.V */ 8081938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 809bfc1af65SNick Piggin index = pos >> PAGE_CACHE_SHIFT; 810bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 811bfc1af65SNick Piggin to = from + len; 812ac27a0ecSDave Kleikamp 813ac27a0ecSDave Kleikamp retry: 814617ba13bSMingming Cao handle = ext4_journal_start(inode, needed_blocks); 8157479d2b9SAndrew Morton if (IS_ERR(handle)) { 8167479d2b9SAndrew Morton ret = PTR_ERR(handle); 8177479d2b9SAndrew Morton goto out; 8187479d2b9SAndrew Morton } 819ac27a0ecSDave Kleikamp 820ebd3610bSJan Kara /* We cannot recurse into the filesystem as the transaction is already 821ebd3610bSJan Kara * started */ 822ebd3610bSJan Kara flags |= AOP_FLAG_NOFS; 823ebd3610bSJan Kara 82454566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 825cf108bcaSJan Kara if (!page) { 826cf108bcaSJan Kara ext4_journal_stop(handle); 827cf108bcaSJan Kara ret = -ENOMEM; 828cf108bcaSJan Kara goto out; 829cf108bcaSJan Kara } 830cf108bcaSJan Kara *pagep = page; 831cf108bcaSJan Kara 832744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 8336e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block_write); 834744692dcSJiaying Zhang else 8356e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block); 836bfc1af65SNick Piggin 837bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 838ac27a0ecSDave Kleikamp ret = walk_page_buffers(handle, page_buffers(page), 839ac27a0ecSDave Kleikamp from, to, NULL, do_journal_get_write_access); 840b46be050SAndrey Savochkin } 841bfc1af65SNick Piggin 842bfc1af65SNick Piggin if (ret) { 843bfc1af65SNick Piggin unlock_page(page); 844bfc1af65SNick Piggin page_cache_release(page); 845ae4d5372SAneesh Kumar K.V /* 8466e1db88dSChristoph Hellwig * __block_write_begin may have instantiated a few blocks 847ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 848ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 8491938a150SAneesh Kumar K.V * 8501938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 8511938a150SAneesh Kumar K.V * truncate finishes 852ae4d5372SAneesh Kumar K.V */ 853ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 8541938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 8551938a150SAneesh Kumar K.V 8561938a150SAneesh Kumar K.V ext4_journal_stop(handle); 8571938a150SAneesh Kumar K.V if (pos + len > inode->i_size) { 858b9a4207dSJan Kara ext4_truncate_failed_write(inode); 8591938a150SAneesh Kumar K.V /* 860ffacfa7aSJan Kara * If truncate failed early the inode might 8611938a150SAneesh Kumar K.V * still be on the orphan list; we need to 8621938a150SAneesh Kumar K.V * make sure the inode is removed from the 8631938a150SAneesh Kumar K.V * orphan list in that case. 8641938a150SAneesh Kumar K.V */ 8651938a150SAneesh Kumar K.V if (inode->i_nlink) 8661938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 8671938a150SAneesh Kumar K.V } 868bfc1af65SNick Piggin } 869bfc1af65SNick Piggin 870617ba13bSMingming Cao if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 871ac27a0ecSDave Kleikamp goto retry; 8727479d2b9SAndrew Morton out: 873ac27a0ecSDave Kleikamp return ret; 874ac27a0ecSDave Kleikamp } 875ac27a0ecSDave Kleikamp 876bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 877bfc1af65SNick Piggin static int write_end_fn(handle_t *handle, struct buffer_head *bh) 878ac27a0ecSDave Kleikamp { 879ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 880ac27a0ecSDave Kleikamp return 0; 881ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 8820390131bSFrank Mayhar return ext4_handle_dirty_metadata(handle, NULL, bh); 883ac27a0ecSDave Kleikamp } 884ac27a0ecSDave Kleikamp 885f8514083SAneesh Kumar K.V static int ext4_generic_write_end(struct file *file, 886f8514083SAneesh Kumar K.V struct address_space *mapping, 887f8514083SAneesh Kumar K.V loff_t pos, unsigned len, unsigned copied, 888f8514083SAneesh Kumar K.V struct page *page, void *fsdata) 889f8514083SAneesh Kumar K.V { 890f8514083SAneesh Kumar K.V int i_size_changed = 0; 891f8514083SAneesh Kumar K.V struct inode *inode = mapping->host; 892f8514083SAneesh Kumar K.V handle_t *handle = ext4_journal_current_handle(); 893f8514083SAneesh Kumar K.V 894f8514083SAneesh Kumar K.V copied = block_write_end(file, mapping, pos, len, copied, page, fsdata); 895f8514083SAneesh Kumar K.V 896f8514083SAneesh Kumar K.V /* 897f8514083SAneesh Kumar K.V * No need to use i_size_read() here, the i_size 898f8514083SAneesh Kumar K.V * cannot change under us because we hold i_mutex. 899f8514083SAneesh Kumar K.V * 900f8514083SAneesh Kumar K.V * But it's important to update i_size while still holding page lock: 901f8514083SAneesh Kumar K.V * page writeout could otherwise come in and zero beyond i_size. 902f8514083SAneesh Kumar K.V */ 903f8514083SAneesh Kumar K.V if (pos + copied > inode->i_size) { 904f8514083SAneesh Kumar K.V i_size_write(inode, pos + copied); 905f8514083SAneesh Kumar K.V i_size_changed = 1; 906f8514083SAneesh Kumar K.V } 907f8514083SAneesh Kumar K.V 908f8514083SAneesh Kumar K.V if (pos + copied > EXT4_I(inode)->i_disksize) { 909f8514083SAneesh Kumar K.V /* We need to mark inode dirty even if 910f8514083SAneesh Kumar K.V * new_i_size is less that inode->i_size 911f8514083SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 912f8514083SAneesh Kumar K.V */ 913f8514083SAneesh Kumar K.V ext4_update_i_disksize(inode, (pos + copied)); 914f8514083SAneesh Kumar K.V i_size_changed = 1; 915f8514083SAneesh Kumar K.V } 916f8514083SAneesh Kumar K.V unlock_page(page); 917f8514083SAneesh Kumar K.V page_cache_release(page); 918f8514083SAneesh Kumar K.V 919f8514083SAneesh Kumar K.V /* 920f8514083SAneesh Kumar K.V * Don't mark the inode dirty under page lock. First, it unnecessarily 921f8514083SAneesh Kumar K.V * makes the holding time of page lock longer. Second, it forces lock 922f8514083SAneesh Kumar K.V * ordering of page lock and transaction start for journaling 923f8514083SAneesh Kumar K.V * filesystems. 924f8514083SAneesh Kumar K.V */ 925f8514083SAneesh Kumar K.V if (i_size_changed) 926f8514083SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 927f8514083SAneesh Kumar K.V 928f8514083SAneesh Kumar K.V return copied; 929f8514083SAneesh Kumar K.V } 930f8514083SAneesh Kumar K.V 931ac27a0ecSDave Kleikamp /* 932ac27a0ecSDave Kleikamp * We need to pick up the new inode size which generic_commit_write gave us 933ac27a0ecSDave Kleikamp * `file' can be NULL - eg, when called from page_symlink(). 934ac27a0ecSDave Kleikamp * 935617ba13bSMingming Cao * ext4 never places buffers on inode->i_mapping->private_list. metadata 936ac27a0ecSDave Kleikamp * buffers are managed internally. 937ac27a0ecSDave Kleikamp */ 938bfc1af65SNick Piggin static int ext4_ordered_write_end(struct file *file, 939bfc1af65SNick Piggin struct address_space *mapping, 940bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 941bfc1af65SNick Piggin struct page *page, void *fsdata) 942ac27a0ecSDave Kleikamp { 943617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 944cf108bcaSJan Kara struct inode *inode = mapping->host; 945ac27a0ecSDave Kleikamp int ret = 0, ret2; 946ac27a0ecSDave Kleikamp 9479bffad1eSTheodore Ts'o trace_ext4_ordered_write_end(inode, pos, len, copied); 948678aaf48SJan Kara ret = ext4_jbd2_file_inode(handle, inode); 949ac27a0ecSDave Kleikamp 950ac27a0ecSDave Kleikamp if (ret == 0) { 951f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 952bfc1af65SNick Piggin page, fsdata); 953f8a87d89SRoel Kluin copied = ret2; 954ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 955f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 956f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 957f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 958f8514083SAneesh Kumar K.V */ 959f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 960f8a87d89SRoel Kluin if (ret2 < 0) 961f8a87d89SRoel Kluin ret = ret2; 962*09e0834fSAkira Fujita } else { 963*09e0834fSAkira Fujita unlock_page(page); 964*09e0834fSAkira Fujita page_cache_release(page); 965ac27a0ecSDave Kleikamp } 966*09e0834fSAkira Fujita 967617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 968ac27a0ecSDave Kleikamp if (!ret) 969ac27a0ecSDave Kleikamp ret = ret2; 970bfc1af65SNick Piggin 971f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 972b9a4207dSJan Kara ext4_truncate_failed_write(inode); 973f8514083SAneesh Kumar K.V /* 974ffacfa7aSJan Kara * If truncate failed early the inode might still be 975f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 976f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 977f8514083SAneesh Kumar K.V */ 978f8514083SAneesh Kumar K.V if (inode->i_nlink) 979f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 980f8514083SAneesh Kumar K.V } 981f8514083SAneesh Kumar K.V 982f8514083SAneesh Kumar K.V 983bfc1af65SNick Piggin return ret ? ret : copied; 984ac27a0ecSDave Kleikamp } 985ac27a0ecSDave Kleikamp 986bfc1af65SNick Piggin static int ext4_writeback_write_end(struct file *file, 987bfc1af65SNick Piggin struct address_space *mapping, 988bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 989bfc1af65SNick Piggin struct page *page, void *fsdata) 990ac27a0ecSDave Kleikamp { 991617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 992cf108bcaSJan Kara struct inode *inode = mapping->host; 993ac27a0ecSDave Kleikamp int ret = 0, ret2; 994ac27a0ecSDave Kleikamp 9959bffad1eSTheodore Ts'o trace_ext4_writeback_write_end(inode, pos, len, copied); 996f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 997bfc1af65SNick Piggin page, fsdata); 998f8a87d89SRoel Kluin copied = ret2; 999ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1000f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1001f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1002f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1003f8514083SAneesh Kumar K.V */ 1004f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1005f8514083SAneesh Kumar K.V 1006f8a87d89SRoel Kluin if (ret2 < 0) 1007f8a87d89SRoel Kluin ret = ret2; 1008ac27a0ecSDave Kleikamp 1009617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1010ac27a0ecSDave Kleikamp if (!ret) 1011ac27a0ecSDave Kleikamp ret = ret2; 1012bfc1af65SNick Piggin 1013f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1014b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1015f8514083SAneesh Kumar K.V /* 1016ffacfa7aSJan Kara * If truncate failed early the inode might still be 1017f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1018f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1019f8514083SAneesh Kumar K.V */ 1020f8514083SAneesh Kumar K.V if (inode->i_nlink) 1021f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1022f8514083SAneesh Kumar K.V } 1023f8514083SAneesh Kumar K.V 1024bfc1af65SNick Piggin return ret ? ret : copied; 1025ac27a0ecSDave Kleikamp } 1026ac27a0ecSDave Kleikamp 1027bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 1028bfc1af65SNick Piggin struct address_space *mapping, 1029bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1030bfc1af65SNick Piggin struct page *page, void *fsdata) 1031ac27a0ecSDave Kleikamp { 1032617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1033bfc1af65SNick Piggin struct inode *inode = mapping->host; 1034ac27a0ecSDave Kleikamp int ret = 0, ret2; 1035ac27a0ecSDave Kleikamp int partial = 0; 1036bfc1af65SNick Piggin unsigned from, to; 1037cf17fea6SAneesh Kumar K.V loff_t new_i_size; 1038ac27a0ecSDave Kleikamp 10399bffad1eSTheodore Ts'o trace_ext4_journalled_write_end(inode, pos, len, copied); 1040bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 1041bfc1af65SNick Piggin to = from + len; 1042bfc1af65SNick Piggin 1043441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1044441c8508SCurt Wohlgemuth 1045bfc1af65SNick Piggin if (copied < len) { 1046bfc1af65SNick Piggin if (!PageUptodate(page)) 1047bfc1af65SNick Piggin copied = 0; 1048bfc1af65SNick Piggin page_zero_new_buffers(page, from+copied, to); 1049bfc1af65SNick Piggin } 1050ac27a0ecSDave Kleikamp 1051ac27a0ecSDave Kleikamp ret = walk_page_buffers(handle, page_buffers(page), from, 1052bfc1af65SNick Piggin to, &partial, write_end_fn); 1053ac27a0ecSDave Kleikamp if (!partial) 1054ac27a0ecSDave Kleikamp SetPageUptodate(page); 1055cf17fea6SAneesh Kumar K.V new_i_size = pos + copied; 1056cf17fea6SAneesh Kumar K.V if (new_i_size > inode->i_size) 1057bfc1af65SNick Piggin i_size_write(inode, pos+copied); 105819f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 10592d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 1060cf17fea6SAneesh Kumar K.V if (new_i_size > EXT4_I(inode)->i_disksize) { 1061cf17fea6SAneesh Kumar K.V ext4_update_i_disksize(inode, new_i_size); 1062617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1063ac27a0ecSDave Kleikamp if (!ret) 1064ac27a0ecSDave Kleikamp ret = ret2; 1065ac27a0ecSDave Kleikamp } 1066bfc1af65SNick Piggin 1067cf108bcaSJan Kara unlock_page(page); 1068f8514083SAneesh Kumar K.V page_cache_release(page); 1069ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1070f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1071f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1072f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1073f8514083SAneesh Kumar K.V */ 1074f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1075f8514083SAneesh Kumar K.V 1076617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1077ac27a0ecSDave Kleikamp if (!ret) 1078ac27a0ecSDave Kleikamp ret = ret2; 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 } 1089bfc1af65SNick Piggin 1090bfc1af65SNick Piggin return ret ? ret : copied; 1091ac27a0ecSDave Kleikamp } 1092d2a17637SMingming Cao 10939d0be502STheodore Ts'o /* 10947b415bf6SAditya Kali * Reserve a single cluster located at lblock 10959d0be502STheodore Ts'o */ 10965356f261SAditya Kali static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock) 1097d2a17637SMingming Cao { 1098030ba6bcSAneesh Kumar K.V int retries = 0; 1099d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 11000637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 11017b415bf6SAditya Kali unsigned int md_needed; 11025dd4056dSChristoph Hellwig int ret; 1103d2a17637SMingming Cao 1104d2a17637SMingming Cao /* 1105d2a17637SMingming Cao * recalculate the amount of metadata blocks to reserve 1106d2a17637SMingming Cao * in order to allocate nrblocks 1107d2a17637SMingming Cao * worse case is one extent per block 1108d2a17637SMingming Cao */ 1109030ba6bcSAneesh Kumar K.V repeat: 11100637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 11117b415bf6SAditya Kali md_needed = EXT4_NUM_B2C(sbi, 11127b415bf6SAditya Kali ext4_calc_metadata_amount(inode, lblock)); 1113f8ec9d68STheodore Ts'o trace_ext4_da_reserve_space(inode, md_needed); 11140637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 1115d2a17637SMingming Cao 111660e58e0fSMingming Cao /* 111772b8ab9dSEric Sandeen * We will charge metadata quota at writeout time; this saves 111872b8ab9dSEric Sandeen * us from metadata over-estimation, though we may go over by 111972b8ab9dSEric Sandeen * a small amount in the end. Here we just reserve for data. 112060e58e0fSMingming Cao */ 11217b415bf6SAditya Kali ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1)); 11225dd4056dSChristoph Hellwig if (ret) 11235dd4056dSChristoph Hellwig return ret; 112472b8ab9dSEric Sandeen /* 112572b8ab9dSEric Sandeen * We do still charge estimated metadata to the sb though; 112672b8ab9dSEric Sandeen * we cannot afford to run out of free blocks. 112772b8ab9dSEric Sandeen */ 1128e7d5f315STheodore Ts'o if (ext4_claim_free_clusters(sbi, md_needed + 1, 0)) { 11297b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1)); 1130030ba6bcSAneesh Kumar K.V if (ext4_should_retry_alloc(inode->i_sb, &retries)) { 1131030ba6bcSAneesh Kumar K.V yield(); 1132030ba6bcSAneesh Kumar K.V goto repeat; 1133030ba6bcSAneesh Kumar K.V } 1134d2a17637SMingming Cao return -ENOSPC; 1135d2a17637SMingming Cao } 11360637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 11379d0be502STheodore Ts'o ei->i_reserved_data_blocks++; 11380637c6f4STheodore Ts'o ei->i_reserved_meta_blocks += md_needed; 11390637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 114039bc680aSDmitry Monakhov 1141d2a17637SMingming Cao return 0; /* success */ 1142d2a17637SMingming Cao } 1143d2a17637SMingming Cao 114412219aeaSAneesh Kumar K.V static void ext4_da_release_space(struct inode *inode, int to_free) 1145d2a17637SMingming Cao { 1146d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 11470637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 1148d2a17637SMingming Cao 1149cd213226SMingming Cao if (!to_free) 1150cd213226SMingming Cao return; /* Nothing to release, exit */ 1151cd213226SMingming Cao 1152d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1153cd213226SMingming Cao 11545a58ec87SLi Zefan trace_ext4_da_release_space(inode, to_free); 11550637c6f4STheodore Ts'o if (unlikely(to_free > ei->i_reserved_data_blocks)) { 1156cd213226SMingming Cao /* 11570637c6f4STheodore Ts'o * if there aren't enough reserved blocks, then the 11580637c6f4STheodore Ts'o * counter is messed up somewhere. Since this 11590637c6f4STheodore Ts'o * function is called from invalidate page, it's 11600637c6f4STheodore Ts'o * harmless to return without any action. 1161cd213226SMingming Cao */ 11620637c6f4STheodore Ts'o ext4_msg(inode->i_sb, KERN_NOTICE, "ext4_da_release_space: " 11630637c6f4STheodore Ts'o "ino %lu, to_free %d with only %d reserved " 11640637c6f4STheodore Ts'o "data blocks\n", inode->i_ino, to_free, 11650637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 11660637c6f4STheodore Ts'o WARN_ON(1); 11670637c6f4STheodore Ts'o to_free = ei->i_reserved_data_blocks; 11680637c6f4STheodore Ts'o } 11690637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= to_free; 11700637c6f4STheodore Ts'o 11710637c6f4STheodore Ts'o if (ei->i_reserved_data_blocks == 0) { 11720637c6f4STheodore Ts'o /* 11730637c6f4STheodore Ts'o * We can release all of the reserved metadata blocks 11740637c6f4STheodore Ts'o * only when we have written all of the delayed 11750637c6f4STheodore Ts'o * allocation blocks. 11767b415bf6SAditya Kali * Note that in case of bigalloc, i_reserved_meta_blocks, 11777b415bf6SAditya Kali * i_reserved_data_blocks, etc. refer to number of clusters. 11780637c6f4STheodore Ts'o */ 117957042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 118072b8ab9dSEric Sandeen ei->i_reserved_meta_blocks); 1181ee5f4d9cSTheodore Ts'o ei->i_reserved_meta_blocks = 0; 11829d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 1183cd213226SMingming Cao } 1184cd213226SMingming Cao 118572b8ab9dSEric Sandeen /* update fs dirty data blocks counter */ 118657042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free); 1187d2a17637SMingming Cao 1188d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 118960e58e0fSMingming Cao 11907b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free)); 1191d2a17637SMingming Cao } 1192d2a17637SMingming Cao 1193d2a17637SMingming Cao static void ext4_da_page_release_reservation(struct page *page, 1194d2a17637SMingming Cao unsigned long offset) 1195d2a17637SMingming Cao { 1196d2a17637SMingming Cao int to_release = 0; 1197d2a17637SMingming Cao struct buffer_head *head, *bh; 1198d2a17637SMingming Cao unsigned int curr_off = 0; 11997b415bf6SAditya Kali struct inode *inode = page->mapping->host; 12007b415bf6SAditya Kali struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 12017b415bf6SAditya Kali int num_clusters; 1202d2a17637SMingming Cao 1203d2a17637SMingming Cao head = page_buffers(page); 1204d2a17637SMingming Cao bh = head; 1205d2a17637SMingming Cao do { 1206d2a17637SMingming Cao unsigned int next_off = curr_off + bh->b_size; 1207d2a17637SMingming Cao 1208d2a17637SMingming Cao if ((offset <= curr_off) && (buffer_delay(bh))) { 1209d2a17637SMingming Cao to_release++; 1210d2a17637SMingming Cao clear_buffer_delay(bh); 12115356f261SAditya Kali clear_buffer_da_mapped(bh); 1212d2a17637SMingming Cao } 1213d2a17637SMingming Cao curr_off = next_off; 1214d2a17637SMingming Cao } while ((bh = bh->b_this_page) != head); 12157b415bf6SAditya Kali 12167b415bf6SAditya Kali /* If we have released all the blocks belonging to a cluster, then we 12177b415bf6SAditya Kali * need to release the reserved space for that cluster. */ 12187b415bf6SAditya Kali num_clusters = EXT4_NUM_B2C(sbi, to_release); 12197b415bf6SAditya Kali while (num_clusters > 0) { 12207b415bf6SAditya Kali ext4_fsblk_t lblk; 12217b415bf6SAditya Kali lblk = (page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits)) + 12227b415bf6SAditya Kali ((num_clusters - 1) << sbi->s_cluster_bits); 12237b415bf6SAditya Kali if (sbi->s_cluster_ratio == 1 || 12247b415bf6SAditya Kali !ext4_find_delalloc_cluster(inode, lblk, 1)) 12257b415bf6SAditya Kali ext4_da_release_space(inode, 1); 12267b415bf6SAditya Kali 12277b415bf6SAditya Kali num_clusters--; 12287b415bf6SAditya Kali } 1229d2a17637SMingming Cao } 1230ac27a0ecSDave Kleikamp 1231ac27a0ecSDave Kleikamp /* 123264769240SAlex Tomas * Delayed allocation stuff 123364769240SAlex Tomas */ 123464769240SAlex Tomas 123564769240SAlex Tomas /* 123664769240SAlex Tomas * mpage_da_submit_io - walks through extent of pages and try to write 1237a1d6cc56SAneesh Kumar K.V * them with writepage() call back 123864769240SAlex Tomas * 123964769240SAlex Tomas * @mpd->inode: inode 124064769240SAlex Tomas * @mpd->first_page: first page of the extent 124164769240SAlex Tomas * @mpd->next_page: page after the last page of the extent 124264769240SAlex Tomas * 124364769240SAlex Tomas * By the time mpage_da_submit_io() is called we expect all blocks 124464769240SAlex Tomas * to be allocated. this may be wrong if allocation failed. 124564769240SAlex Tomas * 124664769240SAlex Tomas * As pages are already locked by write_cache_pages(), we can't use it 124764769240SAlex Tomas */ 12481de3e3dfSTheodore Ts'o static int mpage_da_submit_io(struct mpage_da_data *mpd, 12491de3e3dfSTheodore Ts'o struct ext4_map_blocks *map) 125064769240SAlex Tomas { 1251791b7f08SAneesh Kumar K.V struct pagevec pvec; 1252791b7f08SAneesh Kumar K.V unsigned long index, end; 1253791b7f08SAneesh Kumar K.V int ret = 0, err, nr_pages, i; 1254791b7f08SAneesh Kumar K.V struct inode *inode = mpd->inode; 1255791b7f08SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 1256cb20d518STheodore Ts'o loff_t size = i_size_read(inode); 12573ecdb3a1STheodore Ts'o unsigned int len, block_start; 12583ecdb3a1STheodore Ts'o struct buffer_head *bh, *page_bufs = NULL; 1259cb20d518STheodore Ts'o int journal_data = ext4_should_journal_data(inode); 12601de3e3dfSTheodore Ts'o sector_t pblock = 0, cur_logical = 0; 1261bd2d0210STheodore Ts'o struct ext4_io_submit io_submit; 126264769240SAlex Tomas 126364769240SAlex Tomas BUG_ON(mpd->next_page <= mpd->first_page); 1264bd2d0210STheodore Ts'o memset(&io_submit, 0, sizeof(io_submit)); 1265791b7f08SAneesh Kumar K.V /* 1266791b7f08SAneesh Kumar K.V * We need to start from the first_page to the next_page - 1 1267791b7f08SAneesh Kumar K.V * to make sure we also write the mapped dirty buffer_heads. 12688dc207c0STheodore Ts'o * If we look at mpd->b_blocknr we would only be looking 1269791b7f08SAneesh Kumar K.V * at the currently mapped buffer_heads. 1270791b7f08SAneesh Kumar K.V */ 127164769240SAlex Tomas index = mpd->first_page; 127264769240SAlex Tomas end = mpd->next_page - 1; 127364769240SAlex Tomas 1274791b7f08SAneesh Kumar K.V pagevec_init(&pvec, 0); 127564769240SAlex Tomas while (index <= end) { 1276791b7f08SAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 127764769240SAlex Tomas if (nr_pages == 0) 127864769240SAlex Tomas break; 127964769240SAlex Tomas for (i = 0; i < nr_pages; i++) { 128097498956STheodore Ts'o int commit_write = 0, skip_page = 0; 128164769240SAlex Tomas struct page *page = pvec.pages[i]; 128264769240SAlex Tomas 1283791b7f08SAneesh Kumar K.V index = page->index; 1284791b7f08SAneesh Kumar K.V if (index > end) 1285791b7f08SAneesh Kumar K.V break; 1286cb20d518STheodore Ts'o 1287cb20d518STheodore Ts'o if (index == size >> PAGE_CACHE_SHIFT) 1288cb20d518STheodore Ts'o len = size & ~PAGE_CACHE_MASK; 1289cb20d518STheodore Ts'o else 1290cb20d518STheodore Ts'o len = PAGE_CACHE_SIZE; 12911de3e3dfSTheodore Ts'o if (map) { 12921de3e3dfSTheodore Ts'o cur_logical = index << (PAGE_CACHE_SHIFT - 12931de3e3dfSTheodore Ts'o inode->i_blkbits); 12941de3e3dfSTheodore Ts'o pblock = map->m_pblk + (cur_logical - 12951de3e3dfSTheodore Ts'o map->m_lblk); 12961de3e3dfSTheodore Ts'o } 1297791b7f08SAneesh Kumar K.V index++; 1298791b7f08SAneesh Kumar K.V 1299791b7f08SAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1300791b7f08SAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1301791b7f08SAneesh Kumar K.V 130222208dedSAneesh Kumar K.V /* 1303cb20d518STheodore Ts'o * If the page does not have buffers (for 1304cb20d518STheodore Ts'o * whatever reason), try to create them using 1305a107e5a3STheodore Ts'o * __block_write_begin. If this fails, 130697498956STheodore Ts'o * skip the page and move on. 130722208dedSAneesh Kumar K.V */ 1308cb20d518STheodore Ts'o if (!page_has_buffers(page)) { 1309a107e5a3STheodore Ts'o if (__block_write_begin(page, 0, len, 1310cb20d518STheodore Ts'o noalloc_get_block_write)) { 131197498956STheodore Ts'o skip_page: 1312cb20d518STheodore Ts'o unlock_page(page); 1313cb20d518STheodore Ts'o continue; 1314cb20d518STheodore Ts'o } 1315cb20d518STheodore Ts'o commit_write = 1; 1316cb20d518STheodore Ts'o } 13173ecdb3a1STheodore Ts'o 13183ecdb3a1STheodore Ts'o bh = page_bufs = page_buffers(page); 13193ecdb3a1STheodore Ts'o block_start = 0; 13203ecdb3a1STheodore Ts'o do { 13211de3e3dfSTheodore Ts'o if (!bh) 132297498956STheodore Ts'o goto skip_page; 13231de3e3dfSTheodore Ts'o if (map && (cur_logical >= map->m_lblk) && 13241de3e3dfSTheodore Ts'o (cur_logical <= (map->m_lblk + 13251de3e3dfSTheodore Ts'o (map->m_len - 1)))) { 13261de3e3dfSTheodore Ts'o if (buffer_delay(bh)) { 13271de3e3dfSTheodore Ts'o clear_buffer_delay(bh); 13281de3e3dfSTheodore Ts'o bh->b_blocknr = pblock; 13291de3e3dfSTheodore Ts'o } 13305356f261SAditya Kali if (buffer_da_mapped(bh)) 13315356f261SAditya Kali clear_buffer_da_mapped(bh); 13321de3e3dfSTheodore Ts'o if (buffer_unwritten(bh) || 13331de3e3dfSTheodore Ts'o buffer_mapped(bh)) 13341de3e3dfSTheodore Ts'o BUG_ON(bh->b_blocknr != pblock); 13351de3e3dfSTheodore Ts'o if (map->m_flags & EXT4_MAP_UNINIT) 13361de3e3dfSTheodore Ts'o set_buffer_uninit(bh); 13371de3e3dfSTheodore Ts'o clear_buffer_unwritten(bh); 13381de3e3dfSTheodore Ts'o } 13391de3e3dfSTheodore Ts'o 134097498956STheodore Ts'o /* skip page if block allocation undone */ 13411de3e3dfSTheodore Ts'o if (buffer_delay(bh) || buffer_unwritten(bh)) 134297498956STheodore Ts'o skip_page = 1; 13433ecdb3a1STheodore Ts'o bh = bh->b_this_page; 13443ecdb3a1STheodore Ts'o block_start += bh->b_size; 13451de3e3dfSTheodore Ts'o cur_logical++; 13461de3e3dfSTheodore Ts'o pblock++; 13471de3e3dfSTheodore Ts'o } while (bh != page_bufs); 13481de3e3dfSTheodore Ts'o 134997498956STheodore Ts'o if (skip_page) 135097498956STheodore Ts'o goto skip_page; 1351cb20d518STheodore Ts'o 1352cb20d518STheodore Ts'o if (commit_write) 1353cb20d518STheodore Ts'o /* mark the buffer_heads as dirty & uptodate */ 1354cb20d518STheodore Ts'o block_commit_write(page, 0, len); 1355cb20d518STheodore Ts'o 135697498956STheodore Ts'o clear_page_dirty_for_io(page); 1357bd2d0210STheodore Ts'o /* 1358bd2d0210STheodore Ts'o * Delalloc doesn't support data journalling, 1359bd2d0210STheodore Ts'o * but eventually maybe we'll lift this 1360bd2d0210STheodore Ts'o * restriction. 1361bd2d0210STheodore Ts'o */ 1362bd2d0210STheodore Ts'o if (unlikely(journal_data && PageChecked(page))) 1363cb20d518STheodore Ts'o err = __ext4_journalled_writepage(page, len); 13641449032bSTheodore Ts'o else if (test_opt(inode->i_sb, MBLK_IO_SUBMIT)) 1365bd2d0210STheodore Ts'o err = ext4_bio_write_page(&io_submit, page, 1366bd2d0210STheodore Ts'o len, mpd->wbc); 13679dd75f1fSTheodore Ts'o else if (buffer_uninit(page_bufs)) { 13689dd75f1fSTheodore Ts'o ext4_set_bh_endio(page_bufs, inode); 13699dd75f1fSTheodore Ts'o err = block_write_full_page_endio(page, 13709dd75f1fSTheodore Ts'o noalloc_get_block_write, 13719dd75f1fSTheodore Ts'o mpd->wbc, ext4_end_io_buffer_write); 13729dd75f1fSTheodore Ts'o } else 13731449032bSTheodore Ts'o err = block_write_full_page(page, 13741449032bSTheodore Ts'o noalloc_get_block_write, mpd->wbc); 1375cb20d518STheodore Ts'o 1376cb20d518STheodore Ts'o if (!err) 1377a1d6cc56SAneesh Kumar K.V mpd->pages_written++; 137864769240SAlex Tomas /* 137964769240SAlex Tomas * In error case, we have to continue because 138064769240SAlex Tomas * remaining pages are still locked 138164769240SAlex Tomas */ 138264769240SAlex Tomas if (ret == 0) 138364769240SAlex Tomas ret = err; 138464769240SAlex Tomas } 138564769240SAlex Tomas pagevec_release(&pvec); 138664769240SAlex Tomas } 1387bd2d0210STheodore Ts'o ext4_io_submit(&io_submit); 138864769240SAlex Tomas return ret; 138964769240SAlex Tomas } 139064769240SAlex Tomas 1391c7f5938aSCurt Wohlgemuth static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd) 1392c4a0c46eSAneesh Kumar K.V { 1393c4a0c46eSAneesh Kumar K.V int nr_pages, i; 1394c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 1395c4a0c46eSAneesh Kumar K.V struct pagevec pvec; 1396c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 1397c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 1398c4a0c46eSAneesh Kumar K.V 1399c7f5938aSCurt Wohlgemuth index = mpd->first_page; 1400c7f5938aSCurt Wohlgemuth end = mpd->next_page - 1; 1401c4a0c46eSAneesh Kumar K.V while (index <= end) { 1402c4a0c46eSAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 1403c4a0c46eSAneesh Kumar K.V if (nr_pages == 0) 1404c4a0c46eSAneesh Kumar K.V break; 1405c4a0c46eSAneesh Kumar K.V for (i = 0; i < nr_pages; i++) { 1406c4a0c46eSAneesh Kumar K.V struct page *page = pvec.pages[i]; 14079b1d0998SJan Kara if (page->index > end) 1408c4a0c46eSAneesh Kumar K.V break; 1409c4a0c46eSAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1410c4a0c46eSAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1411c4a0c46eSAneesh Kumar K.V block_invalidatepage(page, 0); 1412c4a0c46eSAneesh Kumar K.V ClearPageUptodate(page); 1413c4a0c46eSAneesh Kumar K.V unlock_page(page); 1414c4a0c46eSAneesh Kumar K.V } 14159b1d0998SJan Kara index = pvec.pages[nr_pages - 1]->index + 1; 14169b1d0998SJan Kara pagevec_release(&pvec); 1417c4a0c46eSAneesh Kumar K.V } 1418c4a0c46eSAneesh Kumar K.V return; 1419c4a0c46eSAneesh Kumar K.V } 1420c4a0c46eSAneesh Kumar K.V 1421df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 1422df22291fSAneesh Kumar K.V { 1423df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 14241693918eSTheodore Ts'o printk(KERN_CRIT "Total free blocks count %lld\n", 14255dee5437STheodore Ts'o EXT4_C2B(EXT4_SB(inode->i_sb), 14265dee5437STheodore Ts'o ext4_count_free_clusters(inode->i_sb))); 14271693918eSTheodore Ts'o printk(KERN_CRIT "Free/Dirty block details\n"); 14281693918eSTheodore Ts'o printk(KERN_CRIT "free_blocks=%lld\n", 142957042651STheodore Ts'o (long long) EXT4_C2B(EXT4_SB(inode->i_sb), 143057042651STheodore Ts'o percpu_counter_sum(&sbi->s_freeclusters_counter))); 14311693918eSTheodore Ts'o printk(KERN_CRIT "dirty_blocks=%lld\n", 14327b415bf6SAditya Kali (long long) EXT4_C2B(EXT4_SB(inode->i_sb), 14337b415bf6SAditya Kali percpu_counter_sum(&sbi->s_dirtyclusters_counter))); 14341693918eSTheodore Ts'o printk(KERN_CRIT "Block reservation details\n"); 14351693918eSTheodore Ts'o printk(KERN_CRIT "i_reserved_data_blocks=%u\n", 1436df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_data_blocks); 14371693918eSTheodore Ts'o printk(KERN_CRIT "i_reserved_meta_blocks=%u\n", 1438df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_meta_blocks); 1439df22291fSAneesh Kumar K.V return; 1440df22291fSAneesh Kumar K.V } 1441df22291fSAneesh Kumar K.V 1442b920c755STheodore Ts'o /* 14435a87b7a5STheodore Ts'o * mpage_da_map_and_submit - go through given space, map them 14445a87b7a5STheodore Ts'o * if necessary, and then submit them for I/O 144564769240SAlex Tomas * 14468dc207c0STheodore Ts'o * @mpd - bh describing space 144764769240SAlex Tomas * 144864769240SAlex Tomas * The function skips space we know is already mapped to disk blocks. 144964769240SAlex Tomas * 145064769240SAlex Tomas */ 14515a87b7a5STheodore Ts'o static void mpage_da_map_and_submit(struct mpage_da_data *mpd) 145264769240SAlex Tomas { 14532ac3b6e0STheodore Ts'o int err, blks, get_blocks_flags; 14541de3e3dfSTheodore Ts'o struct ext4_map_blocks map, *mapp = NULL; 14552fa3cdfbSTheodore Ts'o sector_t next = mpd->b_blocknr; 14562fa3cdfbSTheodore Ts'o unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits; 14572fa3cdfbSTheodore Ts'o loff_t disksize = EXT4_I(mpd->inode)->i_disksize; 14582fa3cdfbSTheodore Ts'o handle_t *handle = NULL; 145964769240SAlex Tomas 146064769240SAlex Tomas /* 14615a87b7a5STheodore Ts'o * If the blocks are mapped already, or we couldn't accumulate 14625a87b7a5STheodore Ts'o * any blocks, then proceed immediately to the submission stage. 146364769240SAlex Tomas */ 14645a87b7a5STheodore Ts'o if ((mpd->b_size == 0) || 14655a87b7a5STheodore Ts'o ((mpd->b_state & (1 << BH_Mapped)) && 146629fa89d0SAneesh Kumar K.V !(mpd->b_state & (1 << BH_Delay)) && 14675a87b7a5STheodore Ts'o !(mpd->b_state & (1 << BH_Unwritten)))) 14685a87b7a5STheodore Ts'o goto submit_io; 14692fa3cdfbSTheodore Ts'o 14702fa3cdfbSTheodore Ts'o handle = ext4_journal_current_handle(); 14712fa3cdfbSTheodore Ts'o BUG_ON(!handle); 14722fa3cdfbSTheodore Ts'o 147379ffab34SAneesh Kumar K.V /* 147479e83036SEric Sandeen * Call ext4_map_blocks() to allocate any delayed allocation 14752ac3b6e0STheodore Ts'o * blocks, or to convert an uninitialized extent to be 14762ac3b6e0STheodore Ts'o * initialized (in the case where we have written into 14772ac3b6e0STheodore Ts'o * one or more preallocated blocks). 14782ac3b6e0STheodore Ts'o * 14792ac3b6e0STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to 14802ac3b6e0STheodore Ts'o * indicate that we are on the delayed allocation path. This 14812ac3b6e0STheodore Ts'o * affects functions in many different parts of the allocation 14822ac3b6e0STheodore Ts'o * call path. This flag exists primarily because we don't 148379e83036SEric Sandeen * want to change *many* call functions, so ext4_map_blocks() 1484f2321097STheodore Ts'o * will set the EXT4_STATE_DELALLOC_RESERVED flag once the 14852ac3b6e0STheodore Ts'o * inode's allocation semaphore is taken. 14862ac3b6e0STheodore Ts'o * 14872ac3b6e0STheodore Ts'o * If the blocks in questions were delalloc blocks, set 14882ac3b6e0STheodore Ts'o * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting 14892ac3b6e0STheodore Ts'o * variables are updated after the blocks have been allocated. 149079ffab34SAneesh Kumar K.V */ 14912ed88685STheodore Ts'o map.m_lblk = next; 14922ed88685STheodore Ts'o map.m_len = max_blocks; 14931296cc85SAneesh Kumar K.V get_blocks_flags = EXT4_GET_BLOCKS_CREATE; 1494744692dcSJiaying Zhang if (ext4_should_dioread_nolock(mpd->inode)) 1495744692dcSJiaying Zhang get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; 14962ac3b6e0STheodore Ts'o if (mpd->b_state & (1 << BH_Delay)) 14971296cc85SAneesh Kumar K.V get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; 14981296cc85SAneesh Kumar K.V 14992ed88685STheodore Ts'o blks = ext4_map_blocks(handle, mpd->inode, &map, get_blocks_flags); 15002fa3cdfbSTheodore Ts'o if (blks < 0) { 1501e3570639SEric Sandeen struct super_block *sb = mpd->inode->i_sb; 1502e3570639SEric Sandeen 15032fa3cdfbSTheodore Ts'o err = blks; 1504ed5bde0bSTheodore Ts'o /* 15055a87b7a5STheodore Ts'o * If get block returns EAGAIN or ENOSPC and there 150697498956STheodore Ts'o * appears to be free blocks we will just let 150797498956STheodore Ts'o * mpage_da_submit_io() unlock all of the pages. 1508c4a0c46eSAneesh Kumar K.V */ 1509c4a0c46eSAneesh Kumar K.V if (err == -EAGAIN) 15105a87b7a5STheodore Ts'o goto submit_io; 1511df22291fSAneesh Kumar K.V 15125dee5437STheodore Ts'o if (err == -ENOSPC && ext4_count_free_clusters(sb)) { 1513df22291fSAneesh Kumar K.V mpd->retval = err; 15145a87b7a5STheodore Ts'o goto submit_io; 1515df22291fSAneesh Kumar K.V } 1516df22291fSAneesh Kumar K.V 1517c4a0c46eSAneesh Kumar K.V /* 1518ed5bde0bSTheodore Ts'o * get block failure will cause us to loop in 1519ed5bde0bSTheodore Ts'o * writepages, because a_ops->writepage won't be able 1520ed5bde0bSTheodore Ts'o * to make progress. The page will be redirtied by 1521ed5bde0bSTheodore Ts'o * writepage and writepages will again try to write 1522ed5bde0bSTheodore Ts'o * the same. 1523c4a0c46eSAneesh Kumar K.V */ 1524e3570639SEric Sandeen if (!(EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) { 1525e3570639SEric Sandeen ext4_msg(sb, KERN_CRIT, 1526e3570639SEric Sandeen "delayed block allocation failed for inode %lu " 1527e3570639SEric Sandeen "at logical offset %llu with max blocks %zd " 1528e3570639SEric Sandeen "with error %d", mpd->inode->i_ino, 1529c4a0c46eSAneesh Kumar K.V (unsigned long long) next, 15308dc207c0STheodore Ts'o mpd->b_size >> mpd->inode->i_blkbits, err); 1531e3570639SEric Sandeen ext4_msg(sb, KERN_CRIT, 1532e3570639SEric Sandeen "This should not happen!! Data will be lost\n"); 1533e3570639SEric Sandeen if (err == -ENOSPC) 1534df22291fSAneesh Kumar K.V ext4_print_free_blocks(mpd->inode); 1535030ba6bcSAneesh Kumar K.V } 15362fa3cdfbSTheodore Ts'o /* invalidate all the pages */ 1537c7f5938aSCurt Wohlgemuth ext4_da_block_invalidatepages(mpd); 1538e0fd9b90SCurt Wohlgemuth 1539e0fd9b90SCurt Wohlgemuth /* Mark this page range as having been completed */ 1540e0fd9b90SCurt Wohlgemuth mpd->io_done = 1; 15415a87b7a5STheodore Ts'o return; 1542c4a0c46eSAneesh Kumar K.V } 15432fa3cdfbSTheodore Ts'o BUG_ON(blks == 0); 15442fa3cdfbSTheodore Ts'o 15451de3e3dfSTheodore Ts'o mapp = ↦ 15462ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 15472ed88685STheodore Ts'o struct block_device *bdev = mpd->inode->i_sb->s_bdev; 15482ed88685STheodore Ts'o int i; 154964769240SAlex Tomas 15502ed88685STheodore Ts'o for (i = 0; i < map.m_len; i++) 15512ed88685STheodore Ts'o unmap_underlying_metadata(bdev, map.m_pblk + i); 155264769240SAlex Tomas 15532fa3cdfbSTheodore Ts'o if (ext4_should_order_data(mpd->inode)) { 15542fa3cdfbSTheodore Ts'o err = ext4_jbd2_file_inode(handle, mpd->inode); 15552fa3cdfbSTheodore Ts'o if (err) 1556decbd919STheodore Ts'o /* Only if the journal is aborted */ 15575a87b7a5STheodore Ts'o return; 15582fa3cdfbSTheodore Ts'o } 1559decbd919STheodore Ts'o } 15602fa3cdfbSTheodore Ts'o 15612fa3cdfbSTheodore Ts'o /* 156203f5d8bcSJan Kara * Update on-disk size along with block allocation. 15632fa3cdfbSTheodore Ts'o */ 15642fa3cdfbSTheodore Ts'o disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits; 15652fa3cdfbSTheodore Ts'o if (disksize > i_size_read(mpd->inode)) 15662fa3cdfbSTheodore Ts'o disksize = i_size_read(mpd->inode); 15672fa3cdfbSTheodore Ts'o if (disksize > EXT4_I(mpd->inode)->i_disksize) { 15682fa3cdfbSTheodore Ts'o ext4_update_i_disksize(mpd->inode, disksize); 15695a87b7a5STheodore Ts'o err = ext4_mark_inode_dirty(handle, mpd->inode); 15705a87b7a5STheodore Ts'o if (err) 15715a87b7a5STheodore Ts'o ext4_error(mpd->inode->i_sb, 15725a87b7a5STheodore Ts'o "Failed to mark inode %lu dirty", 15735a87b7a5STheodore Ts'o mpd->inode->i_ino); 15742fa3cdfbSTheodore Ts'o } 15752fa3cdfbSTheodore Ts'o 15765a87b7a5STheodore Ts'o submit_io: 15771de3e3dfSTheodore Ts'o mpage_da_submit_io(mpd, mapp); 15785a87b7a5STheodore Ts'o mpd->io_done = 1; 157964769240SAlex Tomas } 158064769240SAlex Tomas 1581bf068ee2SAneesh Kumar K.V #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \ 1582bf068ee2SAneesh Kumar K.V (1 << BH_Delay) | (1 << BH_Unwritten)) 158364769240SAlex Tomas 158464769240SAlex Tomas /* 158564769240SAlex Tomas * mpage_add_bh_to_extent - try to add one more block to extent of blocks 158664769240SAlex Tomas * 158764769240SAlex Tomas * @mpd->lbh - extent of blocks 158864769240SAlex Tomas * @logical - logical number of the block in the file 158964769240SAlex Tomas * @bh - bh of the block (used to access block's state) 159064769240SAlex Tomas * 159164769240SAlex Tomas * the function is used to collect contig. blocks in same state 159264769240SAlex Tomas */ 159364769240SAlex Tomas static void mpage_add_bh_to_extent(struct mpage_da_data *mpd, 15948dc207c0STheodore Ts'o sector_t logical, size_t b_size, 15958dc207c0STheodore Ts'o unsigned long b_state) 159664769240SAlex Tomas { 159764769240SAlex Tomas sector_t next; 15988dc207c0STheodore Ts'o int nrblocks = mpd->b_size >> mpd->inode->i_blkbits; 159964769240SAlex Tomas 1600c445e3e0SEric Sandeen /* 1601c445e3e0SEric Sandeen * XXX Don't go larger than mballoc is willing to allocate 1602c445e3e0SEric Sandeen * This is a stopgap solution. We eventually need to fold 1603c445e3e0SEric Sandeen * mpage_da_submit_io() into this function and then call 160479e83036SEric Sandeen * ext4_map_blocks() multiple times in a loop 1605c445e3e0SEric Sandeen */ 1606c445e3e0SEric Sandeen if (nrblocks >= 8*1024*1024/mpd->inode->i_sb->s_blocksize) 1607c445e3e0SEric Sandeen goto flush_it; 1608c445e3e0SEric Sandeen 1609525f4ed8SMingming Cao /* check if thereserved journal credits might overflow */ 161012e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(mpd->inode, EXT4_INODE_EXTENTS))) { 1611525f4ed8SMingming Cao if (nrblocks >= EXT4_MAX_TRANS_DATA) { 1612525f4ed8SMingming Cao /* 1613525f4ed8SMingming Cao * With non-extent format we are limited by the journal 1614525f4ed8SMingming Cao * credit available. Total credit needed to insert 1615525f4ed8SMingming Cao * nrblocks contiguous blocks is dependent on the 1616525f4ed8SMingming Cao * nrblocks. So limit nrblocks. 1617525f4ed8SMingming Cao */ 1618525f4ed8SMingming Cao goto flush_it; 1619525f4ed8SMingming Cao } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) > 1620525f4ed8SMingming Cao EXT4_MAX_TRANS_DATA) { 1621525f4ed8SMingming Cao /* 1622525f4ed8SMingming Cao * Adding the new buffer_head would make it cross the 1623525f4ed8SMingming Cao * allowed limit for which we have journal credit 1624525f4ed8SMingming Cao * reserved. So limit the new bh->b_size 1625525f4ed8SMingming Cao */ 1626525f4ed8SMingming Cao b_size = (EXT4_MAX_TRANS_DATA - nrblocks) << 1627525f4ed8SMingming Cao mpd->inode->i_blkbits; 1628525f4ed8SMingming Cao /* we will do mpage_da_submit_io in the next loop */ 1629525f4ed8SMingming Cao } 1630525f4ed8SMingming Cao } 163164769240SAlex Tomas /* 163264769240SAlex Tomas * First block in the extent 163364769240SAlex Tomas */ 16348dc207c0STheodore Ts'o if (mpd->b_size == 0) { 16358dc207c0STheodore Ts'o mpd->b_blocknr = logical; 16368dc207c0STheodore Ts'o mpd->b_size = b_size; 16378dc207c0STheodore Ts'o mpd->b_state = b_state & BH_FLAGS; 163864769240SAlex Tomas return; 163964769240SAlex Tomas } 164064769240SAlex Tomas 16418dc207c0STheodore Ts'o next = mpd->b_blocknr + nrblocks; 164264769240SAlex Tomas /* 164364769240SAlex Tomas * Can we merge the block to our big extent? 164464769240SAlex Tomas */ 16458dc207c0STheodore Ts'o if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) { 16468dc207c0STheodore Ts'o mpd->b_size += b_size; 164764769240SAlex Tomas return; 164864769240SAlex Tomas } 164964769240SAlex Tomas 1650525f4ed8SMingming Cao flush_it: 165164769240SAlex Tomas /* 165264769240SAlex Tomas * We couldn't merge the block to our extent, so we 165364769240SAlex Tomas * need to flush current extent and start new one 165464769240SAlex Tomas */ 16555a87b7a5STheodore Ts'o mpage_da_map_and_submit(mpd); 1656a1d6cc56SAneesh Kumar K.V return; 165764769240SAlex Tomas } 165864769240SAlex Tomas 1659c364b22cSAneesh Kumar K.V static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh) 166029fa89d0SAneesh Kumar K.V { 1661c364b22cSAneesh Kumar K.V return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 166229fa89d0SAneesh Kumar K.V } 166329fa89d0SAneesh Kumar K.V 166464769240SAlex Tomas /* 16655356f261SAditya Kali * This function is grabs code from the very beginning of 16665356f261SAditya Kali * ext4_map_blocks, but assumes that the caller is from delayed write 16675356f261SAditya Kali * time. This function looks up the requested blocks and sets the 16685356f261SAditya Kali * buffer delay bit under the protection of i_data_sem. 16695356f261SAditya Kali */ 16705356f261SAditya Kali static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, 16715356f261SAditya Kali struct ext4_map_blocks *map, 16725356f261SAditya Kali struct buffer_head *bh) 16735356f261SAditya Kali { 16745356f261SAditya Kali int retval; 16755356f261SAditya Kali sector_t invalid_block = ~((sector_t) 0xffff); 16765356f261SAditya Kali 16775356f261SAditya Kali if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 16785356f261SAditya Kali invalid_block = ~0; 16795356f261SAditya Kali 16805356f261SAditya Kali map->m_flags = 0; 16815356f261SAditya Kali ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u," 16825356f261SAditya Kali "logical block %lu\n", inode->i_ino, map->m_len, 16835356f261SAditya Kali (unsigned long) map->m_lblk); 16845356f261SAditya Kali /* 16855356f261SAditya Kali * Try to see if we can get the block without requesting a new 16865356f261SAditya Kali * file system block. 16875356f261SAditya Kali */ 16885356f261SAditya Kali down_read((&EXT4_I(inode)->i_data_sem)); 16895356f261SAditya Kali if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 16905356f261SAditya Kali retval = ext4_ext_map_blocks(NULL, inode, map, 0); 16915356f261SAditya Kali else 16925356f261SAditya Kali retval = ext4_ind_map_blocks(NULL, inode, map, 0); 16935356f261SAditya Kali 16945356f261SAditya Kali if (retval == 0) { 16955356f261SAditya Kali /* 16965356f261SAditya Kali * XXX: __block_prepare_write() unmaps passed block, 16975356f261SAditya Kali * is it OK? 16985356f261SAditya Kali */ 16995356f261SAditya Kali /* If the block was allocated from previously allocated cluster, 17005356f261SAditya Kali * then we dont need to reserve it again. */ 17015356f261SAditya Kali if (!(map->m_flags & EXT4_MAP_FROM_CLUSTER)) { 17025356f261SAditya Kali retval = ext4_da_reserve_space(inode, iblock); 17035356f261SAditya Kali if (retval) 17045356f261SAditya Kali /* not enough space to reserve */ 17055356f261SAditya Kali goto out_unlock; 17065356f261SAditya Kali } 17075356f261SAditya Kali 17085356f261SAditya Kali /* Clear EXT4_MAP_FROM_CLUSTER flag since its purpose is served 17095356f261SAditya Kali * and it should not appear on the bh->b_state. 17105356f261SAditya Kali */ 17115356f261SAditya Kali map->m_flags &= ~EXT4_MAP_FROM_CLUSTER; 17125356f261SAditya Kali 17135356f261SAditya Kali map_bh(bh, inode->i_sb, invalid_block); 17145356f261SAditya Kali set_buffer_new(bh); 17155356f261SAditya Kali set_buffer_delay(bh); 17165356f261SAditya Kali } 17175356f261SAditya Kali 17185356f261SAditya Kali out_unlock: 17195356f261SAditya Kali up_read((&EXT4_I(inode)->i_data_sem)); 17205356f261SAditya Kali 17215356f261SAditya Kali return retval; 17225356f261SAditya Kali } 17235356f261SAditya Kali 17245356f261SAditya Kali /* 1725b920c755STheodore Ts'o * This is a special get_blocks_t callback which is used by 1726b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 1727b920c755STheodore Ts'o * reserve space for a single block. 172829fa89d0SAneesh Kumar K.V * 172929fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 173029fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 173129fa89d0SAneesh Kumar K.V * 173229fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 173329fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 173429fa89d0SAneesh Kumar K.V * initialized properly. 173564769240SAlex Tomas */ 173664769240SAlex Tomas static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 17372ed88685STheodore Ts'o struct buffer_head *bh, int create) 173864769240SAlex Tomas { 17392ed88685STheodore Ts'o struct ext4_map_blocks map; 174064769240SAlex Tomas int ret = 0; 174164769240SAlex Tomas 174264769240SAlex Tomas BUG_ON(create == 0); 17432ed88685STheodore Ts'o BUG_ON(bh->b_size != inode->i_sb->s_blocksize); 17442ed88685STheodore Ts'o 17452ed88685STheodore Ts'o map.m_lblk = iblock; 17462ed88685STheodore Ts'o map.m_len = 1; 174764769240SAlex Tomas 174864769240SAlex Tomas /* 174964769240SAlex Tomas * first, we need to know whether the block is allocated already 175064769240SAlex Tomas * preallocated blocks are unmapped but should treated 175164769240SAlex Tomas * the same as allocated blocks. 175264769240SAlex Tomas */ 17535356f261SAditya Kali ret = ext4_da_map_blocks(inode, iblock, &map, bh); 17545356f261SAditya Kali if (ret <= 0) 17552ed88685STheodore Ts'o return ret; 175664769240SAlex Tomas 17572ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 17582ed88685STheodore Ts'o bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; 17592ed88685STheodore Ts'o 17602ed88685STheodore Ts'o if (buffer_unwritten(bh)) { 17612ed88685STheodore Ts'o /* A delayed write to unwritten bh should be marked 17622ed88685STheodore Ts'o * new and mapped. Mapped ensures that we don't do 17632ed88685STheodore Ts'o * get_block multiple times when we write to the same 17642ed88685STheodore Ts'o * offset and new ensures that we do proper zero out 17652ed88685STheodore Ts'o * for partial write. 17662ed88685STheodore Ts'o */ 17672ed88685STheodore Ts'o set_buffer_new(bh); 1768c8205636STheodore Ts'o set_buffer_mapped(bh); 17692ed88685STheodore Ts'o } 17702ed88685STheodore Ts'o return 0; 177164769240SAlex Tomas } 177261628a3fSMingming Cao 1773b920c755STheodore Ts'o /* 1774b920c755STheodore Ts'o * This function is used as a standard get_block_t calback function 1775b920c755STheodore Ts'o * when there is no desire to allocate any blocks. It is used as a 1776ebdec241SChristoph Hellwig * callback function for block_write_begin() and block_write_full_page(). 1777206f7ab4SChristoph Hellwig * These functions should only try to map a single block at a time. 1778b920c755STheodore Ts'o * 1779b920c755STheodore Ts'o * Since this function doesn't do block allocations even if the caller 1780b920c755STheodore Ts'o * requests it by passing in create=1, it is critically important that 1781b920c755STheodore Ts'o * any caller checks to make sure that any buffer heads are returned 1782b920c755STheodore Ts'o * by this function are either all already mapped or marked for 1783206f7ab4SChristoph Hellwig * delayed allocation before calling block_write_full_page(). Otherwise, 1784206f7ab4SChristoph Hellwig * b_blocknr could be left unitialized, and the page write functions will 1785206f7ab4SChristoph Hellwig * be taken by surprise. 1786b920c755STheodore Ts'o */ 1787b920c755STheodore Ts'o static int noalloc_get_block_write(struct inode *inode, sector_t iblock, 1788f0e6c985SAneesh Kumar K.V struct buffer_head *bh_result, int create) 1789f0e6c985SAneesh Kumar K.V { 1790a2dc52b5STheodore Ts'o BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize); 17912ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh_result, 0); 179261628a3fSMingming Cao } 179361628a3fSMingming Cao 179462e086beSAneesh Kumar K.V static int bget_one(handle_t *handle, struct buffer_head *bh) 179562e086beSAneesh Kumar K.V { 179662e086beSAneesh Kumar K.V get_bh(bh); 179762e086beSAneesh Kumar K.V return 0; 179862e086beSAneesh Kumar K.V } 179962e086beSAneesh Kumar K.V 180062e086beSAneesh Kumar K.V static int bput_one(handle_t *handle, struct buffer_head *bh) 180162e086beSAneesh Kumar K.V { 180262e086beSAneesh Kumar K.V put_bh(bh); 180362e086beSAneesh Kumar K.V return 0; 180462e086beSAneesh Kumar K.V } 180562e086beSAneesh Kumar K.V 180662e086beSAneesh Kumar K.V static int __ext4_journalled_writepage(struct page *page, 180762e086beSAneesh Kumar K.V unsigned int len) 180862e086beSAneesh Kumar K.V { 180962e086beSAneesh Kumar K.V struct address_space *mapping = page->mapping; 181062e086beSAneesh Kumar K.V struct inode *inode = mapping->host; 181162e086beSAneesh Kumar K.V struct buffer_head *page_bufs; 181262e086beSAneesh Kumar K.V handle_t *handle = NULL; 181362e086beSAneesh Kumar K.V int ret = 0; 181462e086beSAneesh Kumar K.V int err; 181562e086beSAneesh Kumar K.V 1816cb20d518STheodore Ts'o ClearPageChecked(page); 181762e086beSAneesh Kumar K.V page_bufs = page_buffers(page); 181862e086beSAneesh Kumar K.V BUG_ON(!page_bufs); 181962e086beSAneesh Kumar K.V walk_page_buffers(handle, page_bufs, 0, len, NULL, bget_one); 182062e086beSAneesh Kumar K.V /* As soon as we unlock the page, it can go away, but we have 182162e086beSAneesh Kumar K.V * references to buffers so we are safe */ 182262e086beSAneesh Kumar K.V unlock_page(page); 182362e086beSAneesh Kumar K.V 182462e086beSAneesh Kumar K.V handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode)); 182562e086beSAneesh Kumar K.V if (IS_ERR(handle)) { 182662e086beSAneesh Kumar K.V ret = PTR_ERR(handle); 182762e086beSAneesh Kumar K.V goto out; 182862e086beSAneesh Kumar K.V } 182962e086beSAneesh Kumar K.V 1830441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1831441c8508SCurt Wohlgemuth 183262e086beSAneesh Kumar K.V ret = walk_page_buffers(handle, page_bufs, 0, len, NULL, 183362e086beSAneesh Kumar K.V do_journal_get_write_access); 183462e086beSAneesh Kumar K.V 183562e086beSAneesh Kumar K.V err = walk_page_buffers(handle, page_bufs, 0, len, NULL, 183662e086beSAneesh Kumar K.V write_end_fn); 183762e086beSAneesh Kumar K.V if (ret == 0) 183862e086beSAneesh Kumar K.V ret = err; 18392d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 184062e086beSAneesh Kumar K.V err = ext4_journal_stop(handle); 184162e086beSAneesh Kumar K.V if (!ret) 184262e086beSAneesh Kumar K.V ret = err; 184362e086beSAneesh Kumar K.V 184462e086beSAneesh Kumar K.V walk_page_buffers(handle, page_bufs, 0, len, NULL, bput_one); 184519f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 184662e086beSAneesh Kumar K.V out: 184762e086beSAneesh Kumar K.V return ret; 184862e086beSAneesh Kumar K.V } 184962e086beSAneesh Kumar K.V 1850744692dcSJiaying Zhang static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode); 1851744692dcSJiaying Zhang static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate); 1852744692dcSJiaying Zhang 185361628a3fSMingming Cao /* 185443ce1d23SAneesh Kumar K.V * Note that we don't need to start a transaction unless we're journaling data 185543ce1d23SAneesh Kumar K.V * because we should have holes filled from ext4_page_mkwrite(). We even don't 185643ce1d23SAneesh Kumar K.V * need to file the inode to the transaction's list in ordered mode because if 185743ce1d23SAneesh Kumar K.V * we are writing back data added by write(), the inode is already there and if 185843ce1d23SAneesh Kumar K.V * we are writing back data modified via mmap(), no one guarantees in which 185943ce1d23SAneesh Kumar K.V * transaction the data will hit the disk. In case we are journaling data, we 186043ce1d23SAneesh Kumar K.V * cannot start transaction directly because transaction start ranks above page 186143ce1d23SAneesh Kumar K.V * lock so we have to do some magic. 186243ce1d23SAneesh Kumar K.V * 1863b920c755STheodore Ts'o * This function can get called via... 1864b920c755STheodore Ts'o * - ext4_da_writepages after taking page lock (have journal handle) 1865b920c755STheodore Ts'o * - journal_submit_inode_data_buffers (no journal handle) 1866b920c755STheodore Ts'o * - shrink_page_list via pdflush (no journal handle) 1867b920c755STheodore Ts'o * - grab_page_cache when doing write_begin (have journal handle) 186843ce1d23SAneesh Kumar K.V * 186943ce1d23SAneesh Kumar K.V * We don't do any block allocation in this function. If we have page with 187043ce1d23SAneesh Kumar K.V * multiple blocks we need to write those buffer_heads that are mapped. This 187143ce1d23SAneesh Kumar K.V * is important for mmaped based write. So if we do with blocksize 1K 187243ce1d23SAneesh Kumar K.V * truncate(f, 1024); 187343ce1d23SAneesh Kumar K.V * a = mmap(f, 0, 4096); 187443ce1d23SAneesh Kumar K.V * a[0] = 'a'; 187543ce1d23SAneesh Kumar K.V * truncate(f, 4096); 187643ce1d23SAneesh Kumar K.V * we have in the page first buffer_head mapped via page_mkwrite call back 187743ce1d23SAneesh Kumar K.V * but other bufer_heads would be unmapped but dirty(dirty done via the 187843ce1d23SAneesh Kumar K.V * do_wp_page). So writepage should write the first block. If we modify 187943ce1d23SAneesh Kumar K.V * the mmap area beyond 1024 we will again get a page_fault and the 188043ce1d23SAneesh Kumar K.V * page_mkwrite callback will do the block allocation and mark the 188143ce1d23SAneesh Kumar K.V * buffer_heads mapped. 188243ce1d23SAneesh Kumar K.V * 188343ce1d23SAneesh Kumar K.V * We redirty the page if we have any buffer_heads that is either delay or 188443ce1d23SAneesh Kumar K.V * unwritten in the page. 188543ce1d23SAneesh Kumar K.V * 188643ce1d23SAneesh Kumar K.V * We can get recursively called as show below. 188743ce1d23SAneesh Kumar K.V * 188843ce1d23SAneesh Kumar K.V * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> 188943ce1d23SAneesh Kumar K.V * ext4_writepage() 189043ce1d23SAneesh Kumar K.V * 189143ce1d23SAneesh Kumar K.V * But since we don't do any block allocation we should not deadlock. 189243ce1d23SAneesh Kumar K.V * Page also have the dirty flag cleared so we don't get recurive page_lock. 189361628a3fSMingming Cao */ 189443ce1d23SAneesh Kumar K.V static int ext4_writepage(struct page *page, 189564769240SAlex Tomas struct writeback_control *wbc) 189664769240SAlex Tomas { 1897a42afc5fSTheodore Ts'o int ret = 0, commit_write = 0; 189861628a3fSMingming Cao loff_t size; 1899498e5f24STheodore Ts'o unsigned int len; 1900744692dcSJiaying Zhang struct buffer_head *page_bufs = NULL; 190161628a3fSMingming Cao struct inode *inode = page->mapping->host; 190264769240SAlex Tomas 1903a9c667f8SLukas Czerner trace_ext4_writepage(page); 190461628a3fSMingming Cao size = i_size_read(inode); 190561628a3fSMingming Cao if (page->index == size >> PAGE_CACHE_SHIFT) 190661628a3fSMingming Cao len = size & ~PAGE_CACHE_MASK; 190761628a3fSMingming Cao else 190861628a3fSMingming Cao len = PAGE_CACHE_SIZE; 190961628a3fSMingming Cao 1910a42afc5fSTheodore Ts'o /* 1911a42afc5fSTheodore Ts'o * If the page does not have buffers (for whatever reason), 1912a107e5a3STheodore Ts'o * try to create them using __block_write_begin. If this 1913a42afc5fSTheodore Ts'o * fails, redirty the page and move on. 1914a42afc5fSTheodore Ts'o */ 1915b1142e8fSTheodore Ts'o if (!page_has_buffers(page)) { 1916a107e5a3STheodore Ts'o if (__block_write_begin(page, 0, len, 1917a42afc5fSTheodore Ts'o noalloc_get_block_write)) { 1918a42afc5fSTheodore Ts'o redirty_page: 1919a42afc5fSTheodore Ts'o redirty_page_for_writepage(wbc, page); 1920a42afc5fSTheodore Ts'o unlock_page(page); 1921a42afc5fSTheodore Ts'o return 0; 1922a42afc5fSTheodore Ts'o } 1923a42afc5fSTheodore Ts'o commit_write = 1; 1924a42afc5fSTheodore Ts'o } 1925f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 1926f0e6c985SAneesh Kumar K.V if (walk_page_buffers(NULL, page_bufs, 0, len, NULL, 1927c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 192861628a3fSMingming Cao /* 1929b1142e8fSTheodore Ts'o * We don't want to do block allocation, so redirty 1930b1142e8fSTheodore Ts'o * the page and return. We may reach here when we do 1931b1142e8fSTheodore Ts'o * a journal commit via journal_submit_inode_data_buffers. 1932b1142e8fSTheodore Ts'o * We can also reach here via shrink_page_list 1933f0e6c985SAneesh Kumar K.V */ 1934a42afc5fSTheodore Ts'o goto redirty_page; 1935f0e6c985SAneesh Kumar K.V } 1936a42afc5fSTheodore Ts'o if (commit_write) 1937ed9b3e33SAneesh Kumar K.V /* now mark the buffer_heads as dirty and uptodate */ 1938b767e78aSAneesh Kumar K.V block_commit_write(page, 0, len); 193964769240SAlex Tomas 1940cb20d518STheodore Ts'o if (PageChecked(page) && ext4_should_journal_data(inode)) 194143ce1d23SAneesh Kumar K.V /* 194243ce1d23SAneesh Kumar K.V * It's mmapped pagecache. Add buffers and journal it. There 194343ce1d23SAneesh Kumar K.V * doesn't seem much point in redirtying the page here. 194443ce1d23SAneesh Kumar K.V */ 19453f0ca309SWu Fengguang return __ext4_journalled_writepage(page, len); 194643ce1d23SAneesh Kumar K.V 1947a42afc5fSTheodore Ts'o if (buffer_uninit(page_bufs)) { 1948744692dcSJiaying Zhang ext4_set_bh_endio(page_bufs, inode); 1949744692dcSJiaying Zhang ret = block_write_full_page_endio(page, noalloc_get_block_write, 1950744692dcSJiaying Zhang wbc, ext4_end_io_buffer_write); 1951744692dcSJiaying Zhang } else 1952b920c755STheodore Ts'o ret = block_write_full_page(page, noalloc_get_block_write, 1953f0e6c985SAneesh Kumar K.V wbc); 195464769240SAlex Tomas 195564769240SAlex Tomas return ret; 195664769240SAlex Tomas } 195764769240SAlex Tomas 195861628a3fSMingming Cao /* 1959525f4ed8SMingming Cao * This is called via ext4_da_writepages() to 196025985edcSLucas De Marchi * calculate the total number of credits to reserve to fit 1961525f4ed8SMingming Cao * a single extent allocation into a single transaction, 1962525f4ed8SMingming Cao * ext4_da_writpeages() will loop calling this before 1963525f4ed8SMingming Cao * the block allocation. 196461628a3fSMingming Cao */ 1965525f4ed8SMingming Cao 1966525f4ed8SMingming Cao static int ext4_da_writepages_trans_blocks(struct inode *inode) 1967525f4ed8SMingming Cao { 1968525f4ed8SMingming Cao int max_blocks = EXT4_I(inode)->i_reserved_data_blocks; 1969525f4ed8SMingming Cao 1970525f4ed8SMingming Cao /* 1971525f4ed8SMingming Cao * With non-extent format the journal credit needed to 1972525f4ed8SMingming Cao * insert nrblocks contiguous block is dependent on 1973525f4ed8SMingming Cao * number of contiguous block. So we will limit 1974525f4ed8SMingming Cao * number of contiguous block to a sane value 1975525f4ed8SMingming Cao */ 197612e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) && 1977525f4ed8SMingming Cao (max_blocks > EXT4_MAX_TRANS_DATA)) 1978525f4ed8SMingming Cao max_blocks = EXT4_MAX_TRANS_DATA; 1979525f4ed8SMingming Cao 1980525f4ed8SMingming Cao return ext4_chunk_trans_blocks(inode, max_blocks); 1981525f4ed8SMingming Cao } 198261628a3fSMingming Cao 19838e48dcfbSTheodore Ts'o /* 19848e48dcfbSTheodore Ts'o * write_cache_pages_da - walk the list of dirty pages of the given 19858eb9e5ceSTheodore Ts'o * address space and accumulate pages that need writing, and call 1986168fc022STheodore Ts'o * mpage_da_map_and_submit to map a single contiguous memory region 1987168fc022STheodore Ts'o * and then write them. 19888e48dcfbSTheodore Ts'o */ 19898e48dcfbSTheodore Ts'o static int write_cache_pages_da(struct address_space *mapping, 19908e48dcfbSTheodore Ts'o struct writeback_control *wbc, 199172f84e65SEric Sandeen struct mpage_da_data *mpd, 199272f84e65SEric Sandeen pgoff_t *done_index) 19938e48dcfbSTheodore Ts'o { 19948eb9e5ceSTheodore Ts'o struct buffer_head *bh, *head; 1995168fc022STheodore Ts'o struct inode *inode = mapping->host; 19968e48dcfbSTheodore Ts'o struct pagevec pvec; 19974f01b02cSTheodore Ts'o unsigned int nr_pages; 19984f01b02cSTheodore Ts'o sector_t logical; 19994f01b02cSTheodore Ts'o pgoff_t index, end; 20008e48dcfbSTheodore Ts'o long nr_to_write = wbc->nr_to_write; 20014f01b02cSTheodore Ts'o int i, tag, ret = 0; 20028e48dcfbSTheodore Ts'o 2003168fc022STheodore Ts'o memset(mpd, 0, sizeof(struct mpage_da_data)); 2004168fc022STheodore Ts'o mpd->wbc = wbc; 2005168fc022STheodore Ts'o mpd->inode = inode; 20068e48dcfbSTheodore Ts'o pagevec_init(&pvec, 0); 20078e48dcfbSTheodore Ts'o index = wbc->range_start >> PAGE_CACHE_SHIFT; 20088e48dcfbSTheodore Ts'o end = wbc->range_end >> PAGE_CACHE_SHIFT; 20098e48dcfbSTheodore Ts'o 20106e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 20115b41d924SEric Sandeen tag = PAGECACHE_TAG_TOWRITE; 20125b41d924SEric Sandeen else 20135b41d924SEric Sandeen tag = PAGECACHE_TAG_DIRTY; 20145b41d924SEric Sandeen 201572f84e65SEric Sandeen *done_index = index; 20164f01b02cSTheodore Ts'o while (index <= end) { 20175b41d924SEric Sandeen nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, 20188e48dcfbSTheodore Ts'o min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); 20198e48dcfbSTheodore Ts'o if (nr_pages == 0) 20204f01b02cSTheodore Ts'o return 0; 20218e48dcfbSTheodore Ts'o 20228e48dcfbSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 20238e48dcfbSTheodore Ts'o struct page *page = pvec.pages[i]; 20248e48dcfbSTheodore Ts'o 20258e48dcfbSTheodore Ts'o /* 20268e48dcfbSTheodore Ts'o * At this point, the page may be truncated or 20278e48dcfbSTheodore Ts'o * invalidated (changing page->mapping to NULL), or 20288e48dcfbSTheodore Ts'o * even swizzled back from swapper_space to tmpfs file 20298e48dcfbSTheodore Ts'o * mapping. However, page->index will not change 20308e48dcfbSTheodore Ts'o * because we have a reference on the page. 20318e48dcfbSTheodore Ts'o */ 20324f01b02cSTheodore Ts'o if (page->index > end) 20334f01b02cSTheodore Ts'o goto out; 20348e48dcfbSTheodore Ts'o 203572f84e65SEric Sandeen *done_index = page->index + 1; 203672f84e65SEric Sandeen 203778aaced3STheodore Ts'o /* 203878aaced3STheodore Ts'o * If we can't merge this page, and we have 203978aaced3STheodore Ts'o * accumulated an contiguous region, write it 204078aaced3STheodore Ts'o */ 204178aaced3STheodore Ts'o if ((mpd->next_page != page->index) && 204278aaced3STheodore Ts'o (mpd->next_page != mpd->first_page)) { 204378aaced3STheodore Ts'o mpage_da_map_and_submit(mpd); 204478aaced3STheodore Ts'o goto ret_extent_tail; 204578aaced3STheodore Ts'o } 204678aaced3STheodore Ts'o 20478e48dcfbSTheodore Ts'o lock_page(page); 20488e48dcfbSTheodore Ts'o 20498e48dcfbSTheodore Ts'o /* 20504f01b02cSTheodore Ts'o * If the page is no longer dirty, or its 20514f01b02cSTheodore Ts'o * mapping no longer corresponds to inode we 20524f01b02cSTheodore Ts'o * are writing (which means it has been 20534f01b02cSTheodore Ts'o * truncated or invalidated), or the page is 20544f01b02cSTheodore Ts'o * already under writeback and we are not 20554f01b02cSTheodore Ts'o * doing a data integrity writeback, skip the page 20568e48dcfbSTheodore Ts'o */ 20574f01b02cSTheodore Ts'o if (!PageDirty(page) || 20584f01b02cSTheodore Ts'o (PageWriteback(page) && 20594f01b02cSTheodore Ts'o (wbc->sync_mode == WB_SYNC_NONE)) || 20604f01b02cSTheodore Ts'o unlikely(page->mapping != mapping)) { 20618e48dcfbSTheodore Ts'o unlock_page(page); 20628e48dcfbSTheodore Ts'o continue; 20638e48dcfbSTheodore Ts'o } 20648e48dcfbSTheodore Ts'o 20658e48dcfbSTheodore Ts'o wait_on_page_writeback(page); 20668e48dcfbSTheodore Ts'o BUG_ON(PageWriteback(page)); 20678e48dcfbSTheodore Ts'o 2068168fc022STheodore Ts'o if (mpd->next_page != page->index) 20698eb9e5ceSTheodore Ts'o mpd->first_page = page->index; 20708eb9e5ceSTheodore Ts'o mpd->next_page = page->index + 1; 20718eb9e5ceSTheodore Ts'o logical = (sector_t) page->index << 20728eb9e5ceSTheodore Ts'o (PAGE_CACHE_SHIFT - inode->i_blkbits); 20738eb9e5ceSTheodore Ts'o 20748eb9e5ceSTheodore Ts'o if (!page_has_buffers(page)) { 20754f01b02cSTheodore Ts'o mpage_add_bh_to_extent(mpd, logical, 20764f01b02cSTheodore Ts'o PAGE_CACHE_SIZE, 20778eb9e5ceSTheodore Ts'o (1 << BH_Dirty) | (1 << BH_Uptodate)); 20784f01b02cSTheodore Ts'o if (mpd->io_done) 20794f01b02cSTheodore Ts'o goto ret_extent_tail; 20808e48dcfbSTheodore Ts'o } else { 20818eb9e5ceSTheodore Ts'o /* 20824f01b02cSTheodore Ts'o * Page with regular buffer heads, 20834f01b02cSTheodore Ts'o * just add all dirty ones 20848eb9e5ceSTheodore Ts'o */ 20858eb9e5ceSTheodore Ts'o head = page_buffers(page); 20868eb9e5ceSTheodore Ts'o bh = head; 20878eb9e5ceSTheodore Ts'o do { 20888eb9e5ceSTheodore Ts'o BUG_ON(buffer_locked(bh)); 20898eb9e5ceSTheodore Ts'o /* 20908eb9e5ceSTheodore Ts'o * We need to try to allocate 20918eb9e5ceSTheodore Ts'o * unmapped blocks in the same page. 20928eb9e5ceSTheodore Ts'o * Otherwise we won't make progress 20938eb9e5ceSTheodore Ts'o * with the page in ext4_writepage 20948eb9e5ceSTheodore Ts'o */ 20958eb9e5ceSTheodore Ts'o if (ext4_bh_delay_or_unwritten(NULL, bh)) { 20968eb9e5ceSTheodore Ts'o mpage_add_bh_to_extent(mpd, logical, 20978eb9e5ceSTheodore Ts'o bh->b_size, 20988eb9e5ceSTheodore Ts'o bh->b_state); 20994f01b02cSTheodore Ts'o if (mpd->io_done) 21004f01b02cSTheodore Ts'o goto ret_extent_tail; 21018eb9e5ceSTheodore Ts'o } else if (buffer_dirty(bh) && (buffer_mapped(bh))) { 21028eb9e5ceSTheodore Ts'o /* 21034f01b02cSTheodore Ts'o * mapped dirty buffer. We need 21044f01b02cSTheodore Ts'o * to update the b_state 21054f01b02cSTheodore Ts'o * because we look at b_state 21064f01b02cSTheodore Ts'o * in mpage_da_map_blocks. We 21074f01b02cSTheodore Ts'o * don't update b_size because 21084f01b02cSTheodore Ts'o * if we find an unmapped 21094f01b02cSTheodore Ts'o * buffer_head later we need to 21104f01b02cSTheodore Ts'o * use the b_state flag of that 21114f01b02cSTheodore Ts'o * buffer_head. 21128eb9e5ceSTheodore Ts'o */ 21138eb9e5ceSTheodore Ts'o if (mpd->b_size == 0) 21148eb9e5ceSTheodore Ts'o mpd->b_state = bh->b_state & BH_FLAGS; 21158e48dcfbSTheodore Ts'o } 21168eb9e5ceSTheodore Ts'o logical++; 21178eb9e5ceSTheodore Ts'o } while ((bh = bh->b_this_page) != head); 21188e48dcfbSTheodore Ts'o } 21198e48dcfbSTheodore Ts'o 21208e48dcfbSTheodore Ts'o if (nr_to_write > 0) { 21218e48dcfbSTheodore Ts'o nr_to_write--; 21228e48dcfbSTheodore Ts'o if (nr_to_write == 0 && 21234f01b02cSTheodore Ts'o wbc->sync_mode == WB_SYNC_NONE) 21248e48dcfbSTheodore Ts'o /* 21258e48dcfbSTheodore Ts'o * We stop writing back only if we are 21268e48dcfbSTheodore Ts'o * not doing integrity sync. In case of 21278e48dcfbSTheodore Ts'o * integrity sync we have to keep going 21288e48dcfbSTheodore Ts'o * because someone may be concurrently 21298e48dcfbSTheodore Ts'o * dirtying pages, and we might have 21308e48dcfbSTheodore Ts'o * synced a lot of newly appeared dirty 21318e48dcfbSTheodore Ts'o * pages, but have not synced all of the 21328e48dcfbSTheodore Ts'o * old dirty pages. 21338e48dcfbSTheodore Ts'o */ 21344f01b02cSTheodore Ts'o goto out; 21358e48dcfbSTheodore Ts'o } 21368e48dcfbSTheodore Ts'o } 21378e48dcfbSTheodore Ts'o pagevec_release(&pvec); 21388e48dcfbSTheodore Ts'o cond_resched(); 21398e48dcfbSTheodore Ts'o } 21404f01b02cSTheodore Ts'o return 0; 21414f01b02cSTheodore Ts'o ret_extent_tail: 21424f01b02cSTheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 21438eb9e5ceSTheodore Ts'o out: 21448eb9e5ceSTheodore Ts'o pagevec_release(&pvec); 21458eb9e5ceSTheodore Ts'o cond_resched(); 21468e48dcfbSTheodore Ts'o return ret; 21478e48dcfbSTheodore Ts'o } 21488e48dcfbSTheodore Ts'o 21498e48dcfbSTheodore Ts'o 215064769240SAlex Tomas static int ext4_da_writepages(struct address_space *mapping, 215164769240SAlex Tomas struct writeback_control *wbc) 215264769240SAlex Tomas { 215322208dedSAneesh Kumar K.V pgoff_t index; 215422208dedSAneesh Kumar K.V int range_whole = 0; 215561628a3fSMingming Cao handle_t *handle = NULL; 2156df22291fSAneesh Kumar K.V struct mpage_da_data mpd; 21575e745b04SAneesh Kumar K.V struct inode *inode = mapping->host; 2158498e5f24STheodore Ts'o int pages_written = 0; 215955138e0bSTheodore Ts'o unsigned int max_pages; 21602acf2c26SAneesh Kumar K.V int range_cyclic, cycled = 1, io_done = 0; 216155138e0bSTheodore Ts'o int needed_blocks, ret = 0; 216255138e0bSTheodore Ts'o long desired_nr_to_write, nr_to_writebump = 0; 2163de89de6eSTheodore Ts'o loff_t range_start = wbc->range_start; 21645e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 216572f84e65SEric Sandeen pgoff_t done_index = 0; 21665b41d924SEric Sandeen pgoff_t end; 21671bce63d1SShaohua Li struct blk_plug plug; 216861628a3fSMingming Cao 21699bffad1eSTheodore Ts'o trace_ext4_da_writepages(inode, wbc); 2170ba80b101STheodore Ts'o 217161628a3fSMingming Cao /* 217261628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 217361628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 217461628a3fSMingming Cao * because that could violate lock ordering on umount 217561628a3fSMingming Cao */ 2176a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 217761628a3fSMingming Cao return 0; 21782a21e37eSTheodore Ts'o 21792a21e37eSTheodore Ts'o /* 21802a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 21812a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 21822a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 21834ab2f15bSTheodore Ts'o * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because 21842a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 21852a21e37eSTheodore Ts'o * read-only, and in that case, ext4_da_writepages should 21862a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 21872a21e37eSTheodore Ts'o * the stack trace. 21882a21e37eSTheodore Ts'o */ 21894ab2f15bSTheodore Ts'o if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) 21902a21e37eSTheodore Ts'o return -EROFS; 21912a21e37eSTheodore Ts'o 219222208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 219322208dedSAneesh Kumar K.V range_whole = 1; 219461628a3fSMingming Cao 21952acf2c26SAneesh Kumar K.V range_cyclic = wbc->range_cyclic; 21962acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 219722208dedSAneesh Kumar K.V index = mapping->writeback_index; 21982acf2c26SAneesh Kumar K.V if (index) 21992acf2c26SAneesh Kumar K.V cycled = 0; 22002acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 22012acf2c26SAneesh Kumar K.V wbc->range_end = LLONG_MAX; 22022acf2c26SAneesh Kumar K.V wbc->range_cyclic = 0; 22035b41d924SEric Sandeen end = -1; 22045b41d924SEric Sandeen } else { 220522208dedSAneesh Kumar K.V index = wbc->range_start >> PAGE_CACHE_SHIFT; 22065b41d924SEric Sandeen end = wbc->range_end >> PAGE_CACHE_SHIFT; 22075b41d924SEric Sandeen } 2208a1d6cc56SAneesh Kumar K.V 220955138e0bSTheodore Ts'o /* 221055138e0bSTheodore Ts'o * This works around two forms of stupidity. The first is in 221155138e0bSTheodore Ts'o * the writeback code, which caps the maximum number of pages 221255138e0bSTheodore Ts'o * written to be 1024 pages. This is wrong on multiple 221355138e0bSTheodore Ts'o * levels; different architectues have a different page size, 221455138e0bSTheodore Ts'o * which changes the maximum amount of data which gets 221555138e0bSTheodore Ts'o * written. Secondly, 4 megabytes is way too small. XFS 221655138e0bSTheodore Ts'o * forces this value to be 16 megabytes by multiplying 221755138e0bSTheodore Ts'o * nr_to_write parameter by four, and then relies on its 221855138e0bSTheodore Ts'o * allocator to allocate larger extents to make them 221955138e0bSTheodore Ts'o * contiguous. Unfortunately this brings us to the second 222055138e0bSTheodore Ts'o * stupidity, which is that ext4's mballoc code only allocates 222155138e0bSTheodore Ts'o * at most 2048 blocks. So we force contiguous writes up to 222255138e0bSTheodore Ts'o * the number of dirty blocks in the inode, or 222355138e0bSTheodore Ts'o * sbi->max_writeback_mb_bump whichever is smaller. 222455138e0bSTheodore Ts'o */ 222555138e0bSTheodore Ts'o max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT); 2226b443e733SEric Sandeen if (!range_cyclic && range_whole) { 2227b443e733SEric Sandeen if (wbc->nr_to_write == LONG_MAX) 2228b443e733SEric Sandeen desired_nr_to_write = wbc->nr_to_write; 222955138e0bSTheodore Ts'o else 2230b443e733SEric Sandeen desired_nr_to_write = wbc->nr_to_write * 8; 2231b443e733SEric Sandeen } else 223255138e0bSTheodore Ts'o desired_nr_to_write = ext4_num_dirty_pages(inode, index, 223355138e0bSTheodore Ts'o max_pages); 223455138e0bSTheodore Ts'o if (desired_nr_to_write > max_pages) 223555138e0bSTheodore Ts'o desired_nr_to_write = max_pages; 223655138e0bSTheodore Ts'o 223755138e0bSTheodore Ts'o if (wbc->nr_to_write < desired_nr_to_write) { 223855138e0bSTheodore Ts'o nr_to_writebump = desired_nr_to_write - wbc->nr_to_write; 223955138e0bSTheodore Ts'o wbc->nr_to_write = desired_nr_to_write; 224055138e0bSTheodore Ts'o } 224155138e0bSTheodore Ts'o 22422acf2c26SAneesh Kumar K.V retry: 22436e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 22445b41d924SEric Sandeen tag_pages_for_writeback(mapping, index, end); 22455b41d924SEric Sandeen 22461bce63d1SShaohua Li blk_start_plug(&plug); 224722208dedSAneesh Kumar K.V while (!ret && wbc->nr_to_write > 0) { 2248a1d6cc56SAneesh Kumar K.V 2249a1d6cc56SAneesh Kumar K.V /* 2250a1d6cc56SAneesh Kumar K.V * we insert one extent at a time. So we need 2251a1d6cc56SAneesh Kumar K.V * credit needed for single extent allocation. 2252a1d6cc56SAneesh Kumar K.V * journalled mode is currently not supported 2253a1d6cc56SAneesh Kumar K.V * by delalloc 2254a1d6cc56SAneesh Kumar K.V */ 2255a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 2256525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 2257a1d6cc56SAneesh Kumar K.V 225861628a3fSMingming Cao /* start a new transaction*/ 225961628a3fSMingming Cao handle = ext4_journal_start(inode, needed_blocks); 226061628a3fSMingming Cao if (IS_ERR(handle)) { 226161628a3fSMingming Cao ret = PTR_ERR(handle); 22621693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2263fbe845ddSCurt Wohlgemuth "%ld pages, ino %lu; err %d", __func__, 2264a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 226561628a3fSMingming Cao goto out_writepages; 226661628a3fSMingming Cao } 2267f63e6005STheodore Ts'o 2268f63e6005STheodore Ts'o /* 22698eb9e5ceSTheodore Ts'o * Now call write_cache_pages_da() to find the next 2270f63e6005STheodore Ts'o * contiguous region of logical blocks that need 22718eb9e5ceSTheodore Ts'o * blocks to be allocated by ext4 and submit them. 2272f63e6005STheodore Ts'o */ 227372f84e65SEric Sandeen ret = write_cache_pages_da(mapping, wbc, &mpd, &done_index); 2274f63e6005STheodore Ts'o /* 2275af901ca1SAndré Goddard Rosa * If we have a contiguous extent of pages and we 2276f63e6005STheodore Ts'o * haven't done the I/O yet, map the blocks and submit 2277f63e6005STheodore Ts'o * them for I/O. 2278f63e6005STheodore Ts'o */ 2279f63e6005STheodore Ts'o if (!mpd.io_done && mpd.next_page != mpd.first_page) { 22805a87b7a5STheodore Ts'o mpage_da_map_and_submit(&mpd); 2281f63e6005STheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 2282f63e6005STheodore Ts'o } 2283b3a3ca8cSTheodore Ts'o trace_ext4_da_write_pages(inode, &mpd); 2284f63e6005STheodore Ts'o wbc->nr_to_write -= mpd.pages_written; 2285df22291fSAneesh Kumar K.V 228661628a3fSMingming Cao ext4_journal_stop(handle); 2287df22291fSAneesh Kumar K.V 22888f64b32eSEric Sandeen if ((mpd.retval == -ENOSPC) && sbi->s_journal) { 228922208dedSAneesh Kumar K.V /* commit the transaction which would 229022208dedSAneesh Kumar K.V * free blocks released in the transaction 229122208dedSAneesh Kumar K.V * and try again 229222208dedSAneesh Kumar K.V */ 2293df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 229422208dedSAneesh Kumar K.V ret = 0; 229522208dedSAneesh Kumar K.V } else if (ret == MPAGE_DA_EXTENT_TAIL) { 2296a1d6cc56SAneesh Kumar K.V /* 2297a1d6cc56SAneesh Kumar K.V * got one extent now try with 2298a1d6cc56SAneesh Kumar K.V * rest of the pages 2299a1d6cc56SAneesh Kumar K.V */ 230022208dedSAneesh Kumar K.V pages_written += mpd.pages_written; 2301a1d6cc56SAneesh Kumar K.V ret = 0; 23022acf2c26SAneesh Kumar K.V io_done = 1; 230322208dedSAneesh Kumar K.V } else if (wbc->nr_to_write) 230461628a3fSMingming Cao /* 230561628a3fSMingming Cao * There is no more writeout needed 230661628a3fSMingming Cao * or we requested for a noblocking writeout 230761628a3fSMingming Cao * and we found the device congested 230861628a3fSMingming Cao */ 230961628a3fSMingming Cao break; 231061628a3fSMingming Cao } 23111bce63d1SShaohua Li blk_finish_plug(&plug); 23122acf2c26SAneesh Kumar K.V if (!io_done && !cycled) { 23132acf2c26SAneesh Kumar K.V cycled = 1; 23142acf2c26SAneesh Kumar K.V index = 0; 23152acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 23162acf2c26SAneesh Kumar K.V wbc->range_end = mapping->writeback_index - 1; 23172acf2c26SAneesh Kumar K.V goto retry; 23182acf2c26SAneesh Kumar K.V } 231961628a3fSMingming Cao 232022208dedSAneesh Kumar K.V /* Update index */ 23212acf2c26SAneesh Kumar K.V wbc->range_cyclic = range_cyclic; 232222208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 232322208dedSAneesh Kumar K.V /* 232422208dedSAneesh Kumar K.V * set the writeback_index so that range_cyclic 232522208dedSAneesh Kumar K.V * mode will write it back later 232622208dedSAneesh Kumar K.V */ 232772f84e65SEric Sandeen mapping->writeback_index = done_index; 2328a1d6cc56SAneesh Kumar K.V 232961628a3fSMingming Cao out_writepages: 233022208dedSAneesh Kumar K.V wbc->nr_to_write -= nr_to_writebump; 2331de89de6eSTheodore Ts'o wbc->range_start = range_start; 23329bffad1eSTheodore Ts'o trace_ext4_da_writepages_result(inode, wbc, ret, pages_written); 233361628a3fSMingming Cao return ret; 233464769240SAlex Tomas } 233564769240SAlex Tomas 233679f0be8dSAneesh Kumar K.V #define FALL_BACK_TO_NONDELALLOC 1 233779f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 233879f0be8dSAneesh Kumar K.V { 233979f0be8dSAneesh Kumar K.V s64 free_blocks, dirty_blocks; 234079f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 234179f0be8dSAneesh Kumar K.V 234279f0be8dSAneesh Kumar K.V /* 234379f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 234479f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 2345179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 234679f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 234779f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 234879f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 234979f0be8dSAneesh Kumar K.V */ 235057042651STheodore Ts'o free_blocks = EXT4_C2B(sbi, 235157042651STheodore Ts'o percpu_counter_read_positive(&sbi->s_freeclusters_counter)); 235257042651STheodore Ts'o dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyclusters_counter); 235379f0be8dSAneesh Kumar K.V if (2 * free_blocks < 3 * dirty_blocks || 2354df55c99dSTheodore Ts'o free_blocks < (dirty_blocks + EXT4_FREECLUSTERS_WATERMARK)) { 235579f0be8dSAneesh Kumar K.V /* 2356c8afb446SEric Sandeen * free block count is less than 150% of dirty blocks 2357c8afb446SEric Sandeen * or free blocks is less than watermark 235879f0be8dSAneesh Kumar K.V */ 235979f0be8dSAneesh Kumar K.V return 1; 236079f0be8dSAneesh Kumar K.V } 2361c8afb446SEric Sandeen /* 2362c8afb446SEric Sandeen * Even if we don't switch but are nearing capacity, 2363c8afb446SEric Sandeen * start pushing delalloc when 1/2 of free blocks are dirty. 2364c8afb446SEric Sandeen */ 2365c8afb446SEric Sandeen if (free_blocks < 2 * dirty_blocks) 2366c8afb446SEric Sandeen writeback_inodes_sb_if_idle(sb); 2367c8afb446SEric Sandeen 236879f0be8dSAneesh Kumar K.V return 0; 236979f0be8dSAneesh Kumar K.V } 237079f0be8dSAneesh Kumar K.V 237164769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 237264769240SAlex Tomas loff_t pos, unsigned len, unsigned flags, 237364769240SAlex Tomas struct page **pagep, void **fsdata) 237464769240SAlex Tomas { 237572b8ab9dSEric Sandeen int ret, retries = 0; 237664769240SAlex Tomas struct page *page; 237764769240SAlex Tomas pgoff_t index; 237864769240SAlex Tomas struct inode *inode = mapping->host; 237964769240SAlex Tomas handle_t *handle; 238002fac129SAllison Henderson loff_t page_len; 238164769240SAlex Tomas 238264769240SAlex Tomas index = pos >> PAGE_CACHE_SHIFT; 238379f0be8dSAneesh Kumar K.V 238479f0be8dSAneesh Kumar K.V if (ext4_nonda_switch(inode->i_sb)) { 238579f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 238679f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 238779f0be8dSAneesh Kumar K.V len, flags, pagep, fsdata); 238879f0be8dSAneesh Kumar K.V } 238979f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 23909bffad1eSTheodore Ts'o trace_ext4_da_write_begin(inode, pos, len, flags); 2391d2a17637SMingming Cao retry: 239264769240SAlex Tomas /* 239364769240SAlex Tomas * With delayed allocation, we don't log the i_disksize update 239464769240SAlex Tomas * if there is delayed block allocation. But we still need 239564769240SAlex Tomas * to journalling the i_disksize update if writes to the end 239664769240SAlex Tomas * of file which has an already mapped buffer. 239764769240SAlex Tomas */ 239864769240SAlex Tomas handle = ext4_journal_start(inode, 1); 239964769240SAlex Tomas if (IS_ERR(handle)) { 240064769240SAlex Tomas ret = PTR_ERR(handle); 240164769240SAlex Tomas goto out; 240264769240SAlex Tomas } 2403ebd3610bSJan Kara /* We cannot recurse into the filesystem as the transaction is already 2404ebd3610bSJan Kara * started */ 2405ebd3610bSJan Kara flags |= AOP_FLAG_NOFS; 240664769240SAlex Tomas 240754566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 2408d5a0d4f7SEric Sandeen if (!page) { 2409d5a0d4f7SEric Sandeen ext4_journal_stop(handle); 2410d5a0d4f7SEric Sandeen ret = -ENOMEM; 2411d5a0d4f7SEric Sandeen goto out; 2412d5a0d4f7SEric Sandeen } 241364769240SAlex Tomas *pagep = page; 241464769240SAlex Tomas 24156e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep); 241664769240SAlex Tomas if (ret < 0) { 241764769240SAlex Tomas unlock_page(page); 241864769240SAlex Tomas ext4_journal_stop(handle); 241964769240SAlex Tomas page_cache_release(page); 2420ae4d5372SAneesh Kumar K.V /* 2421ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 2422ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 2423ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 2424ae4d5372SAneesh Kumar K.V */ 2425ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 2426b9a4207dSJan Kara ext4_truncate_failed_write(inode); 242702fac129SAllison Henderson } else { 242802fac129SAllison Henderson page_len = pos & (PAGE_CACHE_SIZE - 1); 242902fac129SAllison Henderson if (page_len > 0) { 243002fac129SAllison Henderson ret = ext4_discard_partial_page_buffers_no_lock(handle, 243102fac129SAllison Henderson inode, page, pos - page_len, page_len, 243202fac129SAllison Henderson EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED); 243302fac129SAllison Henderson } 243464769240SAlex Tomas } 243564769240SAlex Tomas 2436d2a17637SMingming Cao if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 2437d2a17637SMingming Cao goto retry; 243864769240SAlex Tomas out: 243964769240SAlex Tomas return ret; 244064769240SAlex Tomas } 244164769240SAlex Tomas 2442632eaeabSMingming Cao /* 2443632eaeabSMingming Cao * Check if we should update i_disksize 2444632eaeabSMingming Cao * when write to the end of file but not require block allocation 2445632eaeabSMingming Cao */ 2446632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page, 2447632eaeabSMingming Cao unsigned long offset) 2448632eaeabSMingming Cao { 2449632eaeabSMingming Cao struct buffer_head *bh; 2450632eaeabSMingming Cao struct inode *inode = page->mapping->host; 2451632eaeabSMingming Cao unsigned int idx; 2452632eaeabSMingming Cao int i; 2453632eaeabSMingming Cao 2454632eaeabSMingming Cao bh = page_buffers(page); 2455632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 2456632eaeabSMingming Cao 2457632eaeabSMingming Cao for (i = 0; i < idx; i++) 2458632eaeabSMingming Cao bh = bh->b_this_page; 2459632eaeabSMingming Cao 246029fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 2461632eaeabSMingming Cao return 0; 2462632eaeabSMingming Cao return 1; 2463632eaeabSMingming Cao } 2464632eaeabSMingming Cao 246564769240SAlex Tomas static int ext4_da_write_end(struct file *file, 246664769240SAlex Tomas struct address_space *mapping, 246764769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 246864769240SAlex Tomas struct page *page, void *fsdata) 246964769240SAlex Tomas { 247064769240SAlex Tomas struct inode *inode = mapping->host; 247164769240SAlex Tomas int ret = 0, ret2; 247264769240SAlex Tomas handle_t *handle = ext4_journal_current_handle(); 247364769240SAlex Tomas loff_t new_i_size; 2474632eaeabSMingming Cao unsigned long start, end; 247579f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 247602fac129SAllison Henderson loff_t page_len; 247779f0be8dSAneesh Kumar K.V 247879f0be8dSAneesh Kumar K.V if (write_mode == FALL_BACK_TO_NONDELALLOC) { 247979f0be8dSAneesh Kumar K.V if (ext4_should_order_data(inode)) { 248079f0be8dSAneesh Kumar K.V return ext4_ordered_write_end(file, mapping, pos, 248179f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 248279f0be8dSAneesh Kumar K.V } else if (ext4_should_writeback_data(inode)) { 248379f0be8dSAneesh Kumar K.V return ext4_writeback_write_end(file, mapping, pos, 248479f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 248579f0be8dSAneesh Kumar K.V } else { 248679f0be8dSAneesh Kumar K.V BUG(); 248779f0be8dSAneesh Kumar K.V } 248879f0be8dSAneesh Kumar K.V } 2489632eaeabSMingming Cao 24909bffad1eSTheodore Ts'o trace_ext4_da_write_end(inode, pos, len, copied); 2491632eaeabSMingming Cao start = pos & (PAGE_CACHE_SIZE - 1); 2492632eaeabSMingming Cao end = start + copied - 1; 249364769240SAlex Tomas 249464769240SAlex Tomas /* 249564769240SAlex Tomas * generic_write_end() will run mark_inode_dirty() if i_size 249664769240SAlex Tomas * changes. So let's piggyback the i_disksize mark_inode_dirty 249764769240SAlex Tomas * into that. 249864769240SAlex Tomas */ 249964769240SAlex Tomas 250064769240SAlex Tomas new_i_size = pos + copied; 2501632eaeabSMingming Cao if (new_i_size > EXT4_I(inode)->i_disksize) { 2502632eaeabSMingming Cao if (ext4_da_should_update_i_disksize(page, end)) { 2503632eaeabSMingming Cao down_write(&EXT4_I(inode)->i_data_sem); 2504632eaeabSMingming Cao if (new_i_size > EXT4_I(inode)->i_disksize) { 250564769240SAlex Tomas /* 2506632eaeabSMingming Cao * Updating i_disksize when extending file 2507632eaeabSMingming Cao * without needing block allocation 250864769240SAlex Tomas */ 250964769240SAlex Tomas if (ext4_should_order_data(inode)) 2510632eaeabSMingming Cao ret = ext4_jbd2_file_inode(handle, 2511632eaeabSMingming Cao inode); 251264769240SAlex Tomas 251364769240SAlex Tomas EXT4_I(inode)->i_disksize = new_i_size; 251464769240SAlex Tomas } 2515632eaeabSMingming Cao up_write(&EXT4_I(inode)->i_data_sem); 2516cf17fea6SAneesh Kumar K.V /* We need to mark inode dirty even if 2517cf17fea6SAneesh Kumar K.V * new_i_size is less that inode->i_size 2518cf17fea6SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 2519cf17fea6SAneesh Kumar K.V */ 2520cf17fea6SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 2521632eaeabSMingming Cao } 2522632eaeabSMingming Cao } 252364769240SAlex Tomas ret2 = generic_write_end(file, mapping, pos, len, copied, 252464769240SAlex Tomas page, fsdata); 252502fac129SAllison Henderson 252602fac129SAllison Henderson page_len = PAGE_CACHE_SIZE - 252702fac129SAllison Henderson ((pos + copied - 1) & (PAGE_CACHE_SIZE - 1)); 252802fac129SAllison Henderson 252902fac129SAllison Henderson if (page_len > 0) { 253002fac129SAllison Henderson ret = ext4_discard_partial_page_buffers_no_lock(handle, 253102fac129SAllison Henderson inode, page, pos + copied - 1, page_len, 253202fac129SAllison Henderson EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED); 253302fac129SAllison Henderson } 253402fac129SAllison Henderson 253564769240SAlex Tomas copied = ret2; 253664769240SAlex Tomas if (ret2 < 0) 253764769240SAlex Tomas ret = ret2; 253864769240SAlex Tomas ret2 = ext4_journal_stop(handle); 253964769240SAlex Tomas if (!ret) 254064769240SAlex Tomas ret = ret2; 254164769240SAlex Tomas 254264769240SAlex Tomas return ret ? ret : copied; 254364769240SAlex Tomas } 254464769240SAlex Tomas 254564769240SAlex Tomas static void ext4_da_invalidatepage(struct page *page, unsigned long offset) 254664769240SAlex Tomas { 254764769240SAlex Tomas /* 254864769240SAlex Tomas * Drop reserved blocks 254964769240SAlex Tomas */ 255064769240SAlex Tomas BUG_ON(!PageLocked(page)); 255164769240SAlex Tomas if (!page_has_buffers(page)) 255264769240SAlex Tomas goto out; 255364769240SAlex Tomas 2554d2a17637SMingming Cao ext4_da_page_release_reservation(page, offset); 255564769240SAlex Tomas 255664769240SAlex Tomas out: 255764769240SAlex Tomas ext4_invalidatepage(page, offset); 255864769240SAlex Tomas 255964769240SAlex Tomas return; 256064769240SAlex Tomas } 256164769240SAlex Tomas 2562ccd2506bSTheodore Ts'o /* 2563ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 2564ccd2506bSTheodore Ts'o */ 2565ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 2566ccd2506bSTheodore Ts'o { 2567fb40ba0dSTheodore Ts'o trace_ext4_alloc_da_blocks(inode); 2568fb40ba0dSTheodore Ts'o 2569ccd2506bSTheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks && 2570ccd2506bSTheodore Ts'o !EXT4_I(inode)->i_reserved_meta_blocks) 2571ccd2506bSTheodore Ts'o return 0; 2572ccd2506bSTheodore Ts'o 2573ccd2506bSTheodore Ts'o /* 2574ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 2575ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 2576ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 2577ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 2578ccd2506bSTheodore Ts'o * would require replicating code paths in: 2579ccd2506bSTheodore Ts'o * 2580ccd2506bSTheodore Ts'o * ext4_da_writepages() -> 2581ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 2582ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 2583ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 2584ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 2585ccd2506bSTheodore Ts'o * 2586ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 2587ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 2588ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 2589ccd2506bSTheodore Ts'o * doing I/O at all. 2590ccd2506bSTheodore Ts'o * 2591ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 2592380cf090SWu Fengguang * the pages by calling redirty_page_for_writepage() but that 2593ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 2594ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 259525985edcSLucas De Marchi * simplifying them because we wouldn't actually intend to 2596ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 2597ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 2598ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 2599ccd2506bSTheodore Ts'o * 2600ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 2601ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 2602ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 2603ccd2506bSTheodore Ts'o */ 2604ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 2605ccd2506bSTheodore Ts'o } 260664769240SAlex Tomas 260764769240SAlex Tomas /* 2608ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 2609ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 2610ac27a0ecSDave Kleikamp * 2611ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 2612617ba13bSMingming Cao * journal. If somebody makes a swapfile on an ext4 data-journaling 2613ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 2614ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 2615ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 2616ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 2617ac27a0ecSDave Kleikamp * 2618ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 2619ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 2620ac27a0ecSDave Kleikamp */ 2621617ba13bSMingming Cao static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 2622ac27a0ecSDave Kleikamp { 2623ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 2624ac27a0ecSDave Kleikamp journal_t *journal; 2625ac27a0ecSDave Kleikamp int err; 2626ac27a0ecSDave Kleikamp 262764769240SAlex Tomas if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 262864769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) { 262964769240SAlex Tomas /* 263064769240SAlex Tomas * With delalloc we want to sync the file 263164769240SAlex Tomas * so that we can make sure we allocate 263264769240SAlex Tomas * blocks for file 263364769240SAlex Tomas */ 263464769240SAlex Tomas filemap_write_and_wait(mapping); 263564769240SAlex Tomas } 263664769240SAlex Tomas 263719f5fb7aSTheodore Ts'o if (EXT4_JOURNAL(inode) && 263819f5fb7aSTheodore Ts'o ext4_test_inode_state(inode, EXT4_STATE_JDATA)) { 2639ac27a0ecSDave Kleikamp /* 2640ac27a0ecSDave Kleikamp * This is a REALLY heavyweight approach, but the use of 2641ac27a0ecSDave Kleikamp * bmap on dirty files is expected to be extremely rare: 2642ac27a0ecSDave Kleikamp * only if we run lilo or swapon on a freshly made file 2643ac27a0ecSDave Kleikamp * do we expect this to happen. 2644ac27a0ecSDave Kleikamp * 2645ac27a0ecSDave Kleikamp * (bmap requires CAP_SYS_RAWIO so this does not 2646ac27a0ecSDave Kleikamp * represent an unprivileged user DOS attack --- we'd be 2647ac27a0ecSDave Kleikamp * in trouble if mortal users could trigger this path at 2648ac27a0ecSDave Kleikamp * will.) 2649ac27a0ecSDave Kleikamp * 2650617ba13bSMingming Cao * NB. EXT4_STATE_JDATA is not set on files other than 2651ac27a0ecSDave Kleikamp * regular files. If somebody wants to bmap a directory 2652ac27a0ecSDave Kleikamp * or symlink and gets confused because the buffer 2653ac27a0ecSDave Kleikamp * hasn't yet been flushed to disk, they deserve 2654ac27a0ecSDave Kleikamp * everything they get. 2655ac27a0ecSDave Kleikamp */ 2656ac27a0ecSDave Kleikamp 265719f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_JDATA); 2658617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 2659dab291afSMingming Cao jbd2_journal_lock_updates(journal); 2660dab291afSMingming Cao err = jbd2_journal_flush(journal); 2661dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 2662ac27a0ecSDave Kleikamp 2663ac27a0ecSDave Kleikamp if (err) 2664ac27a0ecSDave Kleikamp return 0; 2665ac27a0ecSDave Kleikamp } 2666ac27a0ecSDave Kleikamp 2667617ba13bSMingming Cao return generic_block_bmap(mapping, block, ext4_get_block); 2668ac27a0ecSDave Kleikamp } 2669ac27a0ecSDave Kleikamp 2670617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page) 2671ac27a0ecSDave Kleikamp { 26720562e0baSJiaying Zhang trace_ext4_readpage(page); 2673617ba13bSMingming Cao return mpage_readpage(page, ext4_get_block); 2674ac27a0ecSDave Kleikamp } 2675ac27a0ecSDave Kleikamp 2676ac27a0ecSDave Kleikamp static int 2677617ba13bSMingming Cao ext4_readpages(struct file *file, struct address_space *mapping, 2678ac27a0ecSDave Kleikamp struct list_head *pages, unsigned nr_pages) 2679ac27a0ecSDave Kleikamp { 2680617ba13bSMingming Cao return mpage_readpages(mapping, pages, nr_pages, ext4_get_block); 2681ac27a0ecSDave Kleikamp } 2682ac27a0ecSDave Kleikamp 2683744692dcSJiaying Zhang static void ext4_invalidatepage_free_endio(struct page *page, unsigned long offset) 2684744692dcSJiaying Zhang { 2685744692dcSJiaying Zhang struct buffer_head *head, *bh; 2686744692dcSJiaying Zhang unsigned int curr_off = 0; 2687744692dcSJiaying Zhang 2688744692dcSJiaying Zhang if (!page_has_buffers(page)) 2689744692dcSJiaying Zhang return; 2690744692dcSJiaying Zhang head = bh = page_buffers(page); 2691744692dcSJiaying Zhang do { 2692744692dcSJiaying Zhang if (offset <= curr_off && test_clear_buffer_uninit(bh) 2693744692dcSJiaying Zhang && bh->b_private) { 2694744692dcSJiaying Zhang ext4_free_io_end(bh->b_private); 2695744692dcSJiaying Zhang bh->b_private = NULL; 2696744692dcSJiaying Zhang bh->b_end_io = NULL; 2697744692dcSJiaying Zhang } 2698744692dcSJiaying Zhang curr_off = curr_off + bh->b_size; 2699744692dcSJiaying Zhang bh = bh->b_this_page; 2700744692dcSJiaying Zhang } while (bh != head); 2701744692dcSJiaying Zhang } 2702744692dcSJiaying Zhang 2703617ba13bSMingming Cao static void ext4_invalidatepage(struct page *page, unsigned long offset) 2704ac27a0ecSDave Kleikamp { 2705617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 2706ac27a0ecSDave Kleikamp 27070562e0baSJiaying Zhang trace_ext4_invalidatepage(page, offset); 27080562e0baSJiaying Zhang 2709ac27a0ecSDave Kleikamp /* 2710744692dcSJiaying Zhang * free any io_end structure allocated for buffers to be discarded 2711744692dcSJiaying Zhang */ 2712744692dcSJiaying Zhang if (ext4_should_dioread_nolock(page->mapping->host)) 2713744692dcSJiaying Zhang ext4_invalidatepage_free_endio(page, offset); 2714744692dcSJiaying Zhang /* 2715ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 2716ac27a0ecSDave Kleikamp */ 2717ac27a0ecSDave Kleikamp if (offset == 0) 2718ac27a0ecSDave Kleikamp ClearPageChecked(page); 2719ac27a0ecSDave Kleikamp 27200390131bSFrank Mayhar if (journal) 2721dab291afSMingming Cao jbd2_journal_invalidatepage(journal, page, offset); 27220390131bSFrank Mayhar else 27230390131bSFrank Mayhar block_invalidatepage(page, offset); 2724ac27a0ecSDave Kleikamp } 2725ac27a0ecSDave Kleikamp 2726617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait) 2727ac27a0ecSDave Kleikamp { 2728617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 2729ac27a0ecSDave Kleikamp 27300562e0baSJiaying Zhang trace_ext4_releasepage(page); 27310562e0baSJiaying Zhang 2732ac27a0ecSDave Kleikamp WARN_ON(PageChecked(page)); 2733ac27a0ecSDave Kleikamp if (!page_has_buffers(page)) 2734ac27a0ecSDave Kleikamp return 0; 27350390131bSFrank Mayhar if (journal) 2736dab291afSMingming Cao return jbd2_journal_try_to_free_buffers(journal, page, wait); 27370390131bSFrank Mayhar else 27380390131bSFrank Mayhar return try_to_free_buffers(page); 2739ac27a0ecSDave Kleikamp } 2740ac27a0ecSDave Kleikamp 2741ac27a0ecSDave Kleikamp /* 27422ed88685STheodore Ts'o * ext4_get_block used when preparing for a DIO write or buffer write. 27432ed88685STheodore Ts'o * We allocate an uinitialized extent if blocks haven't been allocated. 27442ed88685STheodore Ts'o * The extent will be converted to initialized after the IO is complete. 27452ed88685STheodore Ts'o */ 2746c7064ef1SJiaying Zhang static int ext4_get_block_write(struct inode *inode, sector_t iblock, 27474c0425ffSMingming Cao struct buffer_head *bh_result, int create) 27484c0425ffSMingming Cao { 2749c7064ef1SJiaying Zhang ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n", 27508d5d02e6SMingming Cao inode->i_ino, create); 27512ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh_result, 27522ed88685STheodore Ts'o EXT4_GET_BLOCKS_IO_CREATE_EXT); 27534c0425ffSMingming Cao } 27544c0425ffSMingming Cao 27554c0425ffSMingming Cao static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset, 2756552ef802SChristoph Hellwig ssize_t size, void *private, int ret, 2757552ef802SChristoph Hellwig bool is_async) 27584c0425ffSMingming Cao { 275972c5052dSChristoph Hellwig struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode; 27604c0425ffSMingming Cao ext4_io_end_t *io_end = iocb->private; 27614c0425ffSMingming Cao struct workqueue_struct *wq; 2762744692dcSJiaying Zhang unsigned long flags; 2763744692dcSJiaying Zhang struct ext4_inode_info *ei; 27644c0425ffSMingming Cao 27654b70df18SMingming /* if not async direct IO or dio with 0 bytes write, just return */ 27664b70df18SMingming if (!io_end || !size) 2767552ef802SChristoph Hellwig goto out; 27684b70df18SMingming 27698d5d02e6SMingming Cao ext_debug("ext4_end_io_dio(): io_end 0x%p" 27708d5d02e6SMingming Cao "for inode %lu, iocb 0x%p, offset %llu, size %llu\n", 27718d5d02e6SMingming Cao iocb->private, io_end->inode->i_ino, iocb, offset, 27728d5d02e6SMingming Cao size); 27738d5d02e6SMingming Cao 27748d5d02e6SMingming Cao /* if not aio dio with unwritten extents, just free io and return */ 2775bd2d0210STheodore Ts'o if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) { 27768d5d02e6SMingming Cao ext4_free_io_end(io_end); 27778d5d02e6SMingming Cao iocb->private = NULL; 27785b3ff237Sjiayingz@google.com (Jiaying Zhang) out: 27795b3ff237Sjiayingz@google.com (Jiaying Zhang) if (is_async) 27805b3ff237Sjiayingz@google.com (Jiaying Zhang) aio_complete(iocb, ret, 0); 278172c5052dSChristoph Hellwig inode_dio_done(inode); 27825b3ff237Sjiayingz@google.com (Jiaying Zhang) return; 27838d5d02e6SMingming Cao } 27848d5d02e6SMingming Cao 27854c0425ffSMingming Cao io_end->offset = offset; 27864c0425ffSMingming Cao io_end->size = size; 27875b3ff237Sjiayingz@google.com (Jiaying Zhang) if (is_async) { 27885b3ff237Sjiayingz@google.com (Jiaying Zhang) io_end->iocb = iocb; 27895b3ff237Sjiayingz@google.com (Jiaying Zhang) io_end->result = ret; 27905b3ff237Sjiayingz@google.com (Jiaying Zhang) } 27914c0425ffSMingming Cao wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq; 27924c0425ffSMingming Cao 27938d5d02e6SMingming Cao /* Add the io_end to per-inode completed aio dio list*/ 2794744692dcSJiaying Zhang ei = EXT4_I(io_end->inode); 2795744692dcSJiaying Zhang spin_lock_irqsave(&ei->i_completed_io_lock, flags); 2796744692dcSJiaying Zhang list_add_tail(&io_end->list, &ei->i_completed_io_list); 2797744692dcSJiaying Zhang spin_unlock_irqrestore(&ei->i_completed_io_lock, flags); 2798c999af2bSEric Sandeen 2799c999af2bSEric Sandeen /* queue the work to convert unwritten extents to written */ 2800c999af2bSEric Sandeen queue_work(wq, &io_end->work); 28014c0425ffSMingming Cao iocb->private = NULL; 280272c5052dSChristoph Hellwig 280372c5052dSChristoph Hellwig /* XXX: probably should move into the real I/O completion handler */ 280472c5052dSChristoph Hellwig inode_dio_done(inode); 28054c0425ffSMingming Cao } 2806c7064ef1SJiaying Zhang 2807744692dcSJiaying Zhang static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate) 2808744692dcSJiaying Zhang { 2809744692dcSJiaying Zhang ext4_io_end_t *io_end = bh->b_private; 2810744692dcSJiaying Zhang struct workqueue_struct *wq; 2811744692dcSJiaying Zhang struct inode *inode; 2812744692dcSJiaying Zhang unsigned long flags; 2813744692dcSJiaying Zhang 2814744692dcSJiaying Zhang if (!test_clear_buffer_uninit(bh) || !io_end) 2815744692dcSJiaying Zhang goto out; 2816744692dcSJiaying Zhang 2817744692dcSJiaying Zhang if (!(io_end->inode->i_sb->s_flags & MS_ACTIVE)) { 2818744692dcSJiaying Zhang printk("sb umounted, discard end_io request for inode %lu\n", 2819744692dcSJiaying Zhang io_end->inode->i_ino); 2820744692dcSJiaying Zhang ext4_free_io_end(io_end); 2821744692dcSJiaying Zhang goto out; 2822744692dcSJiaying Zhang } 2823744692dcSJiaying Zhang 282432c80b32STao Ma /* 282532c80b32STao Ma * It may be over-defensive here to check EXT4_IO_END_UNWRITTEN now, 282632c80b32STao Ma * but being more careful is always safe for the future change. 282732c80b32STao Ma */ 2828744692dcSJiaying Zhang inode = io_end->inode; 282932c80b32STao Ma if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) { 283032c80b32STao Ma io_end->flag |= EXT4_IO_END_UNWRITTEN; 283132c80b32STao Ma atomic_inc(&EXT4_I(inode)->i_aiodio_unwritten); 283232c80b32STao Ma } 2833744692dcSJiaying Zhang 2834744692dcSJiaying Zhang /* Add the io_end to per-inode completed io list*/ 2835744692dcSJiaying Zhang spin_lock_irqsave(&EXT4_I(inode)->i_completed_io_lock, flags); 2836744692dcSJiaying Zhang list_add_tail(&io_end->list, &EXT4_I(inode)->i_completed_io_list); 2837744692dcSJiaying Zhang spin_unlock_irqrestore(&EXT4_I(inode)->i_completed_io_lock, flags); 2838744692dcSJiaying Zhang 2839744692dcSJiaying Zhang wq = EXT4_SB(inode->i_sb)->dio_unwritten_wq; 2840744692dcSJiaying Zhang /* queue the work to convert unwritten extents to written */ 2841744692dcSJiaying Zhang queue_work(wq, &io_end->work); 2842744692dcSJiaying Zhang out: 2843744692dcSJiaying Zhang bh->b_private = NULL; 2844744692dcSJiaying Zhang bh->b_end_io = NULL; 2845744692dcSJiaying Zhang clear_buffer_uninit(bh); 2846744692dcSJiaying Zhang end_buffer_async_write(bh, uptodate); 2847744692dcSJiaying Zhang } 2848744692dcSJiaying Zhang 2849744692dcSJiaying Zhang static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode) 2850744692dcSJiaying Zhang { 2851744692dcSJiaying Zhang ext4_io_end_t *io_end; 2852744692dcSJiaying Zhang struct page *page = bh->b_page; 2853744692dcSJiaying Zhang loff_t offset = (sector_t)page->index << PAGE_CACHE_SHIFT; 2854744692dcSJiaying Zhang size_t size = bh->b_size; 2855744692dcSJiaying Zhang 2856744692dcSJiaying Zhang retry: 2857744692dcSJiaying Zhang io_end = ext4_init_io_end(inode, GFP_ATOMIC); 2858744692dcSJiaying Zhang if (!io_end) { 28596db26ffcSAndrew Morton pr_warn_ratelimited("%s: allocation fail\n", __func__); 2860744692dcSJiaying Zhang schedule(); 2861744692dcSJiaying Zhang goto retry; 2862744692dcSJiaying Zhang } 2863744692dcSJiaying Zhang io_end->offset = offset; 2864744692dcSJiaying Zhang io_end->size = size; 2865744692dcSJiaying Zhang /* 2866744692dcSJiaying Zhang * We need to hold a reference to the page to make sure it 2867744692dcSJiaying Zhang * doesn't get evicted before ext4_end_io_work() has a chance 2868744692dcSJiaying Zhang * to convert the extent from written to unwritten. 2869744692dcSJiaying Zhang */ 2870744692dcSJiaying Zhang io_end->page = page; 2871744692dcSJiaying Zhang get_page(io_end->page); 2872744692dcSJiaying Zhang 2873744692dcSJiaying Zhang bh->b_private = io_end; 2874744692dcSJiaying Zhang bh->b_end_io = ext4_end_io_buffer_write; 2875744692dcSJiaying Zhang return 0; 2876744692dcSJiaying Zhang } 2877744692dcSJiaying Zhang 28784c0425ffSMingming Cao /* 28794c0425ffSMingming Cao * For ext4 extent files, ext4 will do direct-io write to holes, 28804c0425ffSMingming Cao * preallocated extents, and those write extend the file, no need to 28814c0425ffSMingming Cao * fall back to buffered IO. 28824c0425ffSMingming Cao * 2883b595076aSUwe Kleine-König * For holes, we fallocate those blocks, mark them as uninitialized 28844c0425ffSMingming Cao * If those blocks were preallocated, we mark sure they are splited, but 2885b595076aSUwe Kleine-König * still keep the range to write as uninitialized. 28864c0425ffSMingming Cao * 28878d5d02e6SMingming Cao * The unwrritten extents will be converted to written when DIO is completed. 28888d5d02e6SMingming Cao * For async direct IO, since the IO may still pending when return, we 288925985edcSLucas De Marchi * set up an end_io call back function, which will do the conversion 28908d5d02e6SMingming Cao * when async direct IO completed. 28914c0425ffSMingming Cao * 28924c0425ffSMingming Cao * If the O_DIRECT write will extend the file then add this inode to the 28934c0425ffSMingming Cao * orphan list. So recovery will truncate it back to the original size 28944c0425ffSMingming Cao * if the machine crashes during the write. 28954c0425ffSMingming Cao * 28964c0425ffSMingming Cao */ 28974c0425ffSMingming Cao static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb, 28984c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 28994c0425ffSMingming Cao unsigned long nr_segs) 29004c0425ffSMingming Cao { 29014c0425ffSMingming Cao struct file *file = iocb->ki_filp; 29024c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 29034c0425ffSMingming Cao ssize_t ret; 29044c0425ffSMingming Cao size_t count = iov_length(iov, nr_segs); 29054c0425ffSMingming Cao 29064c0425ffSMingming Cao loff_t final_size = offset + count; 29074c0425ffSMingming Cao if (rw == WRITE && final_size <= inode->i_size) { 29084c0425ffSMingming Cao /* 29098d5d02e6SMingming Cao * We could direct write to holes and fallocate. 29108d5d02e6SMingming Cao * 29118d5d02e6SMingming Cao * Allocated blocks to fill the hole are marked as uninitialized 291225985edcSLucas De Marchi * to prevent parallel buffered read to expose the stale data 29134c0425ffSMingming Cao * before DIO complete the data IO. 29148d5d02e6SMingming Cao * 29158d5d02e6SMingming Cao * As to previously fallocated extents, ext4 get_block 29164c0425ffSMingming Cao * will just simply mark the buffer mapped but still 29174c0425ffSMingming Cao * keep the extents uninitialized. 29184c0425ffSMingming Cao * 29198d5d02e6SMingming Cao * for non AIO case, we will convert those unwritten extents 29208d5d02e6SMingming Cao * to written after return back from blockdev_direct_IO. 29214c0425ffSMingming Cao * 29228d5d02e6SMingming Cao * for async DIO, the conversion needs to be defered when 29238d5d02e6SMingming Cao * the IO is completed. The ext4 end_io callback function 29248d5d02e6SMingming Cao * will be called to take care of the conversion work. 29258d5d02e6SMingming Cao * Here for async case, we allocate an io_end structure to 29268d5d02e6SMingming Cao * hook to the iocb. 29274c0425ffSMingming Cao */ 29288d5d02e6SMingming Cao iocb->private = NULL; 29298d5d02e6SMingming Cao EXT4_I(inode)->cur_aio_dio = NULL; 29308d5d02e6SMingming Cao if (!is_sync_kiocb(iocb)) { 2931744692dcSJiaying Zhang iocb->private = ext4_init_io_end(inode, GFP_NOFS); 29324c0425ffSMingming Cao if (!iocb->private) 29334c0425ffSMingming Cao return -ENOMEM; 29348d5d02e6SMingming Cao /* 29358d5d02e6SMingming Cao * we save the io structure for current async 293679e83036SEric Sandeen * direct IO, so that later ext4_map_blocks() 29378d5d02e6SMingming Cao * could flag the io structure whether there 29388d5d02e6SMingming Cao * is a unwritten extents needs to be converted 29398d5d02e6SMingming Cao * when IO is completed. 29408d5d02e6SMingming Cao */ 29418d5d02e6SMingming Cao EXT4_I(inode)->cur_aio_dio = iocb->private; 29428d5d02e6SMingming Cao } 29438d5d02e6SMingming Cao 2944aacfc19cSChristoph Hellwig ret = __blockdev_direct_IO(rw, iocb, inode, 29454c0425ffSMingming Cao inode->i_sb->s_bdev, iov, 29464c0425ffSMingming Cao offset, nr_segs, 2947c7064ef1SJiaying Zhang ext4_get_block_write, 2948aacfc19cSChristoph Hellwig ext4_end_io_dio, 2949aacfc19cSChristoph Hellwig NULL, 2950aacfc19cSChristoph Hellwig DIO_LOCKING | DIO_SKIP_HOLES); 29518d5d02e6SMingming Cao if (iocb->private) 29528d5d02e6SMingming Cao EXT4_I(inode)->cur_aio_dio = NULL; 29538d5d02e6SMingming Cao /* 29548d5d02e6SMingming Cao * The io_end structure takes a reference to the inode, 29558d5d02e6SMingming Cao * that structure needs to be destroyed and the 29568d5d02e6SMingming Cao * reference to the inode need to be dropped, when IO is 29578d5d02e6SMingming Cao * complete, even with 0 byte write, or failed. 29588d5d02e6SMingming Cao * 29598d5d02e6SMingming Cao * In the successful AIO DIO case, the io_end structure will be 29608d5d02e6SMingming Cao * desctroyed and the reference to the inode will be dropped 29618d5d02e6SMingming Cao * after the end_io call back function is called. 29628d5d02e6SMingming Cao * 29638d5d02e6SMingming Cao * In the case there is 0 byte write, or error case, since 29648d5d02e6SMingming Cao * VFS direct IO won't invoke the end_io call back function, 29658d5d02e6SMingming Cao * we need to free the end_io structure here. 29668d5d02e6SMingming Cao */ 29678d5d02e6SMingming Cao if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) { 29688d5d02e6SMingming Cao ext4_free_io_end(iocb->private); 29698d5d02e6SMingming Cao iocb->private = NULL; 297019f5fb7aSTheodore Ts'o } else if (ret > 0 && ext4_test_inode_state(inode, 29715f524950SMingming EXT4_STATE_DIO_UNWRITTEN)) { 2972109f5565SMingming int err; 29738d5d02e6SMingming Cao /* 29748d5d02e6SMingming Cao * for non AIO case, since the IO is already 297525985edcSLucas De Marchi * completed, we could do the conversion right here 29768d5d02e6SMingming Cao */ 2977109f5565SMingming err = ext4_convert_unwritten_extents(inode, 29788d5d02e6SMingming Cao offset, ret); 2979109f5565SMingming if (err < 0) 2980109f5565SMingming ret = err; 298119f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN); 2982109f5565SMingming } 29834c0425ffSMingming Cao return ret; 29844c0425ffSMingming Cao } 29858d5d02e6SMingming Cao 29868d5d02e6SMingming Cao /* for write the the end of file case, we fall back to old way */ 29874c0425ffSMingming Cao return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 29884c0425ffSMingming Cao } 29894c0425ffSMingming Cao 29904c0425ffSMingming Cao static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb, 29914c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 29924c0425ffSMingming Cao unsigned long nr_segs) 29934c0425ffSMingming Cao { 29944c0425ffSMingming Cao struct file *file = iocb->ki_filp; 29954c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 29960562e0baSJiaying Zhang ssize_t ret; 29974c0425ffSMingming Cao 299884ebd795STheodore Ts'o /* 299984ebd795STheodore Ts'o * If we are doing data journalling we don't support O_DIRECT 300084ebd795STheodore Ts'o */ 300184ebd795STheodore Ts'o if (ext4_should_journal_data(inode)) 300284ebd795STheodore Ts'o return 0; 300384ebd795STheodore Ts'o 30040562e0baSJiaying Zhang trace_ext4_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw); 300512e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 30060562e0baSJiaying Zhang ret = ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs); 30070562e0baSJiaying Zhang else 30080562e0baSJiaying Zhang ret = ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 30090562e0baSJiaying Zhang trace_ext4_direct_IO_exit(inode, offset, 30100562e0baSJiaying Zhang iov_length(iov, nr_segs), rw, ret); 30110562e0baSJiaying Zhang return ret; 30124c0425ffSMingming Cao } 30134c0425ffSMingming Cao 3014ac27a0ecSDave Kleikamp /* 3015617ba13bSMingming Cao * Pages can be marked dirty completely asynchronously from ext4's journalling 3016ac27a0ecSDave Kleikamp * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do 3017ac27a0ecSDave Kleikamp * much here because ->set_page_dirty is called under VFS locks. The page is 3018ac27a0ecSDave Kleikamp * not necessarily locked. 3019ac27a0ecSDave Kleikamp * 3020ac27a0ecSDave Kleikamp * We cannot just dirty the page and leave attached buffers clean, because the 3021ac27a0ecSDave Kleikamp * buffers' dirty state is "definitive". We cannot just set the buffers dirty 3022ac27a0ecSDave Kleikamp * or jbddirty because all the journalling code will explode. 3023ac27a0ecSDave Kleikamp * 3024ac27a0ecSDave Kleikamp * So what we do is to mark the page "pending dirty" and next time writepage 3025ac27a0ecSDave Kleikamp * is called, propagate that into the buffers appropriately. 3026ac27a0ecSDave Kleikamp */ 3027617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page) 3028ac27a0ecSDave Kleikamp { 3029ac27a0ecSDave Kleikamp SetPageChecked(page); 3030ac27a0ecSDave Kleikamp return __set_page_dirty_nobuffers(page); 3031ac27a0ecSDave Kleikamp } 3032ac27a0ecSDave Kleikamp 3033617ba13bSMingming Cao static const struct address_space_operations ext4_ordered_aops = { 3034617ba13bSMingming Cao .readpage = ext4_readpage, 3035617ba13bSMingming Cao .readpages = ext4_readpages, 303643ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3037bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3038bfc1af65SNick Piggin .write_end = ext4_ordered_write_end, 3039617ba13bSMingming Cao .bmap = ext4_bmap, 3040617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3041617ba13bSMingming Cao .releasepage = ext4_releasepage, 3042617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 3043ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 30448ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3045aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3046ac27a0ecSDave Kleikamp }; 3047ac27a0ecSDave Kleikamp 3048617ba13bSMingming Cao static const struct address_space_operations ext4_writeback_aops = { 3049617ba13bSMingming Cao .readpage = ext4_readpage, 3050617ba13bSMingming Cao .readpages = ext4_readpages, 305143ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3052bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3053bfc1af65SNick Piggin .write_end = ext4_writeback_write_end, 3054617ba13bSMingming Cao .bmap = ext4_bmap, 3055617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3056617ba13bSMingming Cao .releasepage = ext4_releasepage, 3057617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 3058ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 30598ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3060aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3061ac27a0ecSDave Kleikamp }; 3062ac27a0ecSDave Kleikamp 3063617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 3064617ba13bSMingming Cao .readpage = ext4_readpage, 3065617ba13bSMingming Cao .readpages = ext4_readpages, 306643ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3067bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3068bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 3069617ba13bSMingming Cao .set_page_dirty = ext4_journalled_set_page_dirty, 3070617ba13bSMingming Cao .bmap = ext4_bmap, 3071617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3072617ba13bSMingming Cao .releasepage = ext4_releasepage, 307384ebd795STheodore Ts'o .direct_IO = ext4_direct_IO, 30748ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3075aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3076ac27a0ecSDave Kleikamp }; 3077ac27a0ecSDave Kleikamp 307864769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 307964769240SAlex Tomas .readpage = ext4_readpage, 308064769240SAlex Tomas .readpages = ext4_readpages, 308143ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 308264769240SAlex Tomas .writepages = ext4_da_writepages, 308364769240SAlex Tomas .write_begin = ext4_da_write_begin, 308464769240SAlex Tomas .write_end = ext4_da_write_end, 308564769240SAlex Tomas .bmap = ext4_bmap, 308664769240SAlex Tomas .invalidatepage = ext4_da_invalidatepage, 308764769240SAlex Tomas .releasepage = ext4_releasepage, 308864769240SAlex Tomas .direct_IO = ext4_direct_IO, 308964769240SAlex Tomas .migratepage = buffer_migrate_page, 30908ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3091aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 309264769240SAlex Tomas }; 309364769240SAlex Tomas 3094617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 3095ac27a0ecSDave Kleikamp { 3096cd1aac32SAneesh Kumar K.V if (ext4_should_order_data(inode) && 3097cd1aac32SAneesh Kumar K.V test_opt(inode->i_sb, DELALLOC)) 3098cd1aac32SAneesh Kumar K.V inode->i_mapping->a_ops = &ext4_da_aops; 3099cd1aac32SAneesh Kumar K.V else if (ext4_should_order_data(inode)) 3100617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_ordered_aops; 310164769240SAlex Tomas else if (ext4_should_writeback_data(inode) && 310264769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) 310364769240SAlex Tomas inode->i_mapping->a_ops = &ext4_da_aops; 3104617ba13bSMingming Cao else if (ext4_should_writeback_data(inode)) 3105617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_writeback_aops; 3106ac27a0ecSDave Kleikamp else 3107617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 3108ac27a0ecSDave Kleikamp } 3109ac27a0ecSDave Kleikamp 31104e96b2dbSAllison Henderson 31114e96b2dbSAllison Henderson /* 31124e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers() 31134e96b2dbSAllison Henderson * Wrapper function for ext4_discard_partial_page_buffers_no_lock. 31144e96b2dbSAllison Henderson * This function finds and locks the page containing the offset 31154e96b2dbSAllison Henderson * "from" and passes it to ext4_discard_partial_page_buffers_no_lock. 31164e96b2dbSAllison Henderson * Calling functions that already have the page locked should call 31174e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers_no_lock directly. 31184e96b2dbSAllison Henderson */ 31194e96b2dbSAllison Henderson int ext4_discard_partial_page_buffers(handle_t *handle, 31204e96b2dbSAllison Henderson struct address_space *mapping, loff_t from, 31214e96b2dbSAllison Henderson loff_t length, int flags) 31224e96b2dbSAllison Henderson { 31234e96b2dbSAllison Henderson struct inode *inode = mapping->host; 31244e96b2dbSAllison Henderson struct page *page; 31254e96b2dbSAllison Henderson int err = 0; 31264e96b2dbSAllison Henderson 31274e96b2dbSAllison Henderson page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT, 31284e96b2dbSAllison Henderson mapping_gfp_mask(mapping) & ~__GFP_FS); 31294e96b2dbSAllison Henderson if (!page) 31304e96b2dbSAllison Henderson return -EINVAL; 31314e96b2dbSAllison Henderson 31324e96b2dbSAllison Henderson err = ext4_discard_partial_page_buffers_no_lock(handle, inode, page, 31334e96b2dbSAllison Henderson from, length, flags); 31344e96b2dbSAllison Henderson 31354e96b2dbSAllison Henderson unlock_page(page); 31364e96b2dbSAllison Henderson page_cache_release(page); 31374e96b2dbSAllison Henderson return err; 31384e96b2dbSAllison Henderson } 31394e96b2dbSAllison Henderson 31404e96b2dbSAllison Henderson /* 31414e96b2dbSAllison Henderson * ext4_discard_partial_page_buffers_no_lock() 31424e96b2dbSAllison Henderson * Zeros a page range of length 'length' starting from offset 'from'. 31434e96b2dbSAllison Henderson * Buffer heads that correspond to the block aligned regions of the 31444e96b2dbSAllison Henderson * zeroed range will be unmapped. Unblock aligned regions 31454e96b2dbSAllison Henderson * will have the corresponding buffer head mapped if needed so that 31464e96b2dbSAllison Henderson * that region of the page can be updated with the partial zero out. 31474e96b2dbSAllison Henderson * 31484e96b2dbSAllison Henderson * This function assumes that the page has already been locked. The 31494e96b2dbSAllison Henderson * The range to be discarded must be contained with in the given page. 31504e96b2dbSAllison Henderson * If the specified range exceeds the end of the page it will be shortened 31514e96b2dbSAllison Henderson * to the end of the page that corresponds to 'from'. This function is 31524e96b2dbSAllison Henderson * appropriate for updating a page and it buffer heads to be unmapped and 31534e96b2dbSAllison Henderson * zeroed for blocks that have been either released, or are going to be 31544e96b2dbSAllison Henderson * released. 31554e96b2dbSAllison Henderson * 31564e96b2dbSAllison Henderson * handle: The journal handle 31574e96b2dbSAllison Henderson * inode: The files inode 31584e96b2dbSAllison Henderson * page: A locked page that contains the offset "from" 31594e96b2dbSAllison Henderson * from: The starting byte offset (from the begining of the file) 31604e96b2dbSAllison Henderson * to begin discarding 31614e96b2dbSAllison Henderson * len: The length of bytes to discard 31624e96b2dbSAllison Henderson * flags: Optional flags that may be used: 31634e96b2dbSAllison Henderson * 31644e96b2dbSAllison Henderson * EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED 31654e96b2dbSAllison Henderson * Only zero the regions of the page whose buffer heads 31664e96b2dbSAllison Henderson * have already been unmapped. This flag is appropriate 31674e96b2dbSAllison Henderson * for updateing the contents of a page whose blocks may 31684e96b2dbSAllison Henderson * have already been released, and we only want to zero 31694e96b2dbSAllison Henderson * out the regions that correspond to those released blocks. 31704e96b2dbSAllison Henderson * 31714e96b2dbSAllison Henderson * Returns zero on sucess or negative on failure. 31724e96b2dbSAllison Henderson */ 31734e96b2dbSAllison Henderson int ext4_discard_partial_page_buffers_no_lock(handle_t *handle, 31744e96b2dbSAllison Henderson struct inode *inode, struct page *page, loff_t from, 31754e96b2dbSAllison Henderson loff_t length, int flags) 31764e96b2dbSAllison Henderson { 31774e96b2dbSAllison Henderson ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; 31784e96b2dbSAllison Henderson unsigned int offset = from & (PAGE_CACHE_SIZE-1); 31794e96b2dbSAllison Henderson unsigned int blocksize, max, pos; 31804e96b2dbSAllison Henderson unsigned int end_of_block, range_to_discard; 31814e96b2dbSAllison Henderson ext4_lblk_t iblock; 31824e96b2dbSAllison Henderson struct buffer_head *bh; 31834e96b2dbSAllison Henderson int err = 0; 31844e96b2dbSAllison Henderson 31854e96b2dbSAllison Henderson blocksize = inode->i_sb->s_blocksize; 31864e96b2dbSAllison Henderson max = PAGE_CACHE_SIZE - offset; 31874e96b2dbSAllison Henderson 31884e96b2dbSAllison Henderson if (index != page->index) 31894e96b2dbSAllison Henderson return -EINVAL; 31904e96b2dbSAllison Henderson 31914e96b2dbSAllison Henderson /* 31924e96b2dbSAllison Henderson * correct length if it does not fall between 31934e96b2dbSAllison Henderson * 'from' and the end of the page 31944e96b2dbSAllison Henderson */ 31954e96b2dbSAllison Henderson if (length > max || length < 0) 31964e96b2dbSAllison Henderson length = max; 31974e96b2dbSAllison Henderson 31984e96b2dbSAllison Henderson iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); 31994e96b2dbSAllison Henderson 32004e96b2dbSAllison Henderson if (!page_has_buffers(page)) { 32014e96b2dbSAllison Henderson /* 32024e96b2dbSAllison Henderson * If the range to be discarded covers a partial block 32034e96b2dbSAllison Henderson * we need to get the page buffers. This is because 32044e96b2dbSAllison Henderson * partial blocks cannot be released and the page needs 32054e96b2dbSAllison Henderson * to be updated with the contents of the block before 32064e96b2dbSAllison Henderson * we write the zeros on top of it. 32074e96b2dbSAllison Henderson */ 32084e96b2dbSAllison Henderson if (!(from & (blocksize - 1)) || 32094e96b2dbSAllison Henderson !((from + length) & (blocksize - 1))) { 32104e96b2dbSAllison Henderson create_empty_buffers(page, blocksize, 0); 32114e96b2dbSAllison Henderson } else { 32124e96b2dbSAllison Henderson /* 32134e96b2dbSAllison Henderson * If there are no partial blocks, 32144e96b2dbSAllison Henderson * there is nothing to update, 32154e96b2dbSAllison Henderson * so we can return now 32164e96b2dbSAllison Henderson */ 32174e96b2dbSAllison Henderson return 0; 32184e96b2dbSAllison Henderson } 32194e96b2dbSAllison Henderson } 32204e96b2dbSAllison Henderson 32214e96b2dbSAllison Henderson /* Find the buffer that contains "offset" */ 32224e96b2dbSAllison Henderson bh = page_buffers(page); 32234e96b2dbSAllison Henderson pos = blocksize; 32244e96b2dbSAllison Henderson while (offset >= pos) { 32254e96b2dbSAllison Henderson bh = bh->b_this_page; 32264e96b2dbSAllison Henderson iblock++; 32274e96b2dbSAllison Henderson pos += blocksize; 32284e96b2dbSAllison Henderson } 32294e96b2dbSAllison Henderson 32304e96b2dbSAllison Henderson pos = offset; 32314e96b2dbSAllison Henderson while (pos < offset + length) { 32324e96b2dbSAllison Henderson err = 0; 32334e96b2dbSAllison Henderson 32344e96b2dbSAllison Henderson /* The length of space left to zero and unmap */ 32354e96b2dbSAllison Henderson range_to_discard = offset + length - pos; 32364e96b2dbSAllison Henderson 32374e96b2dbSAllison Henderson /* The length of space until the end of the block */ 32384e96b2dbSAllison Henderson end_of_block = blocksize - (pos & (blocksize-1)); 32394e96b2dbSAllison Henderson 32404e96b2dbSAllison Henderson /* 32414e96b2dbSAllison Henderson * Do not unmap or zero past end of block 32424e96b2dbSAllison Henderson * for this buffer head 32434e96b2dbSAllison Henderson */ 32444e96b2dbSAllison Henderson if (range_to_discard > end_of_block) 32454e96b2dbSAllison Henderson range_to_discard = end_of_block; 32464e96b2dbSAllison Henderson 32474e96b2dbSAllison Henderson 32484e96b2dbSAllison Henderson /* 32494e96b2dbSAllison Henderson * Skip this buffer head if we are only zeroing unampped 32504e96b2dbSAllison Henderson * regions of the page 32514e96b2dbSAllison Henderson */ 32524e96b2dbSAllison Henderson if (flags & EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED && 32534e96b2dbSAllison Henderson buffer_mapped(bh)) 32544e96b2dbSAllison Henderson goto next; 32554e96b2dbSAllison Henderson 32564e96b2dbSAllison Henderson /* If the range is block aligned, unmap */ 32574e96b2dbSAllison Henderson if (range_to_discard == blocksize) { 32584e96b2dbSAllison Henderson clear_buffer_dirty(bh); 32594e96b2dbSAllison Henderson bh->b_bdev = NULL; 32604e96b2dbSAllison Henderson clear_buffer_mapped(bh); 32614e96b2dbSAllison Henderson clear_buffer_req(bh); 32624e96b2dbSAllison Henderson clear_buffer_new(bh); 32634e96b2dbSAllison Henderson clear_buffer_delay(bh); 32644e96b2dbSAllison Henderson clear_buffer_unwritten(bh); 32654e96b2dbSAllison Henderson clear_buffer_uptodate(bh); 32664e96b2dbSAllison Henderson zero_user(page, pos, range_to_discard); 32674e96b2dbSAllison Henderson BUFFER_TRACE(bh, "Buffer discarded"); 32684e96b2dbSAllison Henderson goto next; 32694e96b2dbSAllison Henderson } 32704e96b2dbSAllison Henderson 32714e96b2dbSAllison Henderson /* 32724e96b2dbSAllison Henderson * If this block is not completely contained in the range 32734e96b2dbSAllison Henderson * to be discarded, then it is not going to be released. Because 32744e96b2dbSAllison Henderson * we need to keep this block, we need to make sure this part 32754e96b2dbSAllison Henderson * of the page is uptodate before we modify it by writeing 32764e96b2dbSAllison Henderson * partial zeros on it. 32774e96b2dbSAllison Henderson */ 32784e96b2dbSAllison Henderson if (!buffer_mapped(bh)) { 32794e96b2dbSAllison Henderson /* 32804e96b2dbSAllison Henderson * Buffer head must be mapped before we can read 32814e96b2dbSAllison Henderson * from the block 32824e96b2dbSAllison Henderson */ 32834e96b2dbSAllison Henderson BUFFER_TRACE(bh, "unmapped"); 32844e96b2dbSAllison Henderson ext4_get_block(inode, iblock, bh, 0); 32854e96b2dbSAllison Henderson /* unmapped? It's a hole - nothing to do */ 32864e96b2dbSAllison Henderson if (!buffer_mapped(bh)) { 32874e96b2dbSAllison Henderson BUFFER_TRACE(bh, "still unmapped"); 32884e96b2dbSAllison Henderson goto next; 32894e96b2dbSAllison Henderson } 32904e96b2dbSAllison Henderson } 32914e96b2dbSAllison Henderson 32924e96b2dbSAllison Henderson /* Ok, it's mapped. Make sure it's up-to-date */ 32934e96b2dbSAllison Henderson if (PageUptodate(page)) 32944e96b2dbSAllison Henderson set_buffer_uptodate(bh); 32954e96b2dbSAllison Henderson 32964e96b2dbSAllison Henderson if (!buffer_uptodate(bh)) { 32974e96b2dbSAllison Henderson err = -EIO; 32984e96b2dbSAllison Henderson ll_rw_block(READ, 1, &bh); 32994e96b2dbSAllison Henderson wait_on_buffer(bh); 33004e96b2dbSAllison Henderson /* Uhhuh. Read error. Complain and punt.*/ 33014e96b2dbSAllison Henderson if (!buffer_uptodate(bh)) 33024e96b2dbSAllison Henderson goto next; 33034e96b2dbSAllison Henderson } 33044e96b2dbSAllison Henderson 33054e96b2dbSAllison Henderson if (ext4_should_journal_data(inode)) { 33064e96b2dbSAllison Henderson BUFFER_TRACE(bh, "get write access"); 33074e96b2dbSAllison Henderson err = ext4_journal_get_write_access(handle, bh); 33084e96b2dbSAllison Henderson if (err) 33094e96b2dbSAllison Henderson goto next; 33104e96b2dbSAllison Henderson } 33114e96b2dbSAllison Henderson 33124e96b2dbSAllison Henderson zero_user(page, pos, range_to_discard); 33134e96b2dbSAllison Henderson 33144e96b2dbSAllison Henderson err = 0; 33154e96b2dbSAllison Henderson if (ext4_should_journal_data(inode)) { 33164e96b2dbSAllison Henderson err = ext4_handle_dirty_metadata(handle, inode, bh); 3317decbd919STheodore Ts'o } else 33184e96b2dbSAllison Henderson mark_buffer_dirty(bh); 33194e96b2dbSAllison Henderson 33204e96b2dbSAllison Henderson BUFFER_TRACE(bh, "Partial buffer zeroed"); 33214e96b2dbSAllison Henderson next: 33224e96b2dbSAllison Henderson bh = bh->b_this_page; 33234e96b2dbSAllison Henderson iblock++; 33244e96b2dbSAllison Henderson pos += range_to_discard; 33254e96b2dbSAllison Henderson } 33264e96b2dbSAllison Henderson 33274e96b2dbSAllison Henderson return err; 33284e96b2dbSAllison Henderson } 33294e96b2dbSAllison Henderson 3330ac27a0ecSDave Kleikamp /* 3331617ba13bSMingming Cao * ext4_block_truncate_page() zeroes out a mapping from file offset `from' 3332ac27a0ecSDave Kleikamp * up to the end of the block which corresponds to `from'. 3333ac27a0ecSDave Kleikamp * This required during truncate. We need to physically zero the tail end 3334ac27a0ecSDave Kleikamp * of that block so it doesn't yield old data if the file is later grown. 3335ac27a0ecSDave Kleikamp */ 3336cf108bcaSJan Kara int ext4_block_truncate_page(handle_t *handle, 3337ac27a0ecSDave Kleikamp struct address_space *mapping, loff_t from) 3338ac27a0ecSDave Kleikamp { 333930848851SAllison Henderson unsigned offset = from & (PAGE_CACHE_SIZE-1); 334030848851SAllison Henderson unsigned length; 334130848851SAllison Henderson unsigned blocksize; 334230848851SAllison Henderson struct inode *inode = mapping->host; 334330848851SAllison Henderson 334430848851SAllison Henderson blocksize = inode->i_sb->s_blocksize; 334530848851SAllison Henderson length = blocksize - (offset & (blocksize - 1)); 334630848851SAllison Henderson 334730848851SAllison Henderson return ext4_block_zero_page_range(handle, mapping, from, length); 334830848851SAllison Henderson } 334930848851SAllison Henderson 335030848851SAllison Henderson /* 335130848851SAllison Henderson * ext4_block_zero_page_range() zeros out a mapping of length 'length' 335230848851SAllison Henderson * starting from file offset 'from'. The range to be zero'd must 335330848851SAllison Henderson * be contained with in one block. If the specified range exceeds 335430848851SAllison Henderson * the end of the block it will be shortened to end of the block 335530848851SAllison Henderson * that cooresponds to 'from' 335630848851SAllison Henderson */ 335730848851SAllison Henderson int ext4_block_zero_page_range(handle_t *handle, 335830848851SAllison Henderson struct address_space *mapping, loff_t from, loff_t length) 335930848851SAllison Henderson { 3360617ba13bSMingming Cao ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; 3361ac27a0ecSDave Kleikamp unsigned offset = from & (PAGE_CACHE_SIZE-1); 336230848851SAllison Henderson unsigned blocksize, max, pos; 3363725d26d3SAneesh Kumar K.V ext4_lblk_t iblock; 3364ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 3365ac27a0ecSDave Kleikamp struct buffer_head *bh; 3366cf108bcaSJan Kara struct page *page; 3367ac27a0ecSDave Kleikamp int err = 0; 3368ac27a0ecSDave Kleikamp 3369f4a01017STheodore Ts'o page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT, 3370f4a01017STheodore Ts'o mapping_gfp_mask(mapping) & ~__GFP_FS); 3371cf108bcaSJan Kara if (!page) 3372cf108bcaSJan Kara return -EINVAL; 3373cf108bcaSJan Kara 3374ac27a0ecSDave Kleikamp blocksize = inode->i_sb->s_blocksize; 337530848851SAllison Henderson max = blocksize - (offset & (blocksize - 1)); 337630848851SAllison Henderson 337730848851SAllison Henderson /* 337830848851SAllison Henderson * correct length if it does not fall between 337930848851SAllison Henderson * 'from' and the end of the block 338030848851SAllison Henderson */ 338130848851SAllison Henderson if (length > max || length < 0) 338230848851SAllison Henderson length = max; 338330848851SAllison Henderson 3384ac27a0ecSDave Kleikamp iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); 3385ac27a0ecSDave Kleikamp 3386ac27a0ecSDave Kleikamp if (!page_has_buffers(page)) 3387ac27a0ecSDave Kleikamp create_empty_buffers(page, blocksize, 0); 3388ac27a0ecSDave Kleikamp 3389ac27a0ecSDave Kleikamp /* Find the buffer that contains "offset" */ 3390ac27a0ecSDave Kleikamp bh = page_buffers(page); 3391ac27a0ecSDave Kleikamp pos = blocksize; 3392ac27a0ecSDave Kleikamp while (offset >= pos) { 3393ac27a0ecSDave Kleikamp bh = bh->b_this_page; 3394ac27a0ecSDave Kleikamp iblock++; 3395ac27a0ecSDave Kleikamp pos += blocksize; 3396ac27a0ecSDave Kleikamp } 3397ac27a0ecSDave Kleikamp 3398ac27a0ecSDave Kleikamp err = 0; 3399ac27a0ecSDave Kleikamp if (buffer_freed(bh)) { 3400ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "freed: skip"); 3401ac27a0ecSDave Kleikamp goto unlock; 3402ac27a0ecSDave Kleikamp } 3403ac27a0ecSDave Kleikamp 3404ac27a0ecSDave Kleikamp if (!buffer_mapped(bh)) { 3405ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "unmapped"); 3406617ba13bSMingming Cao ext4_get_block(inode, iblock, bh, 0); 3407ac27a0ecSDave Kleikamp /* unmapped? It's a hole - nothing to do */ 3408ac27a0ecSDave Kleikamp if (!buffer_mapped(bh)) { 3409ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "still unmapped"); 3410ac27a0ecSDave Kleikamp goto unlock; 3411ac27a0ecSDave Kleikamp } 3412ac27a0ecSDave Kleikamp } 3413ac27a0ecSDave Kleikamp 3414ac27a0ecSDave Kleikamp /* Ok, it's mapped. Make sure it's up-to-date */ 3415ac27a0ecSDave Kleikamp if (PageUptodate(page)) 3416ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 3417ac27a0ecSDave Kleikamp 3418ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 3419ac27a0ecSDave Kleikamp err = -EIO; 3420ac27a0ecSDave Kleikamp ll_rw_block(READ, 1, &bh); 3421ac27a0ecSDave Kleikamp wait_on_buffer(bh); 3422ac27a0ecSDave Kleikamp /* Uhhuh. Read error. Complain and punt. */ 3423ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) 3424ac27a0ecSDave Kleikamp goto unlock; 3425ac27a0ecSDave Kleikamp } 3426ac27a0ecSDave Kleikamp 3427617ba13bSMingming Cao if (ext4_should_journal_data(inode)) { 3428ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "get write access"); 3429617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, bh); 3430ac27a0ecSDave Kleikamp if (err) 3431ac27a0ecSDave Kleikamp goto unlock; 3432ac27a0ecSDave Kleikamp } 3433ac27a0ecSDave Kleikamp 3434eebd2aa3SChristoph Lameter zero_user(page, offset, length); 3435ac27a0ecSDave Kleikamp 3436ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "zeroed end of block"); 3437ac27a0ecSDave Kleikamp 3438ac27a0ecSDave Kleikamp err = 0; 3439617ba13bSMingming Cao if (ext4_should_journal_data(inode)) { 34400390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 3441decbd919STheodore Ts'o } else 3442ac27a0ecSDave Kleikamp mark_buffer_dirty(bh); 3443ac27a0ecSDave Kleikamp 3444ac27a0ecSDave Kleikamp unlock: 3445ac27a0ecSDave Kleikamp unlock_page(page); 3446ac27a0ecSDave Kleikamp page_cache_release(page); 3447ac27a0ecSDave Kleikamp return err; 3448ac27a0ecSDave Kleikamp } 3449ac27a0ecSDave Kleikamp 345091ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 345191ef4cafSDuane Griffin { 345291ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 345391ef4cafSDuane Griffin return 1; 345491ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 345591ef4cafSDuane Griffin return 1; 345691ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 345791ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 345891ef4cafSDuane Griffin return 0; 345991ef4cafSDuane Griffin } 346091ef4cafSDuane Griffin 3461ac27a0ecSDave Kleikamp /* 3462a4bb6b64SAllison Henderson * ext4_punch_hole: punches a hole in a file by releaseing the blocks 3463a4bb6b64SAllison Henderson * associated with the given offset and length 3464a4bb6b64SAllison Henderson * 3465a4bb6b64SAllison Henderson * @inode: File inode 3466a4bb6b64SAllison Henderson * @offset: The offset where the hole will begin 3467a4bb6b64SAllison Henderson * @len: The length of the hole 3468a4bb6b64SAllison Henderson * 3469a4bb6b64SAllison Henderson * Returns: 0 on sucess or negative on failure 3470a4bb6b64SAllison Henderson */ 3471a4bb6b64SAllison Henderson 3472a4bb6b64SAllison Henderson int ext4_punch_hole(struct file *file, loff_t offset, loff_t length) 3473a4bb6b64SAllison Henderson { 3474a4bb6b64SAllison Henderson struct inode *inode = file->f_path.dentry->d_inode; 3475a4bb6b64SAllison Henderson if (!S_ISREG(inode->i_mode)) 3476a4bb6b64SAllison Henderson return -ENOTSUPP; 3477a4bb6b64SAllison Henderson 3478a4bb6b64SAllison Henderson if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 3479a4bb6b64SAllison Henderson /* TODO: Add support for non extent hole punching */ 3480a4bb6b64SAllison Henderson return -ENOTSUPP; 3481a4bb6b64SAllison Henderson } 3482a4bb6b64SAllison Henderson 3483bab08ab9STheodore Ts'o if (EXT4_SB(inode->i_sb)->s_cluster_ratio > 1) { 3484bab08ab9STheodore Ts'o /* TODO: Add support for bigalloc file systems */ 3485bab08ab9STheodore Ts'o return -ENOTSUPP; 3486bab08ab9STheodore Ts'o } 3487bab08ab9STheodore Ts'o 3488a4bb6b64SAllison Henderson return ext4_ext_punch_hole(file, offset, length); 3489a4bb6b64SAllison Henderson } 3490a4bb6b64SAllison Henderson 3491a4bb6b64SAllison Henderson /* 3492617ba13bSMingming Cao * ext4_truncate() 3493ac27a0ecSDave Kleikamp * 3494617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 3495617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 3496ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 3497ac27a0ecSDave Kleikamp * 3498ac27a0ecSDave Kleikamp * As we work through the truncate and commmit bits of it to the journal there 3499ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 3500ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 3501ac27a0ecSDave Kleikamp * 3502ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 3503ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 3504ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 3505ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 3506ac27a0ecSDave Kleikamp * left-to-right works OK too). 3507ac27a0ecSDave Kleikamp * 3508ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 3509ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 3510ac27a0ecSDave Kleikamp * 3511ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 3512617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 3513ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 3514617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 3515617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 3516ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 3517617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 3518ac27a0ecSDave Kleikamp */ 3519617ba13bSMingming Cao void ext4_truncate(struct inode *inode) 3520ac27a0ecSDave Kleikamp { 35210562e0baSJiaying Zhang trace_ext4_truncate_enter(inode); 35220562e0baSJiaying Zhang 352391ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 3524ac27a0ecSDave Kleikamp return; 3525ac27a0ecSDave Kleikamp 352612e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS); 3527c8d46e41SJiaying Zhang 35285534fb5bSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 352919f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE); 35307d8f9f7dSTheodore Ts'o 3531ff9893dcSAmir Goldstein if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3532cf108bcaSJan Kara ext4_ext_truncate(inode); 3533ff9893dcSAmir Goldstein else 3534ff9893dcSAmir Goldstein ext4_ind_truncate(inode); 3535a86c6181SAlex Tomas 35360562e0baSJiaying Zhang trace_ext4_truncate_exit(inode); 3537ac27a0ecSDave Kleikamp } 3538ac27a0ecSDave Kleikamp 3539ac27a0ecSDave Kleikamp /* 3540617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 3541ac27a0ecSDave Kleikamp * underlying buffer_head on success. If 'in_mem' is true, we have all 3542ac27a0ecSDave Kleikamp * data in memory that is needed to recreate the on-disk version of this 3543ac27a0ecSDave Kleikamp * inode. 3544ac27a0ecSDave Kleikamp */ 3545617ba13bSMingming Cao static int __ext4_get_inode_loc(struct inode *inode, 3546617ba13bSMingming Cao struct ext4_iloc *iloc, int in_mem) 3547ac27a0ecSDave Kleikamp { 3548240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 3549ac27a0ecSDave Kleikamp struct buffer_head *bh; 3550240799cdSTheodore Ts'o struct super_block *sb = inode->i_sb; 3551240799cdSTheodore Ts'o ext4_fsblk_t block; 3552240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 3553ac27a0ecSDave Kleikamp 35543a06d778SAneesh Kumar K.V iloc->bh = NULL; 3555240799cdSTheodore Ts'o if (!ext4_valid_inum(sb, inode->i_ino)) 3556ac27a0ecSDave Kleikamp return -EIO; 3557ac27a0ecSDave Kleikamp 3558240799cdSTheodore Ts'o iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb); 3559240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 3560240799cdSTheodore Ts'o if (!gdp) 3561240799cdSTheodore Ts'o return -EIO; 3562240799cdSTheodore Ts'o 3563240799cdSTheodore Ts'o /* 3564240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 3565240799cdSTheodore Ts'o */ 356600d09882STao Ma inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 3567240799cdSTheodore Ts'o inode_offset = ((inode->i_ino - 1) % 3568240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 3569240799cdSTheodore Ts'o block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); 3570240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 3571240799cdSTheodore Ts'o 3572240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 3573ac27a0ecSDave Kleikamp if (!bh) { 3574c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, block, 3575c398eda0STheodore Ts'o "unable to read itable block"); 3576ac27a0ecSDave Kleikamp return -EIO; 3577ac27a0ecSDave Kleikamp } 3578ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 3579ac27a0ecSDave Kleikamp lock_buffer(bh); 35809c83a923SHidehiro Kawai 35819c83a923SHidehiro Kawai /* 35829c83a923SHidehiro Kawai * If the buffer has the write error flag, we have failed 35839c83a923SHidehiro Kawai * to write out another inode in the same block. In this 35849c83a923SHidehiro Kawai * case, we don't have to read the block because we may 35859c83a923SHidehiro Kawai * read the old inode data successfully. 35869c83a923SHidehiro Kawai */ 35879c83a923SHidehiro Kawai if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) 35889c83a923SHidehiro Kawai set_buffer_uptodate(bh); 35899c83a923SHidehiro Kawai 3590ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) { 3591ac27a0ecSDave Kleikamp /* someone brought it uptodate while we waited */ 3592ac27a0ecSDave Kleikamp unlock_buffer(bh); 3593ac27a0ecSDave Kleikamp goto has_buffer; 3594ac27a0ecSDave Kleikamp } 3595ac27a0ecSDave Kleikamp 3596ac27a0ecSDave Kleikamp /* 3597ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 3598ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 3599ac27a0ecSDave Kleikamp * block. 3600ac27a0ecSDave Kleikamp */ 3601ac27a0ecSDave Kleikamp if (in_mem) { 3602ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 3603240799cdSTheodore Ts'o int i, start; 3604ac27a0ecSDave Kleikamp 3605240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 3606ac27a0ecSDave Kleikamp 3607ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 3608240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 3609ac27a0ecSDave Kleikamp if (!bitmap_bh) 3610ac27a0ecSDave Kleikamp goto make_io; 3611ac27a0ecSDave Kleikamp 3612ac27a0ecSDave Kleikamp /* 3613ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 3614ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 3615ac27a0ecSDave Kleikamp * of one, so skip it. 3616ac27a0ecSDave Kleikamp */ 3617ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 3618ac27a0ecSDave Kleikamp brelse(bitmap_bh); 3619ac27a0ecSDave Kleikamp goto make_io; 3620ac27a0ecSDave Kleikamp } 3621240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 3622ac27a0ecSDave Kleikamp if (i == inode_offset) 3623ac27a0ecSDave Kleikamp continue; 3624617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 3625ac27a0ecSDave Kleikamp break; 3626ac27a0ecSDave Kleikamp } 3627ac27a0ecSDave Kleikamp brelse(bitmap_bh); 3628240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 3629ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 3630ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 3631ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 3632ac27a0ecSDave Kleikamp unlock_buffer(bh); 3633ac27a0ecSDave Kleikamp goto has_buffer; 3634ac27a0ecSDave Kleikamp } 3635ac27a0ecSDave Kleikamp } 3636ac27a0ecSDave Kleikamp 3637ac27a0ecSDave Kleikamp make_io: 3638ac27a0ecSDave Kleikamp /* 3639240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 3640240799cdSTheodore Ts'o * blocks from the inode table. 3641240799cdSTheodore Ts'o */ 3642240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 3643240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 3644240799cdSTheodore Ts'o unsigned num; 3645240799cdSTheodore Ts'o 3646240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 3647b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 3648240799cdSTheodore Ts'o b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1); 3649240799cdSTheodore Ts'o if (table > b) 3650240799cdSTheodore Ts'o b = table; 3651240799cdSTheodore Ts'o end = b + EXT4_SB(sb)->s_inode_readahead_blks; 3652240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 3653240799cdSTheodore Ts'o if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 3654240799cdSTheodore Ts'o EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) 3655560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 3656240799cdSTheodore Ts'o table += num / inodes_per_block; 3657240799cdSTheodore Ts'o if (end > table) 3658240799cdSTheodore Ts'o end = table; 3659240799cdSTheodore Ts'o while (b <= end) 3660240799cdSTheodore Ts'o sb_breadahead(sb, b++); 3661240799cdSTheodore Ts'o } 3662240799cdSTheodore Ts'o 3663240799cdSTheodore Ts'o /* 3664ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 3665ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 3666ac27a0ecSDave Kleikamp * Read the block from disk. 3667ac27a0ecSDave Kleikamp */ 36680562e0baSJiaying Zhang trace_ext4_load_inode(inode); 3669ac27a0ecSDave Kleikamp get_bh(bh); 3670ac27a0ecSDave Kleikamp bh->b_end_io = end_buffer_read_sync; 3671ac27a0ecSDave Kleikamp submit_bh(READ_META, bh); 3672ac27a0ecSDave Kleikamp wait_on_buffer(bh); 3673ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 3674c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, block, 3675c398eda0STheodore Ts'o "unable to read itable block"); 3676ac27a0ecSDave Kleikamp brelse(bh); 3677ac27a0ecSDave Kleikamp return -EIO; 3678ac27a0ecSDave Kleikamp } 3679ac27a0ecSDave Kleikamp } 3680ac27a0ecSDave Kleikamp has_buffer: 3681ac27a0ecSDave Kleikamp iloc->bh = bh; 3682ac27a0ecSDave Kleikamp return 0; 3683ac27a0ecSDave Kleikamp } 3684ac27a0ecSDave Kleikamp 3685617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 3686ac27a0ecSDave Kleikamp { 3687ac27a0ecSDave Kleikamp /* We have all inode data except xattrs in memory here. */ 3688617ba13bSMingming Cao return __ext4_get_inode_loc(inode, iloc, 368919f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_XATTR)); 3690ac27a0ecSDave Kleikamp } 3691ac27a0ecSDave Kleikamp 3692617ba13bSMingming Cao void ext4_set_inode_flags(struct inode *inode) 3693ac27a0ecSDave Kleikamp { 3694617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 3695ac27a0ecSDave Kleikamp 3696ac27a0ecSDave Kleikamp inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); 3697617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 3698ac27a0ecSDave Kleikamp inode->i_flags |= S_SYNC; 3699617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 3700ac27a0ecSDave Kleikamp inode->i_flags |= S_APPEND; 3701617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 3702ac27a0ecSDave Kleikamp inode->i_flags |= S_IMMUTABLE; 3703617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 3704ac27a0ecSDave Kleikamp inode->i_flags |= S_NOATIME; 3705617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 3706ac27a0ecSDave Kleikamp inode->i_flags |= S_DIRSYNC; 3707ac27a0ecSDave Kleikamp } 3708ac27a0ecSDave Kleikamp 3709ff9ddf7eSJan Kara /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */ 3710ff9ddf7eSJan Kara void ext4_get_inode_flags(struct ext4_inode_info *ei) 3711ff9ddf7eSJan Kara { 371284a8dce2SDmitry Monakhov unsigned int vfs_fl; 371384a8dce2SDmitry Monakhov unsigned long old_fl, new_fl; 3714ff9ddf7eSJan Kara 371584a8dce2SDmitry Monakhov do { 371684a8dce2SDmitry Monakhov vfs_fl = ei->vfs_inode.i_flags; 371784a8dce2SDmitry Monakhov old_fl = ei->i_flags; 371884a8dce2SDmitry Monakhov new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL| 371984a8dce2SDmitry Monakhov EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL| 372084a8dce2SDmitry Monakhov EXT4_DIRSYNC_FL); 372184a8dce2SDmitry Monakhov if (vfs_fl & S_SYNC) 372284a8dce2SDmitry Monakhov new_fl |= EXT4_SYNC_FL; 372384a8dce2SDmitry Monakhov if (vfs_fl & S_APPEND) 372484a8dce2SDmitry Monakhov new_fl |= EXT4_APPEND_FL; 372584a8dce2SDmitry Monakhov if (vfs_fl & S_IMMUTABLE) 372684a8dce2SDmitry Monakhov new_fl |= EXT4_IMMUTABLE_FL; 372784a8dce2SDmitry Monakhov if (vfs_fl & S_NOATIME) 372884a8dce2SDmitry Monakhov new_fl |= EXT4_NOATIME_FL; 372984a8dce2SDmitry Monakhov if (vfs_fl & S_DIRSYNC) 373084a8dce2SDmitry Monakhov new_fl |= EXT4_DIRSYNC_FL; 373184a8dce2SDmitry Monakhov } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl); 3732ff9ddf7eSJan Kara } 3733de9a55b8STheodore Ts'o 37340fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 37350fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 37360fc1b451SAneesh Kumar K.V { 37370fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 37388180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 37398180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 37400fc1b451SAneesh Kumar K.V 37410fc1b451SAneesh Kumar K.V if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 37420fc1b451SAneesh Kumar K.V EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) { 37430fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 37440fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 37450fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 374607a03824STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) { 37478180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 37488180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 37498180a562SAneesh Kumar K.V } else { 37500fc1b451SAneesh Kumar K.V return i_blocks; 37518180a562SAneesh Kumar K.V } 37520fc1b451SAneesh Kumar K.V } else { 37530fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 37540fc1b451SAneesh Kumar K.V } 37550fc1b451SAneesh Kumar K.V } 3756ff9ddf7eSJan Kara 37571d1fe1eeSDavid Howells struct inode *ext4_iget(struct super_block *sb, unsigned long ino) 3758ac27a0ecSDave Kleikamp { 3759617ba13bSMingming Cao struct ext4_iloc iloc; 3760617ba13bSMingming Cao struct ext4_inode *raw_inode; 37611d1fe1eeSDavid Howells struct ext4_inode_info *ei; 37621d1fe1eeSDavid Howells struct inode *inode; 3763b436b9beSJan Kara journal_t *journal = EXT4_SB(sb)->s_journal; 37641d1fe1eeSDavid Howells long ret; 3765ac27a0ecSDave Kleikamp int block; 3766ac27a0ecSDave Kleikamp 37671d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 37681d1fe1eeSDavid Howells if (!inode) 37691d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 37701d1fe1eeSDavid Howells if (!(inode->i_state & I_NEW)) 37711d1fe1eeSDavid Howells return inode; 37721d1fe1eeSDavid Howells 37731d1fe1eeSDavid Howells ei = EXT4_I(inode); 37747dc57615SPeter Huewe iloc.bh = NULL; 3775ac27a0ecSDave Kleikamp 37761d1fe1eeSDavid Howells ret = __ext4_get_inode_loc(inode, &iloc, 0); 37771d1fe1eeSDavid Howells if (ret < 0) 3778ac27a0ecSDave Kleikamp goto bad_inode; 3779617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 3780ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 3781ac27a0ecSDave Kleikamp inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 3782ac27a0ecSDave Kleikamp inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 3783ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 3784ac27a0ecSDave Kleikamp inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 3785ac27a0ecSDave Kleikamp inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 3786ac27a0ecSDave Kleikamp } 3787ac27a0ecSDave Kleikamp inode->i_nlink = le16_to_cpu(raw_inode->i_links_count); 3788ac27a0ecSDave Kleikamp 3789353eb83cSTheodore Ts'o ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 3790ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 3791ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 3792ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 3793ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 3794ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 3795ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 3796ac27a0ecSDave Kleikamp */ 3797ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 3798ac27a0ecSDave Kleikamp if (inode->i_mode == 0 || 3799617ba13bSMingming Cao !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) { 3800ac27a0ecSDave Kleikamp /* this inode is deleted */ 38011d1fe1eeSDavid Howells ret = -ESTALE; 3802ac27a0ecSDave Kleikamp goto bad_inode; 3803ac27a0ecSDave Kleikamp } 3804ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 3805ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 3806ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 3807ac27a0ecSDave Kleikamp * the process of deleting those. */ 3808ac27a0ecSDave Kleikamp } 3809ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 38100fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 38117973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 3812a9e81742STheodore Ts'o if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) 3813a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 3814a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 3815a48380f7SAneesh Kumar K.V inode->i_size = ext4_isize(raw_inode); 3816ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 3817a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 3818a9e7f447SDmitry Monakhov ei->i_reserved_quota = 0; 3819a9e7f447SDmitry Monakhov #endif 3820ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 3821ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 3822a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 3823ac27a0ecSDave Kleikamp /* 3824ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 3825ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 3826ac27a0ecSDave Kleikamp */ 3827617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 3828ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 3829ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 3830ac27a0ecSDave Kleikamp 3831b436b9beSJan Kara /* 3832b436b9beSJan Kara * Set transaction id's of transactions that have to be committed 3833b436b9beSJan Kara * to finish f[data]sync. We set them to currently running transaction 3834b436b9beSJan Kara * as we cannot be sure that the inode or some of its metadata isn't 3835b436b9beSJan Kara * part of the transaction - the inode could have been reclaimed and 3836b436b9beSJan Kara * now it is reread from disk. 3837b436b9beSJan Kara */ 3838b436b9beSJan Kara if (journal) { 3839b436b9beSJan Kara transaction_t *transaction; 3840b436b9beSJan Kara tid_t tid; 3841b436b9beSJan Kara 3842a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 3843b436b9beSJan Kara if (journal->j_running_transaction) 3844b436b9beSJan Kara transaction = journal->j_running_transaction; 3845b436b9beSJan Kara else 3846b436b9beSJan Kara transaction = journal->j_committing_transaction; 3847b436b9beSJan Kara if (transaction) 3848b436b9beSJan Kara tid = transaction->t_tid; 3849b436b9beSJan Kara else 3850b436b9beSJan Kara tid = journal->j_commit_sequence; 3851a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 3852b436b9beSJan Kara ei->i_sync_tid = tid; 3853b436b9beSJan Kara ei->i_datasync_tid = tid; 3854b436b9beSJan Kara } 3855b436b9beSJan Kara 38560040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 3857ac27a0ecSDave Kleikamp ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 3858617ba13bSMingming Cao if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 3859e5d2861fSKirill Korotaev EXT4_INODE_SIZE(inode->i_sb)) { 38601d1fe1eeSDavid Howells ret = -EIO; 3861ac27a0ecSDave Kleikamp goto bad_inode; 3862e5d2861fSKirill Korotaev } 3863ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 3864ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 3865617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 3866617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 3867ac27a0ecSDave Kleikamp } else { 3868ac27a0ecSDave Kleikamp __le32 *magic = (void *)raw_inode + 3869617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE + 3870ac27a0ecSDave Kleikamp ei->i_extra_isize; 3871617ba13bSMingming Cao if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) 387219f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_XATTR); 3873ac27a0ecSDave Kleikamp } 3874ac27a0ecSDave Kleikamp } else 3875ac27a0ecSDave Kleikamp ei->i_extra_isize = 0; 3876ac27a0ecSDave Kleikamp 3877ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 3878ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 3879ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 3880ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 3881ef7f3835SKalpak Shah 388225ec56b5SJean Noel Cordenner inode->i_version = le32_to_cpu(raw_inode->i_disk_version); 388325ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 388425ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 388525ec56b5SJean Noel Cordenner inode->i_version |= 388625ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 388725ec56b5SJean Noel Cordenner } 388825ec56b5SJean Noel Cordenner 3889c4b5a614STheodore Ts'o ret = 0; 3890485c26ecSTheodore Ts'o if (ei->i_file_acl && 38911032988cSTheodore Ts'o !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) { 389224676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "bad extended attribute block %llu", 389324676da4STheodore Ts'o ei->i_file_acl); 3894485c26ecSTheodore Ts'o ret = -EIO; 3895485c26ecSTheodore Ts'o goto bad_inode; 389607a03824STheodore Ts'o } else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 3897c4b5a614STheodore Ts'o if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 3898c4b5a614STheodore Ts'o (S_ISLNK(inode->i_mode) && 3899c4b5a614STheodore Ts'o !ext4_inode_is_fast_symlink(inode))) 39007a262f7cSAneesh Kumar K.V /* Validate extent which is part of inode */ 39017a262f7cSAneesh Kumar K.V ret = ext4_ext_check_inode(inode); 3902fe2c8191SThiemo Nagel } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 3903fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 3904fe2c8191SThiemo Nagel !ext4_inode_is_fast_symlink(inode))) { 3905fe2c8191SThiemo Nagel /* Validate block references which are part of inode */ 39061f7d1e77STheodore Ts'o ret = ext4_ind_check_inode(inode); 3907fe2c8191SThiemo Nagel } 3908567f3e9aSTheodore Ts'o if (ret) 39097a262f7cSAneesh Kumar K.V goto bad_inode; 39107a262f7cSAneesh Kumar K.V 3911ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 3912617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 3913617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 3914617ba13bSMingming Cao ext4_set_aops(inode); 3915ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 3916617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 3917617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 3918ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 3919e83c1397SDuane Griffin if (ext4_inode_is_fast_symlink(inode)) { 3920617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 3921e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 3922e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 3923e83c1397SDuane Griffin } else { 3924617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 3925617ba13bSMingming Cao ext4_set_aops(inode); 3926ac27a0ecSDave Kleikamp } 3927563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 3928563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 3929617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 3930ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 3931ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 3932ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 3933ac27a0ecSDave Kleikamp else 3934ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 3935ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 3936563bdd61STheodore Ts'o } else { 3937563bdd61STheodore Ts'o ret = -EIO; 393824676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode); 3939563bdd61STheodore Ts'o goto bad_inode; 3940ac27a0ecSDave Kleikamp } 3941ac27a0ecSDave Kleikamp brelse(iloc.bh); 3942617ba13bSMingming Cao ext4_set_inode_flags(inode); 39431d1fe1eeSDavid Howells unlock_new_inode(inode); 39441d1fe1eeSDavid Howells return inode; 3945ac27a0ecSDave Kleikamp 3946ac27a0ecSDave Kleikamp bad_inode: 3947567f3e9aSTheodore Ts'o brelse(iloc.bh); 39481d1fe1eeSDavid Howells iget_failed(inode); 39491d1fe1eeSDavid Howells return ERR_PTR(ret); 3950ac27a0ecSDave Kleikamp } 3951ac27a0ecSDave Kleikamp 39520fc1b451SAneesh Kumar K.V static int ext4_inode_blocks_set(handle_t *handle, 39530fc1b451SAneesh Kumar K.V struct ext4_inode *raw_inode, 39540fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 39550fc1b451SAneesh Kumar K.V { 39560fc1b451SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 39570fc1b451SAneesh Kumar K.V u64 i_blocks = inode->i_blocks; 39580fc1b451SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 39590fc1b451SAneesh Kumar K.V 39600fc1b451SAneesh Kumar K.V if (i_blocks <= ~0U) { 39610fc1b451SAneesh Kumar K.V /* 39620fc1b451SAneesh Kumar K.V * i_blocks can be represnted in a 32 bit variable 39630fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 39640fc1b451SAneesh Kumar K.V */ 39658180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 39660fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = 0; 396784a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 3968f287a1a5STheodore Ts'o return 0; 3969f287a1a5STheodore Ts'o } 3970f287a1a5STheodore Ts'o if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) 3971f287a1a5STheodore Ts'o return -EFBIG; 3972f287a1a5STheodore Ts'o 3973f287a1a5STheodore Ts'o if (i_blocks <= 0xffffffffffffULL) { 39740fc1b451SAneesh Kumar K.V /* 39750fc1b451SAneesh Kumar K.V * i_blocks can be represented in a 48 bit variable 39760fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 39770fc1b451SAneesh Kumar K.V */ 39788180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 39790fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 398084a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 39810fc1b451SAneesh Kumar K.V } else { 398284a8dce2SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE); 39838180a562SAneesh Kumar K.V /* i_block is stored in file system block size */ 39848180a562SAneesh Kumar K.V i_blocks = i_blocks >> (inode->i_blkbits - 9); 39858180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 39868180a562SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 39870fc1b451SAneesh Kumar K.V } 3988f287a1a5STheodore Ts'o return 0; 39890fc1b451SAneesh Kumar K.V } 39900fc1b451SAneesh Kumar K.V 3991ac27a0ecSDave Kleikamp /* 3992ac27a0ecSDave Kleikamp * Post the struct inode info into an on-disk inode location in the 3993ac27a0ecSDave Kleikamp * buffer-cache. This gobbles the caller's reference to the 3994ac27a0ecSDave Kleikamp * buffer_head in the inode location struct. 3995ac27a0ecSDave Kleikamp * 3996ac27a0ecSDave Kleikamp * The caller must have write access to iloc->bh. 3997ac27a0ecSDave Kleikamp */ 3998617ba13bSMingming Cao static int ext4_do_update_inode(handle_t *handle, 3999ac27a0ecSDave Kleikamp struct inode *inode, 4000830156c7SFrank Mayhar struct ext4_iloc *iloc) 4001ac27a0ecSDave Kleikamp { 4002617ba13bSMingming Cao struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 4003617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 4004ac27a0ecSDave Kleikamp struct buffer_head *bh = iloc->bh; 4005ac27a0ecSDave Kleikamp int err = 0, rc, block; 4006ac27a0ecSDave Kleikamp 4007ac27a0ecSDave Kleikamp /* For fields not not tracking in the in-memory inode, 4008ac27a0ecSDave Kleikamp * initialise them to zero for new inodes. */ 400919f5fb7aSTheodore Ts'o if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) 4010617ba13bSMingming Cao memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 4011ac27a0ecSDave Kleikamp 4012ff9ddf7eSJan Kara ext4_get_inode_flags(ei); 4013ac27a0ecSDave Kleikamp raw_inode->i_mode = cpu_to_le16(inode->i_mode); 4014ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 4015ac27a0ecSDave Kleikamp raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid)); 4016ac27a0ecSDave Kleikamp raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid)); 4017ac27a0ecSDave Kleikamp /* 4018ac27a0ecSDave Kleikamp * Fix up interoperability with old kernels. Otherwise, old inodes get 4019ac27a0ecSDave Kleikamp * re-used with the upper 16 bits of the uid/gid intact 4020ac27a0ecSDave Kleikamp */ 4021ac27a0ecSDave Kleikamp if (!ei->i_dtime) { 4022ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 4023ac27a0ecSDave Kleikamp cpu_to_le16(high_16_bits(inode->i_uid)); 4024ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 4025ac27a0ecSDave Kleikamp cpu_to_le16(high_16_bits(inode->i_gid)); 4026ac27a0ecSDave Kleikamp } else { 4027ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4028ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4029ac27a0ecSDave Kleikamp } 4030ac27a0ecSDave Kleikamp } else { 4031ac27a0ecSDave Kleikamp raw_inode->i_uid_low = 4032ac27a0ecSDave Kleikamp cpu_to_le16(fs_high2lowuid(inode->i_uid)); 4033ac27a0ecSDave Kleikamp raw_inode->i_gid_low = 4034ac27a0ecSDave Kleikamp cpu_to_le16(fs_high2lowgid(inode->i_gid)); 4035ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4036ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4037ac27a0ecSDave Kleikamp } 4038ac27a0ecSDave Kleikamp raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 4039ef7f3835SKalpak Shah 4040ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 4041ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 4042ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 4043ef7f3835SKalpak Shah EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 4044ef7f3835SKalpak Shah 40450fc1b451SAneesh Kumar K.V if (ext4_inode_blocks_set(handle, raw_inode, ei)) 40460fc1b451SAneesh Kumar K.V goto out_brelse; 4047ac27a0ecSDave Kleikamp raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 4048353eb83cSTheodore Ts'o raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF); 40499b8f1f01SMingming Cao if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 40509b8f1f01SMingming Cao cpu_to_le32(EXT4_OS_HURD)) 4051a1ddeb7eSBadari Pulavarty raw_inode->i_file_acl_high = 4052a1ddeb7eSBadari Pulavarty cpu_to_le16(ei->i_file_acl >> 32); 40537973c0c1SAneesh Kumar K.V raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 4054a48380f7SAneesh Kumar K.V ext4_isize_set(raw_inode, ei->i_disksize); 4055ac27a0ecSDave Kleikamp if (ei->i_disksize > 0x7fffffffULL) { 4056ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 4057617ba13bSMingming Cao if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, 4058617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE) || 4059617ba13bSMingming Cao EXT4_SB(sb)->s_es->s_rev_level == 4060617ba13bSMingming Cao cpu_to_le32(EXT4_GOOD_OLD_REV)) { 4061ac27a0ecSDave Kleikamp /* If this is the first large file 4062ac27a0ecSDave Kleikamp * created, add a flag to the superblock. 4063ac27a0ecSDave Kleikamp */ 4064617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, 4065617ba13bSMingming Cao EXT4_SB(sb)->s_sbh); 4066ac27a0ecSDave Kleikamp if (err) 4067ac27a0ecSDave Kleikamp goto out_brelse; 4068617ba13bSMingming Cao ext4_update_dynamic_rev(sb); 4069617ba13bSMingming Cao EXT4_SET_RO_COMPAT_FEATURE(sb, 4070617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE); 4071ac27a0ecSDave Kleikamp sb->s_dirt = 1; 40720390131bSFrank Mayhar ext4_handle_sync(handle); 407373b50c1cSCurt Wohlgemuth err = ext4_handle_dirty_metadata(handle, NULL, 4074617ba13bSMingming Cao EXT4_SB(sb)->s_sbh); 4075ac27a0ecSDave Kleikamp } 4076ac27a0ecSDave Kleikamp } 4077ac27a0ecSDave Kleikamp raw_inode->i_generation = cpu_to_le32(inode->i_generation); 4078ac27a0ecSDave Kleikamp if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 4079ac27a0ecSDave Kleikamp if (old_valid_dev(inode->i_rdev)) { 4080ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 4081ac27a0ecSDave Kleikamp cpu_to_le32(old_encode_dev(inode->i_rdev)); 4082ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 0; 4083ac27a0ecSDave Kleikamp } else { 4084ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 0; 4085ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 4086ac27a0ecSDave Kleikamp cpu_to_le32(new_encode_dev(inode->i_rdev)); 4087ac27a0ecSDave Kleikamp raw_inode->i_block[2] = 0; 4088ac27a0ecSDave Kleikamp } 4089de9a55b8STheodore Ts'o } else 4090de9a55b8STheodore Ts'o for (block = 0; block < EXT4_N_BLOCKS; block++) 4091ac27a0ecSDave Kleikamp raw_inode->i_block[block] = ei->i_data[block]; 4092ac27a0ecSDave Kleikamp 409325ec56b5SJean Noel Cordenner raw_inode->i_disk_version = cpu_to_le32(inode->i_version); 409425ec56b5SJean Noel Cordenner if (ei->i_extra_isize) { 409525ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 409625ec56b5SJean Noel Cordenner raw_inode->i_version_hi = 409725ec56b5SJean Noel Cordenner cpu_to_le32(inode->i_version >> 32); 4098ac27a0ecSDave Kleikamp raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize); 409925ec56b5SJean Noel Cordenner } 410025ec56b5SJean Noel Cordenner 41010390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 410273b50c1cSCurt Wohlgemuth rc = ext4_handle_dirty_metadata(handle, NULL, bh); 4103ac27a0ecSDave Kleikamp if (!err) 4104ac27a0ecSDave Kleikamp err = rc; 410519f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_NEW); 4106ac27a0ecSDave Kleikamp 4107b436b9beSJan Kara ext4_update_inode_fsync_trans(handle, inode, 0); 4108ac27a0ecSDave Kleikamp out_brelse: 4109ac27a0ecSDave Kleikamp brelse(bh); 4110617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4111ac27a0ecSDave Kleikamp return err; 4112ac27a0ecSDave Kleikamp } 4113ac27a0ecSDave Kleikamp 4114ac27a0ecSDave Kleikamp /* 4115617ba13bSMingming Cao * ext4_write_inode() 4116ac27a0ecSDave Kleikamp * 4117ac27a0ecSDave Kleikamp * We are called from a few places: 4118ac27a0ecSDave Kleikamp * 4119ac27a0ecSDave Kleikamp * - Within generic_file_write() for O_SYNC files. 4120ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 4121ac27a0ecSDave Kleikamp * trasnaction to commit. 4122ac27a0ecSDave Kleikamp * 4123ac27a0ecSDave Kleikamp * - Within sys_sync(), kupdate and such. 4124ac27a0ecSDave Kleikamp * We wait on commit, if tol to. 4125ac27a0ecSDave Kleikamp * 4126ac27a0ecSDave Kleikamp * - Within prune_icache() (PF_MEMALLOC == true) 4127ac27a0ecSDave Kleikamp * Here we simply return. We can't afford to block kswapd on the 4128ac27a0ecSDave Kleikamp * journal commit. 4129ac27a0ecSDave Kleikamp * 4130ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 4131ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 4132617ba13bSMingming Cao * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for 4133ac27a0ecSDave Kleikamp * knfsd. 4134ac27a0ecSDave Kleikamp * 4135ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 4136ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 4137ac27a0ecSDave Kleikamp * which we are interested. 4138ac27a0ecSDave Kleikamp * 4139ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 4140ac27a0ecSDave Kleikamp * 4141ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 4142ac27a0ecSDave Kleikamp * stuff(); 4143ac27a0ecSDave Kleikamp * inode->i_size = expr; 4144ac27a0ecSDave Kleikamp * 4145ac27a0ecSDave Kleikamp * is in error because a kswapd-driven write_inode() could occur while 4146ac27a0ecSDave Kleikamp * `stuff()' is running, and the new i_size will be lost. Plus the inode 4147ac27a0ecSDave Kleikamp * will no longer be on the superblock's dirty inode list. 4148ac27a0ecSDave Kleikamp */ 4149a9185b41SChristoph Hellwig int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) 4150ac27a0ecSDave Kleikamp { 415191ac6f43SFrank Mayhar int err; 415291ac6f43SFrank Mayhar 4153ac27a0ecSDave Kleikamp if (current->flags & PF_MEMALLOC) 4154ac27a0ecSDave Kleikamp return 0; 4155ac27a0ecSDave Kleikamp 415691ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal) { 4157617ba13bSMingming Cao if (ext4_journal_current_handle()) { 4158b38bd33aSMingming Cao jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); 4159ac27a0ecSDave Kleikamp dump_stack(); 4160ac27a0ecSDave Kleikamp return -EIO; 4161ac27a0ecSDave Kleikamp } 4162ac27a0ecSDave Kleikamp 4163a9185b41SChristoph Hellwig if (wbc->sync_mode != WB_SYNC_ALL) 4164ac27a0ecSDave Kleikamp return 0; 4165ac27a0ecSDave Kleikamp 416691ac6f43SFrank Mayhar err = ext4_force_commit(inode->i_sb); 416791ac6f43SFrank Mayhar } else { 416891ac6f43SFrank Mayhar struct ext4_iloc iloc; 416991ac6f43SFrank Mayhar 41708b472d73SCurt Wohlgemuth err = __ext4_get_inode_loc(inode, &iloc, 0); 417191ac6f43SFrank Mayhar if (err) 417291ac6f43SFrank Mayhar return err; 4173a9185b41SChristoph Hellwig if (wbc->sync_mode == WB_SYNC_ALL) 4174830156c7SFrank Mayhar sync_dirty_buffer(iloc.bh); 4175830156c7SFrank Mayhar if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { 4176c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr, 4177c398eda0STheodore Ts'o "IO error syncing inode"); 4178830156c7SFrank Mayhar err = -EIO; 4179830156c7SFrank Mayhar } 4180fd2dd9fbSCurt Wohlgemuth brelse(iloc.bh); 418191ac6f43SFrank Mayhar } 418291ac6f43SFrank Mayhar return err; 4183ac27a0ecSDave Kleikamp } 4184ac27a0ecSDave Kleikamp 4185ac27a0ecSDave Kleikamp /* 4186617ba13bSMingming Cao * ext4_setattr() 4187ac27a0ecSDave Kleikamp * 4188ac27a0ecSDave Kleikamp * Called from notify_change. 4189ac27a0ecSDave Kleikamp * 4190ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 4191ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 4192ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 4193ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 4194ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 4195ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 4196ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 4197ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 4198ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 4199ac27a0ecSDave Kleikamp * 4200678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 4201678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 4202678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 4203678aaf48SJan Kara * This way we are sure that all the data written in the previous 4204678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 4205678aaf48SJan Kara * writeback). 4206678aaf48SJan Kara * 4207678aaf48SJan Kara * Called with inode->i_mutex down. 4208ac27a0ecSDave Kleikamp */ 4209617ba13bSMingming Cao int ext4_setattr(struct dentry *dentry, struct iattr *attr) 4210ac27a0ecSDave Kleikamp { 4211ac27a0ecSDave Kleikamp struct inode *inode = dentry->d_inode; 4212ac27a0ecSDave Kleikamp int error, rc = 0; 42133d287de3SDmitry Monakhov int orphan = 0; 4214ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 4215ac27a0ecSDave Kleikamp 4216ac27a0ecSDave Kleikamp error = inode_change_ok(inode, attr); 4217ac27a0ecSDave Kleikamp if (error) 4218ac27a0ecSDave Kleikamp return error; 4219ac27a0ecSDave Kleikamp 422012755627SDmitry Monakhov if (is_quota_modification(inode, attr)) 4221871a2931SChristoph Hellwig dquot_initialize(inode); 4222ac27a0ecSDave Kleikamp if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) || 4223ac27a0ecSDave Kleikamp (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) { 4224ac27a0ecSDave Kleikamp handle_t *handle; 4225ac27a0ecSDave Kleikamp 4226ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 4227ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 42285aca07ebSDmitry Monakhov handle = ext4_journal_start(inode, (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb)+ 4229194074acSDmitry Monakhov EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb))+3); 4230ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 4231ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 4232ac27a0ecSDave Kleikamp goto err_out; 4233ac27a0ecSDave Kleikamp } 4234b43fa828SChristoph Hellwig error = dquot_transfer(inode, attr); 4235ac27a0ecSDave Kleikamp if (error) { 4236617ba13bSMingming Cao ext4_journal_stop(handle); 4237ac27a0ecSDave Kleikamp return error; 4238ac27a0ecSDave Kleikamp } 4239ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 4240ac27a0ecSDave Kleikamp * one transaction */ 4241ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_UID) 4242ac27a0ecSDave Kleikamp inode->i_uid = attr->ia_uid; 4243ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_GID) 4244ac27a0ecSDave Kleikamp inode->i_gid = attr->ia_gid; 4245617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 4246617ba13bSMingming Cao ext4_journal_stop(handle); 4247ac27a0ecSDave Kleikamp } 4248ac27a0ecSDave Kleikamp 4249e2b46574SEric Sandeen if (attr->ia_valid & ATTR_SIZE) { 4250562c72aaSChristoph Hellwig inode_dio_wait(inode); 4251562c72aaSChristoph Hellwig 425212e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 4253e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4254e2b46574SEric Sandeen 42550c095c7fSTheodore Ts'o if (attr->ia_size > sbi->s_bitmap_maxbytes) 42560c095c7fSTheodore Ts'o return -EFBIG; 4257e2b46574SEric Sandeen } 4258e2b46574SEric Sandeen } 4259e2b46574SEric Sandeen 4260ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode) && 4261c8d46e41SJiaying Zhang attr->ia_valid & ATTR_SIZE && 4262072bd7eaSTheodore Ts'o (attr->ia_size < inode->i_size)) { 4263ac27a0ecSDave Kleikamp handle_t *handle; 4264ac27a0ecSDave Kleikamp 4265617ba13bSMingming Cao handle = ext4_journal_start(inode, 3); 4266ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 4267ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 4268ac27a0ecSDave Kleikamp goto err_out; 4269ac27a0ecSDave Kleikamp } 42703d287de3SDmitry Monakhov if (ext4_handle_valid(handle)) { 4271617ba13bSMingming Cao error = ext4_orphan_add(handle, inode); 42723d287de3SDmitry Monakhov orphan = 1; 42733d287de3SDmitry Monakhov } 4274617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 4275617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 4276ac27a0ecSDave Kleikamp if (!error) 4277ac27a0ecSDave Kleikamp error = rc; 4278617ba13bSMingming Cao ext4_journal_stop(handle); 4279678aaf48SJan Kara 4280678aaf48SJan Kara if (ext4_should_order_data(inode)) { 4281678aaf48SJan Kara error = ext4_begin_ordered_truncate(inode, 4282678aaf48SJan Kara attr->ia_size); 4283678aaf48SJan Kara if (error) { 4284678aaf48SJan Kara /* Do as much error cleanup as possible */ 4285678aaf48SJan Kara handle = ext4_journal_start(inode, 3); 4286678aaf48SJan Kara if (IS_ERR(handle)) { 4287678aaf48SJan Kara ext4_orphan_del(NULL, inode); 4288678aaf48SJan Kara goto err_out; 4289678aaf48SJan Kara } 4290678aaf48SJan Kara ext4_orphan_del(handle, inode); 42913d287de3SDmitry Monakhov orphan = 0; 4292678aaf48SJan Kara ext4_journal_stop(handle); 4293678aaf48SJan Kara goto err_out; 4294678aaf48SJan Kara } 4295678aaf48SJan Kara } 4296ac27a0ecSDave Kleikamp } 4297ac27a0ecSDave Kleikamp 4298072bd7eaSTheodore Ts'o if (attr->ia_valid & ATTR_SIZE) { 4299072bd7eaSTheodore Ts'o if (attr->ia_size != i_size_read(inode)) { 4300072bd7eaSTheodore Ts'o truncate_setsize(inode, attr->ia_size); 4301072bd7eaSTheodore Ts'o ext4_truncate(inode); 4302072bd7eaSTheodore Ts'o } else if (ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS)) 4303072bd7eaSTheodore Ts'o ext4_truncate(inode); 4304072bd7eaSTheodore Ts'o } 4305ac27a0ecSDave Kleikamp 43061025774cSChristoph Hellwig if (!rc) { 43071025774cSChristoph Hellwig setattr_copy(inode, attr); 43081025774cSChristoph Hellwig mark_inode_dirty(inode); 43091025774cSChristoph Hellwig } 43101025774cSChristoph Hellwig 43111025774cSChristoph Hellwig /* 43121025774cSChristoph Hellwig * If the call to ext4_truncate failed to get a transaction handle at 43131025774cSChristoph Hellwig * all, we need to clean up the in-core orphan list manually. 43141025774cSChristoph Hellwig */ 43153d287de3SDmitry Monakhov if (orphan && inode->i_nlink) 4316617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 4317ac27a0ecSDave Kleikamp 4318ac27a0ecSDave Kleikamp if (!rc && (ia_valid & ATTR_MODE)) 4319617ba13bSMingming Cao rc = ext4_acl_chmod(inode); 4320ac27a0ecSDave Kleikamp 4321ac27a0ecSDave Kleikamp err_out: 4322617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 4323ac27a0ecSDave Kleikamp if (!error) 4324ac27a0ecSDave Kleikamp error = rc; 4325ac27a0ecSDave Kleikamp return error; 4326ac27a0ecSDave Kleikamp } 4327ac27a0ecSDave Kleikamp 43283e3398a0SMingming Cao int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry, 43293e3398a0SMingming Cao struct kstat *stat) 43303e3398a0SMingming Cao { 43313e3398a0SMingming Cao struct inode *inode; 43323e3398a0SMingming Cao unsigned long delalloc_blocks; 43333e3398a0SMingming Cao 43343e3398a0SMingming Cao inode = dentry->d_inode; 43353e3398a0SMingming Cao generic_fillattr(inode, stat); 43363e3398a0SMingming Cao 43373e3398a0SMingming Cao /* 43383e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 43393e3398a0SMingming Cao * otherwise in the case of system crash before the real block 43403e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 43413e3398a0SMingming Cao * on-disk file blocks. 43423e3398a0SMingming Cao * We always keep i_blocks updated together with real 43433e3398a0SMingming Cao * allocation. But to not confuse with user, stat 43443e3398a0SMingming Cao * will return the blocks that include the delayed allocation 43453e3398a0SMingming Cao * blocks for this file. 43463e3398a0SMingming Cao */ 43473e3398a0SMingming Cao delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks; 43483e3398a0SMingming Cao 43493e3398a0SMingming Cao stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9; 43503e3398a0SMingming Cao return 0; 43513e3398a0SMingming Cao } 4352ac27a0ecSDave Kleikamp 4353a02908f1SMingming Cao static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4354a02908f1SMingming Cao { 435512e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 43568bb2b247SAmir Goldstein return ext4_ind_trans_blocks(inode, nrblocks, chunk); 4357ac51d837STheodore Ts'o return ext4_ext_index_trans_blocks(inode, nrblocks, chunk); 4358a02908f1SMingming Cao } 4359ac51d837STheodore Ts'o 4360a02908f1SMingming Cao /* 4361a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 4362a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 4363a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 4364a02908f1SMingming Cao * 4365a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 4366af901ca1SAndré Goddard Rosa * different block groups too. If they are contiuguous, with flexbg, 4367a02908f1SMingming Cao * they could still across block group boundary. 4368a02908f1SMingming Cao * 4369a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 4370a02908f1SMingming Cao */ 43711f109d5aSTheodore Ts'o static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4372a02908f1SMingming Cao { 43738df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 43748df9675fSTheodore Ts'o int gdpblocks; 4375a02908f1SMingming Cao int idxblocks; 4376a02908f1SMingming Cao int ret = 0; 4377a02908f1SMingming Cao 4378a02908f1SMingming Cao /* 4379a02908f1SMingming Cao * How many index blocks need to touch to modify nrblocks? 4380a02908f1SMingming Cao * The "Chunk" flag indicating whether the nrblocks is 4381a02908f1SMingming Cao * physically contiguous on disk 4382a02908f1SMingming Cao * 4383a02908f1SMingming Cao * For Direct IO and fallocate, they calls get_block to allocate 4384a02908f1SMingming Cao * one single extent at a time, so they could set the "Chunk" flag 4385a02908f1SMingming Cao */ 4386a02908f1SMingming Cao idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk); 4387a02908f1SMingming Cao 4388a02908f1SMingming Cao ret = idxblocks; 4389a02908f1SMingming Cao 4390a02908f1SMingming Cao /* 4391a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 4392a02908f1SMingming Cao * to account 4393a02908f1SMingming Cao */ 4394a02908f1SMingming Cao groups = idxblocks; 4395a02908f1SMingming Cao if (chunk) 4396a02908f1SMingming Cao groups += 1; 4397ac27a0ecSDave Kleikamp else 4398a02908f1SMingming Cao groups += nrblocks; 4399ac27a0ecSDave Kleikamp 4400a02908f1SMingming Cao gdpblocks = groups; 44018df9675fSTheodore Ts'o if (groups > ngroups) 44028df9675fSTheodore Ts'o groups = ngroups; 4403a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 4404a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 4405a02908f1SMingming Cao 4406a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 4407a02908f1SMingming Cao ret += groups + gdpblocks; 4408a02908f1SMingming Cao 4409a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 4410a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 4411ac27a0ecSDave Kleikamp 4412ac27a0ecSDave Kleikamp return ret; 4413ac27a0ecSDave Kleikamp } 4414ac27a0ecSDave Kleikamp 4415ac27a0ecSDave Kleikamp /* 441625985edcSLucas De Marchi * Calculate the total number of credits to reserve to fit 4417f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 4418f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 4419a02908f1SMingming Cao * 4420525f4ed8SMingming Cao * This could be called via ext4_write_begin() 4421a02908f1SMingming Cao * 4422525f4ed8SMingming Cao * We need to consider the worse case, when 4423a02908f1SMingming Cao * one new block per extent. 4424a02908f1SMingming Cao */ 4425a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 4426a02908f1SMingming Cao { 4427a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 4428a02908f1SMingming Cao int ret; 4429a02908f1SMingming Cao 4430a02908f1SMingming Cao ret = ext4_meta_trans_blocks(inode, bpp, 0); 4431a02908f1SMingming Cao 4432a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 4433a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 4434a02908f1SMingming Cao ret += bpp; 4435a02908f1SMingming Cao return ret; 4436a02908f1SMingming Cao } 4437f3bd1f3fSMingming Cao 4438f3bd1f3fSMingming Cao /* 4439f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 4440f3bd1f3fSMingming Cao * 4441f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 444279e83036SEric Sandeen * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks. 4443f3bd1f3fSMingming Cao * 4444f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 4445f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 4446f3bd1f3fSMingming Cao */ 4447f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 4448f3bd1f3fSMingming Cao { 4449f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 4450f3bd1f3fSMingming Cao } 4451f3bd1f3fSMingming Cao 4452a02908f1SMingming Cao /* 4453617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 4454ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 4455ac27a0ecSDave Kleikamp */ 4456617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 4457617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 4458ac27a0ecSDave Kleikamp { 4459ac27a0ecSDave Kleikamp int err = 0; 4460ac27a0ecSDave Kleikamp 446125ec56b5SJean Noel Cordenner if (test_opt(inode->i_sb, I_VERSION)) 446225ec56b5SJean Noel Cordenner inode_inc_iversion(inode); 446325ec56b5SJean Noel Cordenner 4464ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 4465ac27a0ecSDave Kleikamp get_bh(iloc->bh); 4466ac27a0ecSDave Kleikamp 4467dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 4468830156c7SFrank Mayhar err = ext4_do_update_inode(handle, inode, iloc); 4469ac27a0ecSDave Kleikamp put_bh(iloc->bh); 4470ac27a0ecSDave Kleikamp return err; 4471ac27a0ecSDave Kleikamp } 4472ac27a0ecSDave Kleikamp 4473ac27a0ecSDave Kleikamp /* 4474ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 4475ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 4476ac27a0ecSDave Kleikamp */ 4477ac27a0ecSDave Kleikamp 4478ac27a0ecSDave Kleikamp int 4479617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 4480617ba13bSMingming Cao struct ext4_iloc *iloc) 4481ac27a0ecSDave Kleikamp { 44820390131bSFrank Mayhar int err; 44830390131bSFrank Mayhar 4484617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 4485ac27a0ecSDave Kleikamp if (!err) { 4486ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 4487617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, iloc->bh); 4488ac27a0ecSDave Kleikamp if (err) { 4489ac27a0ecSDave Kleikamp brelse(iloc->bh); 4490ac27a0ecSDave Kleikamp iloc->bh = NULL; 4491ac27a0ecSDave Kleikamp } 4492ac27a0ecSDave Kleikamp } 4493617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4494ac27a0ecSDave Kleikamp return err; 4495ac27a0ecSDave Kleikamp } 4496ac27a0ecSDave Kleikamp 4497ac27a0ecSDave Kleikamp /* 44986dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 44996dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 45006dd4ee7cSKalpak Shah */ 45011d03ec98SAneesh Kumar K.V static int ext4_expand_extra_isize(struct inode *inode, 45021d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 45031d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 45041d03ec98SAneesh Kumar K.V handle_t *handle) 45056dd4ee7cSKalpak Shah { 45066dd4ee7cSKalpak Shah struct ext4_inode *raw_inode; 45076dd4ee7cSKalpak Shah struct ext4_xattr_ibody_header *header; 45086dd4ee7cSKalpak Shah 45096dd4ee7cSKalpak Shah if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) 45106dd4ee7cSKalpak Shah return 0; 45116dd4ee7cSKalpak Shah 45126dd4ee7cSKalpak Shah raw_inode = ext4_raw_inode(&iloc); 45136dd4ee7cSKalpak Shah 45146dd4ee7cSKalpak Shah header = IHDR(inode, raw_inode); 45156dd4ee7cSKalpak Shah 45166dd4ee7cSKalpak Shah /* No extended attributes present */ 451719f5fb7aSTheodore Ts'o if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) || 45186dd4ee7cSKalpak Shah header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 45196dd4ee7cSKalpak Shah memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0, 45206dd4ee7cSKalpak Shah new_extra_isize); 45216dd4ee7cSKalpak Shah EXT4_I(inode)->i_extra_isize = new_extra_isize; 45226dd4ee7cSKalpak Shah return 0; 45236dd4ee7cSKalpak Shah } 45246dd4ee7cSKalpak Shah 45256dd4ee7cSKalpak Shah /* try to expand with EAs present */ 45266dd4ee7cSKalpak Shah return ext4_expand_extra_isize_ea(inode, new_extra_isize, 45276dd4ee7cSKalpak Shah raw_inode, handle); 45286dd4ee7cSKalpak Shah } 45296dd4ee7cSKalpak Shah 45306dd4ee7cSKalpak Shah /* 4531ac27a0ecSDave Kleikamp * What we do here is to mark the in-core inode as clean with respect to inode 4532ac27a0ecSDave Kleikamp * dirtiness (it may still be data-dirty). 4533ac27a0ecSDave Kleikamp * This means that the in-core inode may be reaped by prune_icache 4534ac27a0ecSDave Kleikamp * without having to perform any I/O. This is a very good thing, 4535ac27a0ecSDave Kleikamp * because *any* task may call prune_icache - even ones which 4536ac27a0ecSDave Kleikamp * have a transaction open against a different journal. 4537ac27a0ecSDave Kleikamp * 4538ac27a0ecSDave Kleikamp * Is this cheating? Not really. Sure, we haven't written the 4539ac27a0ecSDave Kleikamp * inode out, but prune_icache isn't a user-visible syncing function. 4540ac27a0ecSDave Kleikamp * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 4541ac27a0ecSDave Kleikamp * we start and wait on commits. 4542ac27a0ecSDave Kleikamp * 4543ac27a0ecSDave Kleikamp * Is this efficient/effective? Well, we're being nice to the system 4544ac27a0ecSDave Kleikamp * by cleaning up our inodes proactively so they can be reaped 4545ac27a0ecSDave Kleikamp * without I/O. But we are potentially leaving up to five seconds' 4546ac27a0ecSDave Kleikamp * worth of inodes floating about which prune_icache wants us to 4547ac27a0ecSDave Kleikamp * write out. One way to fix that would be to get prune_icache() 4548ac27a0ecSDave Kleikamp * to do a write_super() to free up some memory. It has the desired 4549ac27a0ecSDave Kleikamp * effect. 4550ac27a0ecSDave Kleikamp */ 4551617ba13bSMingming Cao int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode) 4552ac27a0ecSDave Kleikamp { 4553617ba13bSMingming Cao struct ext4_iloc iloc; 45546dd4ee7cSKalpak Shah struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 45556dd4ee7cSKalpak Shah static unsigned int mnt_count; 45566dd4ee7cSKalpak Shah int err, ret; 4557ac27a0ecSDave Kleikamp 4558ac27a0ecSDave Kleikamp might_sleep(); 45597ff9c073STheodore Ts'o trace_ext4_mark_inode_dirty(inode, _RET_IP_); 4560617ba13bSMingming Cao err = ext4_reserve_inode_write(handle, inode, &iloc); 45610390131bSFrank Mayhar if (ext4_handle_valid(handle) && 45620390131bSFrank Mayhar EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize && 456319f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) { 45646dd4ee7cSKalpak Shah /* 45656dd4ee7cSKalpak Shah * We need extra buffer credits since we may write into EA block 45666dd4ee7cSKalpak Shah * with this same handle. If journal_extend fails, then it will 45676dd4ee7cSKalpak Shah * only result in a minor loss of functionality for that inode. 45686dd4ee7cSKalpak Shah * If this is felt to be critical, then e2fsck should be run to 45696dd4ee7cSKalpak Shah * force a large enough s_min_extra_isize. 45706dd4ee7cSKalpak Shah */ 45716dd4ee7cSKalpak Shah if ((jbd2_journal_extend(handle, 45726dd4ee7cSKalpak Shah EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) { 45736dd4ee7cSKalpak Shah ret = ext4_expand_extra_isize(inode, 45746dd4ee7cSKalpak Shah sbi->s_want_extra_isize, 45756dd4ee7cSKalpak Shah iloc, handle); 45766dd4ee7cSKalpak Shah if (ret) { 457719f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, 457819f5fb7aSTheodore Ts'o EXT4_STATE_NO_EXPAND); 4579c1bddad9SAneesh Kumar K.V if (mnt_count != 4580c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count)) { 458112062dddSEric Sandeen ext4_warning(inode->i_sb, 45826dd4ee7cSKalpak Shah "Unable to expand inode %lu. Delete" 45836dd4ee7cSKalpak Shah " some EAs or run e2fsck.", 45846dd4ee7cSKalpak Shah inode->i_ino); 4585c1bddad9SAneesh Kumar K.V mnt_count = 4586c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count); 45876dd4ee7cSKalpak Shah } 45886dd4ee7cSKalpak Shah } 45896dd4ee7cSKalpak Shah } 45906dd4ee7cSKalpak Shah } 4591ac27a0ecSDave Kleikamp if (!err) 4592617ba13bSMingming Cao err = ext4_mark_iloc_dirty(handle, inode, &iloc); 4593ac27a0ecSDave Kleikamp return err; 4594ac27a0ecSDave Kleikamp } 4595ac27a0ecSDave Kleikamp 4596ac27a0ecSDave Kleikamp /* 4597617ba13bSMingming Cao * ext4_dirty_inode() is called from __mark_inode_dirty() 4598ac27a0ecSDave Kleikamp * 4599ac27a0ecSDave Kleikamp * We're really interested in the case where a file is being extended. 4600ac27a0ecSDave Kleikamp * i_size has been changed by generic_commit_write() and we thus need 4601ac27a0ecSDave Kleikamp * to include the updated inode in the current transaction. 4602ac27a0ecSDave Kleikamp * 46035dd4056dSChristoph Hellwig * Also, dquot_alloc_block() will always dirty the inode when blocks 4604ac27a0ecSDave Kleikamp * are allocated to the file. 4605ac27a0ecSDave Kleikamp * 4606ac27a0ecSDave Kleikamp * If the inode is marked synchronous, we don't honour that here - doing 4607ac27a0ecSDave Kleikamp * so would cause a commit on atime updates, which we don't bother doing. 4608ac27a0ecSDave Kleikamp * We handle synchronous inodes at the highest possible level. 4609ac27a0ecSDave Kleikamp */ 4610aa385729SChristoph Hellwig void ext4_dirty_inode(struct inode *inode, int flags) 4611ac27a0ecSDave Kleikamp { 4612ac27a0ecSDave Kleikamp handle_t *handle; 4613ac27a0ecSDave Kleikamp 4614617ba13bSMingming Cao handle = ext4_journal_start(inode, 2); 4615ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 4616ac27a0ecSDave Kleikamp goto out; 4617f3dc272fSCurt Wohlgemuth 4618617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 4619f3dc272fSCurt Wohlgemuth 4620617ba13bSMingming Cao ext4_journal_stop(handle); 4621ac27a0ecSDave Kleikamp out: 4622ac27a0ecSDave Kleikamp return; 4623ac27a0ecSDave Kleikamp } 4624ac27a0ecSDave Kleikamp 4625ac27a0ecSDave Kleikamp #if 0 4626ac27a0ecSDave Kleikamp /* 4627ac27a0ecSDave Kleikamp * Bind an inode's backing buffer_head into this transaction, to prevent 4628ac27a0ecSDave Kleikamp * it from being flushed to disk early. Unlike 4629617ba13bSMingming Cao * ext4_reserve_inode_write, this leaves behind no bh reference and 4630ac27a0ecSDave Kleikamp * returns no iloc structure, so the caller needs to repeat the iloc 4631ac27a0ecSDave Kleikamp * lookup to mark the inode dirty later. 4632ac27a0ecSDave Kleikamp */ 4633617ba13bSMingming Cao static int ext4_pin_inode(handle_t *handle, struct inode *inode) 4634ac27a0ecSDave Kleikamp { 4635617ba13bSMingming Cao struct ext4_iloc iloc; 4636ac27a0ecSDave Kleikamp 4637ac27a0ecSDave Kleikamp int err = 0; 4638ac27a0ecSDave Kleikamp if (handle) { 4639617ba13bSMingming Cao err = ext4_get_inode_loc(inode, &iloc); 4640ac27a0ecSDave Kleikamp if (!err) { 4641ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc.bh, "get_write_access"); 4642dab291afSMingming Cao err = jbd2_journal_get_write_access(handle, iloc.bh); 4643ac27a0ecSDave Kleikamp if (!err) 46440390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, 464573b50c1cSCurt Wohlgemuth NULL, 4646ac27a0ecSDave Kleikamp iloc.bh); 4647ac27a0ecSDave Kleikamp brelse(iloc.bh); 4648ac27a0ecSDave Kleikamp } 4649ac27a0ecSDave Kleikamp } 4650617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4651ac27a0ecSDave Kleikamp return err; 4652ac27a0ecSDave Kleikamp } 4653ac27a0ecSDave Kleikamp #endif 4654ac27a0ecSDave Kleikamp 4655617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val) 4656ac27a0ecSDave Kleikamp { 4657ac27a0ecSDave Kleikamp journal_t *journal; 4658ac27a0ecSDave Kleikamp handle_t *handle; 4659ac27a0ecSDave Kleikamp int err; 4660ac27a0ecSDave Kleikamp 4661ac27a0ecSDave Kleikamp /* 4662ac27a0ecSDave Kleikamp * We have to be very careful here: changing a data block's 4663ac27a0ecSDave Kleikamp * journaling status dynamically is dangerous. If we write a 4664ac27a0ecSDave Kleikamp * data block to the journal, change the status and then delete 4665ac27a0ecSDave Kleikamp * that block, we risk forgetting to revoke the old log record 4666ac27a0ecSDave Kleikamp * from the journal and so a subsequent replay can corrupt data. 4667ac27a0ecSDave Kleikamp * So, first we make sure that the journal is empty and that 4668ac27a0ecSDave Kleikamp * nobody is changing anything. 4669ac27a0ecSDave Kleikamp */ 4670ac27a0ecSDave Kleikamp 4671617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 46720390131bSFrank Mayhar if (!journal) 46730390131bSFrank Mayhar return 0; 4674d699594dSDave Hansen if (is_journal_aborted(journal)) 4675ac27a0ecSDave Kleikamp return -EROFS; 4676ac27a0ecSDave Kleikamp 4677dab291afSMingming Cao jbd2_journal_lock_updates(journal); 4678dab291afSMingming Cao jbd2_journal_flush(journal); 4679ac27a0ecSDave Kleikamp 4680ac27a0ecSDave Kleikamp /* 4681ac27a0ecSDave Kleikamp * OK, there are no updates running now, and all cached data is 4682ac27a0ecSDave Kleikamp * synced to disk. We are now in a completely consistent state 4683ac27a0ecSDave Kleikamp * which doesn't have anything in the journal, and we know that 4684ac27a0ecSDave Kleikamp * no filesystem updates are running, so it is safe to modify 4685ac27a0ecSDave Kleikamp * the inode's in-core data-journaling state flag now. 4686ac27a0ecSDave Kleikamp */ 4687ac27a0ecSDave Kleikamp 4688ac27a0ecSDave Kleikamp if (val) 468912e9b892SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 4690ac27a0ecSDave Kleikamp else 469112e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 4692617ba13bSMingming Cao ext4_set_aops(inode); 4693ac27a0ecSDave Kleikamp 4694dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 4695ac27a0ecSDave Kleikamp 4696ac27a0ecSDave Kleikamp /* Finally we can mark the inode as dirty. */ 4697ac27a0ecSDave Kleikamp 4698617ba13bSMingming Cao handle = ext4_journal_start(inode, 1); 4699ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 4700ac27a0ecSDave Kleikamp return PTR_ERR(handle); 4701ac27a0ecSDave Kleikamp 4702617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 47030390131bSFrank Mayhar ext4_handle_sync(handle); 4704617ba13bSMingming Cao ext4_journal_stop(handle); 4705617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4706ac27a0ecSDave Kleikamp 4707ac27a0ecSDave Kleikamp return err; 4708ac27a0ecSDave Kleikamp } 47092e9ee850SAneesh Kumar K.V 47102e9ee850SAneesh Kumar K.V static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) 47112e9ee850SAneesh Kumar K.V { 47122e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 47132e9ee850SAneesh Kumar K.V } 47142e9ee850SAneesh Kumar K.V 4715c2ec175cSNick Piggin int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) 47162e9ee850SAneesh Kumar K.V { 4717c2ec175cSNick Piggin struct page *page = vmf->page; 47182e9ee850SAneesh Kumar K.V loff_t size; 47192e9ee850SAneesh Kumar K.V unsigned long len; 47209ea7df53SJan Kara int ret; 47212e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 47222e9ee850SAneesh Kumar K.V struct inode *inode = file->f_path.dentry->d_inode; 47232e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 47249ea7df53SJan Kara handle_t *handle; 47259ea7df53SJan Kara get_block_t *get_block; 47269ea7df53SJan Kara int retries = 0; 47272e9ee850SAneesh Kumar K.V 47282e9ee850SAneesh Kumar K.V /* 47299ea7df53SJan Kara * This check is racy but catches the common case. We rely on 47309ea7df53SJan Kara * __block_page_mkwrite() to do a reliable check. 47312e9ee850SAneesh Kumar K.V */ 47329ea7df53SJan Kara vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); 47339ea7df53SJan Kara /* Delalloc case is easy... */ 47349ea7df53SJan Kara if (test_opt(inode->i_sb, DELALLOC) && 47359ea7df53SJan Kara !ext4_should_journal_data(inode) && 47369ea7df53SJan Kara !ext4_nonda_switch(inode->i_sb)) { 47379ea7df53SJan Kara do { 47389ea7df53SJan Kara ret = __block_page_mkwrite(vma, vmf, 47399ea7df53SJan Kara ext4_da_get_block_prep); 47409ea7df53SJan Kara } while (ret == -ENOSPC && 47419ea7df53SJan Kara ext4_should_retry_alloc(inode->i_sb, &retries)); 47429ea7df53SJan Kara goto out_ret; 47432e9ee850SAneesh Kumar K.V } 47440e499890SDarrick J. Wong 47450e499890SDarrick J. Wong lock_page(page); 47469ea7df53SJan Kara size = i_size_read(inode); 47479ea7df53SJan Kara /* Page got truncated from under us? */ 47489ea7df53SJan Kara if (page->mapping != mapping || page_offset(page) > size) { 47499ea7df53SJan Kara unlock_page(page); 47509ea7df53SJan Kara ret = VM_FAULT_NOPAGE; 47519ea7df53SJan Kara goto out; 47520e499890SDarrick J. Wong } 47532e9ee850SAneesh Kumar K.V 47542e9ee850SAneesh Kumar K.V if (page->index == size >> PAGE_CACHE_SHIFT) 47552e9ee850SAneesh Kumar K.V len = size & ~PAGE_CACHE_MASK; 47562e9ee850SAneesh Kumar K.V else 47572e9ee850SAneesh Kumar K.V len = PAGE_CACHE_SIZE; 4758a827eaffSAneesh Kumar K.V /* 47599ea7df53SJan Kara * Return if we have all the buffers mapped. This avoids the need to do 47609ea7df53SJan Kara * journal_start/journal_stop which can block and take a long time 4761a827eaffSAneesh Kumar K.V */ 47622e9ee850SAneesh Kumar K.V if (page_has_buffers(page)) { 47632e9ee850SAneesh Kumar K.V if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL, 4764a827eaffSAneesh Kumar K.V ext4_bh_unmapped)) { 47659ea7df53SJan Kara /* Wait so that we don't change page under IO */ 47669ea7df53SJan Kara wait_on_page_writeback(page); 47679ea7df53SJan Kara ret = VM_FAULT_LOCKED; 47689ea7df53SJan Kara goto out; 47692e9ee850SAneesh Kumar K.V } 4770a827eaffSAneesh Kumar K.V } 4771a827eaffSAneesh Kumar K.V unlock_page(page); 47729ea7df53SJan Kara /* OK, we need to fill the hole... */ 47739ea7df53SJan Kara if (ext4_should_dioread_nolock(inode)) 47749ea7df53SJan Kara get_block = ext4_get_block_write; 47759ea7df53SJan Kara else 47769ea7df53SJan Kara get_block = ext4_get_block; 47779ea7df53SJan Kara retry_alloc: 47789ea7df53SJan Kara handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode)); 47799ea7df53SJan Kara if (IS_ERR(handle)) { 4780c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 47819ea7df53SJan Kara goto out; 47829ea7df53SJan Kara } 47839ea7df53SJan Kara ret = __block_page_mkwrite(vma, vmf, get_block); 47849ea7df53SJan Kara if (!ret && ext4_should_journal_data(inode)) { 47859ea7df53SJan Kara if (walk_page_buffers(handle, page_buffers(page), 0, 47869ea7df53SJan Kara PAGE_CACHE_SIZE, NULL, do_journal_get_write_access)) { 47879ea7df53SJan Kara unlock_page(page); 47889ea7df53SJan Kara ret = VM_FAULT_SIGBUS; 47899ea7df53SJan Kara goto out; 47909ea7df53SJan Kara } 47919ea7df53SJan Kara ext4_set_inode_state(inode, EXT4_STATE_JDATA); 47929ea7df53SJan Kara } 47939ea7df53SJan Kara ext4_journal_stop(handle); 47949ea7df53SJan Kara if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 47959ea7df53SJan Kara goto retry_alloc; 47969ea7df53SJan Kara out_ret: 47979ea7df53SJan Kara ret = block_page_mkwrite_return(ret); 47989ea7df53SJan Kara out: 47992e9ee850SAneesh Kumar K.V return ret; 48002e9ee850SAneesh Kumar K.V } 4801