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" 45d2a17637SMingming Cao #include "ext4_extents.h" 469f125d64STheodore Ts'o #include "truncate.h" 47ac27a0ecSDave Kleikamp 489bffad1eSTheodore Ts'o #include <trace/events/ext4.h> 499bffad1eSTheodore Ts'o 50a1d6cc56SAneesh Kumar K.V #define MPAGE_DA_EXTENT_TAIL 0x01 51a1d6cc56SAneesh Kumar K.V 52678aaf48SJan Kara static inline int ext4_begin_ordered_truncate(struct inode *inode, 53678aaf48SJan Kara loff_t new_size) 54678aaf48SJan Kara { 557ff9c073STheodore Ts'o trace_ext4_begin_ordered_truncate(inode, new_size); 568aefcd55STheodore Ts'o /* 578aefcd55STheodore Ts'o * If jinode is zero, then we never opened the file for 588aefcd55STheodore Ts'o * writing, so there's no need to call 598aefcd55STheodore Ts'o * jbd2_journal_begin_ordered_truncate() since there's no 608aefcd55STheodore Ts'o * outstanding writes we need to flush. 618aefcd55STheodore Ts'o */ 628aefcd55STheodore Ts'o if (!EXT4_I(inode)->jinode) 638aefcd55STheodore Ts'o return 0; 648aefcd55STheodore Ts'o return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode), 658aefcd55STheodore Ts'o EXT4_I(inode)->jinode, 66678aaf48SJan Kara new_size); 67678aaf48SJan Kara } 68678aaf48SJan Kara 6964769240SAlex Tomas static void ext4_invalidatepage(struct page *page, unsigned long offset); 70cb20d518STheodore Ts'o static int noalloc_get_block_write(struct inode *inode, sector_t iblock, 71cb20d518STheodore Ts'o struct buffer_head *bh_result, int create); 72cb20d518STheodore Ts'o static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode); 73cb20d518STheodore Ts'o static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate); 74cb20d518STheodore Ts'o static int __ext4_journalled_writepage(struct page *page, unsigned int len); 75cb20d518STheodore Ts'o static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh); 7664769240SAlex Tomas 77ac27a0ecSDave Kleikamp /* 78ac27a0ecSDave Kleikamp * Test whether an inode is a fast symlink. 79ac27a0ecSDave Kleikamp */ 80617ba13bSMingming Cao static int ext4_inode_is_fast_symlink(struct inode *inode) 81ac27a0ecSDave Kleikamp { 82617ba13bSMingming Cao int ea_blocks = EXT4_I(inode)->i_file_acl ? 83ac27a0ecSDave Kleikamp (inode->i_sb->s_blocksize >> 9) : 0; 84ac27a0ecSDave Kleikamp 85ac27a0ecSDave Kleikamp return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0); 86ac27a0ecSDave Kleikamp } 87ac27a0ecSDave Kleikamp 88ac27a0ecSDave Kleikamp /* 89ac27a0ecSDave Kleikamp * Restart the transaction associated with *handle. This does a commit, 90ac27a0ecSDave Kleikamp * so before we call here everything must be consistently dirtied against 91ac27a0ecSDave Kleikamp * this transaction. 92ac27a0ecSDave Kleikamp */ 93487caeefSJan Kara int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode, 94487caeefSJan Kara int nblocks) 95ac27a0ecSDave Kleikamp { 96487caeefSJan Kara int ret; 97487caeefSJan Kara 98487caeefSJan Kara /* 99e35fd660STheodore Ts'o * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this 100487caeefSJan Kara * moment, get_block can be called only for blocks inside i_size since 101487caeefSJan Kara * page cache has been already dropped and writes are blocked by 102487caeefSJan Kara * i_mutex. So we can safely drop the i_data_sem here. 103487caeefSJan Kara */ 1040390131bSFrank Mayhar BUG_ON(EXT4_JOURNAL(inode) == NULL); 105ac27a0ecSDave Kleikamp jbd_debug(2, "restarting handle %p\n", handle); 106487caeefSJan Kara up_write(&EXT4_I(inode)->i_data_sem); 1078e8eaabeSAmir Goldstein ret = ext4_journal_restart(handle, nblocks); 108487caeefSJan Kara down_write(&EXT4_I(inode)->i_data_sem); 109fa5d1113SAneesh Kumar K.V ext4_discard_preallocations(inode); 110487caeefSJan Kara 111487caeefSJan Kara return ret; 112ac27a0ecSDave Kleikamp } 113ac27a0ecSDave Kleikamp 114ac27a0ecSDave Kleikamp /* 115ac27a0ecSDave Kleikamp * Called at the last iput() if i_nlink is zero. 116ac27a0ecSDave Kleikamp */ 1170930fcc1SAl Viro void ext4_evict_inode(struct inode *inode) 118ac27a0ecSDave Kleikamp { 119ac27a0ecSDave Kleikamp handle_t *handle; 120bc965ab3STheodore Ts'o int err; 121ac27a0ecSDave Kleikamp 1227ff9c073STheodore Ts'o trace_ext4_evict_inode(inode); 1232581fdc8SJiaying Zhang 1242581fdc8SJiaying Zhang mutex_lock(&inode->i_mutex); 1252581fdc8SJiaying Zhang ext4_flush_completed_IO(inode); 1262581fdc8SJiaying Zhang mutex_unlock(&inode->i_mutex); 1272581fdc8SJiaying Zhang ext4_ioend_wait(inode); 1282581fdc8SJiaying Zhang 1290930fcc1SAl Viro if (inode->i_nlink) { 1302d859db3SJan Kara /* 1312d859db3SJan Kara * When journalling data dirty buffers are tracked only in the 1322d859db3SJan Kara * journal. So although mm thinks everything is clean and 1332d859db3SJan Kara * ready for reaping the inode might still have some pages to 1342d859db3SJan Kara * write in the running transaction or waiting to be 1352d859db3SJan Kara * checkpointed. Thus calling jbd2_journal_invalidatepage() 1362d859db3SJan Kara * (via truncate_inode_pages()) to discard these buffers can 1372d859db3SJan Kara * cause data loss. Also even if we did not discard these 1382d859db3SJan Kara * buffers, we would have no way to find them after the inode 1392d859db3SJan Kara * is reaped and thus user could see stale data if he tries to 1402d859db3SJan Kara * read them before the transaction is checkpointed. So be 1412d859db3SJan Kara * careful and force everything to disk here... We use 1422d859db3SJan Kara * ei->i_datasync_tid to store the newest transaction 1432d859db3SJan Kara * containing inode's data. 1442d859db3SJan Kara * 1452d859db3SJan Kara * Note that directories do not have this problem because they 1462d859db3SJan Kara * don't use page cache. 1472d859db3SJan Kara */ 1482d859db3SJan Kara if (ext4_should_journal_data(inode) && 1492d859db3SJan Kara (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode))) { 1502d859db3SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 1512d859db3SJan Kara tid_t commit_tid = EXT4_I(inode)->i_datasync_tid; 1522d859db3SJan Kara 1532d859db3SJan Kara jbd2_log_start_commit(journal, commit_tid); 1542d859db3SJan Kara jbd2_log_wait_commit(journal, commit_tid); 1552d859db3SJan Kara filemap_write_and_wait(&inode->i_data); 1562d859db3SJan Kara } 1570930fcc1SAl Viro truncate_inode_pages(&inode->i_data, 0); 1580930fcc1SAl Viro goto no_delete; 1590930fcc1SAl Viro } 1600930fcc1SAl Viro 161907f4554SChristoph Hellwig if (!is_bad_inode(inode)) 162871a2931SChristoph Hellwig dquot_initialize(inode); 163907f4554SChristoph Hellwig 164678aaf48SJan Kara if (ext4_should_order_data(inode)) 165678aaf48SJan Kara ext4_begin_ordered_truncate(inode, 0); 166ac27a0ecSDave Kleikamp truncate_inode_pages(&inode->i_data, 0); 167ac27a0ecSDave Kleikamp 168ac27a0ecSDave Kleikamp if (is_bad_inode(inode)) 169ac27a0ecSDave Kleikamp goto no_delete; 170ac27a0ecSDave Kleikamp 1719f125d64STheodore Ts'o handle = ext4_journal_start(inode, ext4_blocks_for_truncate(inode)+3); 172ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 173bc965ab3STheodore Ts'o ext4_std_error(inode->i_sb, PTR_ERR(handle)); 174ac27a0ecSDave Kleikamp /* 175ac27a0ecSDave Kleikamp * If we're going to skip the normal cleanup, we still need to 176ac27a0ecSDave Kleikamp * make sure that the in-core orphan linked list is properly 177ac27a0ecSDave Kleikamp * cleaned up. 178ac27a0ecSDave Kleikamp */ 179617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 180ac27a0ecSDave Kleikamp goto no_delete; 181ac27a0ecSDave Kleikamp } 182ac27a0ecSDave Kleikamp 183ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 1840390131bSFrank Mayhar ext4_handle_sync(handle); 185ac27a0ecSDave Kleikamp inode->i_size = 0; 186bc965ab3STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 187bc965ab3STheodore Ts'o if (err) { 18812062dddSEric Sandeen ext4_warning(inode->i_sb, 189bc965ab3STheodore Ts'o "couldn't mark inode dirty (err %d)", err); 190bc965ab3STheodore Ts'o goto stop_handle; 191bc965ab3STheodore Ts'o } 192ac27a0ecSDave Kleikamp if (inode->i_blocks) 193617ba13bSMingming Cao ext4_truncate(inode); 194bc965ab3STheodore Ts'o 195bc965ab3STheodore Ts'o /* 196bc965ab3STheodore Ts'o * ext4_ext_truncate() doesn't reserve any slop when it 197bc965ab3STheodore Ts'o * restarts journal transactions; therefore there may not be 198bc965ab3STheodore Ts'o * enough credits left in the handle to remove the inode from 199bc965ab3STheodore Ts'o * the orphan list and set the dtime field. 200bc965ab3STheodore Ts'o */ 2010390131bSFrank Mayhar if (!ext4_handle_has_enough_credits(handle, 3)) { 202bc965ab3STheodore Ts'o err = ext4_journal_extend(handle, 3); 203bc965ab3STheodore Ts'o if (err > 0) 204bc965ab3STheodore Ts'o err = ext4_journal_restart(handle, 3); 205bc965ab3STheodore Ts'o if (err != 0) { 20612062dddSEric Sandeen ext4_warning(inode->i_sb, 207bc965ab3STheodore Ts'o "couldn't extend journal (err %d)", err); 208bc965ab3STheodore Ts'o stop_handle: 209bc965ab3STheodore Ts'o ext4_journal_stop(handle); 21045388219STheodore Ts'o ext4_orphan_del(NULL, inode); 211bc965ab3STheodore Ts'o goto no_delete; 212bc965ab3STheodore Ts'o } 213bc965ab3STheodore Ts'o } 214bc965ab3STheodore Ts'o 215ac27a0ecSDave Kleikamp /* 216617ba13bSMingming Cao * Kill off the orphan record which ext4_truncate created. 217ac27a0ecSDave Kleikamp * AKPM: I think this can be inside the above `if'. 218617ba13bSMingming Cao * Note that ext4_orphan_del() has to be able to cope with the 219ac27a0ecSDave Kleikamp * deletion of a non-existent orphan - this is because we don't 220617ba13bSMingming Cao * know if ext4_truncate() actually created an orphan record. 221ac27a0ecSDave Kleikamp * (Well, we could do this if we need to, but heck - it works) 222ac27a0ecSDave Kleikamp */ 223617ba13bSMingming Cao ext4_orphan_del(handle, inode); 224617ba13bSMingming Cao EXT4_I(inode)->i_dtime = get_seconds(); 225ac27a0ecSDave Kleikamp 226ac27a0ecSDave Kleikamp /* 227ac27a0ecSDave Kleikamp * One subtle ordering requirement: if anything has gone wrong 228ac27a0ecSDave Kleikamp * (transaction abort, IO errors, whatever), then we can still 229ac27a0ecSDave Kleikamp * do these next steps (the fs will already have been marked as 230ac27a0ecSDave Kleikamp * having errors), but we can't free the inode if the mark_dirty 231ac27a0ecSDave Kleikamp * fails. 232ac27a0ecSDave Kleikamp */ 233617ba13bSMingming Cao if (ext4_mark_inode_dirty(handle, inode)) 234ac27a0ecSDave Kleikamp /* If that failed, just do the required in-core inode clear. */ 2350930fcc1SAl Viro ext4_clear_inode(inode); 236ac27a0ecSDave Kleikamp else 237617ba13bSMingming Cao ext4_free_inode(handle, inode); 238617ba13bSMingming Cao ext4_journal_stop(handle); 239ac27a0ecSDave Kleikamp return; 240ac27a0ecSDave Kleikamp no_delete: 2410930fcc1SAl Viro ext4_clear_inode(inode); /* We must guarantee clearing of inode... */ 242ac27a0ecSDave Kleikamp } 243ac27a0ecSDave Kleikamp 244a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 245a9e7f447SDmitry Monakhov qsize_t *ext4_get_reserved_space(struct inode *inode) 24660e58e0fSMingming Cao { 247a9e7f447SDmitry Monakhov return &EXT4_I(inode)->i_reserved_quota; 24860e58e0fSMingming Cao } 249a9e7f447SDmitry Monakhov #endif 2509d0be502STheodore Ts'o 25112219aeaSAneesh Kumar K.V /* 25212219aeaSAneesh Kumar K.V * Calculate the number of metadata blocks need to reserve 2539d0be502STheodore Ts'o * to allocate a block located at @lblock 25412219aeaSAneesh Kumar K.V */ 25501f49d0bSTheodore Ts'o static int ext4_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock) 25612219aeaSAneesh Kumar K.V { 25712e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 2589d0be502STheodore Ts'o return ext4_ext_calc_metadata_amount(inode, lblock); 25912219aeaSAneesh Kumar K.V 2608bb2b247SAmir Goldstein return ext4_ind_calc_metadata_amount(inode, lblock); 26112219aeaSAneesh Kumar K.V } 26212219aeaSAneesh Kumar K.V 2630637c6f4STheodore Ts'o /* 2640637c6f4STheodore Ts'o * Called with i_data_sem down, which is important since we can call 2650637c6f4STheodore Ts'o * ext4_discard_preallocations() from here. 2660637c6f4STheodore Ts'o */ 2675f634d06SAneesh Kumar K.V void ext4_da_update_reserve_space(struct inode *inode, 2685f634d06SAneesh Kumar K.V int used, int quota_claim) 26912219aeaSAneesh Kumar K.V { 27012219aeaSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 2710637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 27212219aeaSAneesh Kumar K.V 2730637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 274f8ec9d68STheodore Ts'o trace_ext4_da_update_reserve_space(inode, used); 2750637c6f4STheodore Ts'o if (unlikely(used > ei->i_reserved_data_blocks)) { 2760637c6f4STheodore Ts'o ext4_msg(inode->i_sb, KERN_NOTICE, "%s: ino %lu, used %d " 2770637c6f4STheodore Ts'o "with only %d reserved data blocks\n", 2780637c6f4STheodore Ts'o __func__, inode->i_ino, used, 2790637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 2800637c6f4STheodore Ts'o WARN_ON(1); 2810637c6f4STheodore Ts'o used = ei->i_reserved_data_blocks; 2826bc6e63fSAneesh Kumar K.V } 28312219aeaSAneesh Kumar K.V 2840637c6f4STheodore Ts'o /* Update per-inode reservations */ 2850637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= used; 2860637c6f4STheodore Ts'o ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks; 28772b8ab9dSEric Sandeen percpu_counter_sub(&sbi->s_dirtyblocks_counter, 28872b8ab9dSEric Sandeen used + ei->i_allocated_meta_blocks); 2890637c6f4STheodore Ts'o ei->i_allocated_meta_blocks = 0; 2900637c6f4STheodore Ts'o 2910637c6f4STheodore Ts'o if (ei->i_reserved_data_blocks == 0) { 2920637c6f4STheodore Ts'o /* 2930637c6f4STheodore Ts'o * We can release all of the reserved metadata blocks 2940637c6f4STheodore Ts'o * only when we have written all of the delayed 2950637c6f4STheodore Ts'o * allocation blocks. 2960637c6f4STheodore Ts'o */ 29772b8ab9dSEric Sandeen percpu_counter_sub(&sbi->s_dirtyblocks_counter, 29872b8ab9dSEric Sandeen ei->i_reserved_meta_blocks); 299ee5f4d9cSTheodore Ts'o ei->i_reserved_meta_blocks = 0; 3009d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 3010637c6f4STheodore Ts'o } 30212219aeaSAneesh Kumar K.V spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 30360e58e0fSMingming Cao 30472b8ab9dSEric Sandeen /* Update quota subsystem for data blocks */ 30572b8ab9dSEric Sandeen if (quota_claim) 3065dd4056dSChristoph Hellwig dquot_claim_block(inode, used); 30772b8ab9dSEric Sandeen else { 3085f634d06SAneesh Kumar K.V /* 3095f634d06SAneesh Kumar K.V * We did fallocate with an offset that is already delayed 3105f634d06SAneesh Kumar K.V * allocated. So on delayed allocated writeback we should 31172b8ab9dSEric Sandeen * not re-claim the quota for fallocated blocks. 3125f634d06SAneesh Kumar K.V */ 31372b8ab9dSEric Sandeen dquot_release_reservation_block(inode, used); 3145f634d06SAneesh Kumar K.V } 315d6014301SAneesh Kumar K.V 316d6014301SAneesh Kumar K.V /* 317d6014301SAneesh Kumar K.V * If we have done all the pending block allocations and if 318d6014301SAneesh Kumar K.V * there aren't any writers on the inode, we can discard the 319d6014301SAneesh Kumar K.V * inode's preallocations. 320d6014301SAneesh Kumar K.V */ 3210637c6f4STheodore Ts'o if ((ei->i_reserved_data_blocks == 0) && 3220637c6f4STheodore Ts'o (atomic_read(&inode->i_writecount) == 0)) 323d6014301SAneesh Kumar K.V ext4_discard_preallocations(inode); 32412219aeaSAneesh Kumar K.V } 32512219aeaSAneesh Kumar K.V 326e29136f8STheodore Ts'o static int __check_block_validity(struct inode *inode, const char *func, 327c398eda0STheodore Ts'o unsigned int line, 32824676da4STheodore Ts'o struct ext4_map_blocks *map) 3296fd058f7STheodore Ts'o { 33024676da4STheodore Ts'o if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk, 33124676da4STheodore Ts'o map->m_len)) { 332c398eda0STheodore Ts'o ext4_error_inode(inode, func, line, map->m_pblk, 333c398eda0STheodore Ts'o "lblock %lu mapped to illegal pblock " 33424676da4STheodore Ts'o "(length %d)", (unsigned long) map->m_lblk, 335c398eda0STheodore Ts'o map->m_len); 3366fd058f7STheodore Ts'o return -EIO; 3376fd058f7STheodore Ts'o } 3386fd058f7STheodore Ts'o return 0; 3396fd058f7STheodore Ts'o } 3406fd058f7STheodore Ts'o 341e29136f8STheodore Ts'o #define check_block_validity(inode, map) \ 342c398eda0STheodore Ts'o __check_block_validity((inode), __func__, __LINE__, (map)) 343e29136f8STheodore Ts'o 344f5ab0d1fSMingming Cao /* 3451f94533dSTheodore Ts'o * Return the number of contiguous dirty pages in a given inode 3461f94533dSTheodore Ts'o * starting at page frame idx. 34755138e0bSTheodore Ts'o */ 34855138e0bSTheodore Ts'o static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx, 34955138e0bSTheodore Ts'o unsigned int max_pages) 35055138e0bSTheodore Ts'o { 35155138e0bSTheodore Ts'o struct address_space *mapping = inode->i_mapping; 35255138e0bSTheodore Ts'o pgoff_t index; 35355138e0bSTheodore Ts'o struct pagevec pvec; 35455138e0bSTheodore Ts'o pgoff_t num = 0; 35555138e0bSTheodore Ts'o int i, nr_pages, done = 0; 35655138e0bSTheodore Ts'o 35755138e0bSTheodore Ts'o if (max_pages == 0) 35855138e0bSTheodore Ts'o return 0; 35955138e0bSTheodore Ts'o pagevec_init(&pvec, 0); 36055138e0bSTheodore Ts'o while (!done) { 36155138e0bSTheodore Ts'o index = idx; 36255138e0bSTheodore Ts'o nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 36355138e0bSTheodore Ts'o PAGECACHE_TAG_DIRTY, 36455138e0bSTheodore Ts'o (pgoff_t)PAGEVEC_SIZE); 36555138e0bSTheodore Ts'o if (nr_pages == 0) 36655138e0bSTheodore Ts'o break; 36755138e0bSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 36855138e0bSTheodore Ts'o struct page *page = pvec.pages[i]; 36955138e0bSTheodore Ts'o struct buffer_head *bh, *head; 37055138e0bSTheodore Ts'o 37155138e0bSTheodore Ts'o lock_page(page); 37255138e0bSTheodore Ts'o if (unlikely(page->mapping != mapping) || 37355138e0bSTheodore Ts'o !PageDirty(page) || 37455138e0bSTheodore Ts'o PageWriteback(page) || 37555138e0bSTheodore Ts'o page->index != idx) { 37655138e0bSTheodore Ts'o done = 1; 37755138e0bSTheodore Ts'o unlock_page(page); 37855138e0bSTheodore Ts'o break; 37955138e0bSTheodore Ts'o } 3801f94533dSTheodore Ts'o if (page_has_buffers(page)) { 3811f94533dSTheodore Ts'o bh = head = page_buffers(page); 38255138e0bSTheodore Ts'o do { 38355138e0bSTheodore Ts'o if (!buffer_delay(bh) && 3841f94533dSTheodore Ts'o !buffer_unwritten(bh)) 38555138e0bSTheodore Ts'o done = 1; 3861f94533dSTheodore Ts'o bh = bh->b_this_page; 3871f94533dSTheodore Ts'o } while (!done && (bh != head)); 38855138e0bSTheodore Ts'o } 38955138e0bSTheodore Ts'o unlock_page(page); 39055138e0bSTheodore Ts'o if (done) 39155138e0bSTheodore Ts'o break; 39255138e0bSTheodore Ts'o idx++; 39355138e0bSTheodore Ts'o num++; 394659c6009SEric Sandeen if (num >= max_pages) { 395659c6009SEric Sandeen done = 1; 39655138e0bSTheodore Ts'o break; 39755138e0bSTheodore Ts'o } 398659c6009SEric Sandeen } 39955138e0bSTheodore Ts'o pagevec_release(&pvec); 40055138e0bSTheodore Ts'o } 40155138e0bSTheodore Ts'o return num; 40255138e0bSTheodore Ts'o } 40355138e0bSTheodore Ts'o 40455138e0bSTheodore Ts'o /* 405e35fd660STheodore Ts'o * The ext4_map_blocks() function tries to look up the requested blocks, 4062b2d6d01STheodore Ts'o * and returns if the blocks are already mapped. 407f5ab0d1fSMingming Cao * 408f5ab0d1fSMingming Cao * Otherwise it takes the write lock of the i_data_sem and allocate blocks 409f5ab0d1fSMingming Cao * and store the allocated blocks in the result buffer head and mark it 410f5ab0d1fSMingming Cao * mapped. 411f5ab0d1fSMingming Cao * 412e35fd660STheodore Ts'o * If file type is extents based, it will call ext4_ext_map_blocks(), 413e35fd660STheodore Ts'o * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping 414f5ab0d1fSMingming Cao * based files 415f5ab0d1fSMingming Cao * 416f5ab0d1fSMingming Cao * On success, it returns the number of blocks being mapped or allocate. 417f5ab0d1fSMingming Cao * if create==0 and the blocks are pre-allocated and uninitialized block, 418f5ab0d1fSMingming Cao * the result buffer head is unmapped. If the create ==1, it will make sure 419f5ab0d1fSMingming Cao * the buffer head is mapped. 420f5ab0d1fSMingming Cao * 421f5ab0d1fSMingming Cao * It returns 0 if plain look up failed (blocks have not been allocated), in 422f5ab0d1fSMingming Cao * that casem, buffer head is unmapped 423f5ab0d1fSMingming Cao * 424f5ab0d1fSMingming Cao * It returns the error in case of allocation failure. 425f5ab0d1fSMingming Cao */ 426e35fd660STheodore Ts'o int ext4_map_blocks(handle_t *handle, struct inode *inode, 427e35fd660STheodore Ts'o struct ext4_map_blocks *map, int flags) 4280e855ac8SAneesh Kumar K.V { 4290e855ac8SAneesh Kumar K.V int retval; 430f5ab0d1fSMingming Cao 431e35fd660STheodore Ts'o map->m_flags = 0; 432e35fd660STheodore Ts'o ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u," 433e35fd660STheodore Ts'o "logical block %lu\n", inode->i_ino, flags, map->m_len, 434e35fd660STheodore Ts'o (unsigned long) map->m_lblk); 4354df3d265SAneesh Kumar K.V /* 436b920c755STheodore Ts'o * Try to see if we can get the block without requesting a new 437b920c755STheodore Ts'o * file system block. 4384df3d265SAneesh Kumar K.V */ 4390e855ac8SAneesh Kumar K.V down_read((&EXT4_I(inode)->i_data_sem)); 44012e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 441e35fd660STheodore Ts'o retval = ext4_ext_map_blocks(handle, inode, map, 0); 4424df3d265SAneesh Kumar K.V } else { 443e35fd660STheodore Ts'o retval = ext4_ind_map_blocks(handle, inode, map, 0); 4440e855ac8SAneesh Kumar K.V } 4454df3d265SAneesh Kumar K.V up_read((&EXT4_I(inode)->i_data_sem)); 446f5ab0d1fSMingming Cao 447e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 448e29136f8STheodore Ts'o int ret = check_block_validity(inode, map); 4496fd058f7STheodore Ts'o if (ret != 0) 4506fd058f7STheodore Ts'o return ret; 4516fd058f7STheodore Ts'o } 4526fd058f7STheodore Ts'o 453f5ab0d1fSMingming Cao /* If it is only a block(s) look up */ 454c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 4554df3d265SAneesh Kumar K.V return retval; 4564df3d265SAneesh Kumar K.V 4574df3d265SAneesh Kumar K.V /* 458f5ab0d1fSMingming Cao * Returns if the blocks have already allocated 459f5ab0d1fSMingming Cao * 460f5ab0d1fSMingming Cao * Note that if blocks have been preallocated 461f5ab0d1fSMingming Cao * ext4_ext_get_block() returns th create = 0 462f5ab0d1fSMingming Cao * with buffer head unmapped. 463f5ab0d1fSMingming Cao */ 464e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) 465f5ab0d1fSMingming Cao return retval; 466f5ab0d1fSMingming Cao 467f5ab0d1fSMingming Cao /* 4682a8964d6SAneesh Kumar K.V * When we call get_blocks without the create flag, the 4692a8964d6SAneesh Kumar K.V * BH_Unwritten flag could have gotten set if the blocks 4702a8964d6SAneesh Kumar K.V * requested were part of a uninitialized extent. We need to 4712a8964d6SAneesh Kumar K.V * clear this flag now that we are committed to convert all or 4722a8964d6SAneesh Kumar K.V * part of the uninitialized extent to be an initialized 4732a8964d6SAneesh Kumar K.V * extent. This is because we need to avoid the combination 4742a8964d6SAneesh Kumar K.V * of BH_Unwritten and BH_Mapped flags being simultaneously 4752a8964d6SAneesh Kumar K.V * set on the buffer_head. 4762a8964d6SAneesh Kumar K.V */ 477e35fd660STheodore Ts'o map->m_flags &= ~EXT4_MAP_UNWRITTEN; 4782a8964d6SAneesh Kumar K.V 4792a8964d6SAneesh Kumar K.V /* 480f5ab0d1fSMingming Cao * New blocks allocate and/or writing to uninitialized extent 481f5ab0d1fSMingming Cao * will possibly result in updating i_data, so we take 482f5ab0d1fSMingming Cao * the write lock of i_data_sem, and call get_blocks() 483f5ab0d1fSMingming Cao * with create == 1 flag. 4844df3d265SAneesh Kumar K.V */ 4854df3d265SAneesh Kumar K.V down_write((&EXT4_I(inode)->i_data_sem)); 486d2a17637SMingming Cao 487d2a17637SMingming Cao /* 488d2a17637SMingming Cao * if the caller is from delayed allocation writeout path 489d2a17637SMingming Cao * we have already reserved fs blocks for allocation 490d2a17637SMingming Cao * let the underlying get_block() function know to 491d2a17637SMingming Cao * avoid double accounting 492d2a17637SMingming Cao */ 493c2177057STheodore Ts'o if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 494f2321097STheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED); 4954df3d265SAneesh Kumar K.V /* 4964df3d265SAneesh Kumar K.V * We need to check for EXT4 here because migrate 4974df3d265SAneesh Kumar K.V * could have changed the inode type in between 4984df3d265SAneesh Kumar K.V */ 49912e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 500e35fd660STheodore Ts'o retval = ext4_ext_map_blocks(handle, inode, map, flags); 5010e855ac8SAneesh Kumar K.V } else { 502e35fd660STheodore Ts'o retval = ext4_ind_map_blocks(handle, inode, map, flags); 503267e4db9SAneesh Kumar K.V 504e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_NEW) { 505267e4db9SAneesh Kumar K.V /* 506267e4db9SAneesh Kumar K.V * We allocated new blocks which will result in 507267e4db9SAneesh Kumar K.V * i_data's format changing. Force the migrate 508267e4db9SAneesh Kumar K.V * to fail by clearing migrate flags 509267e4db9SAneesh Kumar K.V */ 51019f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE); 511267e4db9SAneesh Kumar K.V } 5122ac3b6e0STheodore Ts'o 513d2a17637SMingming Cao /* 5142ac3b6e0STheodore Ts'o * Update reserved blocks/metadata blocks after successful 5155f634d06SAneesh Kumar K.V * block allocation which had been deferred till now. We don't 5165f634d06SAneesh Kumar K.V * support fallocate for non extent files. So we can update 5175f634d06SAneesh Kumar K.V * reserve space here. 518d2a17637SMingming Cao */ 5195f634d06SAneesh Kumar K.V if ((retval > 0) && 5201296cc85SAneesh Kumar K.V (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)) 5215f634d06SAneesh Kumar K.V ext4_da_update_reserve_space(inode, retval, 1); 5225f634d06SAneesh Kumar K.V } 5235f634d06SAneesh Kumar K.V if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 524f2321097STheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED); 525d2a17637SMingming Cao 5260e855ac8SAneesh Kumar K.V up_write((&EXT4_I(inode)->i_data_sem)); 527e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 528e29136f8STheodore Ts'o int ret = check_block_validity(inode, map); 5296fd058f7STheodore Ts'o if (ret != 0) 5306fd058f7STheodore Ts'o return ret; 5316fd058f7STheodore Ts'o } 5320e855ac8SAneesh Kumar K.V return retval; 5330e855ac8SAneesh Kumar K.V } 5340e855ac8SAneesh Kumar K.V 535f3bd1f3fSMingming Cao /* Maximum number of blocks we map for direct IO at once. */ 536f3bd1f3fSMingming Cao #define DIO_MAX_BLOCKS 4096 537f3bd1f3fSMingming Cao 5382ed88685STheodore Ts'o static int _ext4_get_block(struct inode *inode, sector_t iblock, 5392ed88685STheodore Ts'o struct buffer_head *bh, int flags) 540ac27a0ecSDave Kleikamp { 5413e4fdaf8SDmitriy Monakhov handle_t *handle = ext4_journal_current_handle(); 5422ed88685STheodore Ts'o struct ext4_map_blocks map; 5437fb5409dSJan Kara int ret = 0, started = 0; 544f3bd1f3fSMingming Cao int dio_credits; 545ac27a0ecSDave Kleikamp 5462ed88685STheodore Ts'o map.m_lblk = iblock; 5472ed88685STheodore Ts'o map.m_len = bh->b_size >> inode->i_blkbits; 5482ed88685STheodore Ts'o 5492ed88685STheodore Ts'o if (flags && !handle) { 5507fb5409dSJan Kara /* Direct IO write... */ 5512ed88685STheodore Ts'o if (map.m_len > DIO_MAX_BLOCKS) 5522ed88685STheodore Ts'o map.m_len = DIO_MAX_BLOCKS; 5532ed88685STheodore Ts'o dio_credits = ext4_chunk_trans_blocks(inode, map.m_len); 554f3bd1f3fSMingming Cao handle = ext4_journal_start(inode, dio_credits); 5557fb5409dSJan Kara if (IS_ERR(handle)) { 556ac27a0ecSDave Kleikamp ret = PTR_ERR(handle); 5572ed88685STheodore Ts'o return ret; 5587fb5409dSJan Kara } 5597fb5409dSJan Kara started = 1; 560ac27a0ecSDave Kleikamp } 561ac27a0ecSDave Kleikamp 5622ed88685STheodore Ts'o ret = ext4_map_blocks(handle, inode, &map, flags); 563ac27a0ecSDave Kleikamp if (ret > 0) { 5642ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 5652ed88685STheodore Ts'o bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; 5662ed88685STheodore Ts'o bh->b_size = inode->i_sb->s_blocksize * map.m_len; 567ac27a0ecSDave Kleikamp ret = 0; 568ac27a0ecSDave Kleikamp } 5697fb5409dSJan Kara if (started) 5707fb5409dSJan Kara ext4_journal_stop(handle); 571ac27a0ecSDave Kleikamp return ret; 572ac27a0ecSDave Kleikamp } 573ac27a0ecSDave Kleikamp 5742ed88685STheodore Ts'o int ext4_get_block(struct inode *inode, sector_t iblock, 5752ed88685STheodore Ts'o struct buffer_head *bh, int create) 5762ed88685STheodore Ts'o { 5772ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh, 5782ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 5792ed88685STheodore Ts'o } 5802ed88685STheodore Ts'o 581ac27a0ecSDave Kleikamp /* 582ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 583ac27a0ecSDave Kleikamp */ 584617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 585725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *errp) 586ac27a0ecSDave Kleikamp { 5872ed88685STheodore Ts'o struct ext4_map_blocks map; 5882ed88685STheodore Ts'o struct buffer_head *bh; 589ac27a0ecSDave Kleikamp int fatal = 0, err; 590ac27a0ecSDave Kleikamp 591ac27a0ecSDave Kleikamp J_ASSERT(handle != NULL || create == 0); 592ac27a0ecSDave Kleikamp 5932ed88685STheodore Ts'o map.m_lblk = block; 5942ed88685STheodore Ts'o map.m_len = 1; 5952ed88685STheodore Ts'o err = ext4_map_blocks(handle, inode, &map, 5962ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 5972ed88685STheodore Ts'o 5982ed88685STheodore Ts'o if (err < 0) 599ac27a0ecSDave Kleikamp *errp = err; 6002ed88685STheodore Ts'o if (err <= 0) 6012ed88685STheodore Ts'o return NULL; 6022ed88685STheodore Ts'o *errp = 0; 6032ed88685STheodore Ts'o 6042ed88685STheodore Ts'o bh = sb_getblk(inode->i_sb, map.m_pblk); 605ac27a0ecSDave Kleikamp if (!bh) { 606ac27a0ecSDave Kleikamp *errp = -EIO; 6072ed88685STheodore Ts'o return NULL; 608ac27a0ecSDave Kleikamp } 6092ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 610ac27a0ecSDave Kleikamp J_ASSERT(create != 0); 611ac39849dSAneesh Kumar K.V J_ASSERT(handle != NULL); 612ac27a0ecSDave Kleikamp 613ac27a0ecSDave Kleikamp /* 614ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 615ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 616ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 617617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 618ac27a0ecSDave Kleikamp * problem. 619ac27a0ecSDave Kleikamp */ 620ac27a0ecSDave Kleikamp lock_buffer(bh); 621ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 622617ba13bSMingming Cao fatal = ext4_journal_get_create_access(handle, bh); 623ac27a0ecSDave Kleikamp if (!fatal && !buffer_uptodate(bh)) { 624ac27a0ecSDave Kleikamp memset(bh->b_data, 0, inode->i_sb->s_blocksize); 625ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 626ac27a0ecSDave Kleikamp } 627ac27a0ecSDave Kleikamp unlock_buffer(bh); 6280390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 6290390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 630ac27a0ecSDave Kleikamp if (!fatal) 631ac27a0ecSDave Kleikamp fatal = err; 632ac27a0ecSDave Kleikamp } else { 633ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "not a new buffer"); 634ac27a0ecSDave Kleikamp } 635ac27a0ecSDave Kleikamp if (fatal) { 636ac27a0ecSDave Kleikamp *errp = fatal; 637ac27a0ecSDave Kleikamp brelse(bh); 638ac27a0ecSDave Kleikamp bh = NULL; 639ac27a0ecSDave Kleikamp } 640ac27a0ecSDave Kleikamp return bh; 641ac27a0ecSDave Kleikamp } 642ac27a0ecSDave Kleikamp 643617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 644725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *err) 645ac27a0ecSDave Kleikamp { 646ac27a0ecSDave Kleikamp struct buffer_head *bh; 647ac27a0ecSDave Kleikamp 648617ba13bSMingming Cao bh = ext4_getblk(handle, inode, block, create, err); 649ac27a0ecSDave Kleikamp if (!bh) 650ac27a0ecSDave Kleikamp return bh; 651ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 652ac27a0ecSDave Kleikamp return bh; 653*65299a3bSChristoph Hellwig ll_rw_block(READ | REQ_META | REQ_PRIO, 1, &bh); 654ac27a0ecSDave Kleikamp wait_on_buffer(bh); 655ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 656ac27a0ecSDave Kleikamp return bh; 657ac27a0ecSDave Kleikamp put_bh(bh); 658ac27a0ecSDave Kleikamp *err = -EIO; 659ac27a0ecSDave Kleikamp return NULL; 660ac27a0ecSDave Kleikamp } 661ac27a0ecSDave Kleikamp 662ac27a0ecSDave Kleikamp static int walk_page_buffers(handle_t *handle, 663ac27a0ecSDave Kleikamp struct buffer_head *head, 664ac27a0ecSDave Kleikamp unsigned from, 665ac27a0ecSDave Kleikamp unsigned to, 666ac27a0ecSDave Kleikamp int *partial, 667ac27a0ecSDave Kleikamp int (*fn)(handle_t *handle, 668ac27a0ecSDave Kleikamp struct buffer_head *bh)) 669ac27a0ecSDave Kleikamp { 670ac27a0ecSDave Kleikamp struct buffer_head *bh; 671ac27a0ecSDave Kleikamp unsigned block_start, block_end; 672ac27a0ecSDave Kleikamp unsigned blocksize = head->b_size; 673ac27a0ecSDave Kleikamp int err, ret = 0; 674ac27a0ecSDave Kleikamp struct buffer_head *next; 675ac27a0ecSDave Kleikamp 676ac27a0ecSDave Kleikamp for (bh = head, block_start = 0; 677ac27a0ecSDave Kleikamp ret == 0 && (bh != head || !block_start); 678de9a55b8STheodore Ts'o block_start = block_end, bh = next) { 679ac27a0ecSDave Kleikamp next = bh->b_this_page; 680ac27a0ecSDave Kleikamp block_end = block_start + blocksize; 681ac27a0ecSDave Kleikamp if (block_end <= from || block_start >= to) { 682ac27a0ecSDave Kleikamp if (partial && !buffer_uptodate(bh)) 683ac27a0ecSDave Kleikamp *partial = 1; 684ac27a0ecSDave Kleikamp continue; 685ac27a0ecSDave Kleikamp } 686ac27a0ecSDave Kleikamp err = (*fn)(handle, bh); 687ac27a0ecSDave Kleikamp if (!ret) 688ac27a0ecSDave Kleikamp ret = err; 689ac27a0ecSDave Kleikamp } 690ac27a0ecSDave Kleikamp return ret; 691ac27a0ecSDave Kleikamp } 692ac27a0ecSDave Kleikamp 693ac27a0ecSDave Kleikamp /* 694ac27a0ecSDave Kleikamp * To preserve ordering, it is essential that the hole instantiation and 695ac27a0ecSDave Kleikamp * the data write be encapsulated in a single transaction. We cannot 696617ba13bSMingming Cao * close off a transaction and start a new one between the ext4_get_block() 697dab291afSMingming Cao * and the commit_write(). So doing the jbd2_journal_start at the start of 698ac27a0ecSDave Kleikamp * prepare_write() is the right place. 699ac27a0ecSDave Kleikamp * 700617ba13bSMingming Cao * Also, this function can nest inside ext4_writepage() -> 701617ba13bSMingming Cao * block_write_full_page(). In that case, we *know* that ext4_writepage() 702ac27a0ecSDave Kleikamp * has generated enough buffer credits to do the whole page. So we won't 703ac27a0ecSDave Kleikamp * block on the journal in that case, which is good, because the caller may 704ac27a0ecSDave Kleikamp * be PF_MEMALLOC. 705ac27a0ecSDave Kleikamp * 706617ba13bSMingming Cao * By accident, ext4 can be reentered when a transaction is open via 707ac27a0ecSDave Kleikamp * quota file writes. If we were to commit the transaction while thus 708ac27a0ecSDave Kleikamp * reentered, there can be a deadlock - we would be holding a quota 709ac27a0ecSDave Kleikamp * lock, and the commit would never complete if another thread had a 710ac27a0ecSDave Kleikamp * transaction open and was blocking on the quota lock - a ranking 711ac27a0ecSDave Kleikamp * violation. 712ac27a0ecSDave Kleikamp * 713dab291afSMingming Cao * So what we do is to rely on the fact that jbd2_journal_stop/journal_start 714ac27a0ecSDave Kleikamp * will _not_ run commit under these circumstances because handle->h_ref 715ac27a0ecSDave Kleikamp * is elevated. We'll still have enough credits for the tiny quotafile 716ac27a0ecSDave Kleikamp * write. 717ac27a0ecSDave Kleikamp */ 718ac27a0ecSDave Kleikamp static int do_journal_get_write_access(handle_t *handle, 719ac27a0ecSDave Kleikamp struct buffer_head *bh) 720ac27a0ecSDave Kleikamp { 72156d35a4cSJan Kara int dirty = buffer_dirty(bh); 72256d35a4cSJan Kara int ret; 72356d35a4cSJan Kara 724ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 725ac27a0ecSDave Kleikamp return 0; 72656d35a4cSJan Kara /* 727ebdec241SChristoph Hellwig * __block_write_begin() could have dirtied some buffers. Clean 72856d35a4cSJan Kara * the dirty bit as jbd2_journal_get_write_access() could complain 72956d35a4cSJan Kara * otherwise about fs integrity issues. Setting of the dirty bit 730ebdec241SChristoph Hellwig * by __block_write_begin() isn't a real problem here as we clear 73156d35a4cSJan Kara * the bit before releasing a page lock and thus writeback cannot 73256d35a4cSJan Kara * ever write the buffer. 73356d35a4cSJan Kara */ 73456d35a4cSJan Kara if (dirty) 73556d35a4cSJan Kara clear_buffer_dirty(bh); 73656d35a4cSJan Kara ret = ext4_journal_get_write_access(handle, bh); 73756d35a4cSJan Kara if (!ret && dirty) 73856d35a4cSJan Kara ret = ext4_handle_dirty_metadata(handle, NULL, bh); 73956d35a4cSJan Kara return ret; 740ac27a0ecSDave Kleikamp } 741ac27a0ecSDave Kleikamp 742744692dcSJiaying Zhang static int ext4_get_block_write(struct inode *inode, sector_t iblock, 743744692dcSJiaying Zhang struct buffer_head *bh_result, int create); 744bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 745bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned flags, 746bfc1af65SNick Piggin struct page **pagep, void **fsdata) 747ac27a0ecSDave Kleikamp { 748bfc1af65SNick Piggin struct inode *inode = mapping->host; 7491938a150SAneesh Kumar K.V int ret, needed_blocks; 750ac27a0ecSDave Kleikamp handle_t *handle; 751ac27a0ecSDave Kleikamp int retries = 0; 752bfc1af65SNick Piggin struct page *page; 753bfc1af65SNick Piggin pgoff_t index; 754bfc1af65SNick Piggin unsigned from, to; 755bfc1af65SNick Piggin 7569bffad1eSTheodore Ts'o trace_ext4_write_begin(inode, pos, len, flags); 7571938a150SAneesh Kumar K.V /* 7581938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 7591938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 7601938a150SAneesh Kumar K.V */ 7611938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 762bfc1af65SNick Piggin index = pos >> PAGE_CACHE_SHIFT; 763bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 764bfc1af65SNick Piggin to = from + len; 765ac27a0ecSDave Kleikamp 766ac27a0ecSDave Kleikamp retry: 767617ba13bSMingming Cao handle = ext4_journal_start(inode, needed_blocks); 7687479d2b9SAndrew Morton if (IS_ERR(handle)) { 7697479d2b9SAndrew Morton ret = PTR_ERR(handle); 7707479d2b9SAndrew Morton goto out; 7717479d2b9SAndrew Morton } 772ac27a0ecSDave Kleikamp 773ebd3610bSJan Kara /* We cannot recurse into the filesystem as the transaction is already 774ebd3610bSJan Kara * started */ 775ebd3610bSJan Kara flags |= AOP_FLAG_NOFS; 776ebd3610bSJan Kara 77754566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 778cf108bcaSJan Kara if (!page) { 779cf108bcaSJan Kara ext4_journal_stop(handle); 780cf108bcaSJan Kara ret = -ENOMEM; 781cf108bcaSJan Kara goto out; 782cf108bcaSJan Kara } 783cf108bcaSJan Kara *pagep = page; 784cf108bcaSJan Kara 785744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 7866e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block_write); 787744692dcSJiaying Zhang else 7886e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block); 789bfc1af65SNick Piggin 790bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 791ac27a0ecSDave Kleikamp ret = walk_page_buffers(handle, page_buffers(page), 792ac27a0ecSDave Kleikamp from, to, NULL, do_journal_get_write_access); 793b46be050SAndrey Savochkin } 794bfc1af65SNick Piggin 795bfc1af65SNick Piggin if (ret) { 796bfc1af65SNick Piggin unlock_page(page); 797bfc1af65SNick Piggin page_cache_release(page); 798ae4d5372SAneesh Kumar K.V /* 7996e1db88dSChristoph Hellwig * __block_write_begin may have instantiated a few blocks 800ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 801ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 8021938a150SAneesh Kumar K.V * 8031938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 8041938a150SAneesh Kumar K.V * truncate finishes 805ae4d5372SAneesh Kumar K.V */ 806ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 8071938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 8081938a150SAneesh Kumar K.V 8091938a150SAneesh Kumar K.V ext4_journal_stop(handle); 8101938a150SAneesh Kumar K.V if (pos + len > inode->i_size) { 811b9a4207dSJan Kara ext4_truncate_failed_write(inode); 8121938a150SAneesh Kumar K.V /* 813ffacfa7aSJan Kara * If truncate failed early the inode might 8141938a150SAneesh Kumar K.V * still be on the orphan list; we need to 8151938a150SAneesh Kumar K.V * make sure the inode is removed from the 8161938a150SAneesh Kumar K.V * orphan list in that case. 8171938a150SAneesh Kumar K.V */ 8181938a150SAneesh Kumar K.V if (inode->i_nlink) 8191938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 8201938a150SAneesh Kumar K.V } 821bfc1af65SNick Piggin } 822bfc1af65SNick Piggin 823617ba13bSMingming Cao if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 824ac27a0ecSDave Kleikamp goto retry; 8257479d2b9SAndrew Morton out: 826ac27a0ecSDave Kleikamp return ret; 827ac27a0ecSDave Kleikamp } 828ac27a0ecSDave Kleikamp 829bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 830bfc1af65SNick Piggin static int write_end_fn(handle_t *handle, struct buffer_head *bh) 831ac27a0ecSDave Kleikamp { 832ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 833ac27a0ecSDave Kleikamp return 0; 834ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 8350390131bSFrank Mayhar return ext4_handle_dirty_metadata(handle, NULL, bh); 836ac27a0ecSDave Kleikamp } 837ac27a0ecSDave Kleikamp 838f8514083SAneesh Kumar K.V static int ext4_generic_write_end(struct file *file, 839f8514083SAneesh Kumar K.V struct address_space *mapping, 840f8514083SAneesh Kumar K.V loff_t pos, unsigned len, unsigned copied, 841f8514083SAneesh Kumar K.V struct page *page, void *fsdata) 842f8514083SAneesh Kumar K.V { 843f8514083SAneesh Kumar K.V int i_size_changed = 0; 844f8514083SAneesh Kumar K.V struct inode *inode = mapping->host; 845f8514083SAneesh Kumar K.V handle_t *handle = ext4_journal_current_handle(); 846f8514083SAneesh Kumar K.V 847f8514083SAneesh Kumar K.V copied = block_write_end(file, mapping, pos, len, copied, page, fsdata); 848f8514083SAneesh Kumar K.V 849f8514083SAneesh Kumar K.V /* 850f8514083SAneesh Kumar K.V * No need to use i_size_read() here, the i_size 851f8514083SAneesh Kumar K.V * cannot change under us because we hold i_mutex. 852f8514083SAneesh Kumar K.V * 853f8514083SAneesh Kumar K.V * But it's important to update i_size while still holding page lock: 854f8514083SAneesh Kumar K.V * page writeout could otherwise come in and zero beyond i_size. 855f8514083SAneesh Kumar K.V */ 856f8514083SAneesh Kumar K.V if (pos + copied > inode->i_size) { 857f8514083SAneesh Kumar K.V i_size_write(inode, pos + copied); 858f8514083SAneesh Kumar K.V i_size_changed = 1; 859f8514083SAneesh Kumar K.V } 860f8514083SAneesh Kumar K.V 861f8514083SAneesh Kumar K.V if (pos + copied > EXT4_I(inode)->i_disksize) { 862f8514083SAneesh Kumar K.V /* We need to mark inode dirty even if 863f8514083SAneesh Kumar K.V * new_i_size is less that inode->i_size 864f8514083SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 865f8514083SAneesh Kumar K.V */ 866f8514083SAneesh Kumar K.V ext4_update_i_disksize(inode, (pos + copied)); 867f8514083SAneesh Kumar K.V i_size_changed = 1; 868f8514083SAneesh Kumar K.V } 869f8514083SAneesh Kumar K.V unlock_page(page); 870f8514083SAneesh Kumar K.V page_cache_release(page); 871f8514083SAneesh Kumar K.V 872f8514083SAneesh Kumar K.V /* 873f8514083SAneesh Kumar K.V * Don't mark the inode dirty under page lock. First, it unnecessarily 874f8514083SAneesh Kumar K.V * makes the holding time of page lock longer. Second, it forces lock 875f8514083SAneesh Kumar K.V * ordering of page lock and transaction start for journaling 876f8514083SAneesh Kumar K.V * filesystems. 877f8514083SAneesh Kumar K.V */ 878f8514083SAneesh Kumar K.V if (i_size_changed) 879f8514083SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 880f8514083SAneesh Kumar K.V 881f8514083SAneesh Kumar K.V return copied; 882f8514083SAneesh Kumar K.V } 883f8514083SAneesh Kumar K.V 884ac27a0ecSDave Kleikamp /* 885ac27a0ecSDave Kleikamp * We need to pick up the new inode size which generic_commit_write gave us 886ac27a0ecSDave Kleikamp * `file' can be NULL - eg, when called from page_symlink(). 887ac27a0ecSDave Kleikamp * 888617ba13bSMingming Cao * ext4 never places buffers on inode->i_mapping->private_list. metadata 889ac27a0ecSDave Kleikamp * buffers are managed internally. 890ac27a0ecSDave Kleikamp */ 891bfc1af65SNick Piggin static int ext4_ordered_write_end(struct file *file, 892bfc1af65SNick Piggin struct address_space *mapping, 893bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 894bfc1af65SNick Piggin struct page *page, void *fsdata) 895ac27a0ecSDave Kleikamp { 896617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 897cf108bcaSJan Kara struct inode *inode = mapping->host; 898ac27a0ecSDave Kleikamp int ret = 0, ret2; 899ac27a0ecSDave Kleikamp 9009bffad1eSTheodore Ts'o trace_ext4_ordered_write_end(inode, pos, len, copied); 901678aaf48SJan Kara ret = ext4_jbd2_file_inode(handle, inode); 902ac27a0ecSDave Kleikamp 903ac27a0ecSDave Kleikamp if (ret == 0) { 904f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 905bfc1af65SNick Piggin page, fsdata); 906f8a87d89SRoel Kluin copied = ret2; 907ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 908f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 909f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 910f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 911f8514083SAneesh Kumar K.V */ 912f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 913f8a87d89SRoel Kluin if (ret2 < 0) 914f8a87d89SRoel Kluin ret = ret2; 915ac27a0ecSDave Kleikamp } 916617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 917ac27a0ecSDave Kleikamp if (!ret) 918ac27a0ecSDave Kleikamp ret = ret2; 919bfc1af65SNick Piggin 920f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 921b9a4207dSJan Kara ext4_truncate_failed_write(inode); 922f8514083SAneesh Kumar K.V /* 923ffacfa7aSJan Kara * If truncate failed early the inode might still be 924f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 925f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 926f8514083SAneesh Kumar K.V */ 927f8514083SAneesh Kumar K.V if (inode->i_nlink) 928f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 929f8514083SAneesh Kumar K.V } 930f8514083SAneesh Kumar K.V 931f8514083SAneesh Kumar K.V 932bfc1af65SNick Piggin return ret ? ret : copied; 933ac27a0ecSDave Kleikamp } 934ac27a0ecSDave Kleikamp 935bfc1af65SNick Piggin static int ext4_writeback_write_end(struct file *file, 936bfc1af65SNick Piggin struct address_space *mapping, 937bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 938bfc1af65SNick Piggin struct page *page, void *fsdata) 939ac27a0ecSDave Kleikamp { 940617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 941cf108bcaSJan Kara struct inode *inode = mapping->host; 942ac27a0ecSDave Kleikamp int ret = 0, ret2; 943ac27a0ecSDave Kleikamp 9449bffad1eSTheodore Ts'o trace_ext4_writeback_write_end(inode, pos, len, copied); 945f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 946bfc1af65SNick Piggin page, fsdata); 947f8a87d89SRoel Kluin copied = ret2; 948ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 949f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 950f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 951f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 952f8514083SAneesh Kumar K.V */ 953f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 954f8514083SAneesh Kumar K.V 955f8a87d89SRoel Kluin if (ret2 < 0) 956f8a87d89SRoel Kluin ret = ret2; 957ac27a0ecSDave Kleikamp 958617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 959ac27a0ecSDave Kleikamp if (!ret) 960ac27a0ecSDave Kleikamp ret = ret2; 961bfc1af65SNick Piggin 962f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 963b9a4207dSJan Kara ext4_truncate_failed_write(inode); 964f8514083SAneesh Kumar K.V /* 965ffacfa7aSJan Kara * If truncate failed early the inode might still be 966f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 967f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 968f8514083SAneesh Kumar K.V */ 969f8514083SAneesh Kumar K.V if (inode->i_nlink) 970f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 971f8514083SAneesh Kumar K.V } 972f8514083SAneesh Kumar K.V 973bfc1af65SNick Piggin return ret ? ret : copied; 974ac27a0ecSDave Kleikamp } 975ac27a0ecSDave Kleikamp 976bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 977bfc1af65SNick Piggin struct address_space *mapping, 978bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 979bfc1af65SNick Piggin struct page *page, void *fsdata) 980ac27a0ecSDave Kleikamp { 981617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 982bfc1af65SNick Piggin struct inode *inode = mapping->host; 983ac27a0ecSDave Kleikamp int ret = 0, ret2; 984ac27a0ecSDave Kleikamp int partial = 0; 985bfc1af65SNick Piggin unsigned from, to; 986cf17fea6SAneesh Kumar K.V loff_t new_i_size; 987ac27a0ecSDave Kleikamp 9889bffad1eSTheodore Ts'o trace_ext4_journalled_write_end(inode, pos, len, copied); 989bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 990bfc1af65SNick Piggin to = from + len; 991bfc1af65SNick Piggin 992441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 993441c8508SCurt Wohlgemuth 994bfc1af65SNick Piggin if (copied < len) { 995bfc1af65SNick Piggin if (!PageUptodate(page)) 996bfc1af65SNick Piggin copied = 0; 997bfc1af65SNick Piggin page_zero_new_buffers(page, from+copied, to); 998bfc1af65SNick Piggin } 999ac27a0ecSDave Kleikamp 1000ac27a0ecSDave Kleikamp ret = walk_page_buffers(handle, page_buffers(page), from, 1001bfc1af65SNick Piggin to, &partial, write_end_fn); 1002ac27a0ecSDave Kleikamp if (!partial) 1003ac27a0ecSDave Kleikamp SetPageUptodate(page); 1004cf17fea6SAneesh Kumar K.V new_i_size = pos + copied; 1005cf17fea6SAneesh Kumar K.V if (new_i_size > inode->i_size) 1006bfc1af65SNick Piggin i_size_write(inode, pos+copied); 100719f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 10082d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 1009cf17fea6SAneesh Kumar K.V if (new_i_size > EXT4_I(inode)->i_disksize) { 1010cf17fea6SAneesh Kumar K.V ext4_update_i_disksize(inode, new_i_size); 1011617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1012ac27a0ecSDave Kleikamp if (!ret) 1013ac27a0ecSDave Kleikamp ret = ret2; 1014ac27a0ecSDave Kleikamp } 1015bfc1af65SNick Piggin 1016cf108bcaSJan Kara unlock_page(page); 1017f8514083SAneesh Kumar K.V page_cache_release(page); 1018ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1019f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1020f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1021f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1022f8514083SAneesh Kumar K.V */ 1023f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1024f8514083SAneesh Kumar K.V 1025617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1026ac27a0ecSDave Kleikamp if (!ret) 1027ac27a0ecSDave Kleikamp ret = ret2; 1028f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1029b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1030f8514083SAneesh Kumar K.V /* 1031ffacfa7aSJan Kara * If truncate failed early the inode might still be 1032f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1033f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1034f8514083SAneesh Kumar K.V */ 1035f8514083SAneesh Kumar K.V if (inode->i_nlink) 1036f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1037f8514083SAneesh Kumar K.V } 1038bfc1af65SNick Piggin 1039bfc1af65SNick Piggin return ret ? ret : copied; 1040ac27a0ecSDave Kleikamp } 1041d2a17637SMingming Cao 10429d0be502STheodore Ts'o /* 10439d0be502STheodore Ts'o * Reserve a single block located at lblock 10449d0be502STheodore Ts'o */ 104501f49d0bSTheodore Ts'o static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock) 1046d2a17637SMingming Cao { 1047030ba6bcSAneesh Kumar K.V int retries = 0; 1048d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 10490637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 105072b8ab9dSEric Sandeen unsigned long md_needed; 10515dd4056dSChristoph Hellwig int ret; 1052d2a17637SMingming Cao 1053d2a17637SMingming Cao /* 1054d2a17637SMingming Cao * recalculate the amount of metadata blocks to reserve 1055d2a17637SMingming Cao * in order to allocate nrblocks 1056d2a17637SMingming Cao * worse case is one extent per block 1057d2a17637SMingming Cao */ 1058030ba6bcSAneesh Kumar K.V repeat: 10590637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 10609d0be502STheodore Ts'o md_needed = ext4_calc_metadata_amount(inode, lblock); 1061f8ec9d68STheodore Ts'o trace_ext4_da_reserve_space(inode, md_needed); 10620637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 1063d2a17637SMingming Cao 106460e58e0fSMingming Cao /* 106572b8ab9dSEric Sandeen * We will charge metadata quota at writeout time; this saves 106672b8ab9dSEric Sandeen * us from metadata over-estimation, though we may go over by 106772b8ab9dSEric Sandeen * a small amount in the end. Here we just reserve for data. 106860e58e0fSMingming Cao */ 106972b8ab9dSEric Sandeen ret = dquot_reserve_block(inode, 1); 10705dd4056dSChristoph Hellwig if (ret) 10715dd4056dSChristoph Hellwig return ret; 107272b8ab9dSEric Sandeen /* 107372b8ab9dSEric Sandeen * We do still charge estimated metadata to the sb though; 107472b8ab9dSEric Sandeen * we cannot afford to run out of free blocks. 107572b8ab9dSEric Sandeen */ 107655f020dbSAllison Henderson if (ext4_claim_free_blocks(sbi, md_needed + 1, 0)) { 107772b8ab9dSEric Sandeen dquot_release_reservation_block(inode, 1); 1078030ba6bcSAneesh Kumar K.V if (ext4_should_retry_alloc(inode->i_sb, &retries)) { 1079030ba6bcSAneesh Kumar K.V yield(); 1080030ba6bcSAneesh Kumar K.V goto repeat; 1081030ba6bcSAneesh Kumar K.V } 1082d2a17637SMingming Cao return -ENOSPC; 1083d2a17637SMingming Cao } 10840637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 10859d0be502STheodore Ts'o ei->i_reserved_data_blocks++; 10860637c6f4STheodore Ts'o ei->i_reserved_meta_blocks += md_needed; 10870637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 108839bc680aSDmitry Monakhov 1089d2a17637SMingming Cao return 0; /* success */ 1090d2a17637SMingming Cao } 1091d2a17637SMingming Cao 109212219aeaSAneesh Kumar K.V static void ext4_da_release_space(struct inode *inode, int to_free) 1093d2a17637SMingming Cao { 1094d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 10950637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 1096d2a17637SMingming Cao 1097cd213226SMingming Cao if (!to_free) 1098cd213226SMingming Cao return; /* Nothing to release, exit */ 1099cd213226SMingming Cao 1100d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1101cd213226SMingming Cao 11025a58ec87SLi Zefan trace_ext4_da_release_space(inode, to_free); 11030637c6f4STheodore Ts'o if (unlikely(to_free > ei->i_reserved_data_blocks)) { 1104cd213226SMingming Cao /* 11050637c6f4STheodore Ts'o * if there aren't enough reserved blocks, then the 11060637c6f4STheodore Ts'o * counter is messed up somewhere. Since this 11070637c6f4STheodore Ts'o * function is called from invalidate page, it's 11080637c6f4STheodore Ts'o * harmless to return without any action. 1109cd213226SMingming Cao */ 11100637c6f4STheodore Ts'o ext4_msg(inode->i_sb, KERN_NOTICE, "ext4_da_release_space: " 11110637c6f4STheodore Ts'o "ino %lu, to_free %d with only %d reserved " 11120637c6f4STheodore Ts'o "data blocks\n", inode->i_ino, to_free, 11130637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 11140637c6f4STheodore Ts'o WARN_ON(1); 11150637c6f4STheodore Ts'o to_free = ei->i_reserved_data_blocks; 11160637c6f4STheodore Ts'o } 11170637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= to_free; 11180637c6f4STheodore Ts'o 11190637c6f4STheodore Ts'o if (ei->i_reserved_data_blocks == 0) { 11200637c6f4STheodore Ts'o /* 11210637c6f4STheodore Ts'o * We can release all of the reserved metadata blocks 11220637c6f4STheodore Ts'o * only when we have written all of the delayed 11230637c6f4STheodore Ts'o * allocation blocks. 11240637c6f4STheodore Ts'o */ 112572b8ab9dSEric Sandeen percpu_counter_sub(&sbi->s_dirtyblocks_counter, 112672b8ab9dSEric Sandeen ei->i_reserved_meta_blocks); 1127ee5f4d9cSTheodore Ts'o ei->i_reserved_meta_blocks = 0; 11289d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 1129cd213226SMingming Cao } 1130cd213226SMingming Cao 113172b8ab9dSEric Sandeen /* update fs dirty data blocks counter */ 11320637c6f4STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyblocks_counter, to_free); 1133d2a17637SMingming Cao 1134d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 113560e58e0fSMingming Cao 11365dd4056dSChristoph Hellwig dquot_release_reservation_block(inode, to_free); 1137d2a17637SMingming Cao } 1138d2a17637SMingming Cao 1139d2a17637SMingming Cao static void ext4_da_page_release_reservation(struct page *page, 1140d2a17637SMingming Cao unsigned long offset) 1141d2a17637SMingming Cao { 1142d2a17637SMingming Cao int to_release = 0; 1143d2a17637SMingming Cao struct buffer_head *head, *bh; 1144d2a17637SMingming Cao unsigned int curr_off = 0; 1145d2a17637SMingming Cao 1146d2a17637SMingming Cao head = page_buffers(page); 1147d2a17637SMingming Cao bh = head; 1148d2a17637SMingming Cao do { 1149d2a17637SMingming Cao unsigned int next_off = curr_off + bh->b_size; 1150d2a17637SMingming Cao 1151d2a17637SMingming Cao if ((offset <= curr_off) && (buffer_delay(bh))) { 1152d2a17637SMingming Cao to_release++; 1153d2a17637SMingming Cao clear_buffer_delay(bh); 1154d2a17637SMingming Cao } 1155d2a17637SMingming Cao curr_off = next_off; 1156d2a17637SMingming Cao } while ((bh = bh->b_this_page) != head); 115712219aeaSAneesh Kumar K.V ext4_da_release_space(page->mapping->host, to_release); 1158d2a17637SMingming Cao } 1159ac27a0ecSDave Kleikamp 1160ac27a0ecSDave Kleikamp /* 116164769240SAlex Tomas * Delayed allocation stuff 116264769240SAlex Tomas */ 116364769240SAlex Tomas 116464769240SAlex Tomas /* 116564769240SAlex Tomas * mpage_da_submit_io - walks through extent of pages and try to write 1166a1d6cc56SAneesh Kumar K.V * them with writepage() call back 116764769240SAlex Tomas * 116864769240SAlex Tomas * @mpd->inode: inode 116964769240SAlex Tomas * @mpd->first_page: first page of the extent 117064769240SAlex Tomas * @mpd->next_page: page after the last page of the extent 117164769240SAlex Tomas * 117264769240SAlex Tomas * By the time mpage_da_submit_io() is called we expect all blocks 117364769240SAlex Tomas * to be allocated. this may be wrong if allocation failed. 117464769240SAlex Tomas * 117564769240SAlex Tomas * As pages are already locked by write_cache_pages(), we can't use it 117664769240SAlex Tomas */ 11771de3e3dfSTheodore Ts'o static int mpage_da_submit_io(struct mpage_da_data *mpd, 11781de3e3dfSTheodore Ts'o struct ext4_map_blocks *map) 117964769240SAlex Tomas { 1180791b7f08SAneesh Kumar K.V struct pagevec pvec; 1181791b7f08SAneesh Kumar K.V unsigned long index, end; 1182791b7f08SAneesh Kumar K.V int ret = 0, err, nr_pages, i; 1183791b7f08SAneesh Kumar K.V struct inode *inode = mpd->inode; 1184791b7f08SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 1185cb20d518STheodore Ts'o loff_t size = i_size_read(inode); 11863ecdb3a1STheodore Ts'o unsigned int len, block_start; 11873ecdb3a1STheodore Ts'o struct buffer_head *bh, *page_bufs = NULL; 1188cb20d518STheodore Ts'o int journal_data = ext4_should_journal_data(inode); 11891de3e3dfSTheodore Ts'o sector_t pblock = 0, cur_logical = 0; 1190bd2d0210STheodore Ts'o struct ext4_io_submit io_submit; 119164769240SAlex Tomas 119264769240SAlex Tomas BUG_ON(mpd->next_page <= mpd->first_page); 1193bd2d0210STheodore Ts'o memset(&io_submit, 0, sizeof(io_submit)); 1194791b7f08SAneesh Kumar K.V /* 1195791b7f08SAneesh Kumar K.V * We need to start from the first_page to the next_page - 1 1196791b7f08SAneesh Kumar K.V * to make sure we also write the mapped dirty buffer_heads. 11978dc207c0STheodore Ts'o * If we look at mpd->b_blocknr we would only be looking 1198791b7f08SAneesh Kumar K.V * at the currently mapped buffer_heads. 1199791b7f08SAneesh Kumar K.V */ 120064769240SAlex Tomas index = mpd->first_page; 120164769240SAlex Tomas end = mpd->next_page - 1; 120264769240SAlex Tomas 1203791b7f08SAneesh Kumar K.V pagevec_init(&pvec, 0); 120464769240SAlex Tomas while (index <= end) { 1205791b7f08SAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 120664769240SAlex Tomas if (nr_pages == 0) 120764769240SAlex Tomas break; 120864769240SAlex Tomas for (i = 0; i < nr_pages; i++) { 120997498956STheodore Ts'o int commit_write = 0, skip_page = 0; 121064769240SAlex Tomas struct page *page = pvec.pages[i]; 121164769240SAlex Tomas 1212791b7f08SAneesh Kumar K.V index = page->index; 1213791b7f08SAneesh Kumar K.V if (index > end) 1214791b7f08SAneesh Kumar K.V break; 1215cb20d518STheodore Ts'o 1216cb20d518STheodore Ts'o if (index == size >> PAGE_CACHE_SHIFT) 1217cb20d518STheodore Ts'o len = size & ~PAGE_CACHE_MASK; 1218cb20d518STheodore Ts'o else 1219cb20d518STheodore Ts'o len = PAGE_CACHE_SIZE; 12201de3e3dfSTheodore Ts'o if (map) { 12211de3e3dfSTheodore Ts'o cur_logical = index << (PAGE_CACHE_SHIFT - 12221de3e3dfSTheodore Ts'o inode->i_blkbits); 12231de3e3dfSTheodore Ts'o pblock = map->m_pblk + (cur_logical - 12241de3e3dfSTheodore Ts'o map->m_lblk); 12251de3e3dfSTheodore Ts'o } 1226791b7f08SAneesh Kumar K.V index++; 1227791b7f08SAneesh Kumar K.V 1228791b7f08SAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1229791b7f08SAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1230791b7f08SAneesh Kumar K.V 123122208dedSAneesh Kumar K.V /* 1232cb20d518STheodore Ts'o * If the page does not have buffers (for 1233cb20d518STheodore Ts'o * whatever reason), try to create them using 1234a107e5a3STheodore Ts'o * __block_write_begin. If this fails, 123597498956STheodore Ts'o * skip the page and move on. 123622208dedSAneesh Kumar K.V */ 1237cb20d518STheodore Ts'o if (!page_has_buffers(page)) { 1238a107e5a3STheodore Ts'o if (__block_write_begin(page, 0, len, 1239cb20d518STheodore Ts'o noalloc_get_block_write)) { 124097498956STheodore Ts'o skip_page: 1241cb20d518STheodore Ts'o unlock_page(page); 1242cb20d518STheodore Ts'o continue; 1243cb20d518STheodore Ts'o } 1244cb20d518STheodore Ts'o commit_write = 1; 1245cb20d518STheodore Ts'o } 12463ecdb3a1STheodore Ts'o 12473ecdb3a1STheodore Ts'o bh = page_bufs = page_buffers(page); 12483ecdb3a1STheodore Ts'o block_start = 0; 12493ecdb3a1STheodore Ts'o do { 12501de3e3dfSTheodore Ts'o if (!bh) 125197498956STheodore Ts'o goto skip_page; 12521de3e3dfSTheodore Ts'o if (map && (cur_logical >= map->m_lblk) && 12531de3e3dfSTheodore Ts'o (cur_logical <= (map->m_lblk + 12541de3e3dfSTheodore Ts'o (map->m_len - 1)))) { 12551de3e3dfSTheodore Ts'o if (buffer_delay(bh)) { 12561de3e3dfSTheodore Ts'o clear_buffer_delay(bh); 12571de3e3dfSTheodore Ts'o bh->b_blocknr = pblock; 12581de3e3dfSTheodore Ts'o } 12591de3e3dfSTheodore Ts'o if (buffer_unwritten(bh) || 12601de3e3dfSTheodore Ts'o buffer_mapped(bh)) 12611de3e3dfSTheodore Ts'o BUG_ON(bh->b_blocknr != pblock); 12621de3e3dfSTheodore Ts'o if (map->m_flags & EXT4_MAP_UNINIT) 12631de3e3dfSTheodore Ts'o set_buffer_uninit(bh); 12641de3e3dfSTheodore Ts'o clear_buffer_unwritten(bh); 12651de3e3dfSTheodore Ts'o } 12661de3e3dfSTheodore Ts'o 126797498956STheodore Ts'o /* skip page if block allocation undone */ 12681de3e3dfSTheodore Ts'o if (buffer_delay(bh) || buffer_unwritten(bh)) 126997498956STheodore Ts'o skip_page = 1; 12703ecdb3a1STheodore Ts'o bh = bh->b_this_page; 12713ecdb3a1STheodore Ts'o block_start += bh->b_size; 12721de3e3dfSTheodore Ts'o cur_logical++; 12731de3e3dfSTheodore Ts'o pblock++; 12741de3e3dfSTheodore Ts'o } while (bh != page_bufs); 12751de3e3dfSTheodore Ts'o 127697498956STheodore Ts'o if (skip_page) 127797498956STheodore Ts'o goto skip_page; 1278cb20d518STheodore Ts'o 1279cb20d518STheodore Ts'o if (commit_write) 1280cb20d518STheodore Ts'o /* mark the buffer_heads as dirty & uptodate */ 1281cb20d518STheodore Ts'o block_commit_write(page, 0, len); 1282cb20d518STheodore Ts'o 128397498956STheodore Ts'o clear_page_dirty_for_io(page); 1284bd2d0210STheodore Ts'o /* 1285bd2d0210STheodore Ts'o * Delalloc doesn't support data journalling, 1286bd2d0210STheodore Ts'o * but eventually maybe we'll lift this 1287bd2d0210STheodore Ts'o * restriction. 1288bd2d0210STheodore Ts'o */ 1289bd2d0210STheodore Ts'o if (unlikely(journal_data && PageChecked(page))) 1290cb20d518STheodore Ts'o err = __ext4_journalled_writepage(page, len); 12911449032bSTheodore Ts'o else if (test_opt(inode->i_sb, MBLK_IO_SUBMIT)) 1292bd2d0210STheodore Ts'o err = ext4_bio_write_page(&io_submit, page, 1293bd2d0210STheodore Ts'o len, mpd->wbc); 12949dd75f1fSTheodore Ts'o else if (buffer_uninit(page_bufs)) { 12959dd75f1fSTheodore Ts'o ext4_set_bh_endio(page_bufs, inode); 12969dd75f1fSTheodore Ts'o err = block_write_full_page_endio(page, 12979dd75f1fSTheodore Ts'o noalloc_get_block_write, 12989dd75f1fSTheodore Ts'o mpd->wbc, ext4_end_io_buffer_write); 12999dd75f1fSTheodore Ts'o } else 13001449032bSTheodore Ts'o err = block_write_full_page(page, 13011449032bSTheodore Ts'o noalloc_get_block_write, mpd->wbc); 1302cb20d518STheodore Ts'o 1303cb20d518STheodore Ts'o if (!err) 1304a1d6cc56SAneesh Kumar K.V mpd->pages_written++; 130564769240SAlex Tomas /* 130664769240SAlex Tomas * In error case, we have to continue because 130764769240SAlex Tomas * remaining pages are still locked 130864769240SAlex Tomas */ 130964769240SAlex Tomas if (ret == 0) 131064769240SAlex Tomas ret = err; 131164769240SAlex Tomas } 131264769240SAlex Tomas pagevec_release(&pvec); 131364769240SAlex Tomas } 1314bd2d0210STheodore Ts'o ext4_io_submit(&io_submit); 131564769240SAlex Tomas return ret; 131664769240SAlex Tomas } 131764769240SAlex Tomas 1318c7f5938aSCurt Wohlgemuth static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd) 1319c4a0c46eSAneesh Kumar K.V { 1320c4a0c46eSAneesh Kumar K.V int nr_pages, i; 1321c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 1322c4a0c46eSAneesh Kumar K.V struct pagevec pvec; 1323c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 1324c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 1325c4a0c46eSAneesh Kumar K.V 1326c7f5938aSCurt Wohlgemuth index = mpd->first_page; 1327c7f5938aSCurt Wohlgemuth end = mpd->next_page - 1; 1328c4a0c46eSAneesh Kumar K.V while (index <= end) { 1329c4a0c46eSAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 1330c4a0c46eSAneesh Kumar K.V if (nr_pages == 0) 1331c4a0c46eSAneesh Kumar K.V break; 1332c4a0c46eSAneesh Kumar K.V for (i = 0; i < nr_pages; i++) { 1333c4a0c46eSAneesh Kumar K.V struct page *page = pvec.pages[i]; 13349b1d0998SJan Kara if (page->index > end) 1335c4a0c46eSAneesh Kumar K.V break; 1336c4a0c46eSAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1337c4a0c46eSAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1338c4a0c46eSAneesh Kumar K.V block_invalidatepage(page, 0); 1339c4a0c46eSAneesh Kumar K.V ClearPageUptodate(page); 1340c4a0c46eSAneesh Kumar K.V unlock_page(page); 1341c4a0c46eSAneesh Kumar K.V } 13429b1d0998SJan Kara index = pvec.pages[nr_pages - 1]->index + 1; 13439b1d0998SJan Kara pagevec_release(&pvec); 1344c4a0c46eSAneesh Kumar K.V } 1345c4a0c46eSAneesh Kumar K.V return; 1346c4a0c46eSAneesh Kumar K.V } 1347c4a0c46eSAneesh Kumar K.V 1348df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 1349df22291fSAneesh Kumar K.V { 1350df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 13511693918eSTheodore Ts'o printk(KERN_CRIT "Total free blocks count %lld\n", 1352df22291fSAneesh Kumar K.V ext4_count_free_blocks(inode->i_sb)); 13531693918eSTheodore Ts'o printk(KERN_CRIT "Free/Dirty block details\n"); 13541693918eSTheodore Ts'o printk(KERN_CRIT "free_blocks=%lld\n", 13558f72fbdfSAlexander Beregalov (long long) percpu_counter_sum(&sbi->s_freeblocks_counter)); 13561693918eSTheodore Ts'o printk(KERN_CRIT "dirty_blocks=%lld\n", 13578f72fbdfSAlexander Beregalov (long long) percpu_counter_sum(&sbi->s_dirtyblocks_counter)); 13581693918eSTheodore Ts'o printk(KERN_CRIT "Block reservation details\n"); 13591693918eSTheodore Ts'o printk(KERN_CRIT "i_reserved_data_blocks=%u\n", 1360df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_data_blocks); 13611693918eSTheodore Ts'o printk(KERN_CRIT "i_reserved_meta_blocks=%u\n", 1362df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_meta_blocks); 1363df22291fSAneesh Kumar K.V return; 1364df22291fSAneesh Kumar K.V } 1365df22291fSAneesh Kumar K.V 1366b920c755STheodore Ts'o /* 13675a87b7a5STheodore Ts'o * mpage_da_map_and_submit - go through given space, map them 13685a87b7a5STheodore Ts'o * if necessary, and then submit them for I/O 136964769240SAlex Tomas * 13708dc207c0STheodore Ts'o * @mpd - bh describing space 137164769240SAlex Tomas * 137264769240SAlex Tomas * The function skips space we know is already mapped to disk blocks. 137364769240SAlex Tomas * 137464769240SAlex Tomas */ 13755a87b7a5STheodore Ts'o static void mpage_da_map_and_submit(struct mpage_da_data *mpd) 137664769240SAlex Tomas { 13772ac3b6e0STheodore Ts'o int err, blks, get_blocks_flags; 13781de3e3dfSTheodore Ts'o struct ext4_map_blocks map, *mapp = NULL; 13792fa3cdfbSTheodore Ts'o sector_t next = mpd->b_blocknr; 13802fa3cdfbSTheodore Ts'o unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits; 13812fa3cdfbSTheodore Ts'o loff_t disksize = EXT4_I(mpd->inode)->i_disksize; 13822fa3cdfbSTheodore Ts'o handle_t *handle = NULL; 138364769240SAlex Tomas 138464769240SAlex Tomas /* 13855a87b7a5STheodore Ts'o * If the blocks are mapped already, or we couldn't accumulate 13865a87b7a5STheodore Ts'o * any blocks, then proceed immediately to the submission stage. 138764769240SAlex Tomas */ 13885a87b7a5STheodore Ts'o if ((mpd->b_size == 0) || 13895a87b7a5STheodore Ts'o ((mpd->b_state & (1 << BH_Mapped)) && 139029fa89d0SAneesh Kumar K.V !(mpd->b_state & (1 << BH_Delay)) && 13915a87b7a5STheodore Ts'o !(mpd->b_state & (1 << BH_Unwritten)))) 13925a87b7a5STheodore Ts'o goto submit_io; 13932fa3cdfbSTheodore Ts'o 13942fa3cdfbSTheodore Ts'o handle = ext4_journal_current_handle(); 13952fa3cdfbSTheodore Ts'o BUG_ON(!handle); 13962fa3cdfbSTheodore Ts'o 139779ffab34SAneesh Kumar K.V /* 139879e83036SEric Sandeen * Call ext4_map_blocks() to allocate any delayed allocation 13992ac3b6e0STheodore Ts'o * blocks, or to convert an uninitialized extent to be 14002ac3b6e0STheodore Ts'o * initialized (in the case where we have written into 14012ac3b6e0STheodore Ts'o * one or more preallocated blocks). 14022ac3b6e0STheodore Ts'o * 14032ac3b6e0STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to 14042ac3b6e0STheodore Ts'o * indicate that we are on the delayed allocation path. This 14052ac3b6e0STheodore Ts'o * affects functions in many different parts of the allocation 14062ac3b6e0STheodore Ts'o * call path. This flag exists primarily because we don't 140779e83036SEric Sandeen * want to change *many* call functions, so ext4_map_blocks() 1408f2321097STheodore Ts'o * will set the EXT4_STATE_DELALLOC_RESERVED flag once the 14092ac3b6e0STheodore Ts'o * inode's allocation semaphore is taken. 14102ac3b6e0STheodore Ts'o * 14112ac3b6e0STheodore Ts'o * If the blocks in questions were delalloc blocks, set 14122ac3b6e0STheodore Ts'o * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting 14132ac3b6e0STheodore Ts'o * variables are updated after the blocks have been allocated. 141479ffab34SAneesh Kumar K.V */ 14152ed88685STheodore Ts'o map.m_lblk = next; 14162ed88685STheodore Ts'o map.m_len = max_blocks; 14171296cc85SAneesh Kumar K.V get_blocks_flags = EXT4_GET_BLOCKS_CREATE; 1418744692dcSJiaying Zhang if (ext4_should_dioread_nolock(mpd->inode)) 1419744692dcSJiaying Zhang get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; 14202ac3b6e0STheodore Ts'o if (mpd->b_state & (1 << BH_Delay)) 14211296cc85SAneesh Kumar K.V get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; 14221296cc85SAneesh Kumar K.V 14232ed88685STheodore Ts'o blks = ext4_map_blocks(handle, mpd->inode, &map, get_blocks_flags); 14242fa3cdfbSTheodore Ts'o if (blks < 0) { 1425e3570639SEric Sandeen struct super_block *sb = mpd->inode->i_sb; 1426e3570639SEric Sandeen 14272fa3cdfbSTheodore Ts'o err = blks; 1428ed5bde0bSTheodore Ts'o /* 14295a87b7a5STheodore Ts'o * If get block returns EAGAIN or ENOSPC and there 143097498956STheodore Ts'o * appears to be free blocks we will just let 143197498956STheodore Ts'o * mpage_da_submit_io() unlock all of the pages. 1432c4a0c46eSAneesh Kumar K.V */ 1433c4a0c46eSAneesh Kumar K.V if (err == -EAGAIN) 14345a87b7a5STheodore Ts'o goto submit_io; 1435df22291fSAneesh Kumar K.V 1436df22291fSAneesh Kumar K.V if (err == -ENOSPC && 1437e3570639SEric Sandeen ext4_count_free_blocks(sb)) { 1438df22291fSAneesh Kumar K.V mpd->retval = err; 14395a87b7a5STheodore Ts'o goto submit_io; 1440df22291fSAneesh Kumar K.V } 1441df22291fSAneesh Kumar K.V 1442c4a0c46eSAneesh Kumar K.V /* 1443ed5bde0bSTheodore Ts'o * get block failure will cause us to loop in 1444ed5bde0bSTheodore Ts'o * writepages, because a_ops->writepage won't be able 1445ed5bde0bSTheodore Ts'o * to make progress. The page will be redirtied by 1446ed5bde0bSTheodore Ts'o * writepage and writepages will again try to write 1447ed5bde0bSTheodore Ts'o * the same. 1448c4a0c46eSAneesh Kumar K.V */ 1449e3570639SEric Sandeen if (!(EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) { 1450e3570639SEric Sandeen ext4_msg(sb, KERN_CRIT, 1451e3570639SEric Sandeen "delayed block allocation failed for inode %lu " 1452e3570639SEric Sandeen "at logical offset %llu with max blocks %zd " 1453e3570639SEric Sandeen "with error %d", mpd->inode->i_ino, 1454c4a0c46eSAneesh Kumar K.V (unsigned long long) next, 14558dc207c0STheodore Ts'o mpd->b_size >> mpd->inode->i_blkbits, err); 1456e3570639SEric Sandeen ext4_msg(sb, KERN_CRIT, 1457e3570639SEric Sandeen "This should not happen!! Data will be lost\n"); 1458e3570639SEric Sandeen if (err == -ENOSPC) 1459df22291fSAneesh Kumar K.V ext4_print_free_blocks(mpd->inode); 1460030ba6bcSAneesh Kumar K.V } 14612fa3cdfbSTheodore Ts'o /* invalidate all the pages */ 1462c7f5938aSCurt Wohlgemuth ext4_da_block_invalidatepages(mpd); 1463e0fd9b90SCurt Wohlgemuth 1464e0fd9b90SCurt Wohlgemuth /* Mark this page range as having been completed */ 1465e0fd9b90SCurt Wohlgemuth mpd->io_done = 1; 14665a87b7a5STheodore Ts'o return; 1467c4a0c46eSAneesh Kumar K.V } 14682fa3cdfbSTheodore Ts'o BUG_ON(blks == 0); 14692fa3cdfbSTheodore Ts'o 14701de3e3dfSTheodore Ts'o mapp = ↦ 14712ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 14722ed88685STheodore Ts'o struct block_device *bdev = mpd->inode->i_sb->s_bdev; 14732ed88685STheodore Ts'o int i; 147464769240SAlex Tomas 14752ed88685STheodore Ts'o for (i = 0; i < map.m_len; i++) 14762ed88685STheodore Ts'o unmap_underlying_metadata(bdev, map.m_pblk + i); 14772ed88685STheodore Ts'o } 147864769240SAlex Tomas 14792fa3cdfbSTheodore Ts'o if (ext4_should_order_data(mpd->inode)) { 14802fa3cdfbSTheodore Ts'o err = ext4_jbd2_file_inode(handle, mpd->inode); 14812fa3cdfbSTheodore Ts'o if (err) 14825a87b7a5STheodore Ts'o /* This only happens if the journal is aborted */ 14835a87b7a5STheodore Ts'o return; 14842fa3cdfbSTheodore Ts'o } 14852fa3cdfbSTheodore Ts'o 14862fa3cdfbSTheodore Ts'o /* 148703f5d8bcSJan Kara * Update on-disk size along with block allocation. 14882fa3cdfbSTheodore Ts'o */ 14892fa3cdfbSTheodore Ts'o disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits; 14902fa3cdfbSTheodore Ts'o if (disksize > i_size_read(mpd->inode)) 14912fa3cdfbSTheodore Ts'o disksize = i_size_read(mpd->inode); 14922fa3cdfbSTheodore Ts'o if (disksize > EXT4_I(mpd->inode)->i_disksize) { 14932fa3cdfbSTheodore Ts'o ext4_update_i_disksize(mpd->inode, disksize); 14945a87b7a5STheodore Ts'o err = ext4_mark_inode_dirty(handle, mpd->inode); 14955a87b7a5STheodore Ts'o if (err) 14965a87b7a5STheodore Ts'o ext4_error(mpd->inode->i_sb, 14975a87b7a5STheodore Ts'o "Failed to mark inode %lu dirty", 14985a87b7a5STheodore Ts'o mpd->inode->i_ino); 14992fa3cdfbSTheodore Ts'o } 15002fa3cdfbSTheodore Ts'o 15015a87b7a5STheodore Ts'o submit_io: 15021de3e3dfSTheodore Ts'o mpage_da_submit_io(mpd, mapp); 15035a87b7a5STheodore Ts'o mpd->io_done = 1; 150464769240SAlex Tomas } 150564769240SAlex Tomas 1506bf068ee2SAneesh Kumar K.V #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \ 1507bf068ee2SAneesh Kumar K.V (1 << BH_Delay) | (1 << BH_Unwritten)) 150864769240SAlex Tomas 150964769240SAlex Tomas /* 151064769240SAlex Tomas * mpage_add_bh_to_extent - try to add one more block to extent of blocks 151164769240SAlex Tomas * 151264769240SAlex Tomas * @mpd->lbh - extent of blocks 151364769240SAlex Tomas * @logical - logical number of the block in the file 151464769240SAlex Tomas * @bh - bh of the block (used to access block's state) 151564769240SAlex Tomas * 151664769240SAlex Tomas * the function is used to collect contig. blocks in same state 151764769240SAlex Tomas */ 151864769240SAlex Tomas static void mpage_add_bh_to_extent(struct mpage_da_data *mpd, 15198dc207c0STheodore Ts'o sector_t logical, size_t b_size, 15208dc207c0STheodore Ts'o unsigned long b_state) 152164769240SAlex Tomas { 152264769240SAlex Tomas sector_t next; 15238dc207c0STheodore Ts'o int nrblocks = mpd->b_size >> mpd->inode->i_blkbits; 152464769240SAlex Tomas 1525c445e3e0SEric Sandeen /* 1526c445e3e0SEric Sandeen * XXX Don't go larger than mballoc is willing to allocate 1527c445e3e0SEric Sandeen * This is a stopgap solution. We eventually need to fold 1528c445e3e0SEric Sandeen * mpage_da_submit_io() into this function and then call 152979e83036SEric Sandeen * ext4_map_blocks() multiple times in a loop 1530c445e3e0SEric Sandeen */ 1531c445e3e0SEric Sandeen if (nrblocks >= 8*1024*1024/mpd->inode->i_sb->s_blocksize) 1532c445e3e0SEric Sandeen goto flush_it; 1533c445e3e0SEric Sandeen 1534525f4ed8SMingming Cao /* check if thereserved journal credits might overflow */ 153512e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(mpd->inode, EXT4_INODE_EXTENTS))) { 1536525f4ed8SMingming Cao if (nrblocks >= EXT4_MAX_TRANS_DATA) { 1537525f4ed8SMingming Cao /* 1538525f4ed8SMingming Cao * With non-extent format we are limited by the journal 1539525f4ed8SMingming Cao * credit available. Total credit needed to insert 1540525f4ed8SMingming Cao * nrblocks contiguous blocks is dependent on the 1541525f4ed8SMingming Cao * nrblocks. So limit nrblocks. 1542525f4ed8SMingming Cao */ 1543525f4ed8SMingming Cao goto flush_it; 1544525f4ed8SMingming Cao } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) > 1545525f4ed8SMingming Cao EXT4_MAX_TRANS_DATA) { 1546525f4ed8SMingming Cao /* 1547525f4ed8SMingming Cao * Adding the new buffer_head would make it cross the 1548525f4ed8SMingming Cao * allowed limit for which we have journal credit 1549525f4ed8SMingming Cao * reserved. So limit the new bh->b_size 1550525f4ed8SMingming Cao */ 1551525f4ed8SMingming Cao b_size = (EXT4_MAX_TRANS_DATA - nrblocks) << 1552525f4ed8SMingming Cao mpd->inode->i_blkbits; 1553525f4ed8SMingming Cao /* we will do mpage_da_submit_io in the next loop */ 1554525f4ed8SMingming Cao } 1555525f4ed8SMingming Cao } 155664769240SAlex Tomas /* 155764769240SAlex Tomas * First block in the extent 155864769240SAlex Tomas */ 15598dc207c0STheodore Ts'o if (mpd->b_size == 0) { 15608dc207c0STheodore Ts'o mpd->b_blocknr = logical; 15618dc207c0STheodore Ts'o mpd->b_size = b_size; 15628dc207c0STheodore Ts'o mpd->b_state = b_state & BH_FLAGS; 156364769240SAlex Tomas return; 156464769240SAlex Tomas } 156564769240SAlex Tomas 15668dc207c0STheodore Ts'o next = mpd->b_blocknr + nrblocks; 156764769240SAlex Tomas /* 156864769240SAlex Tomas * Can we merge the block to our big extent? 156964769240SAlex Tomas */ 15708dc207c0STheodore Ts'o if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) { 15718dc207c0STheodore Ts'o mpd->b_size += b_size; 157264769240SAlex Tomas return; 157364769240SAlex Tomas } 157464769240SAlex Tomas 1575525f4ed8SMingming Cao flush_it: 157664769240SAlex Tomas /* 157764769240SAlex Tomas * We couldn't merge the block to our extent, so we 157864769240SAlex Tomas * need to flush current extent and start new one 157964769240SAlex Tomas */ 15805a87b7a5STheodore Ts'o mpage_da_map_and_submit(mpd); 1581a1d6cc56SAneesh Kumar K.V return; 158264769240SAlex Tomas } 158364769240SAlex Tomas 1584c364b22cSAneesh Kumar K.V static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh) 158529fa89d0SAneesh Kumar K.V { 1586c364b22cSAneesh Kumar K.V return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 158729fa89d0SAneesh Kumar K.V } 158829fa89d0SAneesh Kumar K.V 158964769240SAlex Tomas /* 1590b920c755STheodore Ts'o * This is a special get_blocks_t callback which is used by 1591b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 1592b920c755STheodore Ts'o * reserve space for a single block. 159329fa89d0SAneesh Kumar K.V * 159429fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 159529fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 159629fa89d0SAneesh Kumar K.V * 159729fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 159829fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 159929fa89d0SAneesh Kumar K.V * initialized properly. 160064769240SAlex Tomas */ 160164769240SAlex Tomas static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 16022ed88685STheodore Ts'o struct buffer_head *bh, int create) 160364769240SAlex Tomas { 16042ed88685STheodore Ts'o struct ext4_map_blocks map; 160564769240SAlex Tomas int ret = 0; 160633b9817eSAneesh Kumar K.V sector_t invalid_block = ~((sector_t) 0xffff); 160733b9817eSAneesh Kumar K.V 160833b9817eSAneesh Kumar K.V if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 160933b9817eSAneesh Kumar K.V invalid_block = ~0; 161064769240SAlex Tomas 161164769240SAlex Tomas BUG_ON(create == 0); 16122ed88685STheodore Ts'o BUG_ON(bh->b_size != inode->i_sb->s_blocksize); 16132ed88685STheodore Ts'o 16142ed88685STheodore Ts'o map.m_lblk = iblock; 16152ed88685STheodore Ts'o map.m_len = 1; 161664769240SAlex Tomas 161764769240SAlex Tomas /* 161864769240SAlex Tomas * first, we need to know whether the block is allocated already 161964769240SAlex Tomas * preallocated blocks are unmapped but should treated 162064769240SAlex Tomas * the same as allocated blocks. 162164769240SAlex Tomas */ 16222ed88685STheodore Ts'o ret = ext4_map_blocks(NULL, inode, &map, 0); 16232ed88685STheodore Ts'o if (ret < 0) 16242ed88685STheodore Ts'o return ret; 16252ed88685STheodore Ts'o if (ret == 0) { 16262ed88685STheodore Ts'o if (buffer_delay(bh)) 16272ed88685STheodore Ts'o return 0; /* Not sure this could or should happen */ 162864769240SAlex Tomas /* 1629ebdec241SChristoph Hellwig * XXX: __block_write_begin() unmaps passed block, is it OK? 163064769240SAlex Tomas */ 16319d0be502STheodore Ts'o ret = ext4_da_reserve_space(inode, iblock); 1632d2a17637SMingming Cao if (ret) 1633d2a17637SMingming Cao /* not enough space to reserve */ 1634d2a17637SMingming Cao return ret; 1635d2a17637SMingming Cao 16362ed88685STheodore Ts'o map_bh(bh, inode->i_sb, invalid_block); 16372ed88685STheodore Ts'o set_buffer_new(bh); 16382ed88685STheodore Ts'o set_buffer_delay(bh); 16392ed88685STheodore Ts'o return 0; 164064769240SAlex Tomas } 164164769240SAlex Tomas 16422ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 16432ed88685STheodore Ts'o bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; 16442ed88685STheodore Ts'o 16452ed88685STheodore Ts'o if (buffer_unwritten(bh)) { 16462ed88685STheodore Ts'o /* A delayed write to unwritten bh should be marked 16472ed88685STheodore Ts'o * new and mapped. Mapped ensures that we don't do 16482ed88685STheodore Ts'o * get_block multiple times when we write to the same 16492ed88685STheodore Ts'o * offset and new ensures that we do proper zero out 16502ed88685STheodore Ts'o * for partial write. 16512ed88685STheodore Ts'o */ 16522ed88685STheodore Ts'o set_buffer_new(bh); 1653c8205636STheodore Ts'o set_buffer_mapped(bh); 16542ed88685STheodore Ts'o } 16552ed88685STheodore Ts'o return 0; 165664769240SAlex Tomas } 165761628a3fSMingming Cao 1658b920c755STheodore Ts'o /* 1659b920c755STheodore Ts'o * This function is used as a standard get_block_t calback function 1660b920c755STheodore Ts'o * when there is no desire to allocate any blocks. It is used as a 1661ebdec241SChristoph Hellwig * callback function for block_write_begin() and block_write_full_page(). 1662206f7ab4SChristoph Hellwig * These functions should only try to map a single block at a time. 1663b920c755STheodore Ts'o * 1664b920c755STheodore Ts'o * Since this function doesn't do block allocations even if the caller 1665b920c755STheodore Ts'o * requests it by passing in create=1, it is critically important that 1666b920c755STheodore Ts'o * any caller checks to make sure that any buffer heads are returned 1667b920c755STheodore Ts'o * by this function are either all already mapped or marked for 1668206f7ab4SChristoph Hellwig * delayed allocation before calling block_write_full_page(). Otherwise, 1669206f7ab4SChristoph Hellwig * b_blocknr could be left unitialized, and the page write functions will 1670206f7ab4SChristoph Hellwig * be taken by surprise. 1671b920c755STheodore Ts'o */ 1672b920c755STheodore Ts'o static int noalloc_get_block_write(struct inode *inode, sector_t iblock, 1673f0e6c985SAneesh Kumar K.V struct buffer_head *bh_result, int create) 1674f0e6c985SAneesh Kumar K.V { 1675a2dc52b5STheodore Ts'o BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize); 16762ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh_result, 0); 167761628a3fSMingming Cao } 167861628a3fSMingming Cao 167962e086beSAneesh Kumar K.V static int bget_one(handle_t *handle, struct buffer_head *bh) 168062e086beSAneesh Kumar K.V { 168162e086beSAneesh Kumar K.V get_bh(bh); 168262e086beSAneesh Kumar K.V return 0; 168362e086beSAneesh Kumar K.V } 168462e086beSAneesh Kumar K.V 168562e086beSAneesh Kumar K.V static int bput_one(handle_t *handle, struct buffer_head *bh) 168662e086beSAneesh Kumar K.V { 168762e086beSAneesh Kumar K.V put_bh(bh); 168862e086beSAneesh Kumar K.V return 0; 168962e086beSAneesh Kumar K.V } 169062e086beSAneesh Kumar K.V 169162e086beSAneesh Kumar K.V static int __ext4_journalled_writepage(struct page *page, 169262e086beSAneesh Kumar K.V unsigned int len) 169362e086beSAneesh Kumar K.V { 169462e086beSAneesh Kumar K.V struct address_space *mapping = page->mapping; 169562e086beSAneesh Kumar K.V struct inode *inode = mapping->host; 169662e086beSAneesh Kumar K.V struct buffer_head *page_bufs; 169762e086beSAneesh Kumar K.V handle_t *handle = NULL; 169862e086beSAneesh Kumar K.V int ret = 0; 169962e086beSAneesh Kumar K.V int err; 170062e086beSAneesh Kumar K.V 1701cb20d518STheodore Ts'o ClearPageChecked(page); 170262e086beSAneesh Kumar K.V page_bufs = page_buffers(page); 170362e086beSAneesh Kumar K.V BUG_ON(!page_bufs); 170462e086beSAneesh Kumar K.V walk_page_buffers(handle, page_bufs, 0, len, NULL, bget_one); 170562e086beSAneesh Kumar K.V /* As soon as we unlock the page, it can go away, but we have 170662e086beSAneesh Kumar K.V * references to buffers so we are safe */ 170762e086beSAneesh Kumar K.V unlock_page(page); 170862e086beSAneesh Kumar K.V 170962e086beSAneesh Kumar K.V handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode)); 171062e086beSAneesh Kumar K.V if (IS_ERR(handle)) { 171162e086beSAneesh Kumar K.V ret = PTR_ERR(handle); 171262e086beSAneesh Kumar K.V goto out; 171362e086beSAneesh Kumar K.V } 171462e086beSAneesh Kumar K.V 1715441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1716441c8508SCurt Wohlgemuth 171762e086beSAneesh Kumar K.V ret = walk_page_buffers(handle, page_bufs, 0, len, NULL, 171862e086beSAneesh Kumar K.V do_journal_get_write_access); 171962e086beSAneesh Kumar K.V 172062e086beSAneesh Kumar K.V err = walk_page_buffers(handle, page_bufs, 0, len, NULL, 172162e086beSAneesh Kumar K.V write_end_fn); 172262e086beSAneesh Kumar K.V if (ret == 0) 172362e086beSAneesh Kumar K.V ret = err; 17242d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 172562e086beSAneesh Kumar K.V err = ext4_journal_stop(handle); 172662e086beSAneesh Kumar K.V if (!ret) 172762e086beSAneesh Kumar K.V ret = err; 172862e086beSAneesh Kumar K.V 172962e086beSAneesh Kumar K.V walk_page_buffers(handle, page_bufs, 0, len, NULL, bput_one); 173019f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 173162e086beSAneesh Kumar K.V out: 173262e086beSAneesh Kumar K.V return ret; 173362e086beSAneesh Kumar K.V } 173462e086beSAneesh Kumar K.V 1735744692dcSJiaying Zhang static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode); 1736744692dcSJiaying Zhang static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate); 1737744692dcSJiaying Zhang 173861628a3fSMingming Cao /* 173943ce1d23SAneesh Kumar K.V * Note that we don't need to start a transaction unless we're journaling data 174043ce1d23SAneesh Kumar K.V * because we should have holes filled from ext4_page_mkwrite(). We even don't 174143ce1d23SAneesh Kumar K.V * need to file the inode to the transaction's list in ordered mode because if 174243ce1d23SAneesh Kumar K.V * we are writing back data added by write(), the inode is already there and if 174343ce1d23SAneesh Kumar K.V * we are writing back data modified via mmap(), no one guarantees in which 174443ce1d23SAneesh Kumar K.V * transaction the data will hit the disk. In case we are journaling data, we 174543ce1d23SAneesh Kumar K.V * cannot start transaction directly because transaction start ranks above page 174643ce1d23SAneesh Kumar K.V * lock so we have to do some magic. 174743ce1d23SAneesh Kumar K.V * 1748b920c755STheodore Ts'o * This function can get called via... 1749b920c755STheodore Ts'o * - ext4_da_writepages after taking page lock (have journal handle) 1750b920c755STheodore Ts'o * - journal_submit_inode_data_buffers (no journal handle) 1751b920c755STheodore Ts'o * - shrink_page_list via pdflush (no journal handle) 1752b920c755STheodore Ts'o * - grab_page_cache when doing write_begin (have journal handle) 175343ce1d23SAneesh Kumar K.V * 175443ce1d23SAneesh Kumar K.V * We don't do any block allocation in this function. If we have page with 175543ce1d23SAneesh Kumar K.V * multiple blocks we need to write those buffer_heads that are mapped. This 175643ce1d23SAneesh Kumar K.V * is important for mmaped based write. So if we do with blocksize 1K 175743ce1d23SAneesh Kumar K.V * truncate(f, 1024); 175843ce1d23SAneesh Kumar K.V * a = mmap(f, 0, 4096); 175943ce1d23SAneesh Kumar K.V * a[0] = 'a'; 176043ce1d23SAneesh Kumar K.V * truncate(f, 4096); 176143ce1d23SAneesh Kumar K.V * we have in the page first buffer_head mapped via page_mkwrite call back 176243ce1d23SAneesh Kumar K.V * but other bufer_heads would be unmapped but dirty(dirty done via the 176343ce1d23SAneesh Kumar K.V * do_wp_page). So writepage should write the first block. If we modify 176443ce1d23SAneesh Kumar K.V * the mmap area beyond 1024 we will again get a page_fault and the 176543ce1d23SAneesh Kumar K.V * page_mkwrite callback will do the block allocation and mark the 176643ce1d23SAneesh Kumar K.V * buffer_heads mapped. 176743ce1d23SAneesh Kumar K.V * 176843ce1d23SAneesh Kumar K.V * We redirty the page if we have any buffer_heads that is either delay or 176943ce1d23SAneesh Kumar K.V * unwritten in the page. 177043ce1d23SAneesh Kumar K.V * 177143ce1d23SAneesh Kumar K.V * We can get recursively called as show below. 177243ce1d23SAneesh Kumar K.V * 177343ce1d23SAneesh Kumar K.V * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> 177443ce1d23SAneesh Kumar K.V * ext4_writepage() 177543ce1d23SAneesh Kumar K.V * 177643ce1d23SAneesh Kumar K.V * But since we don't do any block allocation we should not deadlock. 177743ce1d23SAneesh Kumar K.V * Page also have the dirty flag cleared so we don't get recurive page_lock. 177861628a3fSMingming Cao */ 177943ce1d23SAneesh Kumar K.V static int ext4_writepage(struct page *page, 178064769240SAlex Tomas struct writeback_control *wbc) 178164769240SAlex Tomas { 1782a42afc5fSTheodore Ts'o int ret = 0, commit_write = 0; 178361628a3fSMingming Cao loff_t size; 1784498e5f24STheodore Ts'o unsigned int len; 1785744692dcSJiaying Zhang struct buffer_head *page_bufs = NULL; 178661628a3fSMingming Cao struct inode *inode = page->mapping->host; 178764769240SAlex Tomas 1788a9c667f8SLukas Czerner trace_ext4_writepage(page); 178961628a3fSMingming Cao size = i_size_read(inode); 179061628a3fSMingming Cao if (page->index == size >> PAGE_CACHE_SHIFT) 179161628a3fSMingming Cao len = size & ~PAGE_CACHE_MASK; 179261628a3fSMingming Cao else 179361628a3fSMingming Cao len = PAGE_CACHE_SIZE; 179461628a3fSMingming Cao 1795a42afc5fSTheodore Ts'o /* 1796a42afc5fSTheodore Ts'o * If the page does not have buffers (for whatever reason), 1797a107e5a3STheodore Ts'o * try to create them using __block_write_begin. If this 1798a42afc5fSTheodore Ts'o * fails, redirty the page and move on. 1799a42afc5fSTheodore Ts'o */ 1800b1142e8fSTheodore Ts'o if (!page_has_buffers(page)) { 1801a107e5a3STheodore Ts'o if (__block_write_begin(page, 0, len, 1802a42afc5fSTheodore Ts'o noalloc_get_block_write)) { 1803a42afc5fSTheodore Ts'o redirty_page: 1804a42afc5fSTheodore Ts'o redirty_page_for_writepage(wbc, page); 1805a42afc5fSTheodore Ts'o unlock_page(page); 1806a42afc5fSTheodore Ts'o return 0; 1807a42afc5fSTheodore Ts'o } 1808a42afc5fSTheodore Ts'o commit_write = 1; 1809a42afc5fSTheodore Ts'o } 1810f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 1811f0e6c985SAneesh Kumar K.V if (walk_page_buffers(NULL, page_bufs, 0, len, NULL, 1812c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 181361628a3fSMingming Cao /* 1814b1142e8fSTheodore Ts'o * We don't want to do block allocation, so redirty 1815b1142e8fSTheodore Ts'o * the page and return. We may reach here when we do 1816b1142e8fSTheodore Ts'o * a journal commit via journal_submit_inode_data_buffers. 1817b1142e8fSTheodore Ts'o * We can also reach here via shrink_page_list 1818f0e6c985SAneesh Kumar K.V */ 1819a42afc5fSTheodore Ts'o goto redirty_page; 1820f0e6c985SAneesh Kumar K.V } 1821a42afc5fSTheodore Ts'o if (commit_write) 1822ed9b3e33SAneesh Kumar K.V /* now mark the buffer_heads as dirty and uptodate */ 1823b767e78aSAneesh Kumar K.V block_commit_write(page, 0, len); 182464769240SAlex Tomas 1825cb20d518STheodore Ts'o if (PageChecked(page) && ext4_should_journal_data(inode)) 182643ce1d23SAneesh Kumar K.V /* 182743ce1d23SAneesh Kumar K.V * It's mmapped pagecache. Add buffers and journal it. There 182843ce1d23SAneesh Kumar K.V * doesn't seem much point in redirtying the page here. 182943ce1d23SAneesh Kumar K.V */ 18303f0ca309SWu Fengguang return __ext4_journalled_writepage(page, len); 183143ce1d23SAneesh Kumar K.V 1832a42afc5fSTheodore Ts'o if (buffer_uninit(page_bufs)) { 1833744692dcSJiaying Zhang ext4_set_bh_endio(page_bufs, inode); 1834744692dcSJiaying Zhang ret = block_write_full_page_endio(page, noalloc_get_block_write, 1835744692dcSJiaying Zhang wbc, ext4_end_io_buffer_write); 1836744692dcSJiaying Zhang } else 1837b920c755STheodore Ts'o ret = block_write_full_page(page, noalloc_get_block_write, 1838f0e6c985SAneesh Kumar K.V wbc); 183964769240SAlex Tomas 184064769240SAlex Tomas return ret; 184164769240SAlex Tomas } 184264769240SAlex Tomas 184361628a3fSMingming Cao /* 1844525f4ed8SMingming Cao * This is called via ext4_da_writepages() to 184525985edcSLucas De Marchi * calculate the total number of credits to reserve to fit 1846525f4ed8SMingming Cao * a single extent allocation into a single transaction, 1847525f4ed8SMingming Cao * ext4_da_writpeages() will loop calling this before 1848525f4ed8SMingming Cao * the block allocation. 184961628a3fSMingming Cao */ 1850525f4ed8SMingming Cao 1851525f4ed8SMingming Cao static int ext4_da_writepages_trans_blocks(struct inode *inode) 1852525f4ed8SMingming Cao { 1853525f4ed8SMingming Cao int max_blocks = EXT4_I(inode)->i_reserved_data_blocks; 1854525f4ed8SMingming Cao 1855525f4ed8SMingming Cao /* 1856525f4ed8SMingming Cao * With non-extent format the journal credit needed to 1857525f4ed8SMingming Cao * insert nrblocks contiguous block is dependent on 1858525f4ed8SMingming Cao * number of contiguous block. So we will limit 1859525f4ed8SMingming Cao * number of contiguous block to a sane value 1860525f4ed8SMingming Cao */ 186112e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) && 1862525f4ed8SMingming Cao (max_blocks > EXT4_MAX_TRANS_DATA)) 1863525f4ed8SMingming Cao max_blocks = EXT4_MAX_TRANS_DATA; 1864525f4ed8SMingming Cao 1865525f4ed8SMingming Cao return ext4_chunk_trans_blocks(inode, max_blocks); 1866525f4ed8SMingming Cao } 186761628a3fSMingming Cao 18688e48dcfbSTheodore Ts'o /* 18698e48dcfbSTheodore Ts'o * write_cache_pages_da - walk the list of dirty pages of the given 18708eb9e5ceSTheodore Ts'o * address space and accumulate pages that need writing, and call 1871168fc022STheodore Ts'o * mpage_da_map_and_submit to map a single contiguous memory region 1872168fc022STheodore Ts'o * and then write them. 18738e48dcfbSTheodore Ts'o */ 18748e48dcfbSTheodore Ts'o static int write_cache_pages_da(struct address_space *mapping, 18758e48dcfbSTheodore Ts'o struct writeback_control *wbc, 187672f84e65SEric Sandeen struct mpage_da_data *mpd, 187772f84e65SEric Sandeen pgoff_t *done_index) 18788e48dcfbSTheodore Ts'o { 18798eb9e5ceSTheodore Ts'o struct buffer_head *bh, *head; 1880168fc022STheodore Ts'o struct inode *inode = mapping->host; 18818e48dcfbSTheodore Ts'o struct pagevec pvec; 18824f01b02cSTheodore Ts'o unsigned int nr_pages; 18834f01b02cSTheodore Ts'o sector_t logical; 18844f01b02cSTheodore Ts'o pgoff_t index, end; 18858e48dcfbSTheodore Ts'o long nr_to_write = wbc->nr_to_write; 18864f01b02cSTheodore Ts'o int i, tag, ret = 0; 18878e48dcfbSTheodore Ts'o 1888168fc022STheodore Ts'o memset(mpd, 0, sizeof(struct mpage_da_data)); 1889168fc022STheodore Ts'o mpd->wbc = wbc; 1890168fc022STheodore Ts'o mpd->inode = inode; 18918e48dcfbSTheodore Ts'o pagevec_init(&pvec, 0); 18928e48dcfbSTheodore Ts'o index = wbc->range_start >> PAGE_CACHE_SHIFT; 18938e48dcfbSTheodore Ts'o end = wbc->range_end >> PAGE_CACHE_SHIFT; 18948e48dcfbSTheodore Ts'o 18956e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 18965b41d924SEric Sandeen tag = PAGECACHE_TAG_TOWRITE; 18975b41d924SEric Sandeen else 18985b41d924SEric Sandeen tag = PAGECACHE_TAG_DIRTY; 18995b41d924SEric Sandeen 190072f84e65SEric Sandeen *done_index = index; 19014f01b02cSTheodore Ts'o while (index <= end) { 19025b41d924SEric Sandeen nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, 19038e48dcfbSTheodore Ts'o min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); 19048e48dcfbSTheodore Ts'o if (nr_pages == 0) 19054f01b02cSTheodore Ts'o return 0; 19068e48dcfbSTheodore Ts'o 19078e48dcfbSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 19088e48dcfbSTheodore Ts'o struct page *page = pvec.pages[i]; 19098e48dcfbSTheodore Ts'o 19108e48dcfbSTheodore Ts'o /* 19118e48dcfbSTheodore Ts'o * At this point, the page may be truncated or 19128e48dcfbSTheodore Ts'o * invalidated (changing page->mapping to NULL), or 19138e48dcfbSTheodore Ts'o * even swizzled back from swapper_space to tmpfs file 19148e48dcfbSTheodore Ts'o * mapping. However, page->index will not change 19158e48dcfbSTheodore Ts'o * because we have a reference on the page. 19168e48dcfbSTheodore Ts'o */ 19174f01b02cSTheodore Ts'o if (page->index > end) 19184f01b02cSTheodore Ts'o goto out; 19198e48dcfbSTheodore Ts'o 192072f84e65SEric Sandeen *done_index = page->index + 1; 192172f84e65SEric Sandeen 192278aaced3STheodore Ts'o /* 192378aaced3STheodore Ts'o * If we can't merge this page, and we have 192478aaced3STheodore Ts'o * accumulated an contiguous region, write it 192578aaced3STheodore Ts'o */ 192678aaced3STheodore Ts'o if ((mpd->next_page != page->index) && 192778aaced3STheodore Ts'o (mpd->next_page != mpd->first_page)) { 192878aaced3STheodore Ts'o mpage_da_map_and_submit(mpd); 192978aaced3STheodore Ts'o goto ret_extent_tail; 193078aaced3STheodore Ts'o } 193178aaced3STheodore Ts'o 19328e48dcfbSTheodore Ts'o lock_page(page); 19338e48dcfbSTheodore Ts'o 19348e48dcfbSTheodore Ts'o /* 19354f01b02cSTheodore Ts'o * If the page is no longer dirty, or its 19364f01b02cSTheodore Ts'o * mapping no longer corresponds to inode we 19374f01b02cSTheodore Ts'o * are writing (which means it has been 19384f01b02cSTheodore Ts'o * truncated or invalidated), or the page is 19394f01b02cSTheodore Ts'o * already under writeback and we are not 19404f01b02cSTheodore Ts'o * doing a data integrity writeback, skip the page 19418e48dcfbSTheodore Ts'o */ 19424f01b02cSTheodore Ts'o if (!PageDirty(page) || 19434f01b02cSTheodore Ts'o (PageWriteback(page) && 19444f01b02cSTheodore Ts'o (wbc->sync_mode == WB_SYNC_NONE)) || 19454f01b02cSTheodore Ts'o unlikely(page->mapping != mapping)) { 19468e48dcfbSTheodore Ts'o unlock_page(page); 19478e48dcfbSTheodore Ts'o continue; 19488e48dcfbSTheodore Ts'o } 19498e48dcfbSTheodore Ts'o 19508e48dcfbSTheodore Ts'o wait_on_page_writeback(page); 19518e48dcfbSTheodore Ts'o BUG_ON(PageWriteback(page)); 19528e48dcfbSTheodore Ts'o 1953168fc022STheodore Ts'o if (mpd->next_page != page->index) 19548eb9e5ceSTheodore Ts'o mpd->first_page = page->index; 19558eb9e5ceSTheodore Ts'o mpd->next_page = page->index + 1; 19568eb9e5ceSTheodore Ts'o logical = (sector_t) page->index << 19578eb9e5ceSTheodore Ts'o (PAGE_CACHE_SHIFT - inode->i_blkbits); 19588eb9e5ceSTheodore Ts'o 19598eb9e5ceSTheodore Ts'o if (!page_has_buffers(page)) { 19604f01b02cSTheodore Ts'o mpage_add_bh_to_extent(mpd, logical, 19614f01b02cSTheodore Ts'o PAGE_CACHE_SIZE, 19628eb9e5ceSTheodore Ts'o (1 << BH_Dirty) | (1 << BH_Uptodate)); 19634f01b02cSTheodore Ts'o if (mpd->io_done) 19644f01b02cSTheodore Ts'o goto ret_extent_tail; 19658e48dcfbSTheodore Ts'o } else { 19668eb9e5ceSTheodore Ts'o /* 19674f01b02cSTheodore Ts'o * Page with regular buffer heads, 19684f01b02cSTheodore Ts'o * just add all dirty ones 19698eb9e5ceSTheodore Ts'o */ 19708eb9e5ceSTheodore Ts'o head = page_buffers(page); 19718eb9e5ceSTheodore Ts'o bh = head; 19728eb9e5ceSTheodore Ts'o do { 19738eb9e5ceSTheodore Ts'o BUG_ON(buffer_locked(bh)); 19748eb9e5ceSTheodore Ts'o /* 19758eb9e5ceSTheodore Ts'o * We need to try to allocate 19768eb9e5ceSTheodore Ts'o * unmapped blocks in the same page. 19778eb9e5ceSTheodore Ts'o * Otherwise we won't make progress 19788eb9e5ceSTheodore Ts'o * with the page in ext4_writepage 19798eb9e5ceSTheodore Ts'o */ 19808eb9e5ceSTheodore Ts'o if (ext4_bh_delay_or_unwritten(NULL, bh)) { 19818eb9e5ceSTheodore Ts'o mpage_add_bh_to_extent(mpd, logical, 19828eb9e5ceSTheodore Ts'o bh->b_size, 19838eb9e5ceSTheodore Ts'o bh->b_state); 19844f01b02cSTheodore Ts'o if (mpd->io_done) 19854f01b02cSTheodore Ts'o goto ret_extent_tail; 19868eb9e5ceSTheodore Ts'o } else if (buffer_dirty(bh) && (buffer_mapped(bh))) { 19878eb9e5ceSTheodore Ts'o /* 19884f01b02cSTheodore Ts'o * mapped dirty buffer. We need 19894f01b02cSTheodore Ts'o * to update the b_state 19904f01b02cSTheodore Ts'o * because we look at b_state 19914f01b02cSTheodore Ts'o * in mpage_da_map_blocks. We 19924f01b02cSTheodore Ts'o * don't update b_size because 19934f01b02cSTheodore Ts'o * if we find an unmapped 19944f01b02cSTheodore Ts'o * buffer_head later we need to 19954f01b02cSTheodore Ts'o * use the b_state flag of that 19964f01b02cSTheodore Ts'o * buffer_head. 19978eb9e5ceSTheodore Ts'o */ 19988eb9e5ceSTheodore Ts'o if (mpd->b_size == 0) 19998eb9e5ceSTheodore Ts'o mpd->b_state = bh->b_state & BH_FLAGS; 20008e48dcfbSTheodore Ts'o } 20018eb9e5ceSTheodore Ts'o logical++; 20028eb9e5ceSTheodore Ts'o } while ((bh = bh->b_this_page) != head); 20038e48dcfbSTheodore Ts'o } 20048e48dcfbSTheodore Ts'o 20058e48dcfbSTheodore Ts'o if (nr_to_write > 0) { 20068e48dcfbSTheodore Ts'o nr_to_write--; 20078e48dcfbSTheodore Ts'o if (nr_to_write == 0 && 20084f01b02cSTheodore Ts'o wbc->sync_mode == WB_SYNC_NONE) 20098e48dcfbSTheodore Ts'o /* 20108e48dcfbSTheodore Ts'o * We stop writing back only if we are 20118e48dcfbSTheodore Ts'o * not doing integrity sync. In case of 20128e48dcfbSTheodore Ts'o * integrity sync we have to keep going 20138e48dcfbSTheodore Ts'o * because someone may be concurrently 20148e48dcfbSTheodore Ts'o * dirtying pages, and we might have 20158e48dcfbSTheodore Ts'o * synced a lot of newly appeared dirty 20168e48dcfbSTheodore Ts'o * pages, but have not synced all of the 20178e48dcfbSTheodore Ts'o * old dirty pages. 20188e48dcfbSTheodore Ts'o */ 20194f01b02cSTheodore Ts'o goto out; 20208e48dcfbSTheodore Ts'o } 20218e48dcfbSTheodore Ts'o } 20228e48dcfbSTheodore Ts'o pagevec_release(&pvec); 20238e48dcfbSTheodore Ts'o cond_resched(); 20248e48dcfbSTheodore Ts'o } 20254f01b02cSTheodore Ts'o return 0; 20264f01b02cSTheodore Ts'o ret_extent_tail: 20274f01b02cSTheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 20288eb9e5ceSTheodore Ts'o out: 20298eb9e5ceSTheodore Ts'o pagevec_release(&pvec); 20308eb9e5ceSTheodore Ts'o cond_resched(); 20318e48dcfbSTheodore Ts'o return ret; 20328e48dcfbSTheodore Ts'o } 20338e48dcfbSTheodore Ts'o 20348e48dcfbSTheodore Ts'o 203564769240SAlex Tomas static int ext4_da_writepages(struct address_space *mapping, 203664769240SAlex Tomas struct writeback_control *wbc) 203764769240SAlex Tomas { 203822208dedSAneesh Kumar K.V pgoff_t index; 203922208dedSAneesh Kumar K.V int range_whole = 0; 204061628a3fSMingming Cao handle_t *handle = NULL; 2041df22291fSAneesh Kumar K.V struct mpage_da_data mpd; 20425e745b04SAneesh Kumar K.V struct inode *inode = mapping->host; 2043498e5f24STheodore Ts'o int pages_written = 0; 204455138e0bSTheodore Ts'o unsigned int max_pages; 20452acf2c26SAneesh Kumar K.V int range_cyclic, cycled = 1, io_done = 0; 204655138e0bSTheodore Ts'o int needed_blocks, ret = 0; 204755138e0bSTheodore Ts'o long desired_nr_to_write, nr_to_writebump = 0; 2048de89de6eSTheodore Ts'o loff_t range_start = wbc->range_start; 20495e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 205072f84e65SEric Sandeen pgoff_t done_index = 0; 20515b41d924SEric Sandeen pgoff_t end; 205261628a3fSMingming Cao 20539bffad1eSTheodore Ts'o trace_ext4_da_writepages(inode, wbc); 2054ba80b101STheodore Ts'o 205561628a3fSMingming Cao /* 205661628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 205761628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 205861628a3fSMingming Cao * because that could violate lock ordering on umount 205961628a3fSMingming Cao */ 2060a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 206161628a3fSMingming Cao return 0; 20622a21e37eSTheodore Ts'o 20632a21e37eSTheodore Ts'o /* 20642a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 20652a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 20662a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 20674ab2f15bSTheodore Ts'o * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because 20682a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 20692a21e37eSTheodore Ts'o * read-only, and in that case, ext4_da_writepages should 20702a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 20712a21e37eSTheodore Ts'o * the stack trace. 20722a21e37eSTheodore Ts'o */ 20734ab2f15bSTheodore Ts'o if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) 20742a21e37eSTheodore Ts'o return -EROFS; 20752a21e37eSTheodore Ts'o 207622208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 207722208dedSAneesh Kumar K.V range_whole = 1; 207861628a3fSMingming Cao 20792acf2c26SAneesh Kumar K.V range_cyclic = wbc->range_cyclic; 20802acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 208122208dedSAneesh Kumar K.V index = mapping->writeback_index; 20822acf2c26SAneesh Kumar K.V if (index) 20832acf2c26SAneesh Kumar K.V cycled = 0; 20842acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 20852acf2c26SAneesh Kumar K.V wbc->range_end = LLONG_MAX; 20862acf2c26SAneesh Kumar K.V wbc->range_cyclic = 0; 20875b41d924SEric Sandeen end = -1; 20885b41d924SEric Sandeen } else { 208922208dedSAneesh Kumar K.V index = wbc->range_start >> PAGE_CACHE_SHIFT; 20905b41d924SEric Sandeen end = wbc->range_end >> PAGE_CACHE_SHIFT; 20915b41d924SEric Sandeen } 2092a1d6cc56SAneesh Kumar K.V 209355138e0bSTheodore Ts'o /* 209455138e0bSTheodore Ts'o * This works around two forms of stupidity. The first is in 209555138e0bSTheodore Ts'o * the writeback code, which caps the maximum number of pages 209655138e0bSTheodore Ts'o * written to be 1024 pages. This is wrong on multiple 209755138e0bSTheodore Ts'o * levels; different architectues have a different page size, 209855138e0bSTheodore Ts'o * which changes the maximum amount of data which gets 209955138e0bSTheodore Ts'o * written. Secondly, 4 megabytes is way too small. XFS 210055138e0bSTheodore Ts'o * forces this value to be 16 megabytes by multiplying 210155138e0bSTheodore Ts'o * nr_to_write parameter by four, and then relies on its 210255138e0bSTheodore Ts'o * allocator to allocate larger extents to make them 210355138e0bSTheodore Ts'o * contiguous. Unfortunately this brings us to the second 210455138e0bSTheodore Ts'o * stupidity, which is that ext4's mballoc code only allocates 210555138e0bSTheodore Ts'o * at most 2048 blocks. So we force contiguous writes up to 210655138e0bSTheodore Ts'o * the number of dirty blocks in the inode, or 210755138e0bSTheodore Ts'o * sbi->max_writeback_mb_bump whichever is smaller. 210855138e0bSTheodore Ts'o */ 210955138e0bSTheodore Ts'o max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT); 2110b443e733SEric Sandeen if (!range_cyclic && range_whole) { 2111b443e733SEric Sandeen if (wbc->nr_to_write == LONG_MAX) 2112b443e733SEric Sandeen desired_nr_to_write = wbc->nr_to_write; 211355138e0bSTheodore Ts'o else 2114b443e733SEric Sandeen desired_nr_to_write = wbc->nr_to_write * 8; 2115b443e733SEric Sandeen } else 211655138e0bSTheodore Ts'o desired_nr_to_write = ext4_num_dirty_pages(inode, index, 211755138e0bSTheodore Ts'o max_pages); 211855138e0bSTheodore Ts'o if (desired_nr_to_write > max_pages) 211955138e0bSTheodore Ts'o desired_nr_to_write = max_pages; 212055138e0bSTheodore Ts'o 212155138e0bSTheodore Ts'o if (wbc->nr_to_write < desired_nr_to_write) { 212255138e0bSTheodore Ts'o nr_to_writebump = desired_nr_to_write - wbc->nr_to_write; 212355138e0bSTheodore Ts'o wbc->nr_to_write = desired_nr_to_write; 212455138e0bSTheodore Ts'o } 212555138e0bSTheodore Ts'o 21262acf2c26SAneesh Kumar K.V retry: 21276e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 21285b41d924SEric Sandeen tag_pages_for_writeback(mapping, index, end); 21295b41d924SEric Sandeen 213022208dedSAneesh Kumar K.V while (!ret && wbc->nr_to_write > 0) { 2131a1d6cc56SAneesh Kumar K.V 2132a1d6cc56SAneesh Kumar K.V /* 2133a1d6cc56SAneesh Kumar K.V * we insert one extent at a time. So we need 2134a1d6cc56SAneesh Kumar K.V * credit needed for single extent allocation. 2135a1d6cc56SAneesh Kumar K.V * journalled mode is currently not supported 2136a1d6cc56SAneesh Kumar K.V * by delalloc 2137a1d6cc56SAneesh Kumar K.V */ 2138a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 2139525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 2140a1d6cc56SAneesh Kumar K.V 214161628a3fSMingming Cao /* start a new transaction*/ 214261628a3fSMingming Cao handle = ext4_journal_start(inode, needed_blocks); 214361628a3fSMingming Cao if (IS_ERR(handle)) { 214461628a3fSMingming Cao ret = PTR_ERR(handle); 21451693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2146fbe845ddSCurt Wohlgemuth "%ld pages, ino %lu; err %d", __func__, 2147a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 214861628a3fSMingming Cao goto out_writepages; 214961628a3fSMingming Cao } 2150f63e6005STheodore Ts'o 2151f63e6005STheodore Ts'o /* 21528eb9e5ceSTheodore Ts'o * Now call write_cache_pages_da() to find the next 2153f63e6005STheodore Ts'o * contiguous region of logical blocks that need 21548eb9e5ceSTheodore Ts'o * blocks to be allocated by ext4 and submit them. 2155f63e6005STheodore Ts'o */ 215672f84e65SEric Sandeen ret = write_cache_pages_da(mapping, wbc, &mpd, &done_index); 2157f63e6005STheodore Ts'o /* 2158af901ca1SAndré Goddard Rosa * If we have a contiguous extent of pages and we 2159f63e6005STheodore Ts'o * haven't done the I/O yet, map the blocks and submit 2160f63e6005STheodore Ts'o * them for I/O. 2161f63e6005STheodore Ts'o */ 2162f63e6005STheodore Ts'o if (!mpd.io_done && mpd.next_page != mpd.first_page) { 21635a87b7a5STheodore Ts'o mpage_da_map_and_submit(&mpd); 2164f63e6005STheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 2165f63e6005STheodore Ts'o } 2166b3a3ca8cSTheodore Ts'o trace_ext4_da_write_pages(inode, &mpd); 2167f63e6005STheodore Ts'o wbc->nr_to_write -= mpd.pages_written; 2168df22291fSAneesh Kumar K.V 216961628a3fSMingming Cao ext4_journal_stop(handle); 2170df22291fSAneesh Kumar K.V 21718f64b32eSEric Sandeen if ((mpd.retval == -ENOSPC) && sbi->s_journal) { 217222208dedSAneesh Kumar K.V /* commit the transaction which would 217322208dedSAneesh Kumar K.V * free blocks released in the transaction 217422208dedSAneesh Kumar K.V * and try again 217522208dedSAneesh Kumar K.V */ 2176df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 217722208dedSAneesh Kumar K.V ret = 0; 217822208dedSAneesh Kumar K.V } else if (ret == MPAGE_DA_EXTENT_TAIL) { 2179a1d6cc56SAneesh Kumar K.V /* 2180a1d6cc56SAneesh Kumar K.V * got one extent now try with 2181a1d6cc56SAneesh Kumar K.V * rest of the pages 2182a1d6cc56SAneesh Kumar K.V */ 218322208dedSAneesh Kumar K.V pages_written += mpd.pages_written; 2184a1d6cc56SAneesh Kumar K.V ret = 0; 21852acf2c26SAneesh Kumar K.V io_done = 1; 218622208dedSAneesh Kumar K.V } else if (wbc->nr_to_write) 218761628a3fSMingming Cao /* 218861628a3fSMingming Cao * There is no more writeout needed 218961628a3fSMingming Cao * or we requested for a noblocking writeout 219061628a3fSMingming Cao * and we found the device congested 219161628a3fSMingming Cao */ 219261628a3fSMingming Cao break; 219361628a3fSMingming Cao } 21942acf2c26SAneesh Kumar K.V if (!io_done && !cycled) { 21952acf2c26SAneesh Kumar K.V cycled = 1; 21962acf2c26SAneesh Kumar K.V index = 0; 21972acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 21982acf2c26SAneesh Kumar K.V wbc->range_end = mapping->writeback_index - 1; 21992acf2c26SAneesh Kumar K.V goto retry; 22002acf2c26SAneesh Kumar K.V } 220161628a3fSMingming Cao 220222208dedSAneesh Kumar K.V /* Update index */ 22032acf2c26SAneesh Kumar K.V wbc->range_cyclic = range_cyclic; 220422208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 220522208dedSAneesh Kumar K.V /* 220622208dedSAneesh Kumar K.V * set the writeback_index so that range_cyclic 220722208dedSAneesh Kumar K.V * mode will write it back later 220822208dedSAneesh Kumar K.V */ 220972f84e65SEric Sandeen mapping->writeback_index = done_index; 2210a1d6cc56SAneesh Kumar K.V 221161628a3fSMingming Cao out_writepages: 221222208dedSAneesh Kumar K.V wbc->nr_to_write -= nr_to_writebump; 2213de89de6eSTheodore Ts'o wbc->range_start = range_start; 22149bffad1eSTheodore Ts'o trace_ext4_da_writepages_result(inode, wbc, ret, pages_written); 221561628a3fSMingming Cao return ret; 221664769240SAlex Tomas } 221764769240SAlex Tomas 221879f0be8dSAneesh Kumar K.V #define FALL_BACK_TO_NONDELALLOC 1 221979f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 222079f0be8dSAneesh Kumar K.V { 222179f0be8dSAneesh Kumar K.V s64 free_blocks, dirty_blocks; 222279f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 222379f0be8dSAneesh Kumar K.V 222479f0be8dSAneesh Kumar K.V /* 222579f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 222679f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 2227179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 222879f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 222979f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 223079f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 223179f0be8dSAneesh Kumar K.V */ 223279f0be8dSAneesh Kumar K.V free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter); 223379f0be8dSAneesh Kumar K.V dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyblocks_counter); 223479f0be8dSAneesh Kumar K.V if (2 * free_blocks < 3 * dirty_blocks || 223579f0be8dSAneesh Kumar K.V free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) { 223679f0be8dSAneesh Kumar K.V /* 2237c8afb446SEric Sandeen * free block count is less than 150% of dirty blocks 2238c8afb446SEric Sandeen * or free blocks is less than watermark 223979f0be8dSAneesh Kumar K.V */ 224079f0be8dSAneesh Kumar K.V return 1; 224179f0be8dSAneesh Kumar K.V } 2242c8afb446SEric Sandeen /* 2243c8afb446SEric Sandeen * Even if we don't switch but are nearing capacity, 2244c8afb446SEric Sandeen * start pushing delalloc when 1/2 of free blocks are dirty. 2245c8afb446SEric Sandeen */ 2246c8afb446SEric Sandeen if (free_blocks < 2 * dirty_blocks) 2247c8afb446SEric Sandeen writeback_inodes_sb_if_idle(sb); 2248c8afb446SEric Sandeen 224979f0be8dSAneesh Kumar K.V return 0; 225079f0be8dSAneesh Kumar K.V } 225179f0be8dSAneesh Kumar K.V 225264769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 225364769240SAlex Tomas loff_t pos, unsigned len, unsigned flags, 225464769240SAlex Tomas struct page **pagep, void **fsdata) 225564769240SAlex Tomas { 225672b8ab9dSEric Sandeen int ret, retries = 0; 225764769240SAlex Tomas struct page *page; 225864769240SAlex Tomas pgoff_t index; 225964769240SAlex Tomas struct inode *inode = mapping->host; 226064769240SAlex Tomas handle_t *handle; 226164769240SAlex Tomas 226264769240SAlex Tomas index = pos >> PAGE_CACHE_SHIFT; 226379f0be8dSAneesh Kumar K.V 226479f0be8dSAneesh Kumar K.V if (ext4_nonda_switch(inode->i_sb)) { 226579f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 226679f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 226779f0be8dSAneesh Kumar K.V len, flags, pagep, fsdata); 226879f0be8dSAneesh Kumar K.V } 226979f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 22709bffad1eSTheodore Ts'o trace_ext4_da_write_begin(inode, pos, len, flags); 2271d2a17637SMingming Cao retry: 227264769240SAlex Tomas /* 227364769240SAlex Tomas * With delayed allocation, we don't log the i_disksize update 227464769240SAlex Tomas * if there is delayed block allocation. But we still need 227564769240SAlex Tomas * to journalling the i_disksize update if writes to the end 227664769240SAlex Tomas * of file which has an already mapped buffer. 227764769240SAlex Tomas */ 227864769240SAlex Tomas handle = ext4_journal_start(inode, 1); 227964769240SAlex Tomas if (IS_ERR(handle)) { 228064769240SAlex Tomas ret = PTR_ERR(handle); 228164769240SAlex Tomas goto out; 228264769240SAlex Tomas } 2283ebd3610bSJan Kara /* We cannot recurse into the filesystem as the transaction is already 2284ebd3610bSJan Kara * started */ 2285ebd3610bSJan Kara flags |= AOP_FLAG_NOFS; 228664769240SAlex Tomas 228754566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 2288d5a0d4f7SEric Sandeen if (!page) { 2289d5a0d4f7SEric Sandeen ext4_journal_stop(handle); 2290d5a0d4f7SEric Sandeen ret = -ENOMEM; 2291d5a0d4f7SEric Sandeen goto out; 2292d5a0d4f7SEric Sandeen } 229364769240SAlex Tomas *pagep = page; 229464769240SAlex Tomas 22956e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep); 229664769240SAlex Tomas if (ret < 0) { 229764769240SAlex Tomas unlock_page(page); 229864769240SAlex Tomas ext4_journal_stop(handle); 229964769240SAlex Tomas page_cache_release(page); 2300ae4d5372SAneesh Kumar K.V /* 2301ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 2302ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 2303ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 2304ae4d5372SAneesh Kumar K.V */ 2305ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 2306b9a4207dSJan Kara ext4_truncate_failed_write(inode); 230764769240SAlex Tomas } 230864769240SAlex Tomas 2309d2a17637SMingming Cao if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 2310d2a17637SMingming Cao goto retry; 231164769240SAlex Tomas out: 231264769240SAlex Tomas return ret; 231364769240SAlex Tomas } 231464769240SAlex Tomas 2315632eaeabSMingming Cao /* 2316632eaeabSMingming Cao * Check if we should update i_disksize 2317632eaeabSMingming Cao * when write to the end of file but not require block allocation 2318632eaeabSMingming Cao */ 2319632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page, 2320632eaeabSMingming Cao unsigned long offset) 2321632eaeabSMingming Cao { 2322632eaeabSMingming Cao struct buffer_head *bh; 2323632eaeabSMingming Cao struct inode *inode = page->mapping->host; 2324632eaeabSMingming Cao unsigned int idx; 2325632eaeabSMingming Cao int i; 2326632eaeabSMingming Cao 2327632eaeabSMingming Cao bh = page_buffers(page); 2328632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 2329632eaeabSMingming Cao 2330632eaeabSMingming Cao for (i = 0; i < idx; i++) 2331632eaeabSMingming Cao bh = bh->b_this_page; 2332632eaeabSMingming Cao 233329fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 2334632eaeabSMingming Cao return 0; 2335632eaeabSMingming Cao return 1; 2336632eaeabSMingming Cao } 2337632eaeabSMingming Cao 233864769240SAlex Tomas static int ext4_da_write_end(struct file *file, 233964769240SAlex Tomas struct address_space *mapping, 234064769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 234164769240SAlex Tomas struct page *page, void *fsdata) 234264769240SAlex Tomas { 234364769240SAlex Tomas struct inode *inode = mapping->host; 234464769240SAlex Tomas int ret = 0, ret2; 234564769240SAlex Tomas handle_t *handle = ext4_journal_current_handle(); 234664769240SAlex Tomas loff_t new_i_size; 2347632eaeabSMingming Cao unsigned long start, end; 234879f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 234979f0be8dSAneesh Kumar K.V 235079f0be8dSAneesh Kumar K.V if (write_mode == FALL_BACK_TO_NONDELALLOC) { 235179f0be8dSAneesh Kumar K.V if (ext4_should_order_data(inode)) { 235279f0be8dSAneesh Kumar K.V return ext4_ordered_write_end(file, mapping, pos, 235379f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 235479f0be8dSAneesh Kumar K.V } else if (ext4_should_writeback_data(inode)) { 235579f0be8dSAneesh Kumar K.V return ext4_writeback_write_end(file, mapping, pos, 235679f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 235779f0be8dSAneesh Kumar K.V } else { 235879f0be8dSAneesh Kumar K.V BUG(); 235979f0be8dSAneesh Kumar K.V } 236079f0be8dSAneesh Kumar K.V } 2361632eaeabSMingming Cao 23629bffad1eSTheodore Ts'o trace_ext4_da_write_end(inode, pos, len, copied); 2363632eaeabSMingming Cao start = pos & (PAGE_CACHE_SIZE - 1); 2364632eaeabSMingming Cao end = start + copied - 1; 236564769240SAlex Tomas 236664769240SAlex Tomas /* 236764769240SAlex Tomas * generic_write_end() will run mark_inode_dirty() if i_size 236864769240SAlex Tomas * changes. So let's piggyback the i_disksize mark_inode_dirty 236964769240SAlex Tomas * into that. 237064769240SAlex Tomas */ 237164769240SAlex Tomas 237264769240SAlex Tomas new_i_size = pos + copied; 2373632eaeabSMingming Cao if (new_i_size > EXT4_I(inode)->i_disksize) { 2374632eaeabSMingming Cao if (ext4_da_should_update_i_disksize(page, end)) { 2375632eaeabSMingming Cao down_write(&EXT4_I(inode)->i_data_sem); 2376632eaeabSMingming Cao if (new_i_size > EXT4_I(inode)->i_disksize) { 237764769240SAlex Tomas /* 2378632eaeabSMingming Cao * Updating i_disksize when extending file 2379632eaeabSMingming Cao * without needing block allocation 238064769240SAlex Tomas */ 238164769240SAlex Tomas if (ext4_should_order_data(inode)) 2382632eaeabSMingming Cao ret = ext4_jbd2_file_inode(handle, 2383632eaeabSMingming Cao inode); 238464769240SAlex Tomas 238564769240SAlex Tomas EXT4_I(inode)->i_disksize = new_i_size; 238664769240SAlex Tomas } 2387632eaeabSMingming Cao up_write(&EXT4_I(inode)->i_data_sem); 2388cf17fea6SAneesh Kumar K.V /* We need to mark inode dirty even if 2389cf17fea6SAneesh Kumar K.V * new_i_size is less that inode->i_size 2390cf17fea6SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 2391cf17fea6SAneesh Kumar K.V */ 2392cf17fea6SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 2393632eaeabSMingming Cao } 2394632eaeabSMingming Cao } 239564769240SAlex Tomas ret2 = generic_write_end(file, mapping, pos, len, copied, 239664769240SAlex Tomas page, fsdata); 239764769240SAlex Tomas copied = ret2; 239864769240SAlex Tomas if (ret2 < 0) 239964769240SAlex Tomas ret = ret2; 240064769240SAlex Tomas ret2 = ext4_journal_stop(handle); 240164769240SAlex Tomas if (!ret) 240264769240SAlex Tomas ret = ret2; 240364769240SAlex Tomas 240464769240SAlex Tomas return ret ? ret : copied; 240564769240SAlex Tomas } 240664769240SAlex Tomas 240764769240SAlex Tomas static void ext4_da_invalidatepage(struct page *page, unsigned long offset) 240864769240SAlex Tomas { 240964769240SAlex Tomas /* 241064769240SAlex Tomas * Drop reserved blocks 241164769240SAlex Tomas */ 241264769240SAlex Tomas BUG_ON(!PageLocked(page)); 241364769240SAlex Tomas if (!page_has_buffers(page)) 241464769240SAlex Tomas goto out; 241564769240SAlex Tomas 2416d2a17637SMingming Cao ext4_da_page_release_reservation(page, offset); 241764769240SAlex Tomas 241864769240SAlex Tomas out: 241964769240SAlex Tomas ext4_invalidatepage(page, offset); 242064769240SAlex Tomas 242164769240SAlex Tomas return; 242264769240SAlex Tomas } 242364769240SAlex Tomas 2424ccd2506bSTheodore Ts'o /* 2425ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 2426ccd2506bSTheodore Ts'o */ 2427ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 2428ccd2506bSTheodore Ts'o { 2429fb40ba0dSTheodore Ts'o trace_ext4_alloc_da_blocks(inode); 2430fb40ba0dSTheodore Ts'o 2431ccd2506bSTheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks && 2432ccd2506bSTheodore Ts'o !EXT4_I(inode)->i_reserved_meta_blocks) 2433ccd2506bSTheodore Ts'o return 0; 2434ccd2506bSTheodore Ts'o 2435ccd2506bSTheodore Ts'o /* 2436ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 2437ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 2438ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 2439ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 2440ccd2506bSTheodore Ts'o * would require replicating code paths in: 2441ccd2506bSTheodore Ts'o * 2442ccd2506bSTheodore Ts'o * ext4_da_writepages() -> 2443ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 2444ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 2445ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 2446ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 2447ccd2506bSTheodore Ts'o * 2448ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 2449ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 2450ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 2451ccd2506bSTheodore Ts'o * doing I/O at all. 2452ccd2506bSTheodore Ts'o * 2453ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 2454380cf090SWu Fengguang * the pages by calling redirty_page_for_writepage() but that 2455ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 2456ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 245725985edcSLucas De Marchi * simplifying them because we wouldn't actually intend to 2458ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 2459ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 2460ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 2461ccd2506bSTheodore Ts'o * 2462ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 2463ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 2464ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 2465ccd2506bSTheodore Ts'o */ 2466ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 2467ccd2506bSTheodore Ts'o } 246864769240SAlex Tomas 246964769240SAlex Tomas /* 2470ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 2471ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 2472ac27a0ecSDave Kleikamp * 2473ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 2474617ba13bSMingming Cao * journal. If somebody makes a swapfile on an ext4 data-journaling 2475ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 2476ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 2477ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 2478ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 2479ac27a0ecSDave Kleikamp * 2480ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 2481ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 2482ac27a0ecSDave Kleikamp */ 2483617ba13bSMingming Cao static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 2484ac27a0ecSDave Kleikamp { 2485ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 2486ac27a0ecSDave Kleikamp journal_t *journal; 2487ac27a0ecSDave Kleikamp int err; 2488ac27a0ecSDave Kleikamp 248964769240SAlex Tomas if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 249064769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) { 249164769240SAlex Tomas /* 249264769240SAlex Tomas * With delalloc we want to sync the file 249364769240SAlex Tomas * so that we can make sure we allocate 249464769240SAlex Tomas * blocks for file 249564769240SAlex Tomas */ 249664769240SAlex Tomas filemap_write_and_wait(mapping); 249764769240SAlex Tomas } 249864769240SAlex Tomas 249919f5fb7aSTheodore Ts'o if (EXT4_JOURNAL(inode) && 250019f5fb7aSTheodore Ts'o ext4_test_inode_state(inode, EXT4_STATE_JDATA)) { 2501ac27a0ecSDave Kleikamp /* 2502ac27a0ecSDave Kleikamp * This is a REALLY heavyweight approach, but the use of 2503ac27a0ecSDave Kleikamp * bmap on dirty files is expected to be extremely rare: 2504ac27a0ecSDave Kleikamp * only if we run lilo or swapon on a freshly made file 2505ac27a0ecSDave Kleikamp * do we expect this to happen. 2506ac27a0ecSDave Kleikamp * 2507ac27a0ecSDave Kleikamp * (bmap requires CAP_SYS_RAWIO so this does not 2508ac27a0ecSDave Kleikamp * represent an unprivileged user DOS attack --- we'd be 2509ac27a0ecSDave Kleikamp * in trouble if mortal users could trigger this path at 2510ac27a0ecSDave Kleikamp * will.) 2511ac27a0ecSDave Kleikamp * 2512617ba13bSMingming Cao * NB. EXT4_STATE_JDATA is not set on files other than 2513ac27a0ecSDave Kleikamp * regular files. If somebody wants to bmap a directory 2514ac27a0ecSDave Kleikamp * or symlink and gets confused because the buffer 2515ac27a0ecSDave Kleikamp * hasn't yet been flushed to disk, they deserve 2516ac27a0ecSDave Kleikamp * everything they get. 2517ac27a0ecSDave Kleikamp */ 2518ac27a0ecSDave Kleikamp 251919f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_JDATA); 2520617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 2521dab291afSMingming Cao jbd2_journal_lock_updates(journal); 2522dab291afSMingming Cao err = jbd2_journal_flush(journal); 2523dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 2524ac27a0ecSDave Kleikamp 2525ac27a0ecSDave Kleikamp if (err) 2526ac27a0ecSDave Kleikamp return 0; 2527ac27a0ecSDave Kleikamp } 2528ac27a0ecSDave Kleikamp 2529617ba13bSMingming Cao return generic_block_bmap(mapping, block, ext4_get_block); 2530ac27a0ecSDave Kleikamp } 2531ac27a0ecSDave Kleikamp 2532617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page) 2533ac27a0ecSDave Kleikamp { 25340562e0baSJiaying Zhang trace_ext4_readpage(page); 2535617ba13bSMingming Cao return mpage_readpage(page, ext4_get_block); 2536ac27a0ecSDave Kleikamp } 2537ac27a0ecSDave Kleikamp 2538ac27a0ecSDave Kleikamp static int 2539617ba13bSMingming Cao ext4_readpages(struct file *file, struct address_space *mapping, 2540ac27a0ecSDave Kleikamp struct list_head *pages, unsigned nr_pages) 2541ac27a0ecSDave Kleikamp { 2542617ba13bSMingming Cao return mpage_readpages(mapping, pages, nr_pages, ext4_get_block); 2543ac27a0ecSDave Kleikamp } 2544ac27a0ecSDave Kleikamp 2545744692dcSJiaying Zhang static void ext4_invalidatepage_free_endio(struct page *page, unsigned long offset) 2546744692dcSJiaying Zhang { 2547744692dcSJiaying Zhang struct buffer_head *head, *bh; 2548744692dcSJiaying Zhang unsigned int curr_off = 0; 2549744692dcSJiaying Zhang 2550744692dcSJiaying Zhang if (!page_has_buffers(page)) 2551744692dcSJiaying Zhang return; 2552744692dcSJiaying Zhang head = bh = page_buffers(page); 2553744692dcSJiaying Zhang do { 2554744692dcSJiaying Zhang if (offset <= curr_off && test_clear_buffer_uninit(bh) 2555744692dcSJiaying Zhang && bh->b_private) { 2556744692dcSJiaying Zhang ext4_free_io_end(bh->b_private); 2557744692dcSJiaying Zhang bh->b_private = NULL; 2558744692dcSJiaying Zhang bh->b_end_io = NULL; 2559744692dcSJiaying Zhang } 2560744692dcSJiaying Zhang curr_off = curr_off + bh->b_size; 2561744692dcSJiaying Zhang bh = bh->b_this_page; 2562744692dcSJiaying Zhang } while (bh != head); 2563744692dcSJiaying Zhang } 2564744692dcSJiaying Zhang 2565617ba13bSMingming Cao static void ext4_invalidatepage(struct page *page, unsigned long offset) 2566ac27a0ecSDave Kleikamp { 2567617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 2568ac27a0ecSDave Kleikamp 25690562e0baSJiaying Zhang trace_ext4_invalidatepage(page, offset); 25700562e0baSJiaying Zhang 2571ac27a0ecSDave Kleikamp /* 2572744692dcSJiaying Zhang * free any io_end structure allocated for buffers to be discarded 2573744692dcSJiaying Zhang */ 2574744692dcSJiaying Zhang if (ext4_should_dioread_nolock(page->mapping->host)) 2575744692dcSJiaying Zhang ext4_invalidatepage_free_endio(page, offset); 2576744692dcSJiaying Zhang /* 2577ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 2578ac27a0ecSDave Kleikamp */ 2579ac27a0ecSDave Kleikamp if (offset == 0) 2580ac27a0ecSDave Kleikamp ClearPageChecked(page); 2581ac27a0ecSDave Kleikamp 25820390131bSFrank Mayhar if (journal) 2583dab291afSMingming Cao jbd2_journal_invalidatepage(journal, page, offset); 25840390131bSFrank Mayhar else 25850390131bSFrank Mayhar block_invalidatepage(page, offset); 2586ac27a0ecSDave Kleikamp } 2587ac27a0ecSDave Kleikamp 2588617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait) 2589ac27a0ecSDave Kleikamp { 2590617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 2591ac27a0ecSDave Kleikamp 25920562e0baSJiaying Zhang trace_ext4_releasepage(page); 25930562e0baSJiaying Zhang 2594ac27a0ecSDave Kleikamp WARN_ON(PageChecked(page)); 2595ac27a0ecSDave Kleikamp if (!page_has_buffers(page)) 2596ac27a0ecSDave Kleikamp return 0; 25970390131bSFrank Mayhar if (journal) 2598dab291afSMingming Cao return jbd2_journal_try_to_free_buffers(journal, page, wait); 25990390131bSFrank Mayhar else 26000390131bSFrank Mayhar return try_to_free_buffers(page); 2601ac27a0ecSDave Kleikamp } 2602ac27a0ecSDave Kleikamp 2603ac27a0ecSDave Kleikamp /* 26042ed88685STheodore Ts'o * ext4_get_block used when preparing for a DIO write or buffer write. 26052ed88685STheodore Ts'o * We allocate an uinitialized extent if blocks haven't been allocated. 26062ed88685STheodore Ts'o * The extent will be converted to initialized after the IO is complete. 26072ed88685STheodore Ts'o */ 2608c7064ef1SJiaying Zhang static int ext4_get_block_write(struct inode *inode, sector_t iblock, 26094c0425ffSMingming Cao struct buffer_head *bh_result, int create) 26104c0425ffSMingming Cao { 2611c7064ef1SJiaying Zhang ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n", 26128d5d02e6SMingming Cao inode->i_ino, create); 26132ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh_result, 26142ed88685STheodore Ts'o EXT4_GET_BLOCKS_IO_CREATE_EXT); 26154c0425ffSMingming Cao } 26164c0425ffSMingming Cao 26174c0425ffSMingming Cao static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset, 2618552ef802SChristoph Hellwig ssize_t size, void *private, int ret, 2619552ef802SChristoph Hellwig bool is_async) 26204c0425ffSMingming Cao { 262172c5052dSChristoph Hellwig struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode; 26224c0425ffSMingming Cao ext4_io_end_t *io_end = iocb->private; 26234c0425ffSMingming Cao struct workqueue_struct *wq; 2624744692dcSJiaying Zhang unsigned long flags; 2625744692dcSJiaying Zhang struct ext4_inode_info *ei; 26264c0425ffSMingming Cao 26274b70df18SMingming /* if not async direct IO or dio with 0 bytes write, just return */ 26284b70df18SMingming if (!io_end || !size) 2629552ef802SChristoph Hellwig goto out; 26304b70df18SMingming 26318d5d02e6SMingming Cao ext_debug("ext4_end_io_dio(): io_end 0x%p" 26328d5d02e6SMingming Cao "for inode %lu, iocb 0x%p, offset %llu, size %llu\n", 26338d5d02e6SMingming Cao iocb->private, io_end->inode->i_ino, iocb, offset, 26348d5d02e6SMingming Cao size); 26358d5d02e6SMingming Cao 26368d5d02e6SMingming Cao /* if not aio dio with unwritten extents, just free io and return */ 2637bd2d0210STheodore Ts'o if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) { 26388d5d02e6SMingming Cao ext4_free_io_end(io_end); 26398d5d02e6SMingming Cao iocb->private = NULL; 26405b3ff237Sjiayingz@google.com (Jiaying Zhang) out: 26415b3ff237Sjiayingz@google.com (Jiaying Zhang) if (is_async) 26425b3ff237Sjiayingz@google.com (Jiaying Zhang) aio_complete(iocb, ret, 0); 264372c5052dSChristoph Hellwig inode_dio_done(inode); 26445b3ff237Sjiayingz@google.com (Jiaying Zhang) return; 26458d5d02e6SMingming Cao } 26468d5d02e6SMingming Cao 26474c0425ffSMingming Cao io_end->offset = offset; 26484c0425ffSMingming Cao io_end->size = size; 26495b3ff237Sjiayingz@google.com (Jiaying Zhang) if (is_async) { 26505b3ff237Sjiayingz@google.com (Jiaying Zhang) io_end->iocb = iocb; 26515b3ff237Sjiayingz@google.com (Jiaying Zhang) io_end->result = ret; 26525b3ff237Sjiayingz@google.com (Jiaying Zhang) } 26534c0425ffSMingming Cao wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq; 26544c0425ffSMingming Cao 26558d5d02e6SMingming Cao /* Add the io_end to per-inode completed aio dio list*/ 2656744692dcSJiaying Zhang ei = EXT4_I(io_end->inode); 2657744692dcSJiaying Zhang spin_lock_irqsave(&ei->i_completed_io_lock, flags); 2658744692dcSJiaying Zhang list_add_tail(&io_end->list, &ei->i_completed_io_list); 2659744692dcSJiaying Zhang spin_unlock_irqrestore(&ei->i_completed_io_lock, flags); 2660c999af2bSEric Sandeen 2661c999af2bSEric Sandeen /* queue the work to convert unwritten extents to written */ 2662c999af2bSEric Sandeen queue_work(wq, &io_end->work); 26634c0425ffSMingming Cao iocb->private = NULL; 266472c5052dSChristoph Hellwig 266572c5052dSChristoph Hellwig /* XXX: probably should move into the real I/O completion handler */ 266672c5052dSChristoph Hellwig inode_dio_done(inode); 26674c0425ffSMingming Cao } 2668c7064ef1SJiaying Zhang 2669744692dcSJiaying Zhang static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate) 2670744692dcSJiaying Zhang { 2671744692dcSJiaying Zhang ext4_io_end_t *io_end = bh->b_private; 2672744692dcSJiaying Zhang struct workqueue_struct *wq; 2673744692dcSJiaying Zhang struct inode *inode; 2674744692dcSJiaying Zhang unsigned long flags; 2675744692dcSJiaying Zhang 2676744692dcSJiaying Zhang if (!test_clear_buffer_uninit(bh) || !io_end) 2677744692dcSJiaying Zhang goto out; 2678744692dcSJiaying Zhang 2679744692dcSJiaying Zhang if (!(io_end->inode->i_sb->s_flags & MS_ACTIVE)) { 2680744692dcSJiaying Zhang printk("sb umounted, discard end_io request for inode %lu\n", 2681744692dcSJiaying Zhang io_end->inode->i_ino); 2682744692dcSJiaying Zhang ext4_free_io_end(io_end); 2683744692dcSJiaying Zhang goto out; 2684744692dcSJiaying Zhang } 2685744692dcSJiaying Zhang 268632c80b32STao Ma /* 268732c80b32STao Ma * It may be over-defensive here to check EXT4_IO_END_UNWRITTEN now, 268832c80b32STao Ma * but being more careful is always safe for the future change. 268932c80b32STao Ma */ 2690744692dcSJiaying Zhang inode = io_end->inode; 269132c80b32STao Ma if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) { 269232c80b32STao Ma io_end->flag |= EXT4_IO_END_UNWRITTEN; 269332c80b32STao Ma atomic_inc(&EXT4_I(inode)->i_aiodio_unwritten); 269432c80b32STao Ma } 2695744692dcSJiaying Zhang 2696744692dcSJiaying Zhang /* Add the io_end to per-inode completed io list*/ 2697744692dcSJiaying Zhang spin_lock_irqsave(&EXT4_I(inode)->i_completed_io_lock, flags); 2698744692dcSJiaying Zhang list_add_tail(&io_end->list, &EXT4_I(inode)->i_completed_io_list); 2699744692dcSJiaying Zhang spin_unlock_irqrestore(&EXT4_I(inode)->i_completed_io_lock, flags); 2700744692dcSJiaying Zhang 2701744692dcSJiaying Zhang wq = EXT4_SB(inode->i_sb)->dio_unwritten_wq; 2702744692dcSJiaying Zhang /* queue the work to convert unwritten extents to written */ 2703744692dcSJiaying Zhang queue_work(wq, &io_end->work); 2704744692dcSJiaying Zhang out: 2705744692dcSJiaying Zhang bh->b_private = NULL; 2706744692dcSJiaying Zhang bh->b_end_io = NULL; 2707744692dcSJiaying Zhang clear_buffer_uninit(bh); 2708744692dcSJiaying Zhang end_buffer_async_write(bh, uptodate); 2709744692dcSJiaying Zhang } 2710744692dcSJiaying Zhang 2711744692dcSJiaying Zhang static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode) 2712744692dcSJiaying Zhang { 2713744692dcSJiaying Zhang ext4_io_end_t *io_end; 2714744692dcSJiaying Zhang struct page *page = bh->b_page; 2715744692dcSJiaying Zhang loff_t offset = (sector_t)page->index << PAGE_CACHE_SHIFT; 2716744692dcSJiaying Zhang size_t size = bh->b_size; 2717744692dcSJiaying Zhang 2718744692dcSJiaying Zhang retry: 2719744692dcSJiaying Zhang io_end = ext4_init_io_end(inode, GFP_ATOMIC); 2720744692dcSJiaying Zhang if (!io_end) { 27216db26ffcSAndrew Morton pr_warn_ratelimited("%s: allocation fail\n", __func__); 2722744692dcSJiaying Zhang schedule(); 2723744692dcSJiaying Zhang goto retry; 2724744692dcSJiaying Zhang } 2725744692dcSJiaying Zhang io_end->offset = offset; 2726744692dcSJiaying Zhang io_end->size = size; 2727744692dcSJiaying Zhang /* 2728744692dcSJiaying Zhang * We need to hold a reference to the page to make sure it 2729744692dcSJiaying Zhang * doesn't get evicted before ext4_end_io_work() has a chance 2730744692dcSJiaying Zhang * to convert the extent from written to unwritten. 2731744692dcSJiaying Zhang */ 2732744692dcSJiaying Zhang io_end->page = page; 2733744692dcSJiaying Zhang get_page(io_end->page); 2734744692dcSJiaying Zhang 2735744692dcSJiaying Zhang bh->b_private = io_end; 2736744692dcSJiaying Zhang bh->b_end_io = ext4_end_io_buffer_write; 2737744692dcSJiaying Zhang return 0; 2738744692dcSJiaying Zhang } 2739744692dcSJiaying Zhang 27404c0425ffSMingming Cao /* 27414c0425ffSMingming Cao * For ext4 extent files, ext4 will do direct-io write to holes, 27424c0425ffSMingming Cao * preallocated extents, and those write extend the file, no need to 27434c0425ffSMingming Cao * fall back to buffered IO. 27444c0425ffSMingming Cao * 2745b595076aSUwe Kleine-König * For holes, we fallocate those blocks, mark them as uninitialized 27464c0425ffSMingming Cao * If those blocks were preallocated, we mark sure they are splited, but 2747b595076aSUwe Kleine-König * still keep the range to write as uninitialized. 27484c0425ffSMingming Cao * 27498d5d02e6SMingming Cao * The unwrritten extents will be converted to written when DIO is completed. 27508d5d02e6SMingming Cao * For async direct IO, since the IO may still pending when return, we 275125985edcSLucas De Marchi * set up an end_io call back function, which will do the conversion 27528d5d02e6SMingming Cao * when async direct IO completed. 27534c0425ffSMingming Cao * 27544c0425ffSMingming Cao * If the O_DIRECT write will extend the file then add this inode to the 27554c0425ffSMingming Cao * orphan list. So recovery will truncate it back to the original size 27564c0425ffSMingming Cao * if the machine crashes during the write. 27574c0425ffSMingming Cao * 27584c0425ffSMingming Cao */ 27594c0425ffSMingming Cao static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb, 27604c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 27614c0425ffSMingming Cao unsigned long nr_segs) 27624c0425ffSMingming Cao { 27634c0425ffSMingming Cao struct file *file = iocb->ki_filp; 27644c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 27654c0425ffSMingming Cao ssize_t ret; 27664c0425ffSMingming Cao size_t count = iov_length(iov, nr_segs); 27674c0425ffSMingming Cao 27684c0425ffSMingming Cao loff_t final_size = offset + count; 27694c0425ffSMingming Cao if (rw == WRITE && final_size <= inode->i_size) { 27704c0425ffSMingming Cao /* 27718d5d02e6SMingming Cao * We could direct write to holes and fallocate. 27728d5d02e6SMingming Cao * 27738d5d02e6SMingming Cao * Allocated blocks to fill the hole are marked as uninitialized 277425985edcSLucas De Marchi * to prevent parallel buffered read to expose the stale data 27754c0425ffSMingming Cao * before DIO complete the data IO. 27768d5d02e6SMingming Cao * 27778d5d02e6SMingming Cao * As to previously fallocated extents, ext4 get_block 27784c0425ffSMingming Cao * will just simply mark the buffer mapped but still 27794c0425ffSMingming Cao * keep the extents uninitialized. 27804c0425ffSMingming Cao * 27818d5d02e6SMingming Cao * for non AIO case, we will convert those unwritten extents 27828d5d02e6SMingming Cao * to written after return back from blockdev_direct_IO. 27834c0425ffSMingming Cao * 27848d5d02e6SMingming Cao * for async DIO, the conversion needs to be defered when 27858d5d02e6SMingming Cao * the IO is completed. The ext4 end_io callback function 27868d5d02e6SMingming Cao * will be called to take care of the conversion work. 27878d5d02e6SMingming Cao * Here for async case, we allocate an io_end structure to 27888d5d02e6SMingming Cao * hook to the iocb. 27894c0425ffSMingming Cao */ 27908d5d02e6SMingming Cao iocb->private = NULL; 27918d5d02e6SMingming Cao EXT4_I(inode)->cur_aio_dio = NULL; 27928d5d02e6SMingming Cao if (!is_sync_kiocb(iocb)) { 2793744692dcSJiaying Zhang iocb->private = ext4_init_io_end(inode, GFP_NOFS); 27944c0425ffSMingming Cao if (!iocb->private) 27954c0425ffSMingming Cao return -ENOMEM; 27968d5d02e6SMingming Cao /* 27978d5d02e6SMingming Cao * we save the io structure for current async 279879e83036SEric Sandeen * direct IO, so that later ext4_map_blocks() 27998d5d02e6SMingming Cao * could flag the io structure whether there 28008d5d02e6SMingming Cao * is a unwritten extents needs to be converted 28018d5d02e6SMingming Cao * when IO is completed. 28028d5d02e6SMingming Cao */ 28038d5d02e6SMingming Cao EXT4_I(inode)->cur_aio_dio = iocb->private; 28048d5d02e6SMingming Cao } 28058d5d02e6SMingming Cao 2806aacfc19cSChristoph Hellwig ret = __blockdev_direct_IO(rw, iocb, inode, 28074c0425ffSMingming Cao inode->i_sb->s_bdev, iov, 28084c0425ffSMingming Cao offset, nr_segs, 2809c7064ef1SJiaying Zhang ext4_get_block_write, 2810aacfc19cSChristoph Hellwig ext4_end_io_dio, 2811aacfc19cSChristoph Hellwig NULL, 2812aacfc19cSChristoph Hellwig DIO_LOCKING | DIO_SKIP_HOLES); 28138d5d02e6SMingming Cao if (iocb->private) 28148d5d02e6SMingming Cao EXT4_I(inode)->cur_aio_dio = NULL; 28158d5d02e6SMingming Cao /* 28168d5d02e6SMingming Cao * The io_end structure takes a reference to the inode, 28178d5d02e6SMingming Cao * that structure needs to be destroyed and the 28188d5d02e6SMingming Cao * reference to the inode need to be dropped, when IO is 28198d5d02e6SMingming Cao * complete, even with 0 byte write, or failed. 28208d5d02e6SMingming Cao * 28218d5d02e6SMingming Cao * In the successful AIO DIO case, the io_end structure will be 28228d5d02e6SMingming Cao * desctroyed and the reference to the inode will be dropped 28238d5d02e6SMingming Cao * after the end_io call back function is called. 28248d5d02e6SMingming Cao * 28258d5d02e6SMingming Cao * In the case there is 0 byte write, or error case, since 28268d5d02e6SMingming Cao * VFS direct IO won't invoke the end_io call back function, 28278d5d02e6SMingming Cao * we need to free the end_io structure here. 28288d5d02e6SMingming Cao */ 28298d5d02e6SMingming Cao if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) { 28308d5d02e6SMingming Cao ext4_free_io_end(iocb->private); 28318d5d02e6SMingming Cao iocb->private = NULL; 283219f5fb7aSTheodore Ts'o } else if (ret > 0 && ext4_test_inode_state(inode, 28335f524950SMingming EXT4_STATE_DIO_UNWRITTEN)) { 2834109f5565SMingming int err; 28358d5d02e6SMingming Cao /* 28368d5d02e6SMingming Cao * for non AIO case, since the IO is already 283725985edcSLucas De Marchi * completed, we could do the conversion right here 28388d5d02e6SMingming Cao */ 2839109f5565SMingming err = ext4_convert_unwritten_extents(inode, 28408d5d02e6SMingming Cao offset, ret); 2841109f5565SMingming if (err < 0) 2842109f5565SMingming ret = err; 284319f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN); 2844109f5565SMingming } 28454c0425ffSMingming Cao return ret; 28464c0425ffSMingming Cao } 28478d5d02e6SMingming Cao 28488d5d02e6SMingming Cao /* for write the the end of file case, we fall back to old way */ 28494c0425ffSMingming Cao return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 28504c0425ffSMingming Cao } 28514c0425ffSMingming Cao 28524c0425ffSMingming Cao static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb, 28534c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 28544c0425ffSMingming Cao unsigned long nr_segs) 28554c0425ffSMingming Cao { 28564c0425ffSMingming Cao struct file *file = iocb->ki_filp; 28574c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 28580562e0baSJiaying Zhang ssize_t ret; 28594c0425ffSMingming Cao 28600562e0baSJiaying Zhang trace_ext4_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw); 286112e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 28620562e0baSJiaying Zhang ret = ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs); 28630562e0baSJiaying Zhang else 28640562e0baSJiaying Zhang ret = ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 28650562e0baSJiaying Zhang trace_ext4_direct_IO_exit(inode, offset, 28660562e0baSJiaying Zhang iov_length(iov, nr_segs), rw, ret); 28670562e0baSJiaying Zhang return ret; 28684c0425ffSMingming Cao } 28694c0425ffSMingming Cao 2870ac27a0ecSDave Kleikamp /* 2871617ba13bSMingming Cao * Pages can be marked dirty completely asynchronously from ext4's journalling 2872ac27a0ecSDave Kleikamp * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do 2873ac27a0ecSDave Kleikamp * much here because ->set_page_dirty is called under VFS locks. The page is 2874ac27a0ecSDave Kleikamp * not necessarily locked. 2875ac27a0ecSDave Kleikamp * 2876ac27a0ecSDave Kleikamp * We cannot just dirty the page and leave attached buffers clean, because the 2877ac27a0ecSDave Kleikamp * buffers' dirty state is "definitive". We cannot just set the buffers dirty 2878ac27a0ecSDave Kleikamp * or jbddirty because all the journalling code will explode. 2879ac27a0ecSDave Kleikamp * 2880ac27a0ecSDave Kleikamp * So what we do is to mark the page "pending dirty" and next time writepage 2881ac27a0ecSDave Kleikamp * is called, propagate that into the buffers appropriately. 2882ac27a0ecSDave Kleikamp */ 2883617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page) 2884ac27a0ecSDave Kleikamp { 2885ac27a0ecSDave Kleikamp SetPageChecked(page); 2886ac27a0ecSDave Kleikamp return __set_page_dirty_nobuffers(page); 2887ac27a0ecSDave Kleikamp } 2888ac27a0ecSDave Kleikamp 2889617ba13bSMingming Cao static const struct address_space_operations ext4_ordered_aops = { 2890617ba13bSMingming Cao .readpage = ext4_readpage, 2891617ba13bSMingming Cao .readpages = ext4_readpages, 289243ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 2893bfc1af65SNick Piggin .write_begin = ext4_write_begin, 2894bfc1af65SNick Piggin .write_end = ext4_ordered_write_end, 2895617ba13bSMingming Cao .bmap = ext4_bmap, 2896617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 2897617ba13bSMingming Cao .releasepage = ext4_releasepage, 2898617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 2899ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 29008ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 2901aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 2902ac27a0ecSDave Kleikamp }; 2903ac27a0ecSDave Kleikamp 2904617ba13bSMingming Cao static const struct address_space_operations ext4_writeback_aops = { 2905617ba13bSMingming Cao .readpage = ext4_readpage, 2906617ba13bSMingming Cao .readpages = ext4_readpages, 290743ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 2908bfc1af65SNick Piggin .write_begin = ext4_write_begin, 2909bfc1af65SNick Piggin .write_end = ext4_writeback_write_end, 2910617ba13bSMingming Cao .bmap = ext4_bmap, 2911617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 2912617ba13bSMingming Cao .releasepage = ext4_releasepage, 2913617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 2914ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 29158ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 2916aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 2917ac27a0ecSDave Kleikamp }; 2918ac27a0ecSDave Kleikamp 2919617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 2920617ba13bSMingming Cao .readpage = ext4_readpage, 2921617ba13bSMingming Cao .readpages = ext4_readpages, 292243ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 2923bfc1af65SNick Piggin .write_begin = ext4_write_begin, 2924bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 2925617ba13bSMingming Cao .set_page_dirty = ext4_journalled_set_page_dirty, 2926617ba13bSMingming Cao .bmap = ext4_bmap, 2927617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 2928617ba13bSMingming Cao .releasepage = ext4_releasepage, 29298ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 2930aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 2931ac27a0ecSDave Kleikamp }; 2932ac27a0ecSDave Kleikamp 293364769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 293464769240SAlex Tomas .readpage = ext4_readpage, 293564769240SAlex Tomas .readpages = ext4_readpages, 293643ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 293764769240SAlex Tomas .writepages = ext4_da_writepages, 293864769240SAlex Tomas .write_begin = ext4_da_write_begin, 293964769240SAlex Tomas .write_end = ext4_da_write_end, 294064769240SAlex Tomas .bmap = ext4_bmap, 294164769240SAlex Tomas .invalidatepage = ext4_da_invalidatepage, 294264769240SAlex Tomas .releasepage = ext4_releasepage, 294364769240SAlex Tomas .direct_IO = ext4_direct_IO, 294464769240SAlex Tomas .migratepage = buffer_migrate_page, 29458ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 2946aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 294764769240SAlex Tomas }; 294864769240SAlex Tomas 2949617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 2950ac27a0ecSDave Kleikamp { 2951cd1aac32SAneesh Kumar K.V if (ext4_should_order_data(inode) && 2952cd1aac32SAneesh Kumar K.V test_opt(inode->i_sb, DELALLOC)) 2953cd1aac32SAneesh Kumar K.V inode->i_mapping->a_ops = &ext4_da_aops; 2954cd1aac32SAneesh Kumar K.V else if (ext4_should_order_data(inode)) 2955617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_ordered_aops; 295664769240SAlex Tomas else if (ext4_should_writeback_data(inode) && 295764769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) 295864769240SAlex Tomas inode->i_mapping->a_ops = &ext4_da_aops; 2959617ba13bSMingming Cao else if (ext4_should_writeback_data(inode)) 2960617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_writeback_aops; 2961ac27a0ecSDave Kleikamp else 2962617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 2963ac27a0ecSDave Kleikamp } 2964ac27a0ecSDave Kleikamp 2965ac27a0ecSDave Kleikamp /* 2966617ba13bSMingming Cao * ext4_block_truncate_page() zeroes out a mapping from file offset `from' 2967ac27a0ecSDave Kleikamp * up to the end of the block which corresponds to `from'. 2968ac27a0ecSDave Kleikamp * This required during truncate. We need to physically zero the tail end 2969ac27a0ecSDave Kleikamp * of that block so it doesn't yield old data if the file is later grown. 2970ac27a0ecSDave Kleikamp */ 2971cf108bcaSJan Kara int ext4_block_truncate_page(handle_t *handle, 2972ac27a0ecSDave Kleikamp struct address_space *mapping, loff_t from) 2973ac27a0ecSDave Kleikamp { 297430848851SAllison Henderson unsigned offset = from & (PAGE_CACHE_SIZE-1); 297530848851SAllison Henderson unsigned length; 297630848851SAllison Henderson unsigned blocksize; 297730848851SAllison Henderson struct inode *inode = mapping->host; 297830848851SAllison Henderson 297930848851SAllison Henderson blocksize = inode->i_sb->s_blocksize; 298030848851SAllison Henderson length = blocksize - (offset & (blocksize - 1)); 298130848851SAllison Henderson 298230848851SAllison Henderson return ext4_block_zero_page_range(handle, mapping, from, length); 298330848851SAllison Henderson } 298430848851SAllison Henderson 298530848851SAllison Henderson /* 298630848851SAllison Henderson * ext4_block_zero_page_range() zeros out a mapping of length 'length' 298730848851SAllison Henderson * starting from file offset 'from'. The range to be zero'd must 298830848851SAllison Henderson * be contained with in one block. If the specified range exceeds 298930848851SAllison Henderson * the end of the block it will be shortened to end of the block 299030848851SAllison Henderson * that cooresponds to 'from' 299130848851SAllison Henderson */ 299230848851SAllison Henderson int ext4_block_zero_page_range(handle_t *handle, 299330848851SAllison Henderson struct address_space *mapping, loff_t from, loff_t length) 299430848851SAllison Henderson { 2995617ba13bSMingming Cao ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; 2996ac27a0ecSDave Kleikamp unsigned offset = from & (PAGE_CACHE_SIZE-1); 299730848851SAllison Henderson unsigned blocksize, max, pos; 2998725d26d3SAneesh Kumar K.V ext4_lblk_t iblock; 2999ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 3000ac27a0ecSDave Kleikamp struct buffer_head *bh; 3001cf108bcaSJan Kara struct page *page; 3002ac27a0ecSDave Kleikamp int err = 0; 3003ac27a0ecSDave Kleikamp 3004f4a01017STheodore Ts'o page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT, 3005f4a01017STheodore Ts'o mapping_gfp_mask(mapping) & ~__GFP_FS); 3006cf108bcaSJan Kara if (!page) 3007cf108bcaSJan Kara return -EINVAL; 3008cf108bcaSJan Kara 3009ac27a0ecSDave Kleikamp blocksize = inode->i_sb->s_blocksize; 301030848851SAllison Henderson max = blocksize - (offset & (blocksize - 1)); 301130848851SAllison Henderson 301230848851SAllison Henderson /* 301330848851SAllison Henderson * correct length if it does not fall between 301430848851SAllison Henderson * 'from' and the end of the block 301530848851SAllison Henderson */ 301630848851SAllison Henderson if (length > max || length < 0) 301730848851SAllison Henderson length = max; 301830848851SAllison Henderson 3019ac27a0ecSDave Kleikamp iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); 3020ac27a0ecSDave Kleikamp 3021ac27a0ecSDave Kleikamp if (!page_has_buffers(page)) 3022ac27a0ecSDave Kleikamp create_empty_buffers(page, blocksize, 0); 3023ac27a0ecSDave Kleikamp 3024ac27a0ecSDave Kleikamp /* Find the buffer that contains "offset" */ 3025ac27a0ecSDave Kleikamp bh = page_buffers(page); 3026ac27a0ecSDave Kleikamp pos = blocksize; 3027ac27a0ecSDave Kleikamp while (offset >= pos) { 3028ac27a0ecSDave Kleikamp bh = bh->b_this_page; 3029ac27a0ecSDave Kleikamp iblock++; 3030ac27a0ecSDave Kleikamp pos += blocksize; 3031ac27a0ecSDave Kleikamp } 3032ac27a0ecSDave Kleikamp 3033ac27a0ecSDave Kleikamp err = 0; 3034ac27a0ecSDave Kleikamp if (buffer_freed(bh)) { 3035ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "freed: skip"); 3036ac27a0ecSDave Kleikamp goto unlock; 3037ac27a0ecSDave Kleikamp } 3038ac27a0ecSDave Kleikamp 3039ac27a0ecSDave Kleikamp if (!buffer_mapped(bh)) { 3040ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "unmapped"); 3041617ba13bSMingming Cao ext4_get_block(inode, iblock, bh, 0); 3042ac27a0ecSDave Kleikamp /* unmapped? It's a hole - nothing to do */ 3043ac27a0ecSDave Kleikamp if (!buffer_mapped(bh)) { 3044ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "still unmapped"); 3045ac27a0ecSDave Kleikamp goto unlock; 3046ac27a0ecSDave Kleikamp } 3047ac27a0ecSDave Kleikamp } 3048ac27a0ecSDave Kleikamp 3049ac27a0ecSDave Kleikamp /* Ok, it's mapped. Make sure it's up-to-date */ 3050ac27a0ecSDave Kleikamp if (PageUptodate(page)) 3051ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 3052ac27a0ecSDave Kleikamp 3053ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 3054ac27a0ecSDave Kleikamp err = -EIO; 3055ac27a0ecSDave Kleikamp ll_rw_block(READ, 1, &bh); 3056ac27a0ecSDave Kleikamp wait_on_buffer(bh); 3057ac27a0ecSDave Kleikamp /* Uhhuh. Read error. Complain and punt. */ 3058ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) 3059ac27a0ecSDave Kleikamp goto unlock; 3060ac27a0ecSDave Kleikamp } 3061ac27a0ecSDave Kleikamp 3062617ba13bSMingming Cao if (ext4_should_journal_data(inode)) { 3063ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "get write access"); 3064617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, bh); 3065ac27a0ecSDave Kleikamp if (err) 3066ac27a0ecSDave Kleikamp goto unlock; 3067ac27a0ecSDave Kleikamp } 3068ac27a0ecSDave Kleikamp 3069eebd2aa3SChristoph Lameter zero_user(page, offset, length); 3070ac27a0ecSDave Kleikamp 3071ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "zeroed end of block"); 3072ac27a0ecSDave Kleikamp 3073ac27a0ecSDave Kleikamp err = 0; 3074617ba13bSMingming Cao if (ext4_should_journal_data(inode)) { 30750390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 3076ac27a0ecSDave Kleikamp } else { 30778aefcd55STheodore Ts'o if (ext4_should_order_data(inode) && EXT4_I(inode)->jinode) 3078678aaf48SJan Kara err = ext4_jbd2_file_inode(handle, inode); 3079ac27a0ecSDave Kleikamp mark_buffer_dirty(bh); 3080ac27a0ecSDave Kleikamp } 3081ac27a0ecSDave Kleikamp 3082ac27a0ecSDave Kleikamp unlock: 3083ac27a0ecSDave Kleikamp unlock_page(page); 3084ac27a0ecSDave Kleikamp page_cache_release(page); 3085ac27a0ecSDave Kleikamp return err; 3086ac27a0ecSDave Kleikamp } 3087ac27a0ecSDave Kleikamp 308891ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 308991ef4cafSDuane Griffin { 309091ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 309191ef4cafSDuane Griffin return 1; 309291ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 309391ef4cafSDuane Griffin return 1; 309491ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 309591ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 309691ef4cafSDuane Griffin return 0; 309791ef4cafSDuane Griffin } 309891ef4cafSDuane Griffin 3099ac27a0ecSDave Kleikamp /* 3100a4bb6b64SAllison Henderson * ext4_punch_hole: punches a hole in a file by releaseing the blocks 3101a4bb6b64SAllison Henderson * associated with the given offset and length 3102a4bb6b64SAllison Henderson * 3103a4bb6b64SAllison Henderson * @inode: File inode 3104a4bb6b64SAllison Henderson * @offset: The offset where the hole will begin 3105a4bb6b64SAllison Henderson * @len: The length of the hole 3106a4bb6b64SAllison Henderson * 3107a4bb6b64SAllison Henderson * Returns: 0 on sucess or negative on failure 3108a4bb6b64SAllison Henderson */ 3109a4bb6b64SAllison Henderson 3110a4bb6b64SAllison Henderson int ext4_punch_hole(struct file *file, loff_t offset, loff_t length) 3111a4bb6b64SAllison Henderson { 3112a4bb6b64SAllison Henderson struct inode *inode = file->f_path.dentry->d_inode; 3113a4bb6b64SAllison Henderson if (!S_ISREG(inode->i_mode)) 3114a4bb6b64SAllison Henderson return -ENOTSUPP; 3115a4bb6b64SAllison Henderson 3116a4bb6b64SAllison Henderson if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 3117a4bb6b64SAllison Henderson /* TODO: Add support for non extent hole punching */ 3118a4bb6b64SAllison Henderson return -ENOTSUPP; 3119a4bb6b64SAllison Henderson } 3120a4bb6b64SAllison Henderson 3121a4bb6b64SAllison Henderson return ext4_ext_punch_hole(file, offset, length); 3122a4bb6b64SAllison Henderson } 3123a4bb6b64SAllison Henderson 3124a4bb6b64SAllison Henderson /* 3125617ba13bSMingming Cao * ext4_truncate() 3126ac27a0ecSDave Kleikamp * 3127617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 3128617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 3129ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 3130ac27a0ecSDave Kleikamp * 3131ac27a0ecSDave Kleikamp * As we work through the truncate and commmit bits of it to the journal there 3132ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 3133ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 3134ac27a0ecSDave Kleikamp * 3135ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 3136ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 3137ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 3138ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 3139ac27a0ecSDave Kleikamp * left-to-right works OK too). 3140ac27a0ecSDave Kleikamp * 3141ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 3142ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 3143ac27a0ecSDave Kleikamp * 3144ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 3145617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 3146ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 3147617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 3148617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 3149ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 3150617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 3151ac27a0ecSDave Kleikamp */ 3152617ba13bSMingming Cao void ext4_truncate(struct inode *inode) 3153ac27a0ecSDave Kleikamp { 31540562e0baSJiaying Zhang trace_ext4_truncate_enter(inode); 31550562e0baSJiaying Zhang 315691ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 3157ac27a0ecSDave Kleikamp return; 3158ac27a0ecSDave Kleikamp 315912e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS); 3160c8d46e41SJiaying Zhang 31615534fb5bSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 316219f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE); 31637d8f9f7dSTheodore Ts'o 3164ff9893dcSAmir Goldstein if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3165cf108bcaSJan Kara ext4_ext_truncate(inode); 3166ff9893dcSAmir Goldstein else 3167ff9893dcSAmir Goldstein ext4_ind_truncate(inode); 3168a86c6181SAlex Tomas 31690562e0baSJiaying Zhang trace_ext4_truncate_exit(inode); 3170ac27a0ecSDave Kleikamp } 3171ac27a0ecSDave Kleikamp 3172ac27a0ecSDave Kleikamp /* 3173617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 3174ac27a0ecSDave Kleikamp * underlying buffer_head on success. If 'in_mem' is true, we have all 3175ac27a0ecSDave Kleikamp * data in memory that is needed to recreate the on-disk version of this 3176ac27a0ecSDave Kleikamp * inode. 3177ac27a0ecSDave Kleikamp */ 3178617ba13bSMingming Cao static int __ext4_get_inode_loc(struct inode *inode, 3179617ba13bSMingming Cao struct ext4_iloc *iloc, int in_mem) 3180ac27a0ecSDave Kleikamp { 3181240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 3182ac27a0ecSDave Kleikamp struct buffer_head *bh; 3183240799cdSTheodore Ts'o struct super_block *sb = inode->i_sb; 3184240799cdSTheodore Ts'o ext4_fsblk_t block; 3185240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 3186ac27a0ecSDave Kleikamp 31873a06d778SAneesh Kumar K.V iloc->bh = NULL; 3188240799cdSTheodore Ts'o if (!ext4_valid_inum(sb, inode->i_ino)) 3189ac27a0ecSDave Kleikamp return -EIO; 3190ac27a0ecSDave Kleikamp 3191240799cdSTheodore Ts'o iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb); 3192240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 3193240799cdSTheodore Ts'o if (!gdp) 3194240799cdSTheodore Ts'o return -EIO; 3195240799cdSTheodore Ts'o 3196240799cdSTheodore Ts'o /* 3197240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 3198240799cdSTheodore Ts'o */ 319900d09882STao Ma inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 3200240799cdSTheodore Ts'o inode_offset = ((inode->i_ino - 1) % 3201240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 3202240799cdSTheodore Ts'o block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); 3203240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 3204240799cdSTheodore Ts'o 3205240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 3206ac27a0ecSDave Kleikamp if (!bh) { 3207c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, block, 3208c398eda0STheodore Ts'o "unable to read itable block"); 3209ac27a0ecSDave Kleikamp return -EIO; 3210ac27a0ecSDave Kleikamp } 3211ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 3212ac27a0ecSDave Kleikamp lock_buffer(bh); 32139c83a923SHidehiro Kawai 32149c83a923SHidehiro Kawai /* 32159c83a923SHidehiro Kawai * If the buffer has the write error flag, we have failed 32169c83a923SHidehiro Kawai * to write out another inode in the same block. In this 32179c83a923SHidehiro Kawai * case, we don't have to read the block because we may 32189c83a923SHidehiro Kawai * read the old inode data successfully. 32199c83a923SHidehiro Kawai */ 32209c83a923SHidehiro Kawai if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) 32219c83a923SHidehiro Kawai set_buffer_uptodate(bh); 32229c83a923SHidehiro Kawai 3223ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) { 3224ac27a0ecSDave Kleikamp /* someone brought it uptodate while we waited */ 3225ac27a0ecSDave Kleikamp unlock_buffer(bh); 3226ac27a0ecSDave Kleikamp goto has_buffer; 3227ac27a0ecSDave Kleikamp } 3228ac27a0ecSDave Kleikamp 3229ac27a0ecSDave Kleikamp /* 3230ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 3231ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 3232ac27a0ecSDave Kleikamp * block. 3233ac27a0ecSDave Kleikamp */ 3234ac27a0ecSDave Kleikamp if (in_mem) { 3235ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 3236240799cdSTheodore Ts'o int i, start; 3237ac27a0ecSDave Kleikamp 3238240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 3239ac27a0ecSDave Kleikamp 3240ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 3241240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 3242ac27a0ecSDave Kleikamp if (!bitmap_bh) 3243ac27a0ecSDave Kleikamp goto make_io; 3244ac27a0ecSDave Kleikamp 3245ac27a0ecSDave Kleikamp /* 3246ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 3247ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 3248ac27a0ecSDave Kleikamp * of one, so skip it. 3249ac27a0ecSDave Kleikamp */ 3250ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 3251ac27a0ecSDave Kleikamp brelse(bitmap_bh); 3252ac27a0ecSDave Kleikamp goto make_io; 3253ac27a0ecSDave Kleikamp } 3254240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 3255ac27a0ecSDave Kleikamp if (i == inode_offset) 3256ac27a0ecSDave Kleikamp continue; 3257617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 3258ac27a0ecSDave Kleikamp break; 3259ac27a0ecSDave Kleikamp } 3260ac27a0ecSDave Kleikamp brelse(bitmap_bh); 3261240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 3262ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 3263ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 3264ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 3265ac27a0ecSDave Kleikamp unlock_buffer(bh); 3266ac27a0ecSDave Kleikamp goto has_buffer; 3267ac27a0ecSDave Kleikamp } 3268ac27a0ecSDave Kleikamp } 3269ac27a0ecSDave Kleikamp 3270ac27a0ecSDave Kleikamp make_io: 3271ac27a0ecSDave Kleikamp /* 3272240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 3273240799cdSTheodore Ts'o * blocks from the inode table. 3274240799cdSTheodore Ts'o */ 3275240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 3276240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 3277240799cdSTheodore Ts'o unsigned num; 3278240799cdSTheodore Ts'o 3279240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 3280b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 3281240799cdSTheodore Ts'o b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1); 3282240799cdSTheodore Ts'o if (table > b) 3283240799cdSTheodore Ts'o b = table; 3284240799cdSTheodore Ts'o end = b + EXT4_SB(sb)->s_inode_readahead_blks; 3285240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 3286240799cdSTheodore Ts'o if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 3287240799cdSTheodore Ts'o EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) 3288560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 3289240799cdSTheodore Ts'o table += num / inodes_per_block; 3290240799cdSTheodore Ts'o if (end > table) 3291240799cdSTheodore Ts'o end = table; 3292240799cdSTheodore Ts'o while (b <= end) 3293240799cdSTheodore Ts'o sb_breadahead(sb, b++); 3294240799cdSTheodore Ts'o } 3295240799cdSTheodore Ts'o 3296240799cdSTheodore Ts'o /* 3297ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 3298ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 3299ac27a0ecSDave Kleikamp * Read the block from disk. 3300ac27a0ecSDave Kleikamp */ 33010562e0baSJiaying Zhang trace_ext4_load_inode(inode); 3302ac27a0ecSDave Kleikamp get_bh(bh); 3303ac27a0ecSDave Kleikamp bh->b_end_io = end_buffer_read_sync; 3304*65299a3bSChristoph Hellwig submit_bh(READ | REQ_META | REQ_PRIO, bh); 3305ac27a0ecSDave Kleikamp wait_on_buffer(bh); 3306ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 3307c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, block, 3308c398eda0STheodore Ts'o "unable to read itable block"); 3309ac27a0ecSDave Kleikamp brelse(bh); 3310ac27a0ecSDave Kleikamp return -EIO; 3311ac27a0ecSDave Kleikamp } 3312ac27a0ecSDave Kleikamp } 3313ac27a0ecSDave Kleikamp has_buffer: 3314ac27a0ecSDave Kleikamp iloc->bh = bh; 3315ac27a0ecSDave Kleikamp return 0; 3316ac27a0ecSDave Kleikamp } 3317ac27a0ecSDave Kleikamp 3318617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 3319ac27a0ecSDave Kleikamp { 3320ac27a0ecSDave Kleikamp /* We have all inode data except xattrs in memory here. */ 3321617ba13bSMingming Cao return __ext4_get_inode_loc(inode, iloc, 332219f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_XATTR)); 3323ac27a0ecSDave Kleikamp } 3324ac27a0ecSDave Kleikamp 3325617ba13bSMingming Cao void ext4_set_inode_flags(struct inode *inode) 3326ac27a0ecSDave Kleikamp { 3327617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 3328ac27a0ecSDave Kleikamp 3329ac27a0ecSDave Kleikamp inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); 3330617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 3331ac27a0ecSDave Kleikamp inode->i_flags |= S_SYNC; 3332617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 3333ac27a0ecSDave Kleikamp inode->i_flags |= S_APPEND; 3334617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 3335ac27a0ecSDave Kleikamp inode->i_flags |= S_IMMUTABLE; 3336617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 3337ac27a0ecSDave Kleikamp inode->i_flags |= S_NOATIME; 3338617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 3339ac27a0ecSDave Kleikamp inode->i_flags |= S_DIRSYNC; 3340ac27a0ecSDave Kleikamp } 3341ac27a0ecSDave Kleikamp 3342ff9ddf7eSJan Kara /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */ 3343ff9ddf7eSJan Kara void ext4_get_inode_flags(struct ext4_inode_info *ei) 3344ff9ddf7eSJan Kara { 334584a8dce2SDmitry Monakhov unsigned int vfs_fl; 334684a8dce2SDmitry Monakhov unsigned long old_fl, new_fl; 3347ff9ddf7eSJan Kara 334884a8dce2SDmitry Monakhov do { 334984a8dce2SDmitry Monakhov vfs_fl = ei->vfs_inode.i_flags; 335084a8dce2SDmitry Monakhov old_fl = ei->i_flags; 335184a8dce2SDmitry Monakhov new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL| 335284a8dce2SDmitry Monakhov EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL| 335384a8dce2SDmitry Monakhov EXT4_DIRSYNC_FL); 335484a8dce2SDmitry Monakhov if (vfs_fl & S_SYNC) 335584a8dce2SDmitry Monakhov new_fl |= EXT4_SYNC_FL; 335684a8dce2SDmitry Monakhov if (vfs_fl & S_APPEND) 335784a8dce2SDmitry Monakhov new_fl |= EXT4_APPEND_FL; 335884a8dce2SDmitry Monakhov if (vfs_fl & S_IMMUTABLE) 335984a8dce2SDmitry Monakhov new_fl |= EXT4_IMMUTABLE_FL; 336084a8dce2SDmitry Monakhov if (vfs_fl & S_NOATIME) 336184a8dce2SDmitry Monakhov new_fl |= EXT4_NOATIME_FL; 336284a8dce2SDmitry Monakhov if (vfs_fl & S_DIRSYNC) 336384a8dce2SDmitry Monakhov new_fl |= EXT4_DIRSYNC_FL; 336484a8dce2SDmitry Monakhov } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl); 3365ff9ddf7eSJan Kara } 3366de9a55b8STheodore Ts'o 33670fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 33680fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 33690fc1b451SAneesh Kumar K.V { 33700fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 33718180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 33728180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 33730fc1b451SAneesh Kumar K.V 33740fc1b451SAneesh Kumar K.V if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 33750fc1b451SAneesh Kumar K.V EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) { 33760fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 33770fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 33780fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 337907a03824STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) { 33808180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 33818180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 33828180a562SAneesh Kumar K.V } else { 33830fc1b451SAneesh Kumar K.V return i_blocks; 33848180a562SAneesh Kumar K.V } 33850fc1b451SAneesh Kumar K.V } else { 33860fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 33870fc1b451SAneesh Kumar K.V } 33880fc1b451SAneesh Kumar K.V } 3389ff9ddf7eSJan Kara 33901d1fe1eeSDavid Howells struct inode *ext4_iget(struct super_block *sb, unsigned long ino) 3391ac27a0ecSDave Kleikamp { 3392617ba13bSMingming Cao struct ext4_iloc iloc; 3393617ba13bSMingming Cao struct ext4_inode *raw_inode; 33941d1fe1eeSDavid Howells struct ext4_inode_info *ei; 33951d1fe1eeSDavid Howells struct inode *inode; 3396b436b9beSJan Kara journal_t *journal = EXT4_SB(sb)->s_journal; 33971d1fe1eeSDavid Howells long ret; 3398ac27a0ecSDave Kleikamp int block; 3399ac27a0ecSDave Kleikamp 34001d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 34011d1fe1eeSDavid Howells if (!inode) 34021d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 34031d1fe1eeSDavid Howells if (!(inode->i_state & I_NEW)) 34041d1fe1eeSDavid Howells return inode; 34051d1fe1eeSDavid Howells 34061d1fe1eeSDavid Howells ei = EXT4_I(inode); 34077dc57615SPeter Huewe iloc.bh = NULL; 3408ac27a0ecSDave Kleikamp 34091d1fe1eeSDavid Howells ret = __ext4_get_inode_loc(inode, &iloc, 0); 34101d1fe1eeSDavid Howells if (ret < 0) 3411ac27a0ecSDave Kleikamp goto bad_inode; 3412617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 3413ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 3414ac27a0ecSDave Kleikamp inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 3415ac27a0ecSDave Kleikamp inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 3416ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 3417ac27a0ecSDave Kleikamp inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 3418ac27a0ecSDave Kleikamp inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 3419ac27a0ecSDave Kleikamp } 3420ac27a0ecSDave Kleikamp inode->i_nlink = le16_to_cpu(raw_inode->i_links_count); 3421ac27a0ecSDave Kleikamp 3422353eb83cSTheodore Ts'o ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 3423ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 3424ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 3425ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 3426ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 3427ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 3428ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 3429ac27a0ecSDave Kleikamp */ 3430ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 3431ac27a0ecSDave Kleikamp if (inode->i_mode == 0 || 3432617ba13bSMingming Cao !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) { 3433ac27a0ecSDave Kleikamp /* this inode is deleted */ 34341d1fe1eeSDavid Howells ret = -ESTALE; 3435ac27a0ecSDave Kleikamp goto bad_inode; 3436ac27a0ecSDave Kleikamp } 3437ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 3438ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 3439ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 3440ac27a0ecSDave Kleikamp * the process of deleting those. */ 3441ac27a0ecSDave Kleikamp } 3442ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 34430fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 34447973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 3445a9e81742STheodore Ts'o if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) 3446a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 3447a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 3448a48380f7SAneesh Kumar K.V inode->i_size = ext4_isize(raw_inode); 3449ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 3450a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 3451a9e7f447SDmitry Monakhov ei->i_reserved_quota = 0; 3452a9e7f447SDmitry Monakhov #endif 3453ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 3454ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 3455a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 3456ac27a0ecSDave Kleikamp /* 3457ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 3458ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 3459ac27a0ecSDave Kleikamp */ 3460617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 3461ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 3462ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 3463ac27a0ecSDave Kleikamp 3464b436b9beSJan Kara /* 3465b436b9beSJan Kara * Set transaction id's of transactions that have to be committed 3466b436b9beSJan Kara * to finish f[data]sync. We set them to currently running transaction 3467b436b9beSJan Kara * as we cannot be sure that the inode or some of its metadata isn't 3468b436b9beSJan Kara * part of the transaction - the inode could have been reclaimed and 3469b436b9beSJan Kara * now it is reread from disk. 3470b436b9beSJan Kara */ 3471b436b9beSJan Kara if (journal) { 3472b436b9beSJan Kara transaction_t *transaction; 3473b436b9beSJan Kara tid_t tid; 3474b436b9beSJan Kara 3475a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 3476b436b9beSJan Kara if (journal->j_running_transaction) 3477b436b9beSJan Kara transaction = journal->j_running_transaction; 3478b436b9beSJan Kara else 3479b436b9beSJan Kara transaction = journal->j_committing_transaction; 3480b436b9beSJan Kara if (transaction) 3481b436b9beSJan Kara tid = transaction->t_tid; 3482b436b9beSJan Kara else 3483b436b9beSJan Kara tid = journal->j_commit_sequence; 3484a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 3485b436b9beSJan Kara ei->i_sync_tid = tid; 3486b436b9beSJan Kara ei->i_datasync_tid = tid; 3487b436b9beSJan Kara } 3488b436b9beSJan Kara 34890040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 3490ac27a0ecSDave Kleikamp ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 3491617ba13bSMingming Cao if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 3492e5d2861fSKirill Korotaev EXT4_INODE_SIZE(inode->i_sb)) { 34931d1fe1eeSDavid Howells ret = -EIO; 3494ac27a0ecSDave Kleikamp goto bad_inode; 3495e5d2861fSKirill Korotaev } 3496ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 3497ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 3498617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 3499617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 3500ac27a0ecSDave Kleikamp } else { 3501ac27a0ecSDave Kleikamp __le32 *magic = (void *)raw_inode + 3502617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE + 3503ac27a0ecSDave Kleikamp ei->i_extra_isize; 3504617ba13bSMingming Cao if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) 350519f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_XATTR); 3506ac27a0ecSDave Kleikamp } 3507ac27a0ecSDave Kleikamp } else 3508ac27a0ecSDave Kleikamp ei->i_extra_isize = 0; 3509ac27a0ecSDave Kleikamp 3510ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 3511ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 3512ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 3513ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 3514ef7f3835SKalpak Shah 351525ec56b5SJean Noel Cordenner inode->i_version = le32_to_cpu(raw_inode->i_disk_version); 351625ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 351725ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 351825ec56b5SJean Noel Cordenner inode->i_version |= 351925ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 352025ec56b5SJean Noel Cordenner } 352125ec56b5SJean Noel Cordenner 3522c4b5a614STheodore Ts'o ret = 0; 3523485c26ecSTheodore Ts'o if (ei->i_file_acl && 35241032988cSTheodore Ts'o !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) { 352524676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "bad extended attribute block %llu", 352624676da4STheodore Ts'o ei->i_file_acl); 3527485c26ecSTheodore Ts'o ret = -EIO; 3528485c26ecSTheodore Ts'o goto bad_inode; 352907a03824STheodore Ts'o } else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 3530c4b5a614STheodore Ts'o if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 3531c4b5a614STheodore Ts'o (S_ISLNK(inode->i_mode) && 3532c4b5a614STheodore Ts'o !ext4_inode_is_fast_symlink(inode))) 35337a262f7cSAneesh Kumar K.V /* Validate extent which is part of inode */ 35347a262f7cSAneesh Kumar K.V ret = ext4_ext_check_inode(inode); 3535fe2c8191SThiemo Nagel } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 3536fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 3537fe2c8191SThiemo Nagel !ext4_inode_is_fast_symlink(inode))) { 3538fe2c8191SThiemo Nagel /* Validate block references which are part of inode */ 35391f7d1e77STheodore Ts'o ret = ext4_ind_check_inode(inode); 3540fe2c8191SThiemo Nagel } 3541567f3e9aSTheodore Ts'o if (ret) 35427a262f7cSAneesh Kumar K.V goto bad_inode; 35437a262f7cSAneesh Kumar K.V 3544ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 3545617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 3546617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 3547617ba13bSMingming Cao ext4_set_aops(inode); 3548ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 3549617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 3550617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 3551ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 3552e83c1397SDuane Griffin if (ext4_inode_is_fast_symlink(inode)) { 3553617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 3554e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 3555e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 3556e83c1397SDuane Griffin } else { 3557617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 3558617ba13bSMingming Cao ext4_set_aops(inode); 3559ac27a0ecSDave Kleikamp } 3560563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 3561563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 3562617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 3563ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 3564ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 3565ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 3566ac27a0ecSDave Kleikamp else 3567ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 3568ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 3569563bdd61STheodore Ts'o } else { 3570563bdd61STheodore Ts'o ret = -EIO; 357124676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode); 3572563bdd61STheodore Ts'o goto bad_inode; 3573ac27a0ecSDave Kleikamp } 3574ac27a0ecSDave Kleikamp brelse(iloc.bh); 3575617ba13bSMingming Cao ext4_set_inode_flags(inode); 35761d1fe1eeSDavid Howells unlock_new_inode(inode); 35771d1fe1eeSDavid Howells return inode; 3578ac27a0ecSDave Kleikamp 3579ac27a0ecSDave Kleikamp bad_inode: 3580567f3e9aSTheodore Ts'o brelse(iloc.bh); 35811d1fe1eeSDavid Howells iget_failed(inode); 35821d1fe1eeSDavid Howells return ERR_PTR(ret); 3583ac27a0ecSDave Kleikamp } 3584ac27a0ecSDave Kleikamp 35850fc1b451SAneesh Kumar K.V static int ext4_inode_blocks_set(handle_t *handle, 35860fc1b451SAneesh Kumar K.V struct ext4_inode *raw_inode, 35870fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 35880fc1b451SAneesh Kumar K.V { 35890fc1b451SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 35900fc1b451SAneesh Kumar K.V u64 i_blocks = inode->i_blocks; 35910fc1b451SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 35920fc1b451SAneesh Kumar K.V 35930fc1b451SAneesh Kumar K.V if (i_blocks <= ~0U) { 35940fc1b451SAneesh Kumar K.V /* 35950fc1b451SAneesh Kumar K.V * i_blocks can be represnted in a 32 bit variable 35960fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 35970fc1b451SAneesh Kumar K.V */ 35988180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 35990fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = 0; 360084a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 3601f287a1a5STheodore Ts'o return 0; 3602f287a1a5STheodore Ts'o } 3603f287a1a5STheodore Ts'o if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) 3604f287a1a5STheodore Ts'o return -EFBIG; 3605f287a1a5STheodore Ts'o 3606f287a1a5STheodore Ts'o if (i_blocks <= 0xffffffffffffULL) { 36070fc1b451SAneesh Kumar K.V /* 36080fc1b451SAneesh Kumar K.V * i_blocks can be represented in a 48 bit variable 36090fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 36100fc1b451SAneesh Kumar K.V */ 36118180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 36120fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 361384a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 36140fc1b451SAneesh Kumar K.V } else { 361584a8dce2SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE); 36168180a562SAneesh Kumar K.V /* i_block is stored in file system block size */ 36178180a562SAneesh Kumar K.V i_blocks = i_blocks >> (inode->i_blkbits - 9); 36188180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 36198180a562SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 36200fc1b451SAneesh Kumar K.V } 3621f287a1a5STheodore Ts'o return 0; 36220fc1b451SAneesh Kumar K.V } 36230fc1b451SAneesh Kumar K.V 3624ac27a0ecSDave Kleikamp /* 3625ac27a0ecSDave Kleikamp * Post the struct inode info into an on-disk inode location in the 3626ac27a0ecSDave Kleikamp * buffer-cache. This gobbles the caller's reference to the 3627ac27a0ecSDave Kleikamp * buffer_head in the inode location struct. 3628ac27a0ecSDave Kleikamp * 3629ac27a0ecSDave Kleikamp * The caller must have write access to iloc->bh. 3630ac27a0ecSDave Kleikamp */ 3631617ba13bSMingming Cao static int ext4_do_update_inode(handle_t *handle, 3632ac27a0ecSDave Kleikamp struct inode *inode, 3633830156c7SFrank Mayhar struct ext4_iloc *iloc) 3634ac27a0ecSDave Kleikamp { 3635617ba13bSMingming Cao struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 3636617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 3637ac27a0ecSDave Kleikamp struct buffer_head *bh = iloc->bh; 3638ac27a0ecSDave Kleikamp int err = 0, rc, block; 3639ac27a0ecSDave Kleikamp 3640ac27a0ecSDave Kleikamp /* For fields not not tracking in the in-memory inode, 3641ac27a0ecSDave Kleikamp * initialise them to zero for new inodes. */ 364219f5fb7aSTheodore Ts'o if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) 3643617ba13bSMingming Cao memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 3644ac27a0ecSDave Kleikamp 3645ff9ddf7eSJan Kara ext4_get_inode_flags(ei); 3646ac27a0ecSDave Kleikamp raw_inode->i_mode = cpu_to_le16(inode->i_mode); 3647ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 3648ac27a0ecSDave Kleikamp raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid)); 3649ac27a0ecSDave Kleikamp raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid)); 3650ac27a0ecSDave Kleikamp /* 3651ac27a0ecSDave Kleikamp * Fix up interoperability with old kernels. Otherwise, old inodes get 3652ac27a0ecSDave Kleikamp * re-used with the upper 16 bits of the uid/gid intact 3653ac27a0ecSDave Kleikamp */ 3654ac27a0ecSDave Kleikamp if (!ei->i_dtime) { 3655ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 3656ac27a0ecSDave Kleikamp cpu_to_le16(high_16_bits(inode->i_uid)); 3657ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 3658ac27a0ecSDave Kleikamp cpu_to_le16(high_16_bits(inode->i_gid)); 3659ac27a0ecSDave Kleikamp } else { 3660ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 3661ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 3662ac27a0ecSDave Kleikamp } 3663ac27a0ecSDave Kleikamp } else { 3664ac27a0ecSDave Kleikamp raw_inode->i_uid_low = 3665ac27a0ecSDave Kleikamp cpu_to_le16(fs_high2lowuid(inode->i_uid)); 3666ac27a0ecSDave Kleikamp raw_inode->i_gid_low = 3667ac27a0ecSDave Kleikamp cpu_to_le16(fs_high2lowgid(inode->i_gid)); 3668ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 3669ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 3670ac27a0ecSDave Kleikamp } 3671ac27a0ecSDave Kleikamp raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 3672ef7f3835SKalpak Shah 3673ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 3674ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 3675ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 3676ef7f3835SKalpak Shah EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 3677ef7f3835SKalpak Shah 36780fc1b451SAneesh Kumar K.V if (ext4_inode_blocks_set(handle, raw_inode, ei)) 36790fc1b451SAneesh Kumar K.V goto out_brelse; 3680ac27a0ecSDave Kleikamp raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 3681353eb83cSTheodore Ts'o raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF); 36829b8f1f01SMingming Cao if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 36839b8f1f01SMingming Cao cpu_to_le32(EXT4_OS_HURD)) 3684a1ddeb7eSBadari Pulavarty raw_inode->i_file_acl_high = 3685a1ddeb7eSBadari Pulavarty cpu_to_le16(ei->i_file_acl >> 32); 36867973c0c1SAneesh Kumar K.V raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 3687a48380f7SAneesh Kumar K.V ext4_isize_set(raw_inode, ei->i_disksize); 3688ac27a0ecSDave Kleikamp if (ei->i_disksize > 0x7fffffffULL) { 3689ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 3690617ba13bSMingming Cao if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, 3691617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE) || 3692617ba13bSMingming Cao EXT4_SB(sb)->s_es->s_rev_level == 3693617ba13bSMingming Cao cpu_to_le32(EXT4_GOOD_OLD_REV)) { 3694ac27a0ecSDave Kleikamp /* If this is the first large file 3695ac27a0ecSDave Kleikamp * created, add a flag to the superblock. 3696ac27a0ecSDave Kleikamp */ 3697617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, 3698617ba13bSMingming Cao EXT4_SB(sb)->s_sbh); 3699ac27a0ecSDave Kleikamp if (err) 3700ac27a0ecSDave Kleikamp goto out_brelse; 3701617ba13bSMingming Cao ext4_update_dynamic_rev(sb); 3702617ba13bSMingming Cao EXT4_SET_RO_COMPAT_FEATURE(sb, 3703617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE); 3704ac27a0ecSDave Kleikamp sb->s_dirt = 1; 37050390131bSFrank Mayhar ext4_handle_sync(handle); 370673b50c1cSCurt Wohlgemuth err = ext4_handle_dirty_metadata(handle, NULL, 3707617ba13bSMingming Cao EXT4_SB(sb)->s_sbh); 3708ac27a0ecSDave Kleikamp } 3709ac27a0ecSDave Kleikamp } 3710ac27a0ecSDave Kleikamp raw_inode->i_generation = cpu_to_le32(inode->i_generation); 3711ac27a0ecSDave Kleikamp if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 3712ac27a0ecSDave Kleikamp if (old_valid_dev(inode->i_rdev)) { 3713ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 3714ac27a0ecSDave Kleikamp cpu_to_le32(old_encode_dev(inode->i_rdev)); 3715ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 0; 3716ac27a0ecSDave Kleikamp } else { 3717ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 0; 3718ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 3719ac27a0ecSDave Kleikamp cpu_to_le32(new_encode_dev(inode->i_rdev)); 3720ac27a0ecSDave Kleikamp raw_inode->i_block[2] = 0; 3721ac27a0ecSDave Kleikamp } 3722de9a55b8STheodore Ts'o } else 3723de9a55b8STheodore Ts'o for (block = 0; block < EXT4_N_BLOCKS; block++) 3724ac27a0ecSDave Kleikamp raw_inode->i_block[block] = ei->i_data[block]; 3725ac27a0ecSDave Kleikamp 372625ec56b5SJean Noel Cordenner raw_inode->i_disk_version = cpu_to_le32(inode->i_version); 372725ec56b5SJean Noel Cordenner if (ei->i_extra_isize) { 372825ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 372925ec56b5SJean Noel Cordenner raw_inode->i_version_hi = 373025ec56b5SJean Noel Cordenner cpu_to_le32(inode->i_version >> 32); 3731ac27a0ecSDave Kleikamp raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize); 373225ec56b5SJean Noel Cordenner } 373325ec56b5SJean Noel Cordenner 37340390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 373573b50c1cSCurt Wohlgemuth rc = ext4_handle_dirty_metadata(handle, NULL, bh); 3736ac27a0ecSDave Kleikamp if (!err) 3737ac27a0ecSDave Kleikamp err = rc; 373819f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_NEW); 3739ac27a0ecSDave Kleikamp 3740b436b9beSJan Kara ext4_update_inode_fsync_trans(handle, inode, 0); 3741ac27a0ecSDave Kleikamp out_brelse: 3742ac27a0ecSDave Kleikamp brelse(bh); 3743617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 3744ac27a0ecSDave Kleikamp return err; 3745ac27a0ecSDave Kleikamp } 3746ac27a0ecSDave Kleikamp 3747ac27a0ecSDave Kleikamp /* 3748617ba13bSMingming Cao * ext4_write_inode() 3749ac27a0ecSDave Kleikamp * 3750ac27a0ecSDave Kleikamp * We are called from a few places: 3751ac27a0ecSDave Kleikamp * 3752ac27a0ecSDave Kleikamp * - Within generic_file_write() for O_SYNC files. 3753ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 3754ac27a0ecSDave Kleikamp * trasnaction to commit. 3755ac27a0ecSDave Kleikamp * 3756ac27a0ecSDave Kleikamp * - Within sys_sync(), kupdate and such. 3757ac27a0ecSDave Kleikamp * We wait on commit, if tol to. 3758ac27a0ecSDave Kleikamp * 3759ac27a0ecSDave Kleikamp * - Within prune_icache() (PF_MEMALLOC == true) 3760ac27a0ecSDave Kleikamp * Here we simply return. We can't afford to block kswapd on the 3761ac27a0ecSDave Kleikamp * journal commit. 3762ac27a0ecSDave Kleikamp * 3763ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 3764ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 3765617ba13bSMingming Cao * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for 3766ac27a0ecSDave Kleikamp * knfsd. 3767ac27a0ecSDave Kleikamp * 3768ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 3769ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 3770ac27a0ecSDave Kleikamp * which we are interested. 3771ac27a0ecSDave Kleikamp * 3772ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 3773ac27a0ecSDave Kleikamp * 3774ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 3775ac27a0ecSDave Kleikamp * stuff(); 3776ac27a0ecSDave Kleikamp * inode->i_size = expr; 3777ac27a0ecSDave Kleikamp * 3778ac27a0ecSDave Kleikamp * is in error because a kswapd-driven write_inode() could occur while 3779ac27a0ecSDave Kleikamp * `stuff()' is running, and the new i_size will be lost. Plus the inode 3780ac27a0ecSDave Kleikamp * will no longer be on the superblock's dirty inode list. 3781ac27a0ecSDave Kleikamp */ 3782a9185b41SChristoph Hellwig int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) 3783ac27a0ecSDave Kleikamp { 378491ac6f43SFrank Mayhar int err; 378591ac6f43SFrank Mayhar 3786ac27a0ecSDave Kleikamp if (current->flags & PF_MEMALLOC) 3787ac27a0ecSDave Kleikamp return 0; 3788ac27a0ecSDave Kleikamp 378991ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal) { 3790617ba13bSMingming Cao if (ext4_journal_current_handle()) { 3791b38bd33aSMingming Cao jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); 3792ac27a0ecSDave Kleikamp dump_stack(); 3793ac27a0ecSDave Kleikamp return -EIO; 3794ac27a0ecSDave Kleikamp } 3795ac27a0ecSDave Kleikamp 3796a9185b41SChristoph Hellwig if (wbc->sync_mode != WB_SYNC_ALL) 3797ac27a0ecSDave Kleikamp return 0; 3798ac27a0ecSDave Kleikamp 379991ac6f43SFrank Mayhar err = ext4_force_commit(inode->i_sb); 380091ac6f43SFrank Mayhar } else { 380191ac6f43SFrank Mayhar struct ext4_iloc iloc; 380291ac6f43SFrank Mayhar 38038b472d73SCurt Wohlgemuth err = __ext4_get_inode_loc(inode, &iloc, 0); 380491ac6f43SFrank Mayhar if (err) 380591ac6f43SFrank Mayhar return err; 3806a9185b41SChristoph Hellwig if (wbc->sync_mode == WB_SYNC_ALL) 3807830156c7SFrank Mayhar sync_dirty_buffer(iloc.bh); 3808830156c7SFrank Mayhar if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { 3809c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr, 3810c398eda0STheodore Ts'o "IO error syncing inode"); 3811830156c7SFrank Mayhar err = -EIO; 3812830156c7SFrank Mayhar } 3813fd2dd9fbSCurt Wohlgemuth brelse(iloc.bh); 381491ac6f43SFrank Mayhar } 381591ac6f43SFrank Mayhar return err; 3816ac27a0ecSDave Kleikamp } 3817ac27a0ecSDave Kleikamp 3818ac27a0ecSDave Kleikamp /* 3819617ba13bSMingming Cao * ext4_setattr() 3820ac27a0ecSDave Kleikamp * 3821ac27a0ecSDave Kleikamp * Called from notify_change. 3822ac27a0ecSDave Kleikamp * 3823ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 3824ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 3825ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 3826ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 3827ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 3828ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 3829ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 3830ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 3831ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 3832ac27a0ecSDave Kleikamp * 3833678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 3834678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 3835678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 3836678aaf48SJan Kara * This way we are sure that all the data written in the previous 3837678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 3838678aaf48SJan Kara * writeback). 3839678aaf48SJan Kara * 3840678aaf48SJan Kara * Called with inode->i_mutex down. 3841ac27a0ecSDave Kleikamp */ 3842617ba13bSMingming Cao int ext4_setattr(struct dentry *dentry, struct iattr *attr) 3843ac27a0ecSDave Kleikamp { 3844ac27a0ecSDave Kleikamp struct inode *inode = dentry->d_inode; 3845ac27a0ecSDave Kleikamp int error, rc = 0; 38463d287de3SDmitry Monakhov int orphan = 0; 3847ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 3848ac27a0ecSDave Kleikamp 3849ac27a0ecSDave Kleikamp error = inode_change_ok(inode, attr); 3850ac27a0ecSDave Kleikamp if (error) 3851ac27a0ecSDave Kleikamp return error; 3852ac27a0ecSDave Kleikamp 385312755627SDmitry Monakhov if (is_quota_modification(inode, attr)) 3854871a2931SChristoph Hellwig dquot_initialize(inode); 3855ac27a0ecSDave Kleikamp if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) || 3856ac27a0ecSDave Kleikamp (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) { 3857ac27a0ecSDave Kleikamp handle_t *handle; 3858ac27a0ecSDave Kleikamp 3859ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 3860ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 38615aca07ebSDmitry Monakhov handle = ext4_journal_start(inode, (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb)+ 3862194074acSDmitry Monakhov EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb))+3); 3863ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 3864ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 3865ac27a0ecSDave Kleikamp goto err_out; 3866ac27a0ecSDave Kleikamp } 3867b43fa828SChristoph Hellwig error = dquot_transfer(inode, attr); 3868ac27a0ecSDave Kleikamp if (error) { 3869617ba13bSMingming Cao ext4_journal_stop(handle); 3870ac27a0ecSDave Kleikamp return error; 3871ac27a0ecSDave Kleikamp } 3872ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 3873ac27a0ecSDave Kleikamp * one transaction */ 3874ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_UID) 3875ac27a0ecSDave Kleikamp inode->i_uid = attr->ia_uid; 3876ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_GID) 3877ac27a0ecSDave Kleikamp inode->i_gid = attr->ia_gid; 3878617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 3879617ba13bSMingming Cao ext4_journal_stop(handle); 3880ac27a0ecSDave Kleikamp } 3881ac27a0ecSDave Kleikamp 3882e2b46574SEric Sandeen if (attr->ia_valid & ATTR_SIZE) { 3883562c72aaSChristoph Hellwig inode_dio_wait(inode); 3884562c72aaSChristoph Hellwig 388512e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 3886e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3887e2b46574SEric Sandeen 38880c095c7fSTheodore Ts'o if (attr->ia_size > sbi->s_bitmap_maxbytes) 38890c095c7fSTheodore Ts'o return -EFBIG; 3890e2b46574SEric Sandeen } 3891e2b46574SEric Sandeen } 3892e2b46574SEric Sandeen 3893ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode) && 3894c8d46e41SJiaying Zhang attr->ia_valid & ATTR_SIZE && 3895072bd7eaSTheodore Ts'o (attr->ia_size < inode->i_size)) { 3896ac27a0ecSDave Kleikamp handle_t *handle; 3897ac27a0ecSDave Kleikamp 3898617ba13bSMingming Cao handle = ext4_journal_start(inode, 3); 3899ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 3900ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 3901ac27a0ecSDave Kleikamp goto err_out; 3902ac27a0ecSDave Kleikamp } 39033d287de3SDmitry Monakhov if (ext4_handle_valid(handle)) { 3904617ba13bSMingming Cao error = ext4_orphan_add(handle, inode); 39053d287de3SDmitry Monakhov orphan = 1; 39063d287de3SDmitry Monakhov } 3907617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 3908617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 3909ac27a0ecSDave Kleikamp if (!error) 3910ac27a0ecSDave Kleikamp error = rc; 3911617ba13bSMingming Cao ext4_journal_stop(handle); 3912678aaf48SJan Kara 3913678aaf48SJan Kara if (ext4_should_order_data(inode)) { 3914678aaf48SJan Kara error = ext4_begin_ordered_truncate(inode, 3915678aaf48SJan Kara attr->ia_size); 3916678aaf48SJan Kara if (error) { 3917678aaf48SJan Kara /* Do as much error cleanup as possible */ 3918678aaf48SJan Kara handle = ext4_journal_start(inode, 3); 3919678aaf48SJan Kara if (IS_ERR(handle)) { 3920678aaf48SJan Kara ext4_orphan_del(NULL, inode); 3921678aaf48SJan Kara goto err_out; 3922678aaf48SJan Kara } 3923678aaf48SJan Kara ext4_orphan_del(handle, inode); 39243d287de3SDmitry Monakhov orphan = 0; 3925678aaf48SJan Kara ext4_journal_stop(handle); 3926678aaf48SJan Kara goto err_out; 3927678aaf48SJan Kara } 3928678aaf48SJan Kara } 3929ac27a0ecSDave Kleikamp } 3930ac27a0ecSDave Kleikamp 3931072bd7eaSTheodore Ts'o if (attr->ia_valid & ATTR_SIZE) { 3932072bd7eaSTheodore Ts'o if (attr->ia_size != i_size_read(inode)) { 3933072bd7eaSTheodore Ts'o truncate_setsize(inode, attr->ia_size); 3934072bd7eaSTheodore Ts'o ext4_truncate(inode); 3935072bd7eaSTheodore Ts'o } else if (ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS)) 3936072bd7eaSTheodore Ts'o ext4_truncate(inode); 3937072bd7eaSTheodore Ts'o } 3938ac27a0ecSDave Kleikamp 39391025774cSChristoph Hellwig if (!rc) { 39401025774cSChristoph Hellwig setattr_copy(inode, attr); 39411025774cSChristoph Hellwig mark_inode_dirty(inode); 39421025774cSChristoph Hellwig } 39431025774cSChristoph Hellwig 39441025774cSChristoph Hellwig /* 39451025774cSChristoph Hellwig * If the call to ext4_truncate failed to get a transaction handle at 39461025774cSChristoph Hellwig * all, we need to clean up the in-core orphan list manually. 39471025774cSChristoph Hellwig */ 39483d287de3SDmitry Monakhov if (orphan && inode->i_nlink) 3949617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 3950ac27a0ecSDave Kleikamp 3951ac27a0ecSDave Kleikamp if (!rc && (ia_valid & ATTR_MODE)) 3952617ba13bSMingming Cao rc = ext4_acl_chmod(inode); 3953ac27a0ecSDave Kleikamp 3954ac27a0ecSDave Kleikamp err_out: 3955617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 3956ac27a0ecSDave Kleikamp if (!error) 3957ac27a0ecSDave Kleikamp error = rc; 3958ac27a0ecSDave Kleikamp return error; 3959ac27a0ecSDave Kleikamp } 3960ac27a0ecSDave Kleikamp 39613e3398a0SMingming Cao int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry, 39623e3398a0SMingming Cao struct kstat *stat) 39633e3398a0SMingming Cao { 39643e3398a0SMingming Cao struct inode *inode; 39653e3398a0SMingming Cao unsigned long delalloc_blocks; 39663e3398a0SMingming Cao 39673e3398a0SMingming Cao inode = dentry->d_inode; 39683e3398a0SMingming Cao generic_fillattr(inode, stat); 39693e3398a0SMingming Cao 39703e3398a0SMingming Cao /* 39713e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 39723e3398a0SMingming Cao * otherwise in the case of system crash before the real block 39733e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 39743e3398a0SMingming Cao * on-disk file blocks. 39753e3398a0SMingming Cao * We always keep i_blocks updated together with real 39763e3398a0SMingming Cao * allocation. But to not confuse with user, stat 39773e3398a0SMingming Cao * will return the blocks that include the delayed allocation 39783e3398a0SMingming Cao * blocks for this file. 39793e3398a0SMingming Cao */ 39803e3398a0SMingming Cao delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks; 39813e3398a0SMingming Cao 39823e3398a0SMingming Cao stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9; 39833e3398a0SMingming Cao return 0; 39843e3398a0SMingming Cao } 3985ac27a0ecSDave Kleikamp 3986a02908f1SMingming Cao static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) 3987a02908f1SMingming Cao { 398812e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 39898bb2b247SAmir Goldstein return ext4_ind_trans_blocks(inode, nrblocks, chunk); 3990ac51d837STheodore Ts'o return ext4_ext_index_trans_blocks(inode, nrblocks, chunk); 3991a02908f1SMingming Cao } 3992ac51d837STheodore Ts'o 3993a02908f1SMingming Cao /* 3994a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 3995a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 3996a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 3997a02908f1SMingming Cao * 3998a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 3999af901ca1SAndré Goddard Rosa * different block groups too. If they are contiuguous, with flexbg, 4000a02908f1SMingming Cao * they could still across block group boundary. 4001a02908f1SMingming Cao * 4002a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 4003a02908f1SMingming Cao */ 40041f109d5aSTheodore Ts'o static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4005a02908f1SMingming Cao { 40068df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 40078df9675fSTheodore Ts'o int gdpblocks; 4008a02908f1SMingming Cao int idxblocks; 4009a02908f1SMingming Cao int ret = 0; 4010a02908f1SMingming Cao 4011a02908f1SMingming Cao /* 4012a02908f1SMingming Cao * How many index blocks need to touch to modify nrblocks? 4013a02908f1SMingming Cao * The "Chunk" flag indicating whether the nrblocks is 4014a02908f1SMingming Cao * physically contiguous on disk 4015a02908f1SMingming Cao * 4016a02908f1SMingming Cao * For Direct IO and fallocate, they calls get_block to allocate 4017a02908f1SMingming Cao * one single extent at a time, so they could set the "Chunk" flag 4018a02908f1SMingming Cao */ 4019a02908f1SMingming Cao idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk); 4020a02908f1SMingming Cao 4021a02908f1SMingming Cao ret = idxblocks; 4022a02908f1SMingming Cao 4023a02908f1SMingming Cao /* 4024a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 4025a02908f1SMingming Cao * to account 4026a02908f1SMingming Cao */ 4027a02908f1SMingming Cao groups = idxblocks; 4028a02908f1SMingming Cao if (chunk) 4029a02908f1SMingming Cao groups += 1; 4030ac27a0ecSDave Kleikamp else 4031a02908f1SMingming Cao groups += nrblocks; 4032ac27a0ecSDave Kleikamp 4033a02908f1SMingming Cao gdpblocks = groups; 40348df9675fSTheodore Ts'o if (groups > ngroups) 40358df9675fSTheodore Ts'o groups = ngroups; 4036a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 4037a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 4038a02908f1SMingming Cao 4039a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 4040a02908f1SMingming Cao ret += groups + gdpblocks; 4041a02908f1SMingming Cao 4042a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 4043a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 4044ac27a0ecSDave Kleikamp 4045ac27a0ecSDave Kleikamp return ret; 4046ac27a0ecSDave Kleikamp } 4047ac27a0ecSDave Kleikamp 4048ac27a0ecSDave Kleikamp /* 404925985edcSLucas De Marchi * Calculate the total number of credits to reserve to fit 4050f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 4051f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 4052a02908f1SMingming Cao * 4053525f4ed8SMingming Cao * This could be called via ext4_write_begin() 4054a02908f1SMingming Cao * 4055525f4ed8SMingming Cao * We need to consider the worse case, when 4056a02908f1SMingming Cao * one new block per extent. 4057a02908f1SMingming Cao */ 4058a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 4059a02908f1SMingming Cao { 4060a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 4061a02908f1SMingming Cao int ret; 4062a02908f1SMingming Cao 4063a02908f1SMingming Cao ret = ext4_meta_trans_blocks(inode, bpp, 0); 4064a02908f1SMingming Cao 4065a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 4066a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 4067a02908f1SMingming Cao ret += bpp; 4068a02908f1SMingming Cao return ret; 4069a02908f1SMingming Cao } 4070f3bd1f3fSMingming Cao 4071f3bd1f3fSMingming Cao /* 4072f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 4073f3bd1f3fSMingming Cao * 4074f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 407579e83036SEric Sandeen * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks. 4076f3bd1f3fSMingming Cao * 4077f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 4078f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 4079f3bd1f3fSMingming Cao */ 4080f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 4081f3bd1f3fSMingming Cao { 4082f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 4083f3bd1f3fSMingming Cao } 4084f3bd1f3fSMingming Cao 4085a02908f1SMingming Cao /* 4086617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 4087ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 4088ac27a0ecSDave Kleikamp */ 4089617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 4090617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 4091ac27a0ecSDave Kleikamp { 4092ac27a0ecSDave Kleikamp int err = 0; 4093ac27a0ecSDave Kleikamp 409425ec56b5SJean Noel Cordenner if (test_opt(inode->i_sb, I_VERSION)) 409525ec56b5SJean Noel Cordenner inode_inc_iversion(inode); 409625ec56b5SJean Noel Cordenner 4097ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 4098ac27a0ecSDave Kleikamp get_bh(iloc->bh); 4099ac27a0ecSDave Kleikamp 4100dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 4101830156c7SFrank Mayhar err = ext4_do_update_inode(handle, inode, iloc); 4102ac27a0ecSDave Kleikamp put_bh(iloc->bh); 4103ac27a0ecSDave Kleikamp return err; 4104ac27a0ecSDave Kleikamp } 4105ac27a0ecSDave Kleikamp 4106ac27a0ecSDave Kleikamp /* 4107ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 4108ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 4109ac27a0ecSDave Kleikamp */ 4110ac27a0ecSDave Kleikamp 4111ac27a0ecSDave Kleikamp int 4112617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 4113617ba13bSMingming Cao struct ext4_iloc *iloc) 4114ac27a0ecSDave Kleikamp { 41150390131bSFrank Mayhar int err; 41160390131bSFrank Mayhar 4117617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 4118ac27a0ecSDave Kleikamp if (!err) { 4119ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 4120617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, iloc->bh); 4121ac27a0ecSDave Kleikamp if (err) { 4122ac27a0ecSDave Kleikamp brelse(iloc->bh); 4123ac27a0ecSDave Kleikamp iloc->bh = NULL; 4124ac27a0ecSDave Kleikamp } 4125ac27a0ecSDave Kleikamp } 4126617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4127ac27a0ecSDave Kleikamp return err; 4128ac27a0ecSDave Kleikamp } 4129ac27a0ecSDave Kleikamp 4130ac27a0ecSDave Kleikamp /* 41316dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 41326dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 41336dd4ee7cSKalpak Shah */ 41341d03ec98SAneesh Kumar K.V static int ext4_expand_extra_isize(struct inode *inode, 41351d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 41361d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 41371d03ec98SAneesh Kumar K.V handle_t *handle) 41386dd4ee7cSKalpak Shah { 41396dd4ee7cSKalpak Shah struct ext4_inode *raw_inode; 41406dd4ee7cSKalpak Shah struct ext4_xattr_ibody_header *header; 41416dd4ee7cSKalpak Shah 41426dd4ee7cSKalpak Shah if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) 41436dd4ee7cSKalpak Shah return 0; 41446dd4ee7cSKalpak Shah 41456dd4ee7cSKalpak Shah raw_inode = ext4_raw_inode(&iloc); 41466dd4ee7cSKalpak Shah 41476dd4ee7cSKalpak Shah header = IHDR(inode, raw_inode); 41486dd4ee7cSKalpak Shah 41496dd4ee7cSKalpak Shah /* No extended attributes present */ 415019f5fb7aSTheodore Ts'o if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) || 41516dd4ee7cSKalpak Shah header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 41526dd4ee7cSKalpak Shah memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0, 41536dd4ee7cSKalpak Shah new_extra_isize); 41546dd4ee7cSKalpak Shah EXT4_I(inode)->i_extra_isize = new_extra_isize; 41556dd4ee7cSKalpak Shah return 0; 41566dd4ee7cSKalpak Shah } 41576dd4ee7cSKalpak Shah 41586dd4ee7cSKalpak Shah /* try to expand with EAs present */ 41596dd4ee7cSKalpak Shah return ext4_expand_extra_isize_ea(inode, new_extra_isize, 41606dd4ee7cSKalpak Shah raw_inode, handle); 41616dd4ee7cSKalpak Shah } 41626dd4ee7cSKalpak Shah 41636dd4ee7cSKalpak Shah /* 4164ac27a0ecSDave Kleikamp * What we do here is to mark the in-core inode as clean with respect to inode 4165ac27a0ecSDave Kleikamp * dirtiness (it may still be data-dirty). 4166ac27a0ecSDave Kleikamp * This means that the in-core inode may be reaped by prune_icache 4167ac27a0ecSDave Kleikamp * without having to perform any I/O. This is a very good thing, 4168ac27a0ecSDave Kleikamp * because *any* task may call prune_icache - even ones which 4169ac27a0ecSDave Kleikamp * have a transaction open against a different journal. 4170ac27a0ecSDave Kleikamp * 4171ac27a0ecSDave Kleikamp * Is this cheating? Not really. Sure, we haven't written the 4172ac27a0ecSDave Kleikamp * inode out, but prune_icache isn't a user-visible syncing function. 4173ac27a0ecSDave Kleikamp * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 4174ac27a0ecSDave Kleikamp * we start and wait on commits. 4175ac27a0ecSDave Kleikamp * 4176ac27a0ecSDave Kleikamp * Is this efficient/effective? Well, we're being nice to the system 4177ac27a0ecSDave Kleikamp * by cleaning up our inodes proactively so they can be reaped 4178ac27a0ecSDave Kleikamp * without I/O. But we are potentially leaving up to five seconds' 4179ac27a0ecSDave Kleikamp * worth of inodes floating about which prune_icache wants us to 4180ac27a0ecSDave Kleikamp * write out. One way to fix that would be to get prune_icache() 4181ac27a0ecSDave Kleikamp * to do a write_super() to free up some memory. It has the desired 4182ac27a0ecSDave Kleikamp * effect. 4183ac27a0ecSDave Kleikamp */ 4184617ba13bSMingming Cao int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode) 4185ac27a0ecSDave Kleikamp { 4186617ba13bSMingming Cao struct ext4_iloc iloc; 41876dd4ee7cSKalpak Shah struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 41886dd4ee7cSKalpak Shah static unsigned int mnt_count; 41896dd4ee7cSKalpak Shah int err, ret; 4190ac27a0ecSDave Kleikamp 4191ac27a0ecSDave Kleikamp might_sleep(); 41927ff9c073STheodore Ts'o trace_ext4_mark_inode_dirty(inode, _RET_IP_); 4193617ba13bSMingming Cao err = ext4_reserve_inode_write(handle, inode, &iloc); 41940390131bSFrank Mayhar if (ext4_handle_valid(handle) && 41950390131bSFrank Mayhar EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize && 419619f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) { 41976dd4ee7cSKalpak Shah /* 41986dd4ee7cSKalpak Shah * We need extra buffer credits since we may write into EA block 41996dd4ee7cSKalpak Shah * with this same handle. If journal_extend fails, then it will 42006dd4ee7cSKalpak Shah * only result in a minor loss of functionality for that inode. 42016dd4ee7cSKalpak Shah * If this is felt to be critical, then e2fsck should be run to 42026dd4ee7cSKalpak Shah * force a large enough s_min_extra_isize. 42036dd4ee7cSKalpak Shah */ 42046dd4ee7cSKalpak Shah if ((jbd2_journal_extend(handle, 42056dd4ee7cSKalpak Shah EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) { 42066dd4ee7cSKalpak Shah ret = ext4_expand_extra_isize(inode, 42076dd4ee7cSKalpak Shah sbi->s_want_extra_isize, 42086dd4ee7cSKalpak Shah iloc, handle); 42096dd4ee7cSKalpak Shah if (ret) { 421019f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, 421119f5fb7aSTheodore Ts'o EXT4_STATE_NO_EXPAND); 4212c1bddad9SAneesh Kumar K.V if (mnt_count != 4213c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count)) { 421412062dddSEric Sandeen ext4_warning(inode->i_sb, 42156dd4ee7cSKalpak Shah "Unable to expand inode %lu. Delete" 42166dd4ee7cSKalpak Shah " some EAs or run e2fsck.", 42176dd4ee7cSKalpak Shah inode->i_ino); 4218c1bddad9SAneesh Kumar K.V mnt_count = 4219c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count); 42206dd4ee7cSKalpak Shah } 42216dd4ee7cSKalpak Shah } 42226dd4ee7cSKalpak Shah } 42236dd4ee7cSKalpak Shah } 4224ac27a0ecSDave Kleikamp if (!err) 4225617ba13bSMingming Cao err = ext4_mark_iloc_dirty(handle, inode, &iloc); 4226ac27a0ecSDave Kleikamp return err; 4227ac27a0ecSDave Kleikamp } 4228ac27a0ecSDave Kleikamp 4229ac27a0ecSDave Kleikamp /* 4230617ba13bSMingming Cao * ext4_dirty_inode() is called from __mark_inode_dirty() 4231ac27a0ecSDave Kleikamp * 4232ac27a0ecSDave Kleikamp * We're really interested in the case where a file is being extended. 4233ac27a0ecSDave Kleikamp * i_size has been changed by generic_commit_write() and we thus need 4234ac27a0ecSDave Kleikamp * to include the updated inode in the current transaction. 4235ac27a0ecSDave Kleikamp * 42365dd4056dSChristoph Hellwig * Also, dquot_alloc_block() will always dirty the inode when blocks 4237ac27a0ecSDave Kleikamp * are allocated to the file. 4238ac27a0ecSDave Kleikamp * 4239ac27a0ecSDave Kleikamp * If the inode is marked synchronous, we don't honour that here - doing 4240ac27a0ecSDave Kleikamp * so would cause a commit on atime updates, which we don't bother doing. 4241ac27a0ecSDave Kleikamp * We handle synchronous inodes at the highest possible level. 4242ac27a0ecSDave Kleikamp */ 4243aa385729SChristoph Hellwig void ext4_dirty_inode(struct inode *inode, int flags) 4244ac27a0ecSDave Kleikamp { 4245ac27a0ecSDave Kleikamp handle_t *handle; 4246ac27a0ecSDave Kleikamp 4247617ba13bSMingming Cao handle = ext4_journal_start(inode, 2); 4248ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 4249ac27a0ecSDave Kleikamp goto out; 4250f3dc272fSCurt Wohlgemuth 4251617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 4252f3dc272fSCurt Wohlgemuth 4253617ba13bSMingming Cao ext4_journal_stop(handle); 4254ac27a0ecSDave Kleikamp out: 4255ac27a0ecSDave Kleikamp return; 4256ac27a0ecSDave Kleikamp } 4257ac27a0ecSDave Kleikamp 4258ac27a0ecSDave Kleikamp #if 0 4259ac27a0ecSDave Kleikamp /* 4260ac27a0ecSDave Kleikamp * Bind an inode's backing buffer_head into this transaction, to prevent 4261ac27a0ecSDave Kleikamp * it from being flushed to disk early. Unlike 4262617ba13bSMingming Cao * ext4_reserve_inode_write, this leaves behind no bh reference and 4263ac27a0ecSDave Kleikamp * returns no iloc structure, so the caller needs to repeat the iloc 4264ac27a0ecSDave Kleikamp * lookup to mark the inode dirty later. 4265ac27a0ecSDave Kleikamp */ 4266617ba13bSMingming Cao static int ext4_pin_inode(handle_t *handle, struct inode *inode) 4267ac27a0ecSDave Kleikamp { 4268617ba13bSMingming Cao struct ext4_iloc iloc; 4269ac27a0ecSDave Kleikamp 4270ac27a0ecSDave Kleikamp int err = 0; 4271ac27a0ecSDave Kleikamp if (handle) { 4272617ba13bSMingming Cao err = ext4_get_inode_loc(inode, &iloc); 4273ac27a0ecSDave Kleikamp if (!err) { 4274ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc.bh, "get_write_access"); 4275dab291afSMingming Cao err = jbd2_journal_get_write_access(handle, iloc.bh); 4276ac27a0ecSDave Kleikamp if (!err) 42770390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, 427873b50c1cSCurt Wohlgemuth NULL, 4279ac27a0ecSDave Kleikamp iloc.bh); 4280ac27a0ecSDave Kleikamp brelse(iloc.bh); 4281ac27a0ecSDave Kleikamp } 4282ac27a0ecSDave Kleikamp } 4283617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4284ac27a0ecSDave Kleikamp return err; 4285ac27a0ecSDave Kleikamp } 4286ac27a0ecSDave Kleikamp #endif 4287ac27a0ecSDave Kleikamp 4288617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val) 4289ac27a0ecSDave Kleikamp { 4290ac27a0ecSDave Kleikamp journal_t *journal; 4291ac27a0ecSDave Kleikamp handle_t *handle; 4292ac27a0ecSDave Kleikamp int err; 4293ac27a0ecSDave Kleikamp 4294ac27a0ecSDave Kleikamp /* 4295ac27a0ecSDave Kleikamp * We have to be very careful here: changing a data block's 4296ac27a0ecSDave Kleikamp * journaling status dynamically is dangerous. If we write a 4297ac27a0ecSDave Kleikamp * data block to the journal, change the status and then delete 4298ac27a0ecSDave Kleikamp * that block, we risk forgetting to revoke the old log record 4299ac27a0ecSDave Kleikamp * from the journal and so a subsequent replay can corrupt data. 4300ac27a0ecSDave Kleikamp * So, first we make sure that the journal is empty and that 4301ac27a0ecSDave Kleikamp * nobody is changing anything. 4302ac27a0ecSDave Kleikamp */ 4303ac27a0ecSDave Kleikamp 4304617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 43050390131bSFrank Mayhar if (!journal) 43060390131bSFrank Mayhar return 0; 4307d699594dSDave Hansen if (is_journal_aborted(journal)) 4308ac27a0ecSDave Kleikamp return -EROFS; 4309ac27a0ecSDave Kleikamp 4310dab291afSMingming Cao jbd2_journal_lock_updates(journal); 4311dab291afSMingming Cao jbd2_journal_flush(journal); 4312ac27a0ecSDave Kleikamp 4313ac27a0ecSDave Kleikamp /* 4314ac27a0ecSDave Kleikamp * OK, there are no updates running now, and all cached data is 4315ac27a0ecSDave Kleikamp * synced to disk. We are now in a completely consistent state 4316ac27a0ecSDave Kleikamp * which doesn't have anything in the journal, and we know that 4317ac27a0ecSDave Kleikamp * no filesystem updates are running, so it is safe to modify 4318ac27a0ecSDave Kleikamp * the inode's in-core data-journaling state flag now. 4319ac27a0ecSDave Kleikamp */ 4320ac27a0ecSDave Kleikamp 4321ac27a0ecSDave Kleikamp if (val) 432212e9b892SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 4323ac27a0ecSDave Kleikamp else 432412e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 4325617ba13bSMingming Cao ext4_set_aops(inode); 4326ac27a0ecSDave Kleikamp 4327dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 4328ac27a0ecSDave Kleikamp 4329ac27a0ecSDave Kleikamp /* Finally we can mark the inode as dirty. */ 4330ac27a0ecSDave Kleikamp 4331617ba13bSMingming Cao handle = ext4_journal_start(inode, 1); 4332ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 4333ac27a0ecSDave Kleikamp return PTR_ERR(handle); 4334ac27a0ecSDave Kleikamp 4335617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 43360390131bSFrank Mayhar ext4_handle_sync(handle); 4337617ba13bSMingming Cao ext4_journal_stop(handle); 4338617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4339ac27a0ecSDave Kleikamp 4340ac27a0ecSDave Kleikamp return err; 4341ac27a0ecSDave Kleikamp } 43422e9ee850SAneesh Kumar K.V 43432e9ee850SAneesh Kumar K.V static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) 43442e9ee850SAneesh Kumar K.V { 43452e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 43462e9ee850SAneesh Kumar K.V } 43472e9ee850SAneesh Kumar K.V 4348c2ec175cSNick Piggin int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) 43492e9ee850SAneesh Kumar K.V { 4350c2ec175cSNick Piggin struct page *page = vmf->page; 43512e9ee850SAneesh Kumar K.V loff_t size; 43522e9ee850SAneesh Kumar K.V unsigned long len; 43539ea7df53SJan Kara int ret; 43542e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 43552e9ee850SAneesh Kumar K.V struct inode *inode = file->f_path.dentry->d_inode; 43562e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 43579ea7df53SJan Kara handle_t *handle; 43589ea7df53SJan Kara get_block_t *get_block; 43599ea7df53SJan Kara int retries = 0; 43602e9ee850SAneesh Kumar K.V 43612e9ee850SAneesh Kumar K.V /* 43629ea7df53SJan Kara * This check is racy but catches the common case. We rely on 43639ea7df53SJan Kara * __block_page_mkwrite() to do a reliable check. 43642e9ee850SAneesh Kumar K.V */ 43659ea7df53SJan Kara vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); 43669ea7df53SJan Kara /* Delalloc case is easy... */ 43679ea7df53SJan Kara if (test_opt(inode->i_sb, DELALLOC) && 43689ea7df53SJan Kara !ext4_should_journal_data(inode) && 43699ea7df53SJan Kara !ext4_nonda_switch(inode->i_sb)) { 43709ea7df53SJan Kara do { 43719ea7df53SJan Kara ret = __block_page_mkwrite(vma, vmf, 43729ea7df53SJan Kara ext4_da_get_block_prep); 43739ea7df53SJan Kara } while (ret == -ENOSPC && 43749ea7df53SJan Kara ext4_should_retry_alloc(inode->i_sb, &retries)); 43759ea7df53SJan Kara goto out_ret; 43762e9ee850SAneesh Kumar K.V } 43770e499890SDarrick J. Wong 43780e499890SDarrick J. Wong lock_page(page); 43799ea7df53SJan Kara size = i_size_read(inode); 43809ea7df53SJan Kara /* Page got truncated from under us? */ 43819ea7df53SJan Kara if (page->mapping != mapping || page_offset(page) > size) { 43829ea7df53SJan Kara unlock_page(page); 43839ea7df53SJan Kara ret = VM_FAULT_NOPAGE; 43849ea7df53SJan Kara goto out; 43850e499890SDarrick J. Wong } 43862e9ee850SAneesh Kumar K.V 43872e9ee850SAneesh Kumar K.V if (page->index == size >> PAGE_CACHE_SHIFT) 43882e9ee850SAneesh Kumar K.V len = size & ~PAGE_CACHE_MASK; 43892e9ee850SAneesh Kumar K.V else 43902e9ee850SAneesh Kumar K.V len = PAGE_CACHE_SIZE; 4391a827eaffSAneesh Kumar K.V /* 43929ea7df53SJan Kara * Return if we have all the buffers mapped. This avoids the need to do 43939ea7df53SJan Kara * journal_start/journal_stop which can block and take a long time 4394a827eaffSAneesh Kumar K.V */ 43952e9ee850SAneesh Kumar K.V if (page_has_buffers(page)) { 43962e9ee850SAneesh Kumar K.V if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL, 4397a827eaffSAneesh Kumar K.V ext4_bh_unmapped)) { 43989ea7df53SJan Kara /* Wait so that we don't change page under IO */ 43999ea7df53SJan Kara wait_on_page_writeback(page); 44009ea7df53SJan Kara ret = VM_FAULT_LOCKED; 44019ea7df53SJan Kara goto out; 44022e9ee850SAneesh Kumar K.V } 4403a827eaffSAneesh Kumar K.V } 4404a827eaffSAneesh Kumar K.V unlock_page(page); 44059ea7df53SJan Kara /* OK, we need to fill the hole... */ 44069ea7df53SJan Kara if (ext4_should_dioread_nolock(inode)) 44079ea7df53SJan Kara get_block = ext4_get_block_write; 44089ea7df53SJan Kara else 44099ea7df53SJan Kara get_block = ext4_get_block; 44109ea7df53SJan Kara retry_alloc: 44119ea7df53SJan Kara handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode)); 44129ea7df53SJan Kara if (IS_ERR(handle)) { 4413c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 44149ea7df53SJan Kara goto out; 44159ea7df53SJan Kara } 44169ea7df53SJan Kara ret = __block_page_mkwrite(vma, vmf, get_block); 44179ea7df53SJan Kara if (!ret && ext4_should_journal_data(inode)) { 44189ea7df53SJan Kara if (walk_page_buffers(handle, page_buffers(page), 0, 44199ea7df53SJan Kara PAGE_CACHE_SIZE, NULL, do_journal_get_write_access)) { 44209ea7df53SJan Kara unlock_page(page); 44219ea7df53SJan Kara ret = VM_FAULT_SIGBUS; 44229ea7df53SJan Kara goto out; 44239ea7df53SJan Kara } 44249ea7df53SJan Kara ext4_set_inode_state(inode, EXT4_STATE_JDATA); 44259ea7df53SJan Kara } 44269ea7df53SJan Kara ext4_journal_stop(handle); 44279ea7df53SJan Kara if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 44289ea7df53SJan Kara goto retry_alloc; 44299ea7df53SJan Kara out_ret: 44309ea7df53SJan Kara ret = block_page_mkwrite_return(ret); 44319ea7df53SJan Kara out: 44322e9ee850SAneesh Kumar K.V return ret; 44332e9ee850SAneesh Kumar K.V } 4434